function parsing

generics
cosmonaut 2021-04-18 13:30:50 -07:00
parent 4bc7aca15e
commit 5861d07b07
3 changed files with 33 additions and 13 deletions

11
ast.h
View File

@ -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,

View File

@ -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;

View File

@ -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);
}