Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
70a3118a99 | ||
|
|
842473742e |
@@ -1,5 +1,5 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS=-g -Wall -DDEBUG
|
CFLAGS=-g -Wall -DDEBUG -Wpedantic
|
||||||
SRCDIR=src
|
SRCDIR=src
|
||||||
OBJDIR=obj
|
OBJDIR=obj
|
||||||
SRCS=$(wildcard $(SRCDIR)/*.c)
|
SRCS=$(wildcard $(SRCDIR)/*.c)
|
||||||
@@ -11,7 +11,7 @@ BIN=$(BINDIR)/clox
|
|||||||
|
|
||||||
all: $(BIN)
|
all: $(BIN)
|
||||||
|
|
||||||
release: CFLAGS=-Wall -O2
|
release: CFLAGS=-Wall -Wpedantic -O2
|
||||||
release: clean
|
release: clean
|
||||||
release: $(BIN)
|
release: $(BIN)
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ void RunPrompt(void);
|
|||||||
char* GetFileContents(const char*);
|
char* GetFileContents(const char*);
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("Debug clox build.\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
printf("Useage: clox [script]\n");
|
printf("Useage: clox [script]\n");
|
||||||
exit(EX_USAGE);
|
exit(EX_USAGE);
|
||||||
@@ -40,13 +44,15 @@ void RunFile(const char* path) {
|
|||||||
TokenList* tokens = ScanTokens(contents);
|
TokenList* tokens = ScanTokens(contents);
|
||||||
free(contents);
|
free(contents);
|
||||||
printf("TOKENS:\n");
|
printf("TOKENS:\n");
|
||||||
Print(tokens);
|
//Print(tokens);
|
||||||
printf("EXPRESSION TREE:\n");
|
printf("EXPRESSION TREE:\n");
|
||||||
Expr* tree = GenerateExpressionTree(tokens);
|
//Expr* tree = GenerateExpressionTree(tokens);
|
||||||
PrintExpressionTree(tree);
|
StatementList* statements = Parse(tokens);
|
||||||
|
//PrintExpressionTree(tree);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
Interpret(tree);
|
//Interpret(tree);
|
||||||
FreeExpressionTree(tree);
|
FreeStatementList(statements);
|
||||||
|
//FreeExpressionTree(tree);
|
||||||
DestroyTokenList(tokens);
|
DestroyTokenList(tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+82
-4
@@ -3,6 +3,8 @@
|
|||||||
#include "statement.h"
|
#include "statement.h"
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
Expr* Expression(void);
|
Expr* Expression(void);
|
||||||
Expr* Equality(void);
|
Expr* Equality(void);
|
||||||
@@ -14,6 +16,8 @@ Expr* Primary(void);
|
|||||||
Stmt* Statement(void);
|
Stmt* Statement(void);
|
||||||
Stmt* PrintStatement(void);
|
Stmt* PrintStatement(void);
|
||||||
Stmt* ExpressionStatement(void);
|
Stmt* ExpressionStatement(void);
|
||||||
|
StatementList* CreateStatementList(void);
|
||||||
|
int AddStatementToList(Stmt*, StatementList*);
|
||||||
int Match(int, ...);
|
int Match(int, ...);
|
||||||
int Check(TokenType);
|
int Check(TokenType);
|
||||||
int ParserAtEnd(void);
|
int ParserAtEnd(void);
|
||||||
@@ -21,14 +25,29 @@ Token* ParserPeek(void);
|
|||||||
Token* Previous(void);
|
Token* Previous(void);
|
||||||
Token* AdvanceParser(void);
|
Token* AdvanceParser(void);
|
||||||
void SynchronizeParser(void);
|
void SynchronizeParser(void);
|
||||||
|
void FreeExpressionTree(Expr*);
|
||||||
|
|
||||||
const TokenList* ListOfTokens;
|
const TokenList* ListOfTokens;
|
||||||
int Current = 0;
|
int Current = 0;
|
||||||
|
|
||||||
Expr* GenerateExpressionTree(const TokenList* list) {
|
StatementList* Parse(const TokenList* list) {
|
||||||
ListOfTokens = 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
|
//Simply expands the equality rule
|
||||||
@@ -45,7 +64,10 @@ Stmt* Statement(void) {
|
|||||||
Stmt* PrintStatement(void) {
|
Stmt* PrintStatement(void) {
|
||||||
Expr* value = Expression();
|
Expr* value = Expression();
|
||||||
|
|
||||||
if (!Match(1, Semicolon)) fprintf(stderr, "Expected ';' after value\n");
|
if (!Match(1, Semicolon)) {
|
||||||
|
fprintf(stderr, "Expected ';' after value\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
@@ -55,7 +77,10 @@ Stmt* PrintStatement(void) {
|
|||||||
Stmt* ExpressionStatement(void) {
|
Stmt* ExpressionStatement(void) {
|
||||||
Expr* expr = Expression();
|
Expr* expr = Expression();
|
||||||
|
|
||||||
if (!Match(1, Semicolon)) fprintf(stderr, "Expected ';' after expression\n");
|
if (!Match(1, Semicolon)) {
|
||||||
|
fprintf(stderr, "Expected ';' after expression\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
@@ -286,3 +311,56 @@ void FreeExpressionTree(Expr* tree) {
|
|||||||
|
|
||||||
free(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
list->capacity = STATEMENTLIST_DEFAULT_SIZE;
|
||||||
|
|
||||||
|
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 <stdlib.h>
|
||||||
#include <string.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 PrintExpressionTree(const Expr*);
|
||||||
void FreeExpressionTree(Expr*);
|
void FreeStatementList(StatementList*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user