diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-17 06:54:32 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-17 06:54:32 +0000 |
| commit | 74b6d06711afd3efbbc47436f1491900994313f3 (patch) | |
| tree | b9050214b147cd2b1b54fd27e75d669bf6146e48 /lib/Parser | |
| parent | 39661855c0816ac248be437704c76f58359cb5f0 (diff) | |
feat(classes): allocate objects with NEW and rebind named class variables
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'lib/Parser')
| -rw-r--r-- | lib/Parser/Parser.cc | 2 | ||||
| -rw-r--r-- | lib/Parser/ParserExpression.cc | 19 |
2 files changed, 20 insertions, 1 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index 26eeac6..d3b2daa 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -242,7 +242,7 @@ std::unique_ptr<StmtAST> Parser::ParseAssignStmt() { return nullptr; } getNextToken(); - auto Expr = ParseExpression(); + auto Expr = ParseAssignmentRHS(); if (!Expr) return nullptr; if (Accesses.empty()) diff --git a/lib/Parser/ParserExpression.cc b/lib/Parser/ParserExpression.cc index f4e074e..96132aa 100644 --- a/lib/Parser/ParserExpression.cc +++ b/lib/Parser/ParserExpression.cc @@ -168,6 +168,9 @@ std::unique_ptr<ExprAST> Parser::ParsePrimary() { std::unique_ptr<ExprAST> Parser::ParsePrimaryImpl() { switch (CurTok) { + case tok_new: + fprintf(stderr, "error: NEW is only allowed on the right of an assignment at line %d\n", Lex.getLine()); + return nullptr; case tok_identifier: return ParseIdentifierExpr(); case tok_number_int: return ParseNumberExpr(); case tok_number_real: return ParseNumberExpr(); @@ -299,3 +302,19 @@ std::optional<std::vector<std::unique_ptr<ExprAST>>> Parser::ParseExprList(char getNextToken(); return Elems; } + +std::unique_ptr<ExprAST> Parser::ParseAssignmentRHS() { + if (CurTok != tok_new) return ParseExpression(); + int Line = Lex.getLine(); + getNextToken(); + std::string ClassName = ParseTypeName(false); + if (ClassName.empty()) return nullptr; + std::vector<std::unique_ptr<ExprAST>> Args; + if (CurTok == '(') { + getNextToken(); + auto Parsed = ParseExprList(')', true); + if (!Parsed) return nullptr; + Args = std::move(*Parsed); + } + return std::make_unique<NewExprAST>(ClassName, std::move(Args), Line); +} |
