The Scanner has been refactored and some responsibilities were pulled from it. Confirmed working, the Parser still needs to be touched on however.

This commit is contained in:
2022-03-02 20:52:40 +00:00
parent 1b33468343
commit e20ccb8cfb
7 changed files with 238 additions and 177 deletions
+47 -46
View File
@@ -1,9 +1,4 @@
#include "parser.h"
#include "expr.h"
#include "scanner.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
Expr* Expression(void);
Expr* Equality(void);
@@ -12,17 +7,23 @@ Expr* Term(void);
Expr* Factor(void);
Expr* Unary(void);
Expr* Primary(void);
int Match(int, ...);
int SMatch(int, ...);
int Check(TokenType);
int IsAtEnd(void);
Token* Peek(void);
int SIsAtEnd(void);
Token* SPeek(void);
Token* Previous(void);
Token* Advance(void);
Token* CreateToken(char*, TokenType);
Token* SAdvance(void);
//Token* CreateToken(char*, TokenType);
const TokenList* tokens;
const TokenList* ListOfTokens;
int Current = 0;
Expr* GenerateExpressionTree(const TokenList* list) {
ListOfTokens = list;
return Expression();
}
//Simply expands the equality rule
Expr* Expression() {
return Equality();
@@ -31,7 +32,7 @@ Expr* Expression() {
Expr* Equality() {
Expr* expr = Comparison();
while(Match(2, Bang_Equal, Equal_Equal)) {
while(SMatch(2, Bang_Equal, Equal_Equal)) {
Token* operator = Previous();
Expr* right = Comparison();
Expr* temp = calloc(1, sizeof(Expr));
@@ -48,7 +49,7 @@ Expr* Equality() {
Expr* Comparison() {
Expr* expr = Term();
while(Match(4, Greater, Greater_Equal, Less, Less_Equal)) {
while(SMatch(4, Greater, Greater_Equal, Less, Less_Equal)) {
Token* operator = Previous();
Expr* right = Term();
Expr* temp = calloc(1, sizeof(Expr));
@@ -65,7 +66,7 @@ Expr* Comparison() {
Expr* Term() {
Expr* expr = Factor();
while(Match(2, Minus, Plus)) {
while(SMatch(2, Minus, Plus)) {
Token* operator = Previous();
Expr* right = Factor();
Expr* temp = calloc(1, sizeof(Expr));
@@ -82,7 +83,7 @@ Expr* Term() {
Expr* Factor() {
Expr* expr = Unary();
while(Match(2, Slash, Star)) {
while(SMatch(2, Slash, Star)) {
Token* operator = Previous();
Expr* right = Unary();
Expr* temp = calloc(1, sizeof(Expr));
@@ -97,7 +98,7 @@ Expr* Factor() {
}
Expr* Unary() {
if (Match(2, Bang, Minus)) {
if (SMatch(2, Bang, Minus)) {
Token* operator = Previous();
Expr* right = Unary();
Expr* expr = calloc(1, sizeof(Expr));
@@ -114,25 +115,25 @@ Expr* Primary() {
Expr* expr = calloc(1, sizeof(Expr));
expr->type = LITERAL;
if (Match(1, FALSE)) {
expr->expression.Literal.type = CreateToken("false", FALSE);
return expr;
}
if (Match(1, TRUE)) {
expr->expression.Literal.type = CreateToken("true", TRUE);
return expr;
}
if (Match(1, NIL)) {
expr->expression.Literal.type = CreateToken("nil", NIL);
if (SMatch(3, FALSE, TRUE, NIL)) {
expr->expression.Literal.type = SPeek();//CreateToken("false", FALSE);
return expr;
}
// if (SMatch(1, TRUE)) {
// expr->expression.Literal.type = //CreateToken("true", TRUE);
// return expr;
// }
// if (SMatch(1, NIL)) {
// expr->expression.Literal.type = //CreateToken("nil", NIL);
// return expr;
// }
if (Match(2, Number, String)) {
if (SMatch(2, Number, String)) {
expr->expression.Literal.type = Previous();
return expr;
}
if (Match(1, LParen)) {
if (SMatch(1, LParen)) {
free(expr);
expr = Expression();
//Consume(RParen, "Expect ')' after expression.");
@@ -143,13 +144,13 @@ Expr* Primary() {
return expr;
}
int Match(int count, ...) {
int SMatch(int count, ...) {
va_list list;
va_start(list, count);
for(int i = 0; i < count; i++) {
if(Check(va_arg(list, TokenType))) {
Advance();
SAdvance();
return 1;
}
}
@@ -158,33 +159,33 @@ int Match(int count, ...) {
}
int Check(TokenType type) {
if (IsAtEnd()) return 0;
return Peek()->type == type;
if (SIsAtEnd()) return 0;
return SPeek()->type == type;
}
int IsAtEnd() {
return Peek()->type == EndOF;
int SIsAtEnd() {
return SPeek()->type == EndOF;
}
Token* Peek() {
return tokens->tokens[Current];
Token* SPeek() {
return ListOfTokens->tokens[Current];
}
Token* Previous() {
return tokens->tokens[Current - 1];
return ListOfTokens->tokens[Current - 1];
}
Token* Advance() {
if (!IsAtEnd()) Current++;
Token* SAdvance() {
if (!SIsAtEnd()) Current++;
return Previous();
}
Token* CreateToken(char* lexeme, TokenType type) {
Token* token = calloc(1, sizeof(Token));
token->type = type;
token->lexeme = lexeme;
token->length = strlen(lexeme);
// Token* CreateToken(char* lexeme, TokenType type) {
// Token* token = calloc(1, sizeof(Token));
// token->type = type;
// token->lexeme = lexeme;
// token->length = strlen(lexeme);
return token;
}
// return token;
// }