Further refactoring done. The Parser runs without crashing, however it's output is still to be checked and verified.
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
#include "scanner.h"
|
#include "scanner.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include "expr.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sysexits.h>
|
#include <sysexits.h>
|
||||||
@@ -37,7 +39,8 @@ void RunFile(const char* path) {
|
|||||||
TokenList* tokens = ScanTokens(contents);
|
TokenList* tokens = ScanTokens(contents);
|
||||||
free(contents);
|
free(contents);
|
||||||
Print(tokens);
|
Print(tokens);
|
||||||
|
Expr* tree = GenerateExpressionTree(tokens);
|
||||||
|
FreeExpressionTree(tree);
|
||||||
DestroyTokenList(tokens);
|
DestroyTokenList(tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,13 +7,12 @@ Expr* Term(void);
|
|||||||
Expr* Factor(void);
|
Expr* Factor(void);
|
||||||
Expr* Unary(void);
|
Expr* Unary(void);
|
||||||
Expr* Primary(void);
|
Expr* Primary(void);
|
||||||
int SMatch(int, ...);
|
int Match(int, ...);
|
||||||
int Check(TokenType);
|
int Check(TokenType);
|
||||||
int SIsAtEnd(void);
|
int ParserAtEnd(void);
|
||||||
Token* SPeek(void);
|
Token* ParserPeek(void);
|
||||||
Token* Previous(void);
|
Token* Previous(void);
|
||||||
Token* SAdvance(void);
|
Token* AdvanceParser(void);
|
||||||
//Token* CreateToken(char*, TokenType);
|
|
||||||
|
|
||||||
const TokenList* ListOfTokens;
|
const TokenList* ListOfTokens;
|
||||||
int Current = 0;
|
int Current = 0;
|
||||||
@@ -32,7 +31,7 @@ Expr* Expression() {
|
|||||||
Expr* Equality() {
|
Expr* Equality() {
|
||||||
Expr* expr = Comparison();
|
Expr* expr = Comparison();
|
||||||
|
|
||||||
while(SMatch(2, Bang_Equal, Equal_Equal)) {
|
while(Match(2, Bang_Equal, Equal_Equal)) {
|
||||||
Token* operator = Previous();
|
Token* operator = Previous();
|
||||||
Expr* right = Comparison();
|
Expr* right = Comparison();
|
||||||
Expr* temp = calloc(1, sizeof(Expr));
|
Expr* temp = calloc(1, sizeof(Expr));
|
||||||
@@ -49,7 +48,7 @@ Expr* Equality() {
|
|||||||
Expr* Comparison() {
|
Expr* Comparison() {
|
||||||
Expr* expr = Term();
|
Expr* expr = Term();
|
||||||
|
|
||||||
while(SMatch(4, Greater, Greater_Equal, Less, Less_Equal)) {
|
while(Match(4, Greater, Greater_Equal, Less, Less_Equal)) {
|
||||||
Token* operator = Previous();
|
Token* operator = Previous();
|
||||||
Expr* right = Term();
|
Expr* right = Term();
|
||||||
Expr* temp = calloc(1, sizeof(Expr));
|
Expr* temp = calloc(1, sizeof(Expr));
|
||||||
@@ -66,7 +65,7 @@ Expr* Comparison() {
|
|||||||
Expr* Term() {
|
Expr* Term() {
|
||||||
Expr* expr = Factor();
|
Expr* expr = Factor();
|
||||||
|
|
||||||
while(SMatch(2, Minus, Plus)) {
|
while(Match(2, Minus, Plus)) {
|
||||||
Token* operator = Previous();
|
Token* operator = Previous();
|
||||||
Expr* right = Factor();
|
Expr* right = Factor();
|
||||||
Expr* temp = calloc(1, sizeof(Expr));
|
Expr* temp = calloc(1, sizeof(Expr));
|
||||||
@@ -83,7 +82,7 @@ Expr* Term() {
|
|||||||
Expr* Factor() {
|
Expr* Factor() {
|
||||||
Expr* expr = Unary();
|
Expr* expr = Unary();
|
||||||
|
|
||||||
while(SMatch(2, Slash, Star)) {
|
while(Match(2, Slash, Star)) {
|
||||||
Token* operator = Previous();
|
Token* operator = Previous();
|
||||||
Expr* right = Unary();
|
Expr* right = Unary();
|
||||||
Expr* temp = calloc(1, sizeof(Expr));
|
Expr* temp = calloc(1, sizeof(Expr));
|
||||||
@@ -98,7 +97,7 @@ Expr* Factor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Expr* Unary() {
|
Expr* Unary() {
|
||||||
if (SMatch(2, Bang, Minus)) {
|
if (Match(2, Bang, Minus)) {
|
||||||
Token* operator = Previous();
|
Token* operator = Previous();
|
||||||
Expr* right = Unary();
|
Expr* right = Unary();
|
||||||
Expr* expr = calloc(1, sizeof(Expr));
|
Expr* expr = calloc(1, sizeof(Expr));
|
||||||
@@ -115,8 +114,8 @@ Expr* Primary() {
|
|||||||
Expr* expr = calloc(1, sizeof(Expr));
|
Expr* expr = calloc(1, sizeof(Expr));
|
||||||
expr->type = LITERAL;
|
expr->type = LITERAL;
|
||||||
|
|
||||||
if (SMatch(3, FALSE, TRUE, NIL)) {
|
if (Match(3, FALSE, TRUE, NIL)) {
|
||||||
expr->expression.Literal.type = SPeek();//CreateToken("false", FALSE);
|
expr->expression.Literal.type = ParserPeek();//CreateToken("false", FALSE);
|
||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
// if (SMatch(1, TRUE)) {
|
// if (SMatch(1, TRUE)) {
|
||||||
@@ -128,12 +127,12 @@ Expr* Primary() {
|
|||||||
// return expr;
|
// return expr;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (SMatch(2, Number, String)) {
|
if (Match(2, Number, String)) {
|
||||||
expr->expression.Literal.type = Previous();
|
expr->expression.Literal.type = Previous();
|
||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SMatch(1, LParen)) {
|
if (Match(1, LParen)) {
|
||||||
free(expr);
|
free(expr);
|
||||||
expr = Expression();
|
expr = Expression();
|
||||||
//Consume(RParen, "Expect ')' after expression.");
|
//Consume(RParen, "Expect ')' after expression.");
|
||||||
@@ -144,13 +143,13 @@ Expr* Primary() {
|
|||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SMatch(int count, ...) {
|
int Match(int count, ...) {
|
||||||
va_list list;
|
va_list list;
|
||||||
va_start(list, count);
|
va_start(list, count);
|
||||||
|
|
||||||
for(int i = 0; i < count; i++) {
|
for(int i = 0; i < count; i++) {
|
||||||
if(Check(va_arg(list, TokenType))) {
|
if(Check(va_arg(list, TokenType))) {
|
||||||
SAdvance();
|
AdvanceParser();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -159,15 +158,15 @@ int SMatch(int count, ...) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int Check(TokenType type) {
|
int Check(TokenType type) {
|
||||||
if (SIsAtEnd()) return 0;
|
if (ParserAtEnd()) return 0;
|
||||||
return SPeek()->type == type;
|
return ParserPeek()->type == type;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SIsAtEnd() {
|
int ParserAtEnd() {
|
||||||
return SPeek()->type == EndOF;
|
return ParserPeek()->type == EndOF;
|
||||||
}
|
}
|
||||||
|
|
||||||
Token* SPeek() {
|
Token* ParserPeek() {
|
||||||
return ListOfTokens->tokens[Current];
|
return ListOfTokens->tokens[Current];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,17 +174,29 @@ Token* Previous() {
|
|||||||
return ListOfTokens->tokens[Current - 1];
|
return ListOfTokens->tokens[Current - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
Token* SAdvance() {
|
Token* AdvanceParser() {
|
||||||
if (!SIsAtEnd()) Current++;
|
if (!ParserAtEnd()) Current++;
|
||||||
|
|
||||||
return Previous();
|
return Previous();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token* CreateToken(char* lexeme, TokenType type) {
|
void PrintExpressionTree(const Expr* tree) {
|
||||||
// Token* token = calloc(1, sizeof(Token));
|
|
||||||
// token->type = type;
|
|
||||||
// token->lexeme = lexeme;
|
|
||||||
// token->length = strlen(lexeme);
|
|
||||||
|
|
||||||
// return token;
|
}
|
||||||
// }
|
|
||||||
|
void FreeExpressionTree(Expr* tree) {
|
||||||
|
if (!tree) return;
|
||||||
|
|
||||||
|
if (tree->type == BINARY) {
|
||||||
|
FreeExpressionTree(tree->expression.Binary.left);
|
||||||
|
FreeExpressionTree(tree->expression.Binary.right);
|
||||||
|
//FreeToken(tree->expression.Binary.op);
|
||||||
|
}
|
||||||
|
else if (tree->type == UNARY) {
|
||||||
|
FreeExpressionTree(tree->expression.Unary.right);
|
||||||
|
//FreeToken(tree->expression.Unary.op);
|
||||||
|
}
|
||||||
|
//else if (tree->type == LITERAL) FreeToken(tree->expression.Literal.type);
|
||||||
|
|
||||||
|
free(tree);
|
||||||
|
}
|
||||||
@@ -9,5 +9,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
Expr* GenerateExpressionTree(const TokenList* list);
|
Expr* GenerateExpressionTree(const TokenList* list);
|
||||||
|
void PrintExpressionTree(const Expr*);
|
||||||
|
void FreeExpressionTree(Expr*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -8,13 +8,13 @@ int current; //points to the character currently being considered.
|
|||||||
int length;
|
int length;
|
||||||
int line = 1;
|
int line = 1;
|
||||||
|
|
||||||
const char* SAdvance(void);
|
const char* AdvanceScanner(void);
|
||||||
int SIsAtEnd(void);
|
int ScannerAtEnd(void);
|
||||||
void ScanToken(TokenList*);
|
void ScanToken(TokenList*);
|
||||||
TokenList* CreateList(void);
|
TokenList* CreateList(void);
|
||||||
int AddToTokenList(Token*, TokenList*);
|
int AddToTokenList(Token*, TokenList*);
|
||||||
int SMatch(char);
|
int ScannerMatch(char);
|
||||||
char SPeek(void);
|
char ScannerPeek(void);
|
||||||
char PeekNext(void);
|
char PeekNext(void);
|
||||||
void ParseString(TokenList *);
|
void ParseString(TokenList *);
|
||||||
void ParseNumber(TokenList *);
|
void ParseNumber(TokenList *);
|
||||||
@@ -32,7 +32,7 @@ TokenList* ScanTokens(const char* source) {
|
|||||||
|
|
||||||
TokenList* tokens = CreateList();
|
TokenList* tokens = CreateList();
|
||||||
|
|
||||||
while(!SIsAtEnd()) {
|
while(!ScannerAtEnd()) {
|
||||||
start = current;
|
start = current;
|
||||||
ScanToken(tokens);
|
ScanToken(tokens);
|
||||||
}
|
}
|
||||||
@@ -68,11 +68,7 @@ void DestroyTokenList(TokenList* list) {
|
|||||||
if (!list) return;
|
if (!list) return;
|
||||||
|
|
||||||
for(int i = 0; i < list->size; i++) {
|
for(int i = 0; i < list->size; i++) {
|
||||||
//Only free objects that required allocation, i.e. not in the TokenTypeMappings
|
FreeToken(list->tokens[i]);
|
||||||
if (list->tokens[i]->type == String || list->tokens[i]->type == Number || list->tokens[i]->type == Identifier)
|
|
||||||
free((void *) list->tokens[i]->lexeme);
|
|
||||||
|
|
||||||
free(list->tokens[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(list->tokens);
|
free(list->tokens);
|
||||||
@@ -102,7 +98,7 @@ int AddToTokenList(Token* token, TokenList* list) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ScanToken(TokenList* tokens) {
|
void ScanToken(TokenList* tokens) {
|
||||||
const char* c = SAdvance();
|
const char* c = AdvanceScanner();
|
||||||
|
|
||||||
switch (*c) {
|
switch (*c) {
|
||||||
case '(':
|
case '(':
|
||||||
@@ -136,23 +132,23 @@ void ScanToken(TokenList* tokens) {
|
|||||||
AddToTokenList(CreateToken(NULL, line, Star), tokens);
|
AddToTokenList(CreateToken(NULL, line, Star), tokens);
|
||||||
break;
|
break;
|
||||||
case '!':
|
case '!':
|
||||||
if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Bang_Equal), tokens);
|
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Bang_Equal), tokens);
|
||||||
else AddToTokenList(CreateToken(NULL, line, Bang), tokens);
|
else AddToTokenList(CreateToken(NULL, line, Bang), tokens);
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Equal_Equal), tokens);
|
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Equal_Equal), tokens);
|
||||||
else AddToTokenList(CreateToken(NULL, line, Equal), tokens);
|
else AddToTokenList(CreateToken(NULL, line, Equal), tokens);
|
||||||
break;
|
break;
|
||||||
case '<':
|
case '<':
|
||||||
if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Less_Equal), tokens);
|
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Less_Equal), tokens);
|
||||||
else AddToTokenList(CreateToken(NULL, line, Less), tokens);
|
else AddToTokenList(CreateToken(NULL, line, Less), tokens);
|
||||||
break;
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Greater_Equal), tokens);
|
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Greater_Equal), tokens);
|
||||||
else AddToTokenList(CreateToken(NULL, line, Greater), tokens);
|
else AddToTokenList(CreateToken(NULL, line, Greater), tokens);
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
if (SMatch('/')) while(SPeek() != '\n' && !SIsAtEnd()) { SAdvance(); }
|
if (ScannerMatch('/')) while(ScannerPeek() != '\n' && !ScannerAtEnd()) { AdvanceScanner(); }
|
||||||
else AddToTokenList(CreateToken(NULL, line, Slash), tokens);
|
else AddToTokenList(CreateToken(NULL, line, Slash), tokens);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -180,16 +176,16 @@ void ScanToken(TokenList* tokens) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int SIsAtEnd() {
|
int ScannerAtEnd() {
|
||||||
return current >= length;
|
return current >= length;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* SAdvance() {
|
const char* AdvanceScanner() {
|
||||||
return &source_code[current++];
|
return &source_code[current++];
|
||||||
}
|
}
|
||||||
|
|
||||||
int SMatch(char expected) {
|
int ScannerMatch(char expected) {
|
||||||
if (SIsAtEnd()) return 0;
|
if (ScannerAtEnd()) return 0;
|
||||||
if (source_code[current] != expected) return 0;
|
if (source_code[current] != expected) return 0;
|
||||||
|
|
||||||
current++;
|
current++;
|
||||||
@@ -197,8 +193,8 @@ int SMatch(char expected) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char SPeek() {
|
char ScannerPeek() {
|
||||||
if (SIsAtEnd()) return '\0';
|
if (ScannerAtEnd()) return '\0';
|
||||||
|
|
||||||
return source_code[current];
|
return source_code[current];
|
||||||
}
|
}
|
||||||
@@ -206,12 +202,12 @@ char SPeek() {
|
|||||||
void ParseString(TokenList *list) {
|
void ParseString(TokenList *list) {
|
||||||
start = current; //The start is currently pointing to the first double quote so we need to move it
|
start = current; //The start is currently pointing to the first double quote so we need to move it
|
||||||
//to the next (first character) of the string literal.
|
//to the next (first character) of the string literal.
|
||||||
while(SPeek() != '"' && !SIsAtEnd()) {
|
while(ScannerPeek() != '"' && !ScannerAtEnd()) {
|
||||||
if (SPeek() == '\n') line++;
|
if (ScannerPeek() == '\n') line++;
|
||||||
SAdvance();
|
AdvanceScanner();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SIsAtEnd()) {
|
if (ScannerAtEnd()) {
|
||||||
fprintf(stderr, "Unterminated string.\n");
|
fprintf(stderr, "Unterminated string.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -227,16 +223,16 @@ void ParseString(TokenList *list) {
|
|||||||
|
|
||||||
AddToTokenList(CreateToken(lexeme, line, String), list);
|
AddToTokenList(CreateToken(lexeme, line, String), list);
|
||||||
|
|
||||||
SAdvance(); // The closing ".
|
AdvanceScanner(); // The closing ".
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParseNumber(TokenList* list) {
|
void ParseNumber(TokenList* list) {
|
||||||
while(isdigit(SPeek())) SAdvance();
|
while(isdigit(ScannerPeek())) AdvanceScanner();
|
||||||
|
|
||||||
if (SPeek() == '.' && isdigit(PeekNext())) {
|
if (ScannerPeek() == '.' && isdigit(PeekNext())) {
|
||||||
SAdvance();
|
AdvanceScanner();
|
||||||
|
|
||||||
while(isdigit(SPeek())) SAdvance();
|
while(isdigit(ScannerPeek())) AdvanceScanner();
|
||||||
}
|
}
|
||||||
|
|
||||||
char* lexeme = calloc(current - start + 2, sizeof(char));
|
char* lexeme = calloc(current - start + 2, sizeof(char));
|
||||||
@@ -258,12 +254,12 @@ char PeekNext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ParseIdentifier(TokenList * list) {
|
void ParseIdentifier(TokenList * list) {
|
||||||
while(IsAlpha(SPeek())) SAdvance();
|
while(IsAlpha(ScannerPeek())) AdvanceScanner();
|
||||||
|
|
||||||
char* lexeme = calloc(current - start + 2, sizeof(char));
|
char* lexeme = calloc(current - start + 2, sizeof(char));
|
||||||
|
|
||||||
snprintf(lexeme, current - start + 1, "%s", &source_code[start]);
|
snprintf(lexeme, current - start + 1, "%s", &source_code[start]);
|
||||||
printf("%s\n", lexeme);
|
|
||||||
KeyValuePair* result = Get(lexeme);
|
KeyValuePair* result = Get(lexeme);
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
|
|||||||
@@ -48,4 +48,12 @@ const char* GetLexemeMapping(TokenType type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FreeToken(Token* token) {
|
||||||
|
if (!token) return;
|
||||||
|
|
||||||
|
if (token->type == String || token->type == Number || token->type == Identifier) free((void *) token->lexeme);
|
||||||
|
|
||||||
|
free(token);
|
||||||
}
|
}
|
||||||
@@ -50,5 +50,6 @@ extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT];
|
|||||||
|
|
||||||
//If the first parameter is NULL, the token creation will attempt to infer the lexeme from the TokenType.
|
//If the first parameter is NULL, the token creation will attempt to infer the lexeme from the TokenType.
|
||||||
Token* CreateToken(const char*, int, TokenType);
|
Token* CreateToken(const char*, int, TokenType);
|
||||||
|
void FreeToken(Token*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user