-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.h
123 lines (107 loc) · 3.1 KB
/
codegen.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef CODEGEN_H_INCLUDED
#define CODEGEN_H_INCLUDED
#include "Absyn.H"
#include "pstcode.h"
#include <stdexcept>
#include <string>
class UnknownVar : public std::logic_error
{
public:
UnknownVar(const std::string &name)
: logic_error("Unknown variable \"" + name + "\"")
{}
};
class MismatchedArgCount : public std::logic_error
{
public:
MismatchedArgCount(const std::string &name)
: logic_error("Mismatched Argument Count \"" + name + "\"")
{}
};
class UnknownFunc : public std::logic_error
{
public:
UnknownFunc(const std::string &name)
: logic_error("Unknown function \"" + name + "\"")
{}
};
class Redeclared : public std::logic_error
{
public:
Redeclared(const std::string &name)
: logic_error("Symbol \"" + name + "\" redeclared")
{}
};
class Unimplemented : public std::logic_error
{
public:
Unimplemented(const std::string &what) : logic_error(what) {}
};
class CodeGen : public Visitor
{
private:
Ident currid; // identifier from last visitIdent
type_t currtype; // type from last visitT{Int,Double}
PstackCode code; // buffer to hold generated code
SymbolTable symbols; // symbol table
int funargs; // number of parameters in current function.
public:
CodeGen()
: currid(""), currtype(TY_BAD), code(), symbols(), funargs(-1)
{}
PstackCode generate(Visitable *vis);
// These will never actually be called; instead, node->accept(this) will
// call the method for the concrete class (visitProg rather than
// visitProgram, visitTInt or visitTDouble rather than visitType, etc.)
void visitProgram(Program *) {}
void visitFunction(Function *) {}
void visitDecl(Decl *) {}
void visitStm(Stm *) {}
void visitExp(Exp *) {}
void visitType(Type *) {}
void visitProg(Prog *p);
void visitFun(Fun *p);
void visitDec(Dec *p);
void visitListFunction(ListFunction* p);
void visitListExp(ListExp* p);
void visitListStm(ListStm* p);
void visitListDecl(ListDecl* p);
void visitListIdent(ListIdent* p);
// Statements
void visitSDecl(SDecl *p);
void visitSExp(SExp *p);
void visitSBlock(SBlock *p);
void visitSWhile(SWhile *p);
void visitSDo(SDo *p);
void visitSReturn(SReturn *p);
void visitSIf(SIf *p);
void visitSIfElse(SIfElse *p);
void visitSFor2(SFor2 *p);
void visitSFor3(SFor3 *p);
// Expressions
void visitEAss(EAss *p);
void visitELt(ELt *p);
void visitEGt(EGt *p);
void visitEEq(EEq *p);
void visitEInc(EInc *p);
void visitEDec(EDec *p);
void visitEAdd(EAdd *p);
void visitESub(ESub *p);
void visitEMul(EMul *p);
void visitEDiv(EDiv *p);
void visitCall(Call *p);
void visitEVar(EVar *p);
void visitEStr(EStr *p);
void visitEInt(EInt *p);
void visitEDouble(EDouble *p);
// Types
void visitTInt(TInt *p);
void visitTDouble(TDouble *p);
// Literals and identifiers.
void visitInteger(Integer i);
void visitDouble(Double d);
void visitChar(Char c);
void visitString(String s);
void visitIdent(String s);
};
#endif