Fix parsing issues

This commit is contained in:
Augusto Gunsch 2020-12-22 14:38:10 -03:00
parent 351446bb6d
commit d65bcc202f
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
7 changed files with 23 additions and 38 deletions

View File

@ -26,18 +26,12 @@ mkstrlist(operators, opsarr);
TERM* parsetermnullified(PARSER* p) { TERM* parsetermnullified(PARSER* p) {
TOKENTYPE type = p->current->type; TOKENTYPE type = p->current->type;
if(type == integer) if(type == integer) return parseint(p);
return parseint(p); if(type == string) return parsestr(p);
else if(type == string) if(type == keyword) return parsekeyword(p);
return parsestr(p); if(type == identifier) return parseidentifierterm(p);
else if(type == keyword) if(equals(p, "-") || equals(p, "~")) return parseunaryopterm(p);
return parsekeyword(p); if(equals(p, "(")) return parseinnerexpression(p);
else if(type == identifier)
return parseidentifierterm(p);
else if(equals(p, "-") || equals(p, "~"))
return parseunaryopterm(p);
else if(equals(p, "("))
return parseinnerexpression(p);
return NULL; return NULL;
} }
@ -94,8 +88,10 @@ TERM* parseinnerexpression(PARSER* p) {
} }
TERM* parsecalltermnullified(PARSER* p) { TERM* parsecalltermnullified(PARSER* p) {
TERM* t = mkterm(subroutcall);
SUBROUTCALL* call = parsesubroutcallnullified(p); SUBROUTCALL* call = parsesubroutcallnullified(p);
if(call == NULL)
return NULL;
TERM* t = mkterm(subroutcall);
t->call == call; t->call == call;
return t; return t;
} }
@ -184,6 +180,7 @@ SUBROUTCALL* parsesubroutcallnullified(PARSER* p) {
if(p->current->type != identifier) if(p->current->type != identifier)
return nullsubroutcall(p, c); return nullsubroutcall(p, c);
c->name = p->current->token; c->name = p->current->token;
next(p);
if(differs(p, "(")) if(differs(p, "("))
return nullsubroutcall(p, c); return nullsubroutcall(p, c);

View File

@ -5,8 +5,8 @@
#define mkstrlist(name, array) STRINGARRAY name = { .items = array, .size = strcount(array) } #define mkstrlist(name, array) STRINGARRAY name = { .items = array, .size = strcount(array) }
#define next(parser) parser->current = p->current->next #define next(parser) parser->current = p->current->next
#define rewindparser(parser) p->checkpoint = p->current #define anchorparser(parser) p->checkpoint = p->current
#define anchorparser(parser) p->current = p->checkpoint #define rewindparser(parser) p->current = p->checkpoint
#define differs(parser, str) strcmp(parser->current->token, str) #define differs(parser, str) strcmp(parser->current->token, str)
#define nextdiffers(parser, str) strcmp(parser->current->next->token, str) #define nextdiffers(parser, str) strcmp(parser->current->next->token, str)
#define equals(parser, str) !differs(parser, str) #define equals(parser, str) !differs(parser, str)

View File

@ -18,18 +18,13 @@ STATEMENT* mkstatement(STATEMENTTYPE t) {
return s; return s;
} }
// Though nullified, will throw errors if the parsing was on-going // Though nullified, will throw errors if the parsing fails while on-going
STATEMENT* parsestatementnullified(PARSER* p) { STATEMENT* parsestatementnullified(PARSER* p) {
if(equals(p, "let")) if(equals(p, "let")) return parselet(p);
return parselet(p); if(equals(p, "if")) return parseif(p);
else if(equals(p, "if")) if(equals(p, "while")) return parsewhile(p);
return parseif(p); if(equals(p, "do")) return parsedo(p);
else if(equals(p, "while")) if(equals(p, "return")) return parsereturn(p);
return parsewhile(p);
else if(equals(p, "do"))
return parsedo(p);
else if(equals(p, "return"))
return parsereturn(p);
return NULL; return NULL;
} }

View File

@ -111,12 +111,6 @@ SUBROUTDEC* parsesubroutdec(PARSER* p) {
SUBROUTDEC* subroutdec = (SUBROUTDEC*)malloc(sizeof(SUBROUTDEC)); SUBROUTDEC* subroutdec = (SUBROUTDEC*)malloc(sizeof(SUBROUTDEC));
subroutdec->subroutclass = subroutclass; subroutdec->subroutclass = subroutclass;
subroutdec->typeclass = p->current->type;
if(equals(p, "void")) {
subroutdec->type = p->current->token;
next(p);
}
else
subroutdec->type = parsetype(p); subroutdec->type = parsetype(p);
subroutdec->debug = getdebug(p); subroutdec->debug = getdebug(p);
@ -185,7 +179,7 @@ bool isprimitive(TOKEN* tk) {
} }
char* parsetype(PARSER* p) { char* parsetype(PARSER* p) {
if(p->current->type != identifier); if(p->current->type != identifier)
unexpected(p); unexpected(p);
char* result = p->current->token; char* result = p->current->token;

View File

@ -50,7 +50,6 @@ typedef enum {
typedef struct subroutdec { typedef struct subroutdec {
SUBROUTCLASS subroutclass; SUBROUTCLASS subroutclass;
char* type; char* type;
TOKENTYPE typeclass;
char* name; char* name;
struct parameter* parameters; struct parameter* parameters;
struct subroutbody* body; struct subroutbody* body;

View File

@ -14,6 +14,6 @@ PARSER* mkparser(TOKEN* tokens, char* file) {
return parser; return parser;
} }
void parse(PARSER* parser) { void parse(PARSER* p) {
parser->output = parseclasses(parser); p->output = parseclasses(p);
} }

View File

@ -12,5 +12,5 @@ typedef struct {
} PARSER; } PARSER;
PARSER* mkparser(TOKEN* tokens, char* file); PARSER* mkparser(TOKEN* tokens, char* file);
void parse(PARSER* parser); void parse(PARSER* p);
#endif #endif