Unify style

This commit is contained in:
Augusto Gunsch 2020-12-31 20:24:27 -03:00
parent a9a78c62c4
commit b880e97990
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
11 changed files with 101 additions and 51 deletions

View File

@ -5,7 +5,7 @@
#include "compiler.h" #include "compiler.h"
/* compiler-util /* compiler-util
* Random utilities for the compiler. */ * Random utilities used in the compiler module. */
#define mkln(id) mksimpleln(id, strcount(id)) #define mkln(id) mksimpleln(id, strcount(id))

View File

@ -1,8 +1,19 @@
#include <stdlib.h> #include <stdlib.h>
#include "util.h" #include "util.h"
#include "parser-internal.h" #include "parser-util.h"
#include "parser-expressions.h" #include "parser-expressions.h"
const char* keywordsarr[] = { "true", "false", "null", "this" };
const char* opsarr[] = { "+", "-", "*", "/", "&", "|", "<", ">", "=" };
mkstrlist(keywordconstants, keywordsarr);
mkstrlist(operators, opsarr);
/* BEGIN FORWARD DECLARATIONS */
// Miscelaneous
bool isop(TOKEN* t);
// Parsing methods
TERM* parsetermnullified(PARSER* p); TERM* parsetermnullified(PARSER* p);
TERM* parseterm(PARSER* p); TERM* parseterm(PARSER* p);
TERM* mkterm(TERMTYPE type); TERM* mkterm(TERMTYPE type);
@ -15,15 +26,20 @@ TERM* parsecalltermnullified(PARSER* p);
TERM* parsearrayterm(PARSER* p); TERM* parsearrayterm(PARSER* p);
TERM* parsevarterm(PARSER* p); TERM* parsevarterm(PARSER* p);
TERM* parseidentifierterm(PARSER* p); TERM* parseidentifierterm(PARSER* p);
bool isop(TOKEN* t);
SUBROUTCALL* nullsubroutcall(PARSER* p, SUBROUTCALL* c); SUBROUTCALL* nullsubroutcall(PARSER* p, SUBROUTCALL* c);
SUBROUTCALL* parsesubroutcallnullified(PARSER* p); SUBROUTCALL* parsesubroutcallnullified(PARSER* p);
const char* keywordsarr[] = { "true", "false", "null", "this" }; /* END FORWARD DECLARATIONS */
const char* opsarr[] = { "+", "-", "*", "/", "&", "|", "<", ">", "=" };
mkstrlist(keywordconstants, keywordsarr);
mkstrlist(operators, opsarr);
// Miscelaneous
bool isop(TOKEN* t) {
for(int i = 0; i < operators.size; i++)
if(!strcmp(t->token, operators.items[i]))
return true;
return false;
}
// Parsing methods
TERM* parsetermnullified(PARSER* p) { TERM* parsetermnullified(PARSER* p) {
TOKENTYPE type = p->current->type; TOKENTYPE type = p->current->type;
if(type == integer) return parseint(p); if(type == integer) return parseint(p);
@ -126,13 +142,6 @@ TERM* parseidentifierterm(PARSER* p) {
return t; return t;
} }
bool isop(TOKEN* t) {
for(int i = 0; i < operators.size; i++)
if(!strcmp(t->token, operators.items[i]))
return true;
return false;
}
TERM* parseexpressionnullified(PARSER* p) { TERM* parseexpressionnullified(PARSER* p) {
TERM* head = parsetermnullified(p); TERM* head = parsetermnullified(p);
TERM* current = head; TERM* current = head;

View File

@ -1,6 +1,10 @@
#ifndef PARSER_EXPRESSIONS_H #ifndef PARSER_EXPRESSIONS_H
#define PARSER_EXPRESSIONS_H #define PARSER_EXPRESSIONS_H
#include "parser.h" #include "parser.h"
/* parser-expressions
* Functions for parsing expressions. */
TERM* parseexpressionnullified(PARSER* p); TERM* parseexpressionnullified(PARSER* p);
TERM* parseexpression(PARSER* p); TERM* parseexpression(PARSER* p);
SUBROUTCALL* parsesubroutcall(PARSER* p); SUBROUTCALL* parsesubroutcall(PARSER* p);

View File

@ -1,9 +1,13 @@
#include <stdlib.h> #include <stdlib.h>
#include "parser-expressions.h" #include "parser-expressions.h"
#include "parser-internal.h" #include "parser-util.h"
#include "parser-statements.h" #include "parser-statements.h"
/* BEGIN FORWARD DECLARATIONS */
// Miscelaneous
STATEMENT* mkstatement(PARSER* p, STATEMENTTYPE t); STATEMENT* mkstatement(PARSER* p, STATEMENTTYPE t);
STATEMENT* parsestatementnullified(PARSER* p); STATEMENT* parsestatementnullified(PARSER* p);
STATEMENT* parselet(PARSER* p); STATEMENT* parselet(PARSER* p);
CONDSTATEMENT* parsecond(PARSER* p); CONDSTATEMENT* parsecond(PARSER* p);
@ -12,6 +16,9 @@ STATEMENT* parsewhile(PARSER* p);
STATEMENT* parsedo(PARSER* p); STATEMENT* parsedo(PARSER* p);
STATEMENT* parsereturn(PARSER* p); STATEMENT* parsereturn(PARSER* p);
/* END FORWARD DECLARATIONS */
// Miscelaneous
STATEMENT* mkstatement(PARSER* p, STATEMENTTYPE t) { STATEMENT* mkstatement(PARSER* p, STATEMENTTYPE t) {
STATEMENT* s = (STATEMENT*)malloc(sizeof(STATEMENT)); STATEMENT* s = (STATEMENT*)malloc(sizeof(STATEMENT));
s->type = t; s->type = t;
@ -19,6 +26,8 @@ STATEMENT* mkstatement(PARSER* p, STATEMENTTYPE t) {
return s; return s;
} }
// Parsing methods
// Though nullified, will throw errors if the parsing fails while 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")) return parselet(p); if(equals(p, "let")) return parselet(p);

View File

@ -1,5 +1,9 @@
#ifndef PARSER_STATEMENTS_H #ifndef PARSER_STATEMENTS_H
#define PARSER_STATEMENTS_H #define PARSER_STATEMENTS_H
#include "parser.h" #include "parser.h"
/* parser-statements
* Function for parsing statements. */
STATEMENT* parsestatements(PARSER* p); STATEMENT* parsestatements(PARSER* p);
#endif #endif

View File

@ -1,10 +1,24 @@
#include <stdlib.h> #include <stdlib.h>
#include "parser-internal.h" #include "parser-util.h"
#include "parser-structure.h" #include "parser-structure.h"
#include "parser-statements.h" #include "parser-statements.h"
CLASS* parseclass(PARSER* p); const char* classvartypesarr[] = { "static", "field" };
const char* vartypesarr[] = { "int", "char", "boolean" };
const char* subroutclassesarr[] = { "constructor", "function", "method" };
mkstrlist(classvartypes, classvartypesarr);
mkstrlist(vartypes, vartypesarr);
mkstrlist(subroutclasses, subroutclassesarr);
/* BEGIN FORWARD DECLARATIONS */
// Miscelaneous
bool isprimitive(TOKEN* tk);
char* parsetype(PARSER* p);
int parsepossibilities(PARSER* p, STRINGARRAY* poss); int parsepossibilities(PARSER* p, STRINGARRAY* poss);
// Parsing methods
CLASS* parseclass(PARSER* p);
CLASSVARTYPE parseclassvartype(PARSER* p); CLASSVARTYPE parseclassvartype(PARSER* p);
CLASSVARDEC* parseclassvardec(PARSER* p); CLASSVARDEC* parseclassvardec(PARSER* p);
CLASSVARDEC* parseclassvardecs(PARSER* p); CLASSVARDEC* parseclassvardecs(PARSER* p);
@ -14,19 +28,38 @@ SUBROUTDEC* parsesubroutdecs(PARSER* p, CLASS* c);
PARAMETER* parseparameter(PARSER* p); PARAMETER* parseparameter(PARSER* p);
PARAMETER* parseparameters(PARSER* p); PARAMETER* parseparameters(PARSER* p);
SUBROUTBODY* parsesubroutbody(PARSER* p); SUBROUTBODY* parsesubroutbody(PARSER* p);
bool isprimitive(TOKEN* tk);
char* parsetype(PARSER* p);
void parsevardeccommon(PARSER* p, VARDEC* v); void parsevardeccommon(PARSER* p, VARDEC* v);
VARDEC* parsevardec(PARSER* p); VARDEC* parsevardec(PARSER* p);
VARDEC* parsevardecs(PARSER* p); VARDEC* parsevardecs(PARSER* p);
const char* classvartypesarr[] = { "static", "field" }; /* END FORWARD DECLARATIONS */
const char* vartypesarr[] = { "int", "char", "boolean" };
const char* subroutclassesarr[] = { "constructor", "function", "method" };
mkstrlist(classvartypes, classvartypesarr);
mkstrlist(vartypes, vartypesarr);
mkstrlist(subroutclasses, subroutclassesarr);
// Miscelaneous
bool isprimitive(TOKEN* tk) {
if(tk->type == keyword)
if(existsinarray(&vartypes, tk->token))
return true;
return false;
}
char* parsetype(PARSER* p) {
if(p->current->type != identifier && p->current->type != keyword)
unexpected(p);
char* result = p->current->token;
next(p);
return result;
}
int parsepossibilities(PARSER* p, STRINGARRAY* poss) {
for(int i = 0; i < poss->size; i++)
if(equals(p, poss->items[i]))
return i;
return -1;
}
// Parsing methods
CLASS* parseclass(PARSER* p) { CLASS* parseclass(PARSER* p) {
checkcontent(p, "class"); checkcontent(p, "class");
@ -50,13 +83,6 @@ CLASS* parseclass(PARSER* p) {
return class; return class;
} }
int parsepossibilities(PARSER* p, STRINGARRAY* poss) {
for(int i = 0; i < poss->size; i++)
if(equals(p, poss->items[i]))
return i;
return -1;
}
CLASSVARTYPE parseclassvartype(PARSER* p) { CLASSVARTYPE parseclassvartype(PARSER* p) {
return parsepossibilities(p, &classvartypes); return parsepossibilities(p, &classvartypes);
} }
@ -176,22 +202,6 @@ SUBROUTBODY* parsesubroutbody(PARSER* p) {
return subroutbody; return subroutbody;
} }
bool isprimitive(TOKEN* tk) {
if(tk->type == keyword)
if(existsinarray(&vartypes, tk->token))
return true;
return false;
}
char* parsetype(PARSER* p) {
if(p->current->type != identifier && p->current->type != keyword)
unexpected(p);
char* result = p->current->token;
next(p);
return result;
}
void parsevardeccommon(PARSER* p, VARDEC* v) { void parsevardeccommon(PARSER* p, VARDEC* v) {
v->typeclass = p->current->type; v->typeclass = p->current->type;
v->primitive = isprimitive(p->current); v->primitive = isprimitive(p->current);

View File

@ -1,5 +1,9 @@
#ifndef PARSER_STRUCTURE_H #ifndef PARSER_STRUCTURE_H
#define PARSER_STRUCTURE_H #define PARSER_STRUCTURE_H
#include "parser.h" #include "parser.h"
/* parser-structure
* Function for parsing a class. */
CLASS* parseclass(PARSER* p); CLASS* parseclass(PARSER* p);
#endif #endif

View File

@ -4,7 +4,10 @@
#include "tokenizer.h" #include "tokenizer.h"
#include "util.h" #include "util.h"
// Forward declarations /* parser-tree
* Type definitions for the parsing tree. */
/* BEGIN FORWARD DECLARATIONS */
struct classvardec; struct classvardec;
struct parameter; struct parameter;
struct subroutbody; struct subroutbody;
@ -16,6 +19,7 @@ struct condstatement;
struct subroutcall; struct subroutcall;
struct term; struct term;
struct expressionlist; struct expressionlist;
/* END FORWARD DECLARATIONS */
// Misc // Misc
typedef struct { typedef struct {

View File

@ -1,8 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "parser-internal.h" #include "parser-util.h"
#include "util.h" #include "util.h"
const char* tokentypesarr[] = { "keyword", "identifier", "symbol", const char* tokentypesarr[] = { "keyword", "identifier", "symbol",
"integerConstant", "stringConstant" }; "integerConstant", "stringConstant" };
mkstrlist(tokentypes, tokentypesarr); mkstrlist(tokentypes, tokentypesarr);

View File

@ -3,6 +3,9 @@
#include <string.h> #include <string.h>
#include "parser.h" #include "parser.h"
/* parser-util
* Random utilities used in the parser module. */
#define next(parser) parser->current = p->current->next #define next(parser) parser->current = p->current->next
#define anchorparser(parser) p->checkpoint = p->current #define anchorparser(parser) p->checkpoint = p->current
#define rewindparser(parser) p->current = p->checkpoint #define rewindparser(parser) p->current = p->checkpoint

View File

@ -3,6 +3,10 @@
#include "tokenizer.h" #include "tokenizer.h"
#include "parser-tree.h" #include "parser-tree.h"
/* parser
* This is the file that should be included in other modules
* that want to parse a file. */
typedef struct { typedef struct {
TOKEN* tokens; TOKEN* tokens;
TOKEN* current; TOKEN* current;