jack-compiler/parser/parser-tree.h

155 lines
2.7 KiB
C
Raw Normal View History

2020-12-21 18:35:41 -05:00
#ifndef PARSER_TREE_H
#define PARSER_TREE_H
2020-12-21 19:56:59 -05:00
#include <stdbool.h>
2020-12-21 18:35:41 -05:00
#include "tokenizer.h"
#include "util.h"
// Forward declarations
struct classvardec;
struct parameter;
struct subroutbody;
struct subroutdec;
struct vardec;
struct letstatement;
struct ifstatement;
struct condstatement;
struct subroutcall;
struct term;
struct expressionlist;
// Misc
typedef struct {
char* file;
int definedat;
} DEBUGINFO;
// Program structure
typedef struct class {
char* name;
struct classvardec* vardecs;
struct subroutdec* subroutdecs;
DEBUGINFO* debug;
struct class* next;
} CLASS;
typedef enum {
stat, field
} CLASSVARTYPE;
typedef struct classvardec {
CLASSVARTYPE type;
struct vardec* base;
struct classvardec* next;
} CLASSVARDEC;
typedef enum {
constructor, function, method
} SUBROUTCLASS;
typedef struct subroutdec {
SUBROUTCLASS subroutclass;
CLASS* class;
2020-12-21 18:35:41 -05:00
char* type;
char* name;
struct parameter* parameters;
struct subroutbody* body;
DEBUGINFO* debug;
struct subroutdec* next;
} SUBROUTDEC;
typedef struct parameter {
char* type;
char* name;
2020-12-27 16:52:28 -05:00
bool primitive;
2020-12-24 14:22:22 -05:00
DEBUGINFO* debug;
2020-12-21 18:35:41 -05:00
struct parameter* next;
} PARAMETER;
typedef struct subroutbody {
struct vardec* vardecs;
struct statement* statements;
} SUBROUTBODY;
typedef struct vardec {
char* type;
bool primitive;
TOKENTYPE typeclass;
STRINGLIST* names;
DEBUGINFO* debug;
2020-12-24 14:22:22 -05:00
struct vardec* next;
2020-12-21 18:35:41 -05:00
} VARDEC;
// Statements
typedef enum {
ifstatement, whilestatement, letstatement, dostatement, returnstatement
} STATEMENTTYPE;
typedef struct statement {
STATEMENTTYPE type;
union {
struct letstatement* letstatement;
struct ifstatement* ifstatement;
struct condstatement* whilestatement;
struct subroutcall* dostatement;
struct term* retstatement;
};
2020-12-27 16:52:28 -05:00
DEBUGINFO* debug;
2020-12-21 18:35:41 -05:00
struct statement* next;
} STATEMENT;
typedef struct letstatement {
char* varname;
struct term* arrayind;
struct term* expression;
} LETSTATEMENT;
typedef struct ifstatement {
struct condstatement* base;
struct statement* elsestatements;
} IFSTATEMENT;
typedef struct condstatement {
struct term* expression;
struct statement* statements;
} CONDSTATEMENT;
// Expressions
typedef enum {
varname, intconstant, stringconstant, keywordconstant, arrayitem, subroutcall, innerexpression, unaryopterm
} TERMTYPE;
2020-12-31 14:49:38 -05:00
typedef struct {
char* name;
struct term* exp;
} ARRAY;
2020-12-21 18:35:41 -05:00
typedef struct term {
TERMTYPE type;
union {
char* string;
int integer;
struct subroutcall* call;
struct term* expression;
2020-12-31 14:49:38 -05:00
ARRAY* array;
2020-12-21 18:35:41 -05:00
};
char op;
2020-12-27 16:52:28 -05:00
char unaryop;
2020-12-21 18:35:41 -05:00
struct term* next;
} TERM;
typedef struct subroutcall {
char* parentname;
char* name;
struct expressionlist* parameters;
DEBUGINFO* debug;
} SUBROUTCALL;
typedef struct expressionlist {
TERM* expression;
struct expressionlist* next;
} EXPRESSIONLIST;
#endif