Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix + untested FUNEXP extension #10

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/compiler/parser/ExpressionParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define TABLE_SIZE 9
#define STACK_SIZE 20

ParserResult __Parser_parseFunctionCallExpression(Parser *parser);

int precedence_table[TABLE_SIZE][TABLE_SIZE] = { // [stack top terminal][input token]
// +-|*/| ! |??|r |i |( |) |$
{R, S, S, R, R, S, S, R, R}, // +-
Expand Down Expand Up @@ -262,20 +264,41 @@ ParserResult __Parser_parseExpression(Parser *parser) {

bool reductionSuccess;
int offset = 1;
enum PrecTableRelation operation;

LexerResult current = Lexer_peekToken(parser->lexer, offset);
LexerResult leftParen;
LexerResult removeFromTokenStream;

while(true) {
if(!current.success) return LexerToParserError(current);

StackItem *topTerminal = Expr_getTopTerminal(stack);
// topTerminal returns S_BOTTOM, which has no token,
// this token is being dereferenced in Expr_getPrecTbIndex, thus causing a segfault
// check if there is a function call in expression
if(current.token->type == TOKEN_IDENTIFIER){
leftParen = Lexer_peekToken(parser->lexer, offset + 1);
if(!leftParen.success) return LexerToParserError(current);

if(leftParen.token->kind == TOKEN_LEFT_PAREN){
ParserResult functionCallExpression = __Parser_parseFunctionCallExpression(parser);
if(!functionCallExpression.success) return functionCallExpression;

int topTerminalIndex = Expr_getPrecTbIndex(topTerminal->token);
int currentTokenIndex = Expr_getPrecTbIndex(current.token);
StackItem *function = mem_alloc(sizeof(StackItem));
function->node = functionCallExpression.node;
function->Stype = S_NONTERMINAL;
function->token = NULL;
Array_push(stack, function);
}
}

enum PrecTableRelation operation = precedence_table[topTerminalIndex][currentTokenIndex];
if(operation != X){

StackItem *topTerminal = Expr_getTopTerminal(stack);

int topTerminalIndex = Expr_getPrecTbIndex(topTerminal->token);
int currentTokenIndex = Expr_getPrecTbIndex(current.token);

operation = precedence_table[topTerminalIndex][currentTokenIndex];
}

StackItem *isItFinal = Array_get(stack, stack->size - 1);
if(isItFinal->Stype == S_NONTERMINAL && stack->size == 2 && operation == X) {
Expand Down
Loading