From 5861d07b070e5314c7967099dcae4c58c60772b0 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sun, 18 Apr 2021 13:30:50 -0700 Subject: [PATCH] function parsing --- ast.h | 11 +++++++++++ wraith.lex | 1 + wraith.y | 34 +++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/ast.h b/ast.h index bf58d26..d0fbbb4 100644 --- a/ast.h +++ b/ast.h @@ -219,6 +219,17 @@ Node* MakeStatementSequenceNode( return node; } +Node* MakeReturnStatementNode( + Node *expressionNode +) { + Node* node = (Node*) malloc(sizeof(Node)); + node->syntaxKind = Return; + node->children = (Node**) malloc(sizeof(Node*)); + node->childCount = 1; + node->children[0] = expressionNode; + return node; +} + Node* MakeFunctionSignatureNode( Node *identifierNode, Node* typeNode, diff --git a/wraith.lex b/wraith.lex index e8f3d36..2dec2b4 100644 --- a/wraith.lex +++ b/wraith.lex @@ -9,6 +9,7 @@ "string" return STRING; "bool" return BOOL; "struct" return STRUCT; +"return" return RETURN; [a-zA-Z][a-zA-Z0-9]* return ID; \"[a-zA-Z][a-zA-Z0-9]*\" return STRING_LITERAL; "+" return PLUS; diff --git a/wraith.y b/wraith.y index 3a0e040..0ab63de 100644 --- a/wraith.y +++ b/wraith.y @@ -22,6 +22,7 @@ int yydebug=1; %token STRING %token BOOL %token STRUCT +%token RETURN %token ID %token STRING_LITERAL %token PLUS @@ -135,7 +136,7 @@ Expression : PrimaryExpression | BinaryExpression ; -VariableDeclaration : Type Identifier SEMICOLON +VariableDeclaration : Type Identifier { $$ = MakeDeclarationNode($1, $2); } @@ -149,26 +150,25 @@ AssignmentStatement : VariableDeclaration EQUAL Expression $$ = MakeAssignmentNode($1, $3); } +ReturnStatement : RETURN Expression + { + $$ = MakeReturnStatementNode($2); + } + PartialStatement : AssignmentStatement | VariableDeclaration + | ReturnStatement ; Statement : PartialStatement SEMICOLON; -Statements : Statement +Statements : Statement Statements { - Node **statements; - uint32_t statementCount; - AddStatement(stack, $1); - - statements = GetStatements(stack, &statementCount); - $$ = MakeStatementSequenceNode(statements, statementCount); - PopStackFrame(stack); } - | Statement Statements + | { - AddStatement(stack, $2); + PushStackFrame(stack); } Arguments : Arguments COMMA VariableDeclaration @@ -177,7 +177,15 @@ Arguments : Arguments COMMA VariableDeclaration Body : LEFT_BRACE Statements RIGHT_BRACE { - $$ = $2; + Node **statements; + Node *statementSequence; + uint32_t statementCount; + + statements = GetStatements(stack, &statementCount); + statementSequence = MakeStatementSequenceNode(statements, statementCount); + $$ = MakeStatementSequenceNode(statements, statementCount); + + PopStackFrame(stack); } FunctionSignature : Type Identifier LEFT_PAREN Arguments RIGHT_PAREN @@ -190,7 +198,7 @@ FunctionDeclaration : FunctionSignature Body $$ = MakeFunctionDeclarationNode($1, $2); } -VariableDeclarations : VariableDeclaration VariableDeclarations +VariableDeclarations : VariableDeclaration SEMICOLON VariableDeclarations { AddDeclaration(stack, $1); }