Skip to content

Commit b02ade1

Browse files
committed
feat: introduce AST node creation functions and parser structure
1 parent 3783a8a commit b02ade1

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/compiler/parser/ast.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,33 @@ Expr *createAwaitExpr(Expr *expression, int line, int column) {
312312
return expr;
313313
}
314314

315+
Expr *createThisExpr(int line, int column) {
316+
Expr *expr = ALLOCATE(Expr, 1);
317+
expr->type = EXPR_THIS;
318+
expr->line = line;
319+
expr->column = column;
320+
return expr;
321+
}
322+
323+
Expr *createSuperExpr(const char *method, int line, int column) {
324+
Expr *expr = ALLOCATE(Expr, 1);
325+
expr->type = EXPR_SUPER;
326+
expr->line = line;
327+
expr->column = column;
328+
expr->as.super_expr.method = method ? strdup(method) : NULL;
329+
return expr;
330+
}
331+
332+
Expr *createNewExpr(Expr *clazz, ExprList *args, int line, int column) {
333+
Expr *expr = ALLOCATE(Expr, 1);
334+
expr->type = EXPR_NEW;
335+
expr->line = line;
336+
expr->column = column;
337+
expr->as.new_expr.clazz = clazz;
338+
expr->as.new_expr.args = args;
339+
return expr;
340+
}
341+
315342
// --- Statement Creation Functions ---
316343

317344
Stmt *createExpressionStmt(Expr *expression, int line, int column) {
@@ -573,6 +600,16 @@ void freeExpr(Expr *expr) {
573600
case EXPR_AWAIT:
574601
freeExpr(expr->as.await_expr.expression);
575602
break;
603+
case EXPR_THIS:
604+
// Nothing to free
605+
break;
606+
case EXPR_SUPER:
607+
if (expr->as.super_expr.method) free(expr->as.super_expr.method);
608+
break;
609+
case EXPR_NEW:
610+
freeExpr(expr->as.new_expr.clazz);
611+
freeExprList(expr->as.new_expr.args);
612+
break;
576613
}
577614

578615
FREE(Expr, expr);

src/compiler/parser/parser.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,47 @@ static Expr *primary(Parser *p) {
857857
return expr;
858858
}
859859

860+
return expr;
861+
}
862+
863+
if (match(p, 1, TOKEN_THIS)) {
864+
return createThisExpr(previous(p).line, 0);
865+
}
866+
867+
if (match(p, 1, TOKEN_SUPER)) {
868+
Token keyword = previous(p);
869+
consume(p, TOKEN_DOT, "Expect '.' after 'super'.");
870+
Token method = consume(p, TOKEN_IDENTIFIER, "Expect superclass method name.");
871+
char *methodName = tokenToString(method);
872+
Expr *expr = createSuperExpr(methodName, keyword.line, 0);
873+
free(methodName);
874+
return expr;
875+
}
876+
877+
if (match(p, 1, TOKEN_NEW)) {
878+
Token keyword = previous(p);
879+
// createNewExpr expects an Expr* for the class/callee
880+
// Parse the class name (or expression evaluating to class)
881+
// Usually "new Identifier(args)" or "new Identifier"
882+
// Let's assume syntax: new Primary() or new Primary
883+
// Using call() logic but starting with primary?
884+
// Actually standard: new CallExpr
885+
// Parse primary for class
886+
Expr *clazz = primary(p); // Recursive primary? `new Foo` -> variable `Foo`
887+
888+
ExprList *args = createExprList();
889+
if (match(p, 1, TOKEN_LEFT_PAREN)) {
890+
if (!check(p, TOKEN_RIGHT_PAREN)) {
891+
do {
892+
appendExpr(args, expression(p));
893+
} while (match(p, 1, TOKEN_COMMA));
894+
}
895+
consume(p, TOKEN_RIGHT_PAREN, "Expect ')' after arguments.");
896+
}
897+
898+
return createNewExpr(clazz, args, keyword.line, 0);
899+
}
900+
860901
if (match(p, 1, TOKEN_IDENTIFIER)) {
861902
Token token = previous(p);
862903
char *name = tokenToString(token);

0 commit comments

Comments
 (0)