parser sequence refactor

pull/1/head
cosmonaut 2021-04-29 12:42:51 -07:00
parent a320086038
commit 7ca87d6b13
8 changed files with 173 additions and 238 deletions

View File

@ -42,11 +42,9 @@ add_executable(
src/ast.h src/ast.h
src/codegen.h src/codegen.h
src/parser.h src/parser.h
src/stack.h
src/ast.c src/ast.c
src/codegen.c src/codegen.c
src/parser.c src/parser.c
src/stack.c
src/main.c src/main.c
# Generated code # Generated code
${BISON_Parser_OUTPUTS} ${BISON_Parser_OUTPUTS}

View File

@ -15,6 +15,11 @@ struct MyStruct
myInt = myInt + 1; myInt = myInt + 1;
} }
Decrement(): void
{
myInt = myInt - 1;
}
static MyFunction(input: int): int static MyFunction(input: int): int
{ {
return input * 2; return input * 2;
@ -40,6 +45,10 @@ struct Program
{ {
myStruct.Increment(); myStruct.Increment();
} }
else
{
myStruct.Decrement();
}
return myStruct.myInt; return myStruct.myInt;
} }

View File

@ -1,13 +1,12 @@
%code requires { %code requires {
#include "../src/stack.h" #include "../src/ast.h"
} }
%{ %{
#include <stdio.h> #include <stdio.h>
#include "../src/ast.h" #include "../src/ast.h"
#include "../src/stack.h"
void yyerror(FILE *fp, Stack *stack, Node **pRootNode, char *s) void yyerror(FILE *fp, Node **pRootNode, char *s)
{ {
fprintf (stderr, "%s\n", s); fprintf (stderr, "%s\n", s);
} }
@ -62,7 +61,7 @@ extern FILE *yyin;
%token COMMENT %token COMMENT
%token NEWLINE %token NEWLINE
%parse-param { FILE* fp } { Stack *stack } { Node **pRootNode } %parse-param { FILE* fp } { Node **pRootNode }
%define parse.error verbose %define parse.error verbose
@ -73,16 +72,7 @@ extern FILE *yyin;
%% %%
Program : TopLevelDeclarations Program : TopLevelDeclarations
{ {
Node **declarations; *pRootNode = $1;
Node *declarationSequence;
uint32_t declarationCount;
declarations = GetNodes(stack, &declarationCount);
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
PopStackFrame(stack);
*pRootNode = declarationSequence;
} }
BaseType : VOID BaseType : VOID
@ -220,13 +210,7 @@ ReturnStatement : RETURN Expression
FunctionCallExpression : AccessExpression LEFT_PAREN Arguments RIGHT_PAREN FunctionCallExpression : AccessExpression LEFT_PAREN Arguments RIGHT_PAREN
{ {
Node **arguments; $$ = MakeFunctionCallExpressionNode($1, $3);
uint32_t argumentCount;
arguments = GetNodes(stack, &argumentCount);
$$ = MakeFunctionCallExpressionNode($1, MakeFunctionArgumentSequenceNode(arguments, argumentCount));
PopStackFrame(stack);
} }
PartialStatement : FunctionCallExpression PartialStatement : FunctionCallExpression
@ -237,91 +221,68 @@ PartialStatement : FunctionCallExpression
IfStatement : IF LEFT_PAREN Expression RIGHT_PAREN LEFT_BRACE Statements RIGHT_BRACE IfStatement : IF LEFT_PAREN Expression RIGHT_PAREN LEFT_BRACE Statements RIGHT_BRACE
{ {
Node **statements; $$ = MakeIfNode($3, $6);
Node *statementSequence; }
uint32_t statementCount;
statements = GetNodes(stack, &statementCount); Conditional : IfStatement
statementSequence = MakeStatementSequenceNode(statements, statementCount); | IfStatement ELSE LEFT_BRACE Statements RIGHT_BRACE
{
$$ = MakeIfNode($3, statementSequence); $$ = MakeIfElseNode($1, $4);
PopStackFrame(stack);
} }
Statement : PartialStatement SEMICOLON Statement : PartialStatement SEMICOLON
| IfStatement | Conditional
; ;
Statements : Statement Statements Statements : Statement
{ {
AddNode(stack, $1); $$ = StartStatementSequenceNode($1);
} }
| | Statements Statement
{ {
PushStackFrame(stack); $$ = AddStatement($1, $2);
} }
Arguments : PrimaryExpression COMMA Arguments Arguments : PrimaryExpression
{ {
AddNode(stack, $1); $$ = StartFunctionArgumentSequenceNode($1);
} }
| PrimaryExpression | Arguments COMMA PrimaryExpression
{ {
PushStackFrame(stack); $$ = AddFunctionArgumentNode($1, $3);
AddNode(stack, $1);
} }
| |
{ {
PushStackFrame(stack); $$ = MakeEmptyFunctionArgumentSequenceNode();
} }
;
SignatureArguments : VariableDeclaration COMMA SignatureArguments SignatureArguments : VariableDeclaration
{ {
AddNode(stack, $1); $$ = StartFunctionSignatureArgumentsNode($1);
} }
| VariableDeclaration | SignatureArguments COMMA VariableDeclaration
{ {
PushStackFrame(stack); $$ = AddFunctionSignatureArgumentNode($1, $3);
AddNode(stack, $1);
} }
| |
{
$$ = MakeEmptyFunctionSignatureArgumentsNode();
}
; ;
Body : LEFT_BRACE Statements RIGHT_BRACE Body : LEFT_BRACE Statements RIGHT_BRACE
{ {
Node **statements; $$ = $2;
Node *statementSequence;
uint32_t statementCount;
statements = GetNodes(stack, &statementCount);
statementSequence = MakeStatementSequenceNode(statements, statementCount);
$$ = MakeStatementSequenceNode(statements, statementCount);
PopStackFrame(stack);
} }
FunctionSignature : Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type FunctionSignature : Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
{ {
Node **declarations; $$ = MakeFunctionSignatureNode($1, $6, $3, MakeFunctionModifiersNode(NULL, 0));
uint32_t declarationCount;
declarations = GetNodes(stack, &declarationCount);
$$ = MakeFunctionSignatureNode($1, $6, MakeFunctionSignatureArgumentsNode(declarations, declarationCount), MakeFunctionModifiersNode(NULL, 0));
PopStackFrame(stack);
} }
| STATIC Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type | STATIC Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
{ {
Node **declarations;
uint32_t declarationCount;
Node *modifier = MakeStaticNode(); Node *modifier = MakeStaticNode();
$$ = MakeFunctionSignatureNode($2, $7, $4, MakeFunctionModifiersNode(&modifier, 1));
declarations = GetNodes(stack, &declarationCount);
$$ = MakeFunctionSignatureNode($2, $7, MakeFunctionSignatureArgumentsNode(declarations, declarationCount), MakeFunctionModifiersNode(&modifier, 1));
PopStackFrame(stack);
} }
FunctionDeclaration : FunctionSignature Body FunctionDeclaration : FunctionSignature Body
@ -331,39 +292,30 @@ FunctionDeclaration : FunctionSignature Body
StructDeclaration : STRUCT Identifier LEFT_BRACE Declarations RIGHT_BRACE StructDeclaration : STRUCT Identifier LEFT_BRACE Declarations RIGHT_BRACE
{ {
Node **declarations; $$ = MakeStructDeclarationNode($2, $4);
Node *declarationSequence;
uint32_t declarationCount;
declarations = GetNodes(stack, &declarationCount);
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
$$ = MakeStructDeclarationNode($2, declarationSequence);
PopStackFrame(stack);
} }
Declaration : FunctionDeclaration Declaration : FunctionDeclaration
| VariableDeclaration SEMICOLON | VariableDeclaration SEMICOLON
; ;
Declarations : Declaration Declarations Declarations : Declaration
{ {
AddNode(stack, $1); $$ = StartDeclarationSequenceNode($1);
} }
| | Declarations Declaration
{ {
PushStackFrame(stack); $$ = AddDeclarationNode($1, $2);
} }
TopLevelDeclaration : StructDeclaration; TopLevelDeclaration : StructDeclaration;
TopLevelDeclarations : TopLevelDeclaration TopLevelDeclarations TopLevelDeclarations : TopLevelDeclaration
{ {
AddNode(stack, $1); $$ = StartDeclarationSequenceNode($1);
} }
| | TopLevelDeclarations TopLevelDeclaration
{ {
PushStackFrame(stack); $$ = AddDeclarationNode($1, $2);
} }
%% %%

131
src/ast.c
View File

@ -36,6 +36,8 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
case FunctionSignature: return "FunctionSignature"; case FunctionSignature: return "FunctionSignature";
case FunctionSignatureArguments: return "FunctionSignatureArguments"; case FunctionSignatureArguments: return "FunctionSignatureArguments";
case Identifier: return "Identifier"; case Identifier: return "Identifier";
case IfStatement: return "If";
case IfElseStatement: return "IfElse";
case Number: return "Number"; case Number: return "Number";
case PrimitiveTypeNode: return "PrimitiveTypeNode"; case PrimitiveTypeNode: return "PrimitiveTypeNode";
case ReferenceTypeNode: return "ReferenceTypeNode"; case ReferenceTypeNode: return "ReferenceTypeNode";
@ -211,22 +213,27 @@ Node* MakeAssignmentNode(
return node; return node;
} }
Node* MakeStatementSequenceNode( Node* StartStatementSequenceNode(
Node** pNodes, Node *statementNode
uint32_t nodeCount
) { ) {
int32_t i;
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = StatementSequence; node->syntaxKind = StatementSequence;
node->children = (Node**) malloc(sizeof(Node*) * nodeCount); node->children = (Node**) malloc(sizeof(Node*));
node->childCount = nodeCount; node->childCount = 1;
for (i = nodeCount - 1; i >= 0; i -= 1) node->children[0] = statementNode;
{
node->children[nodeCount - 1 - i] = pNodes[i];
}
return node; return node;
} }
Node* AddStatement(
Node* statementSequenceNode,
Node *statementNode
) {
statementSequenceNode->children = realloc(statementSequenceNode->children, sizeof(Node*) * (statementSequenceNode->childCount + 1));
statementSequenceNode->children[statementSequenceNode->childCount] = statementNode;
statementSequenceNode->childCount += 1;
return statementSequenceNode;
}
Node* MakeReturnStatementNode( Node* MakeReturnStatementNode(
Node *expressionNode Node *expressionNode
) { ) {
@ -247,21 +254,33 @@ Node* MakeReturnVoidStatementNode()
return node; return node;
} }
Node *MakeFunctionSignatureArgumentsNode( Node *StartFunctionSignatureArgumentsNode(
Node **pArgumentNodes, Node *argumentNode
uint32_t argumentCount
) { ) {
int32_t i;
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionSignatureArguments; node->syntaxKind = FunctionSignatureArguments;
node->childCount = argumentCount; node->childCount = 1;
node->children = (Node**) malloc(sizeof(Node*) * (node->childCount)); node->children = (Node**) malloc(sizeof(Node*));
node->children[0] = argumentNode;
return node;
}
for (i = argumentCount - 1; i >= 0; i -= 1) Node* AddFunctionSignatureArgumentNode(
{ Node *argumentsNode,
node->children[argumentCount - 1 - i] = pArgumentNodes[i]; Node *argumentNode
} ) {
argumentsNode->children = realloc(argumentsNode->children, sizeof(Node*) * (argumentsNode->childCount + 1));
argumentsNode->children[argumentsNode->childCount] = argumentNode;
argumentsNode->childCount += 1;
return argumentsNode;
}
Node *MakeEmptyFunctionSignatureArgumentsNode()
{
Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionSignatureArguments;
node->childCount = 0;
node->children = NULL;
return node; return node;
} }
@ -308,35 +327,54 @@ Node* MakeStructDeclarationNode(
return node; return node;
} }
Node* MakeDeclarationSequenceNode( Node* StartDeclarationSequenceNode(
Node **pNodes, Node *declarationNode
uint32_t nodeCount
) { ) {
int32_t i;
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = DeclarationSequence; node->syntaxKind = DeclarationSequence;
node->children = (Node**) malloc(sizeof(Node*) * nodeCount); node->children = (Node**) malloc(sizeof(Node*));
node->childCount = nodeCount; node->childCount = 1;
for (i = nodeCount - 1; i >= 0; i -= 1) node->children[0] = declarationNode;
{
node->children[nodeCount - 1 - i] = pNodes[i];
}
return node; return node;
} }
Node *MakeFunctionArgumentSequenceNode( Node* AddDeclarationNode(
Node **pArgumentNodes, Node *declarationSequenceNode,
uint32_t argumentCount Node *declarationNode
) {
declarationSequenceNode->children = realloc(declarationSequenceNode->children, sizeof(Node*) * (declarationSequenceNode->childCount + 1));
declarationSequenceNode->children[declarationSequenceNode->childCount] = declarationNode;
declarationSequenceNode->childCount += 1;
return declarationSequenceNode;
}
Node* StartFunctionArgumentSequenceNode(
Node *argumentNode
) { ) {
int32_t i;
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionArgumentSequence; node->syntaxKind = FunctionArgumentSequence;
node->childCount = argumentCount; node->childCount = 1;
node->children = (Node**) malloc(sizeof(Node*) * node->childCount); node->children = (Node**) malloc(sizeof(Node*));
for (i = argumentCount - 1; i >= 0; i -= 1) node->children[0] = argumentNode;
{ return node;
node->children[argumentCount - 1 - i] = pArgumentNodes[i]; }
}
Node* AddFunctionArgumentNode(
Node *argumentSequenceNode,
Node *argumentNode
) {
argumentSequenceNode->children = realloc(argumentSequenceNode->children, sizeof(Node*) * (argumentSequenceNode->childCount + 1));
argumentSequenceNode->children[argumentSequenceNode->childCount] = argumentNode;
argumentSequenceNode->childCount += 1;
return argumentSequenceNode;
}
Node *MakeEmptyFunctionArgumentSequenceNode()
{
Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionArgumentSequence;
node->childCount = 0;
node->children = NULL;
return node; return node;
} }
@ -389,6 +427,19 @@ Node* MakeIfNode(
return node; return node;
} }
Node* MakeIfElseNode(
Node *ifNode,
Node *statementSequenceNode
) {
Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = IfElseStatement;
node->childCount = 2;
node->children = (Node**) malloc(sizeof(Node*));
node->children[0] = ifNode;
node->children[1] = statementSequenceNode;
return node;
}
static const char* PrimitiveTypeToString(PrimitiveType type) static const char* PrimitiveTypeToString(PrimitiveType type)
{ {
switch (type) switch (type)

View File

@ -23,6 +23,7 @@ typedef enum
FunctionSignatureArguments, FunctionSignatureArguments,
Identifier, Identifier,
IfStatement, IfStatement,
IfElseStatement,
Number, Number,
PrimitiveTypeNode, PrimitiveTypeNode,
ReferenceTypeNode, ReferenceTypeNode,
@ -131,22 +132,29 @@ Node* MakeAssignmentNode(
Node *left, Node *left,
Node *right Node *right
); );
Node* MakeStatementSequenceNode( Node* StartStatementSequenceNode(
Node** pNodes, Node* statementNode
uint32_t nodeCount );
Node* AddStatement(
Node* statementSequenceNode,
Node *statementNode
); );
Node* MakeReturnStatementNode( Node* MakeReturnStatementNode(
Node *expressionNode Node *expressionNode
); );
Node* MakeReturnVoidStatementNode(); Node* MakeReturnVoidStatementNode();
Node* MakeFunctionSignatureArgumentsNode( Node* StartFunctionSignatureArgumentsNode(
Node **pArgumentNodes, Node *argumentNode
uint32_t argumentCount
); );
Node* AddFunctionSignatureArgumentNode(
Node *argumentsNode,
Node *argumentNode
);
Node *MakeEmptyFunctionSignatureArgumentsNode();
Node* MakeFunctionSignatureNode( Node* MakeFunctionSignatureNode(
Node *identifierNode, Node *identifierNode,
Node* typeNode, Node* typeNode,
Node* arguments, Node* argumentsNode,
Node* modifiersNode Node* modifiersNode
); );
Node* MakeFunctionDeclarationNode( Node* MakeFunctionDeclarationNode(
@ -157,14 +165,21 @@ Node* MakeStructDeclarationNode(
Node *identifierNode, Node *identifierNode,
Node *declarationSequenceNode Node *declarationSequenceNode
); );
Node* MakeDeclarationSequenceNode( Node* StartDeclarationSequenceNode(
Node **pNodes, Node *declarationNode
uint32_t nodeCount
); );
Node *MakeFunctionArgumentSequenceNode( Node* AddDeclarationNode(
Node **pArgumentNodes, Node *declarationSequenceNode,
uint32_t argumentCount Node *declarationNode
); );
Node *StartFunctionArgumentSequenceNode(
Node *argumentNode
);
Node *AddFunctionArgumentNode(
Node *argumentSequenceNode,
Node *argumentNode
);
Node *MakeEmptyFunctionArgumentSequenceNode();
Node* MakeFunctionCallExpressionNode( Node* MakeFunctionCallExpressionNode(
Node *identifierNode, Node *identifierNode,
Node *argumentSequenceNode Node *argumentSequenceNode
@ -180,6 +195,10 @@ Node* MakeIfNode(
Node *expressionNode, Node *expressionNode,
Node *statementSequenceNode Node *statementSequenceNode
); );
Node* MakeIfElseNode(
Node *ifNode,
Node *statementSequenceNode
);
void PrintTree(Node *node, uint32_t tabCount); void PrintTree(Node *node, uint32_t tabCount);

View File

@ -2,7 +2,6 @@
#include "y.tab.h" #include "y.tab.h"
#include "ast.h" #include "ast.h"
#include "stack.h"
extern FILE *yyin; extern FILE *yyin;
extern int yydebug; extern int yydebug;
@ -10,14 +9,13 @@ extern int yydebug;
int Parse(char *inputFilename, Node **pRootNode, uint8_t parseVerbose) int Parse(char *inputFilename, Node **pRootNode, uint8_t parseVerbose)
{ {
int result; int result;
Stack *stack = CreateStack();
yydebug = parseVerbose; yydebug = parseVerbose;
FILE *fp = fopen(inputFilename, "r"); FILE *fp = fopen(inputFilename, "r");
if (fp != NULL) if (fp != NULL)
{ {
yyin = fp; yyin = fp;
result = yyparse(fp, stack, pRootNode); result = yyparse(fp, pRootNode);
fclose(fp); fclose(fp);
} }
else else

View File

@ -1,65 +0,0 @@
#include <stdint.h>
#include <stdlib.h>
#include "stack.h"
Stack* CreateStack()
{
int32_t i;
Stack *stack = (Stack*) malloc(sizeof(Stack));
stack->stackCapacity = 4;
stack->stackFrames = (StackFrame*) malloc(sizeof(StackFrame) * stack->stackCapacity);
for (i = 0; i < stack->stackCapacity; i += 1)
{
stack->stackFrames[i].nodes = NULL;
stack->stackFrames[i].nodeCapacity = 0;
stack->stackFrames[i].nodeCount = 0;
}
stack->stackIndex = 0;
PushStackFrame(stack);
return stack;
}
void PushStackFrame(Stack *stack)
{
stack->stackIndex += 1;
if (stack->stackIndex == stack->stackCapacity)
{
stack->stackCapacity += 1;
stack->stackFrames = (StackFrame*) realloc(stack->stackFrames, sizeof(StackFrame) * stack->stackCapacity);
stack->stackFrames[stack->stackIndex].nodes = NULL;
stack->stackFrames[stack->stackIndex].nodeCapacity = 0;
stack->stackFrames[stack->stackIndex].nodeCount = 0;
}
stack->stackFrames[stack->stackIndex].nodeCount = 0;
}
void PopStackFrame(Stack *stack)
{
stack->stackIndex -= 1;
}
void AddNode(Stack *stack, Node *statementNode)
{
StackFrame *stackFrame = &stack->stackFrames[stack->stackIndex];
if (stackFrame->nodeCount == stackFrame->nodeCapacity)
{
stackFrame->nodeCapacity += 1;
stackFrame->nodes = (Node**) realloc(stackFrame->nodes, sizeof(Node*) * stackFrame->nodeCapacity);
}
stackFrame->nodes[stackFrame->nodeCount] = statementNode;
stackFrame->nodeCount += 1;
}
Node** GetNodes(Stack *stack, uint32_t *pCount)
{
if (stack->stackIndex < 0) {
*pCount= 0;
return NULL;
}
*pCount = stack->stackFrames[stack->stackIndex].nodeCount;
return stack->stackFrames[stack->stackIndex].nodes;
}

View File

@ -1,27 +0,0 @@
#ifndef WRAITH_STACK_H
#define WRAITH_STACK_H
#include "ast.h"
typedef struct StackFrame
{
Node **nodes;
uint32_t nodeCount;
uint32_t nodeCapacity;
} StackFrame;
typedef struct Stack
{
StackFrame *stackFrames;
int32_t stackCapacity;
int32_t stackIndex;
} Stack;
Stack* CreateStack();
void PushStackFrame(Stack *stack);
void PopStackFrame(Stack *stack);
void AddNode(Stack *stack, Node *statementNode);
Node** GetNodes(Stack *stack, uint32_t *pCount);
#endif /* WRAITH_STACK_H */