191 lines
4.2 KiB
C
191 lines
4.2 KiB
C
#include "parser.h"
|
|
|
|
Expr* Expression(void);
|
|
Expr* Equality(void);
|
|
Expr* Comparison(void);
|
|
Expr* Term(void);
|
|
Expr* Factor(void);
|
|
Expr* Unary(void);
|
|
Expr* Primary(void);
|
|
int SMatch(int, ...);
|
|
int Check(TokenType);
|
|
int SIsAtEnd(void);
|
|
Token* SPeek(void);
|
|
Token* Previous(void);
|
|
Token* SAdvance(void);
|
|
//Token* CreateToken(char*, TokenType);
|
|
|
|
const TokenList* ListOfTokens;
|
|
int Current = 0;
|
|
|
|
Expr* GenerateExpressionTree(const TokenList* list) {
|
|
ListOfTokens = list;
|
|
|
|
return Expression();
|
|
}
|
|
|
|
//Simply expands the equality rule
|
|
Expr* Expression() {
|
|
return Equality();
|
|
}
|
|
|
|
Expr* Equality() {
|
|
Expr* expr = Comparison();
|
|
|
|
while(SMatch(2, Bang_Equal, Equal_Equal)) {
|
|
Token* operator = Previous();
|
|
Expr* right = Comparison();
|
|
Expr* temp = calloc(1, sizeof(Expr));
|
|
temp->type = BINARY;
|
|
temp->expression.Binary.left = expr;
|
|
temp->expression.Binary.op = operator;
|
|
temp->expression.Binary.right = right;
|
|
expr = temp;
|
|
}
|
|
|
|
return expr;
|
|
}
|
|
|
|
Expr* Comparison() {
|
|
Expr* expr = Term();
|
|
|
|
while(SMatch(4, Greater, Greater_Equal, Less, Less_Equal)) {
|
|
Token* operator = Previous();
|
|
Expr* right = Term();
|
|
Expr* temp = calloc(1, sizeof(Expr));
|
|
temp->type = BINARY;
|
|
temp->expression.Binary.left = expr;
|
|
temp->expression.Binary.op = operator;
|
|
temp->expression.Binary.right = right;
|
|
expr = temp;
|
|
}
|
|
|
|
return expr;
|
|
}
|
|
|
|
Expr* Term() {
|
|
Expr* expr = Factor();
|
|
|
|
while(SMatch(2, Minus, Plus)) {
|
|
Token* operator = Previous();
|
|
Expr* right = Factor();
|
|
Expr* temp = calloc(1, sizeof(Expr));
|
|
temp->type = BINARY;
|
|
temp->expression.Binary.left = expr;
|
|
temp->expression.Binary.op = operator;
|
|
temp->expression.Binary.right = right;
|
|
expr = temp;
|
|
}
|
|
|
|
return expr;
|
|
}
|
|
|
|
Expr* Factor() {
|
|
Expr* expr = Unary();
|
|
|
|
while(SMatch(2, Slash, Star)) {
|
|
Token* operator = Previous();
|
|
Expr* right = Unary();
|
|
Expr* temp = calloc(1, sizeof(Expr));
|
|
temp->type = BINARY;
|
|
temp->expression.Binary.left = expr;
|
|
temp->expression.Binary.op = operator;
|
|
temp->expression.Binary.right = right;
|
|
expr = temp;
|
|
}
|
|
|
|
return expr;
|
|
}
|
|
|
|
Expr* Unary() {
|
|
if (SMatch(2, Bang, Minus)) {
|
|
Token* operator = Previous();
|
|
Expr* right = Unary();
|
|
Expr* expr = calloc(1, sizeof(Expr));
|
|
expr->type = UNARY;
|
|
expr->expression.Unary.op = operator;
|
|
expr->expression.Unary.right = right;
|
|
return expr;
|
|
}
|
|
|
|
return Primary();
|
|
}
|
|
|
|
Expr* Primary() {
|
|
Expr* expr = calloc(1, sizeof(Expr));
|
|
expr->type = LITERAL;
|
|
|
|
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 (SMatch(2, Number, String)) {
|
|
expr->expression.Literal.type = Previous();
|
|
return expr;
|
|
}
|
|
|
|
if (SMatch(1, LParen)) {
|
|
free(expr);
|
|
expr = Expression();
|
|
//Consume(RParen, "Expect ')' after expression.");
|
|
expr->type = GROUPING;
|
|
return expr;
|
|
}
|
|
|
|
return expr;
|
|
}
|
|
|
|
int SMatch(int count, ...) {
|
|
va_list list;
|
|
va_start(list, count);
|
|
|
|
for(int i = 0; i < count; i++) {
|
|
if(Check(va_arg(list, TokenType))) {
|
|
SAdvance();
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int Check(TokenType type) {
|
|
if (SIsAtEnd()) return 0;
|
|
return SPeek()->type == type;
|
|
}
|
|
|
|
int SIsAtEnd() {
|
|
return SPeek()->type == EndOF;
|
|
}
|
|
|
|
Token* SPeek() {
|
|
return ListOfTokens->tokens[Current];
|
|
}
|
|
|
|
Token* Previous() {
|
|
return ListOfTokens->tokens[Current - 1];
|
|
}
|
|
|
|
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);
|
|
|
|
// return token;
|
|
// }
|