jack-compiler/parser.h

140 lines
2.3 KiB
C
Raw Normal View History

2020-12-14 14:12:20 -05:00
#ifndef PARSER_H
#define PARSER_H
2020-12-20 13:58:10 -05:00
#include <stdbool.h>
2020-12-14 14:12:20 -05:00
#include "tokenizer.h"
2020-12-21 13:05:49 -05:00
#include "util.h"
2020-12-14 14:12:20 -05:00
struct statement;
struct explist;
2020-12-21 13:05:49 -05:00
typedef struct {
char* file;
int definedat;
} DEBUGINFO;
2020-12-14 14:12:20 -05:00
typedef enum {
ifstatement, whilestatement, letstatement, dostatement, returnstatement
} STATEMENTTYPE;
typedef enum {
varname, intconstant, stringconstant, keywordconstant, arrayitem, subroutcall, innerexpression, unaryopterm
} TERMTYPE;
typedef struct {
char* parentname;
char* name;
struct explist* parameters;
2020-12-21 13:05:49 -05:00
DEBUGINFO* debug;
2020-12-14 14:12:20 -05:00
} SUBROUTCALL;
typedef struct term {
TERMTYPE type;
union {
char* string;
int integer;
SUBROUTCALL* call;
struct term* expression;
};
struct term* arrayexp;
char op;
struct term* next;
} TERM;
typedef struct explist {
TERM* expression;
struct explist* next;
} EXPRESSIONLIST;
typedef struct {
TERM* expression;
struct statement* statements;
} CONDSTATEMENT;
typedef struct {
CONDSTATEMENT* base;
struct statement* elsestatements;
} IFSTATEMENT;
typedef struct {
char* varname;
TERM* arrayind;
TERM* expression;
} LETSTATEMENT;
typedef struct statement {
STATEMENTTYPE type;
union {
CONDSTATEMENT* whilest;
IFSTATEMENT* ifst;
LETSTATEMENT* letst;
SUBROUTCALL* dost;
TERM* retst;
};
struct statement* next;
} STATEMENT;
typedef enum {
stat, field
} VARCLASS;
typedef struct vardec {
char* type;
2020-12-20 13:58:10 -05:00
bool primitive;
2020-12-14 14:12:20 -05:00
TOKENTYPE typeclass;
STRINGLIST* names;
struct vardec* next;
2020-12-21 13:05:49 -05:00
DEBUGINFO* debug;
2020-12-14 14:12:20 -05:00
} VARDEC;
typedef struct classvardec {
VARCLASS varclass;
VARDEC* base;
struct classvardec* next;
} CLASSVARDEC;
typedef enum {
constructor, function, method
} SUBROUTCLASS;
typedef struct parameter {
char* type;
char* name;
struct parameter* next;
} PARAMETER;
typedef struct SUBROUTBODY {
VARDEC* vardecs;
STATEMENT* statements;
} SUBROUTBODY;
typedef struct subdec {
SUBROUTCLASS subroutclass;
char* type;
TOKENTYPE typeclass;
char* name;
PARAMETER* parameters;
SUBROUTBODY* body;
2020-12-21 13:05:49 -05:00
DEBUGINFO* debug;
2020-12-14 14:12:20 -05:00
struct subdec* next;
} SUBDEC;
2020-12-20 13:58:10 -05:00
typedef struct cl {
2020-12-14 14:12:20 -05:00
char* name;
CLASSVARDEC* vardecs;
SUBDEC* subdecs;
2020-12-21 13:05:49 -05:00
DEBUGINFO* debug;
2020-12-20 13:58:10 -05:00
struct cl* next;
2020-12-14 14:12:20 -05:00
} CLASS;
typedef struct {
TOKEN* tokens;
TOKEN* current;
TOKEN* checkpoint;
char* file;
CLASS* output;
} PARSER;
PARSER* mkparser(TOKEN* tokens, char* file);
void parse(PARSER* parser);
#endif