forked from cosmonaut/wraith-lang
parser sequence refactor
parent
a320086038
commit
7ca87d6b13
|
@ -42,11 +42,9 @@ add_executable(
|
|||
src/ast.h
|
||||
src/codegen.h
|
||||
src/parser.h
|
||||
src/stack.h
|
||||
src/ast.c
|
||||
src/codegen.c
|
||||
src/parser.c
|
||||
src/stack.c
|
||||
src/main.c
|
||||
# Generated code
|
||||
${BISON_Parser_OUTPUTS}
|
||||
|
|
|
@ -15,6 +15,11 @@ struct MyStruct
|
|||
myInt = myInt + 1;
|
||||
}
|
||||
|
||||
Decrement(): void
|
||||
{
|
||||
myInt = myInt - 1;
|
||||
}
|
||||
|
||||
static MyFunction(input: int): int
|
||||
{
|
||||
return input * 2;
|
||||
|
@ -40,6 +45,10 @@ struct Program
|
|||
{
|
||||
myStruct.Increment();
|
||||
}
|
||||
else
|
||||
{
|
||||
myStruct.Decrement();
|
||||
}
|
||||
|
||||
return myStruct.myInt;
|
||||
}
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
%code requires {
|
||||
#include "../src/stack.h"
|
||||
#include "../src/ast.h"
|
||||
}
|
||||
|
||||
%{
|
||||
#include <stdio.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);
|
||||
}
|
||||
|
@ -62,7 +61,7 @@ extern FILE *yyin;
|
|||
%token COMMENT
|
||||
%token NEWLINE
|
||||
|
||||
%parse-param { FILE* fp } { Stack *stack } { Node **pRootNode }
|
||||
%parse-param { FILE* fp } { Node **pRootNode }
|
||||
|
||||
%define parse.error verbose
|
||||
|
||||
|
@ -73,16 +72,7 @@ extern FILE *yyin;
|
|||
%%
|
||||
Program : TopLevelDeclarations
|
||||
{
|
||||
Node **declarations;
|
||||
Node *declarationSequence;
|
||||
uint32_t declarationCount;
|
||||
|
||||
declarations = GetNodes(stack, &declarationCount);
|
||||
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
|
||||
|
||||
PopStackFrame(stack);
|
||||
|
||||
*pRootNode = declarationSequence;
|
||||
*pRootNode = $1;
|
||||
}
|
||||
|
||||
BaseType : VOID
|
||||
|
@ -220,13 +210,7 @@ ReturnStatement : RETURN Expression
|
|||
|
||||
FunctionCallExpression : AccessExpression LEFT_PAREN Arguments RIGHT_PAREN
|
||||
{
|
||||
Node **arguments;
|
||||
uint32_t argumentCount;
|
||||
|
||||
arguments = GetNodes(stack, &argumentCount);
|
||||
$$ = MakeFunctionCallExpressionNode($1, MakeFunctionArgumentSequenceNode(arguments, argumentCount));
|
||||
|
||||
PopStackFrame(stack);
|
||||
$$ = MakeFunctionCallExpressionNode($1, $3);
|
||||
}
|
||||
|
||||
PartialStatement : FunctionCallExpression
|
||||
|
@ -237,91 +221,68 @@ PartialStatement : FunctionCallExpression
|
|||
|
||||
IfStatement : IF LEFT_PAREN Expression RIGHT_PAREN LEFT_BRACE Statements RIGHT_BRACE
|
||||
{
|
||||
Node **statements;
|
||||
Node *statementSequence;
|
||||
uint32_t statementCount;
|
||||
$$ = MakeIfNode($3, $6);
|
||||
}
|
||||
|
||||
statements = GetNodes(stack, &statementCount);
|
||||
statementSequence = MakeStatementSequenceNode(statements, statementCount);
|
||||
|
||||
$$ = MakeIfNode($3, statementSequence);
|
||||
|
||||
PopStackFrame(stack);
|
||||
Conditional : IfStatement
|
||||
| IfStatement ELSE LEFT_BRACE Statements RIGHT_BRACE
|
||||
{
|
||||
$$ = MakeIfElseNode($1, $4);
|
||||
}
|
||||
|
||||
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);
|
||||
AddNode(stack, $1);
|
||||
$$ = AddFunctionArgumentNode($1, $3);
|
||||
}
|
||||
|
|
||||
{
|
||||
PushStackFrame(stack);
|
||||
$$ = MakeEmptyFunctionArgumentSequenceNode();
|
||||
}
|
||||
;
|
||||
|
||||
SignatureArguments : VariableDeclaration COMMA SignatureArguments
|
||||
SignatureArguments : VariableDeclaration
|
||||
{
|
||||
AddNode(stack, $1);
|
||||
$$ = StartFunctionSignatureArgumentsNode($1);
|
||||
}
|
||||
| VariableDeclaration
|
||||
| SignatureArguments COMMA VariableDeclaration
|
||||
{
|
||||
PushStackFrame(stack);
|
||||
AddNode(stack, $1);
|
||||
$$ = AddFunctionSignatureArgumentNode($1, $3);
|
||||
}
|
||||
|
|
||||
{
|
||||
$$ = MakeEmptyFunctionSignatureArgumentsNode();
|
||||
}
|
||||
;
|
||||
|
||||
Body : LEFT_BRACE Statements RIGHT_BRACE
|
||||
{
|
||||
Node **statements;
|
||||
Node *statementSequence;
|
||||
uint32_t statementCount;
|
||||
|
||||
statements = GetNodes(stack, &statementCount);
|
||||
statementSequence = MakeStatementSequenceNode(statements, statementCount);
|
||||
$$ = MakeStatementSequenceNode(statements, statementCount);
|
||||
|
||||
PopStackFrame(stack);
|
||||
$$ = $2;
|
||||
}
|
||||
|
||||
FunctionSignature : Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
|
||||
{
|
||||
Node **declarations;
|
||||
uint32_t declarationCount;
|
||||
|
||||
declarations = GetNodes(stack, &declarationCount);
|
||||
$$ = MakeFunctionSignatureNode($1, $6, MakeFunctionSignatureArgumentsNode(declarations, declarationCount), MakeFunctionModifiersNode(NULL, 0));
|
||||
|
||||
PopStackFrame(stack);
|
||||
$$ = MakeFunctionSignatureNode($1, $6, $3, MakeFunctionModifiersNode(NULL, 0));
|
||||
}
|
||||
| STATIC Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
|
||||
{
|
||||
Node **declarations;
|
||||
uint32_t declarationCount;
|
||||
Node *modifier = MakeStaticNode();
|
||||
|
||||
declarations = GetNodes(stack, &declarationCount);
|
||||
$$ = MakeFunctionSignatureNode($2, $7, MakeFunctionSignatureArgumentsNode(declarations, declarationCount), MakeFunctionModifiersNode(&modifier, 1));
|
||||
|
||||
PopStackFrame(stack);
|
||||
$$ = MakeFunctionSignatureNode($2, $7, $4, MakeFunctionModifiersNode(&modifier, 1));
|
||||
}
|
||||
|
||||
FunctionDeclaration : FunctionSignature Body
|
||||
|
@ -331,39 +292,30 @@ FunctionDeclaration : FunctionSignature Body
|
|||
|
||||
StructDeclaration : STRUCT Identifier LEFT_BRACE Declarations RIGHT_BRACE
|
||||
{
|
||||
Node **declarations;
|
||||
Node *declarationSequence;
|
||||
uint32_t declarationCount;
|
||||
|
||||
declarations = GetNodes(stack, &declarationCount);
|
||||
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
|
||||
$$ = MakeStructDeclarationNode($2, declarationSequence);
|
||||
|
||||
PopStackFrame(stack);
|
||||
$$ = MakeStructDeclarationNode($2, $4);
|
||||
}
|
||||
|
||||
|
||||
Declaration : FunctionDeclaration
|
||||
| VariableDeclaration SEMICOLON
|
||||
;
|
||||
|
||||
Declarations : Declaration Declarations
|
||||
Declarations : Declaration
|
||||
{
|
||||
AddNode(stack, $1);
|
||||
$$ = StartDeclarationSequenceNode($1);
|
||||
}
|
||||
|
|
||||
| Declarations Declaration
|
||||
{
|
||||
PushStackFrame(stack);
|
||||
$$ = AddDeclarationNode($1, $2);
|
||||
}
|
||||
|
||||
TopLevelDeclaration : StructDeclaration;
|
||||
|
||||
TopLevelDeclarations : TopLevelDeclaration TopLevelDeclarations
|
||||
TopLevelDeclarations : TopLevelDeclaration
|
||||
{
|
||||
AddNode(stack, $1);
|
||||
$$ = StartDeclarationSequenceNode($1);
|
||||
}
|
||||
|
|
||||
| TopLevelDeclarations TopLevelDeclaration
|
||||
{
|
||||
PushStackFrame(stack);
|
||||
$$ = AddDeclarationNode($1, $2);
|
||||
}
|
||||
%%
|
||||
|
|
131
src/ast.c
131
src/ast.c
|
@ -36,6 +36,8 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
|
|||
case FunctionSignature: return "FunctionSignature";
|
||||
case FunctionSignatureArguments: return "FunctionSignatureArguments";
|
||||
case Identifier: return "Identifier";
|
||||
case IfStatement: return "If";
|
||||
case IfElseStatement: return "IfElse";
|
||||
case Number: return "Number";
|
||||
case PrimitiveTypeNode: return "PrimitiveTypeNode";
|
||||
case ReferenceTypeNode: return "ReferenceTypeNode";
|
||||
|
@ -211,22 +213,27 @@ Node* MakeAssignmentNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeStatementSequenceNode(
|
||||
Node** pNodes,
|
||||
uint32_t nodeCount
|
||||
Node* StartStatementSequenceNode(
|
||||
Node *statementNode
|
||||
) {
|
||||
int32_t i;
|
||||
Node* node = (Node*) malloc(sizeof(Node));
|
||||
node->syntaxKind = StatementSequence;
|
||||
node->children = (Node**) malloc(sizeof(Node*) * nodeCount);
|
||||
node->childCount = nodeCount;
|
||||
for (i = nodeCount - 1; i >= 0; i -= 1)
|
||||
{
|
||||
node->children[nodeCount - 1 - i] = pNodes[i];
|
||||
}
|
||||
node->children = (Node**) malloc(sizeof(Node*));
|
||||
node->childCount = 1;
|
||||
node->children[0] = statementNode;
|
||||
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 *expressionNode
|
||||
) {
|
||||
|
@ -247,21 +254,33 @@ Node* MakeReturnVoidStatementNode()
|
|||
return node;
|
||||
}
|
||||
|
||||
Node *MakeFunctionSignatureArgumentsNode(
|
||||
Node **pArgumentNodes,
|
||||
uint32_t argumentCount
|
||||
Node *StartFunctionSignatureArgumentsNode(
|
||||
Node *argumentNode
|
||||
) {
|
||||
int32_t i;
|
||||
Node* node = (Node*) malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionSignatureArguments;
|
||||
node->childCount = argumentCount;
|
||||
node->children = (Node**) malloc(sizeof(Node*) * (node->childCount));
|
||||
node->childCount = 1;
|
||||
node->children = (Node**) malloc(sizeof(Node*));
|
||||
node->children[0] = argumentNode;
|
||||
return node;
|
||||
}
|
||||
|
||||
for (i = argumentCount - 1; i >= 0; i -= 1)
|
||||
{
|
||||
node->children[argumentCount - 1 - i] = pArgumentNodes[i];
|
||||
}
|
||||
Node* AddFunctionSignatureArgumentNode(
|
||||
Node *argumentsNode,
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -308,35 +327,54 @@ Node* MakeStructDeclarationNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeDeclarationSequenceNode(
|
||||
Node **pNodes,
|
||||
uint32_t nodeCount
|
||||
Node* StartDeclarationSequenceNode(
|
||||
Node *declarationNode
|
||||
) {
|
||||
int32_t i;
|
||||
Node* node = (Node*) malloc(sizeof(Node));
|
||||
node->syntaxKind = DeclarationSequence;
|
||||
node->children = (Node**) malloc(sizeof(Node*) * nodeCount);
|
||||
node->childCount = nodeCount;
|
||||
for (i = nodeCount - 1; i >= 0; i -= 1)
|
||||
{
|
||||
node->children[nodeCount - 1 - i] = pNodes[i];
|
||||
}
|
||||
node->children = (Node**) malloc(sizeof(Node*));
|
||||
node->childCount = 1;
|
||||
node->children[0] = declarationNode;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node *MakeFunctionArgumentSequenceNode(
|
||||
Node **pArgumentNodes,
|
||||
uint32_t argumentCount
|
||||
Node* AddDeclarationNode(
|
||||
Node *declarationSequenceNode,
|
||||
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->syntaxKind = FunctionArgumentSequence;
|
||||
node->childCount = argumentCount;
|
||||
node->children = (Node**) malloc(sizeof(Node*) * node->childCount);
|
||||
for (i = argumentCount - 1; i >= 0; i -= 1)
|
||||
{
|
||||
node->children[argumentCount - 1 - i] = pArgumentNodes[i];
|
||||
}
|
||||
node->childCount = 1;
|
||||
node->children = (Node**) malloc(sizeof(Node*));
|
||||
node->children[0] = argumentNode;
|
||||
return node;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -389,6 +427,19 @@ Node* MakeIfNode(
|
|||
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)
|
||||
{
|
||||
switch (type)
|
||||
|
|
45
src/ast.h
45
src/ast.h
|
@ -23,6 +23,7 @@ typedef enum
|
|||
FunctionSignatureArguments,
|
||||
Identifier,
|
||||
IfStatement,
|
||||
IfElseStatement,
|
||||
Number,
|
||||
PrimitiveTypeNode,
|
||||
ReferenceTypeNode,
|
||||
|
@ -131,22 +132,29 @@ Node* MakeAssignmentNode(
|
|||
Node *left,
|
||||
Node *right
|
||||
);
|
||||
Node* MakeStatementSequenceNode(
|
||||
Node** pNodes,
|
||||
uint32_t nodeCount
|
||||
Node* StartStatementSequenceNode(
|
||||
Node* statementNode
|
||||
);
|
||||
Node* AddStatement(
|
||||
Node* statementSequenceNode,
|
||||
Node *statementNode
|
||||
);
|
||||
Node* MakeReturnStatementNode(
|
||||
Node *expressionNode
|
||||
);
|
||||
Node* MakeReturnVoidStatementNode();
|
||||
Node* MakeFunctionSignatureArgumentsNode(
|
||||
Node **pArgumentNodes,
|
||||
uint32_t argumentCount
|
||||
Node* StartFunctionSignatureArgumentsNode(
|
||||
Node *argumentNode
|
||||
);
|
||||
Node* AddFunctionSignatureArgumentNode(
|
||||
Node *argumentsNode,
|
||||
Node *argumentNode
|
||||
);
|
||||
Node *MakeEmptyFunctionSignatureArgumentsNode();
|
||||
Node* MakeFunctionSignatureNode(
|
||||
Node *identifierNode,
|
||||
Node* typeNode,
|
||||
Node* arguments,
|
||||
Node* argumentsNode,
|
||||
Node* modifiersNode
|
||||
);
|
||||
Node* MakeFunctionDeclarationNode(
|
||||
|
@ -157,14 +165,21 @@ Node* MakeStructDeclarationNode(
|
|||
Node *identifierNode,
|
||||
Node *declarationSequenceNode
|
||||
);
|
||||
Node* MakeDeclarationSequenceNode(
|
||||
Node **pNodes,
|
||||
uint32_t nodeCount
|
||||
Node* StartDeclarationSequenceNode(
|
||||
Node *declarationNode
|
||||
);
|
||||
Node *MakeFunctionArgumentSequenceNode(
|
||||
Node **pArgumentNodes,
|
||||
uint32_t argumentCount
|
||||
Node* AddDeclarationNode(
|
||||
Node *declarationSequenceNode,
|
||||
Node *declarationNode
|
||||
);
|
||||
Node *StartFunctionArgumentSequenceNode(
|
||||
Node *argumentNode
|
||||
);
|
||||
Node *AddFunctionArgumentNode(
|
||||
Node *argumentSequenceNode,
|
||||
Node *argumentNode
|
||||
);
|
||||
Node *MakeEmptyFunctionArgumentSequenceNode();
|
||||
Node* MakeFunctionCallExpressionNode(
|
||||
Node *identifierNode,
|
||||
Node *argumentSequenceNode
|
||||
|
@ -180,6 +195,10 @@ Node* MakeIfNode(
|
|||
Node *expressionNode,
|
||||
Node *statementSequenceNode
|
||||
);
|
||||
Node* MakeIfElseNode(
|
||||
Node *ifNode,
|
||||
Node *statementSequenceNode
|
||||
);
|
||||
|
||||
void PrintTree(Node *node, uint32_t tabCount);
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
#include "y.tab.h"
|
||||
#include "ast.h"
|
||||
#include "stack.h"
|
||||
|
||||
extern FILE *yyin;
|
||||
extern int yydebug;
|
||||
|
@ -10,14 +9,13 @@ extern int yydebug;
|
|||
int Parse(char *inputFilename, Node **pRootNode, uint8_t parseVerbose)
|
||||
{
|
||||
int result;
|
||||
Stack *stack = CreateStack();
|
||||
yydebug = parseVerbose;
|
||||
|
||||
FILE *fp = fopen(inputFilename, "r");
|
||||
if (fp != NULL)
|
||||
{
|
||||
yyin = fp;
|
||||
result = yyparse(fp, stack, pRootNode);
|
||||
result = yyparse(fp, pRootNode);
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
|
|
65
src/stack.c
65
src/stack.c
|
@ -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;
|
||||
}
|
27
src/stack.h
27
src/stack.h
|
@ -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 */
|
Loading…
Reference in New Issue