Skip to content
Open
Show file tree
Hide file tree
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
26 changes: 4 additions & 22 deletions src/lexer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
Expand Down Expand Up @@ -268,29 +269,10 @@ void initList(struct list **list)

int getPrec(enum OPCODE opcode)
{
if (opcode == OP_Equal) {
return 5;
int prec[] = {5, 4, 4, 3, 3, 2, 2, 1};
if (opcode < sizeof(prec) / sizeof(int)) {
return prec[opcode];
}

if (opcode == OP_Open_parenthesis ||
opcode == OP_Closed_parenthesis) {
return 4;
}

if (opcode == OP_Plus ||
opcode == OP_Minus) {
return 3;
}

if (opcode == OP_Multi ||
opcode == OP_Div) {
return 2;
}

if (opcode == OP_Pow) {
return 1;
}

return 0;
}

Expand Down
7 changes: 4 additions & 3 deletions src/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ enum TOKEN_TYPE {
T_None
};

// A change to this enum should be coupled with a change to getPrec in lexer.c
enum OPCODE {
OP_Equal,
OP_Open_parenthesis,
OP_Closed_parenthesis,
OP_Plus,
OP_Minus,
OP_Multi,
OP_Div,
OP_Equal,
OP_Pow,
OP_Fact,
OP_Sqrt,
Expand All @@ -35,8 +38,6 @@ enum OPCODE {
OP_Asin,
OP_Acos,
OP_Atan,
OP_Closed_parenthesis,
OP_Open_parenthesis,
OP_None,
};

Expand Down