add assign, nil
[ccc.git] / parse.y
diff --git a/parse.y b/parse.y
index 76bbdd0..9c24ef0 100644 (file)
--- a/parse.y
+++ b/parse.y
@@ -24,10 +24,10 @@ int yywrap()
 
 %}
 
-%define parse.error verbose
-%token INTEGER PLUS MINUS TIMES DIVIDE BOPEN BCLOSE SEMICOLON POWER CONS MODULO
-%token BINOR BINAND INVERSE VAR ASSIGN IDENT COMMA COPEN CCLOSE IF ELSE WHILE
-%token BOOL CHAR
+//%define parse.error verbose
+%token ASSIGN BCLOSE BINAND BINOR BOOL BOPEN CCLOSE CHAR COMMA CONS COPEN
+%token DIVIDE ELSE IDENT IF INTEGER INVERSE MINUS MODULO NIL PLUS POWER RETURN
+%token SEMICOLON TIMES VAR WHILE
 
 %parse-param { struct ast **result }
 
@@ -66,6 +66,15 @@ nargs
        : nargs COMMA IDENT { $$ = ast_cons($3, $1); }
        | IDENT { $$ = ast_cons($1, NULL); }
        ;
+
+fargs
+       : { $$ = NULL; }
+       | fnargs
+       ;
+fnargs
+       : fnargs COMMA expr { $$ = ast_cons($3, $1); }
+       | expr { $$ = ast_cons($1, NULL); }
+       ;
 body
        : { $$ = NULL; }
        | body vardecl SEMICOLON { $$ = ast_cons($2, $1); }
@@ -78,6 +87,9 @@ stmt
        | WHILE BOPEN expr BCLOSE COPEN body CCLOSE
                { $$ = ast_while($3, ast_list($6)); }
        | expr SEMICOLON { $$ = ast_stmt_expr($1); }
+       | IDENT ASSIGN expr SEMICOLON { $$ = ast_assign($1, $3); }
+       | RETURN expr SEMICOLON { $$ = ast_return($2); }
+       | RETURN SEMICOLON { $$ = ast_return(NULL); }
        ;
 
 expr
@@ -99,8 +111,10 @@ expr
        | MINUS expr { $$ = ast_unop(negate, $2); }
        | INVERSE expr { $$ = ast_unop(inverse, $2); }
        | BOPEN expr BCLOSE { $$ = $2; }
+       | IDENT BOPEN fargs BCLOSE { $$ = ast_funcall($1, ast_list($3)); }
        | INTEGER
        | BOOL
        | CHAR
        | IDENT
+       | NIL { $$ = ast_nil(); }
        ;