-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-gen.h
86 lines (78 loc) · 2.42 KB
/
code-gen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "uthash.h"
#include "utarray.h"
#include "pascal.h"
typedef enum SymbolType{
kProgram,
kVariable,
kArray,
kProcedure,
kFunction
} SymbolType;
typedef struct Array {
int data_type;
int start_index;
int end_index;
} Array;
typedef struct Procedure {
UT_array* arguments_type;
UT_array* arguments_data_type;
} Procedure;
typedef struct Function {
UT_array* arguments_type;
UT_array* arguments_data_type;
int return_type;
} Function;
// Structure of the symbol table
typedef struct SymbolTable {
char* symbol;
SymbolType type;
union {
int data_type;
Array* array;
Procedure* procedure;
Function* function;
} attributes;
UT_hash_handle hh;
} SymbolTable;
// Routines to add symbols to symbol table
void addProgramName(char*);
void addVariable(char*, Type*);
int addProcedure(char*, ParameterList*, int);
int addFunction(char*, ParameterList*, int, int);
void printSymbols();
// Error check helpers
SymbolTable* isDeclared(char*);
int checkExpression(char*, Expression*, int);
// Code generation routines
void genCode(Program*);
void genCodeDeclarations(Declarations*, FILE*);
void genCodeSubDeclarations(SubDeclarations*);
void genCodeSubprogDeclaration(SubprogDeclaration*);
void genCodeSubprogramHead(SubprogramHead*);
void genCodeFunction(FunctionRule*);
void genCodeProcedure(ProcedureRule*);
void genCodeArguments(ParameterList*, FILE*, FILE*);
void genCodeCompoundStatement(StatementList*, FILE*);
void genCodeStatement(Statement*, FILE*);
void genCodeAssignment(Assignment*, FILE*);
void genCodeIfThen(IfThen*, FILE*);
void genCodeWhileDo(WhileDo*, FILE*);
void genCodeForTo(ForTo*, FILE*);
void genCodeRead(char*, FILE*);
void genCodeWrite(char*, FILE*);
void genCodeReadln(char*, FILE*);
void genCodeWriteln(char*, FILE*);
void genCodeVariable(Variable*, FILE*);
void genCodeVariableExpression(VariableExpression*, FILE*);
void genCodeProcStatement(ProcStatement*, FILE*);
void genCodeProcStatementExpressionList(ProcStatementExpressionList*, FILE*);
void genCodeExpressionList(ExpressionList*, FILE*);
void genCodeExpression(Expression*, FILE*);
void genCodeRelationalExpression(RelationalExpression*, FILE*);
void genCodeSimpleExpression(SimpleExpression*, FILE*);
void genCodeSignedTerm(SignedTerm*, FILE*);
void genCodeAddition(Addition*, FILE*);
void genCodeTerm(Term*, FILE*);
void genCodeMultiplication(Multiplication*, FILE*);
void genCodeFactor(Factor*, FILE*);
void genCodeFactorExpressionList(FactorExpressionList*, FILE*);