Begun refactoring the Parser to generate a list of Stmt which the Interpreter will then execute (to be done).
This commit is contained in:
@@ -13,6 +13,10 @@ void RunPrompt(void);
|
||||
char* GetFileContents(const char*);
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
#ifdef DEBUG
|
||||
printf("Debug clox build.\n");
|
||||
#endif
|
||||
|
||||
if (argc > 2) {
|
||||
printf("Useage: clox [script]\n");
|
||||
exit(EX_USAGE);
|
||||
@@ -40,13 +44,15 @@ void RunFile(const char* path) {
|
||||
TokenList* tokens = ScanTokens(contents);
|
||||
free(contents);
|
||||
printf("TOKENS:\n");
|
||||
Print(tokens);
|
||||
//Print(tokens);
|
||||
printf("EXPRESSION TREE:\n");
|
||||
Expr* tree = GenerateExpressionTree(tokens);
|
||||
PrintExpressionTree(tree);
|
||||
//Expr* tree = GenerateExpressionTree(tokens);
|
||||
StatementList* statements = Parse(tokens);
|
||||
//PrintExpressionTree(tree);
|
||||
printf("\n");
|
||||
Interpret(tree);
|
||||
FreeExpressionTree(tree);
|
||||
FreeStatementList(statements);
|
||||
//FreeExpressionTree(tree);
|
||||
DestroyTokenList(tokens);
|
||||
}
|
||||
|
||||
|
||||
+72
-2
@@ -3,6 +3,8 @@
|
||||
#include "statement.h"
|
||||
#include "token.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Expr* Expression(void);
|
||||
Expr* Equality(void);
|
||||
@@ -14,6 +16,8 @@ Expr* Primary(void);
|
||||
Stmt* Statement(void);
|
||||
Stmt* PrintStatement(void);
|
||||
Stmt* ExpressionStatement(void);
|
||||
StatementList* CreateStatementList(void);
|
||||
int AddStatementToList(Stmt*, StatementList*);
|
||||
int Match(int, ...);
|
||||
int Check(TokenType);
|
||||
int ParserAtEnd(void);
|
||||
@@ -21,14 +25,29 @@ Token* ParserPeek(void);
|
||||
Token* Previous(void);
|
||||
Token* AdvanceParser(void);
|
||||
void SynchronizeParser(void);
|
||||
void FreeExpressionTree(Expr*);
|
||||
|
||||
const TokenList* ListOfTokens;
|
||||
int Current = 0;
|
||||
|
||||
Expr* GenerateExpressionTree(const TokenList* list) {
|
||||
StatementList* Parse(const TokenList* list) {
|
||||
ListOfTokens = list;
|
||||
StatementList* statements = CreateStatementList();
|
||||
Stmt* statement = NULL;
|
||||
|
||||
return Expression();
|
||||
if (!statements) return NULL;
|
||||
|
||||
while(!ParserAtEnd()) {
|
||||
statement = Statement();
|
||||
if (!statement) {
|
||||
fprintf(stderr, "[Warn] No statement generated\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
AddStatementToList(statement, statements);
|
||||
}
|
||||
|
||||
return statements;
|
||||
}
|
||||
|
||||
//Simply expands the equality rule
|
||||
@@ -285,4 +304,55 @@ void FreeExpressionTree(Expr* tree) {
|
||||
}
|
||||
|
||||
free(tree);
|
||||
}
|
||||
|
||||
void FreeStatementList(StatementList* list) {
|
||||
if (!list) return;
|
||||
|
||||
for(int i = 0; i < list->size; i++) {
|
||||
FreeExpressionTree(list->content[i]->expression);
|
||||
free(list->content[i]);
|
||||
}
|
||||
|
||||
free(list);
|
||||
}
|
||||
|
||||
StatementList* CreateStatementList(void) {
|
||||
StatementList* list = calloc(1, sizeof(StatementList));
|
||||
|
||||
if (!list) {
|
||||
fprintf(stderr, "Failed to calloc StatementList. %s\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list->content = calloc(STATEMENTLIST_DEFAULT_SIZE, sizeof(Stmt));
|
||||
|
||||
if (!list->content) {
|
||||
fprintf(stderr, "Failed to calloc StatementList contents. %s\n", strerror(errno));
|
||||
free(list);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
int AddStatementToList(Stmt* statement, StatementList* list) {
|
||||
if (!statement || !list) return 0;
|
||||
|
||||
if (list->size == list->capacity) {
|
||||
void* new_ptr = realloc(list->content, sizeof(Stmt) * list->capacity * 2);
|
||||
|
||||
if (!new_ptr) {
|
||||
fprintf(stderr, "Failed to realloc StatmentList to size %d. %s\n", list->capacity * 2, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
list->content = new_ptr;
|
||||
list->capacity *= 2;
|
||||
}
|
||||
|
||||
list->content[list->size] = statement;
|
||||
list->size++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
+10
-2
@@ -9,8 +9,16 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Expr* GenerateExpressionTree(const TokenList* list);
|
||||
#define STATEMENTLIST_DEFAULT_SIZE 32
|
||||
|
||||
typedef struct {
|
||||
Stmt** content;
|
||||
int size;
|
||||
int capacity;
|
||||
} StatementList;
|
||||
|
||||
StatementList* Parse(const TokenList*);
|
||||
void PrintExpressionTree(const Expr*);
|
||||
void FreeExpressionTree(Expr*);
|
||||
void FreeStatementList(StatementList*);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user