-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.hpp
115 lines (96 loc) · 2.86 KB
/
classes.hpp
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
#pragma once
#include "types.h"
#include "cg.hpp"
#include <string>
// Generic node class, bison applies `new Node(yynext())` so this needs to accept strings.
// this is YYSTYPE
class Node {
public:
std::string lexeme;
Node (const std::string name){lexeme = name;};
Node () {lexeme = std::string("@");}; //no lexeme defined.
Node (Node * node) {lexeme = node->lexeme;};
virtual ~Node() = default;
};
// Type class, parses `int, byte, bool` etc.
class Type : public Node {
public:
TypesEnum type;
Type(TypesEnum t) {type = t;};
Type(Type* T) {type = T->type;};
};
// Exp class, stores numbers, parses numerical operations.
// Stores type, T_INT or T_BYTE.
class Exp : public Node {
public:
TypesEnum type;
std::string reg;
Exp(TypesEnum type_num) : Node()
{
type = type_num;
reg = "ERROR";
};
Exp(TypesEnum type_num, std::string reg_name) : Node()
{
type = type_num;
reg = reg_name;
};
Exp(Exp* T) : Node(T)
{
type = T->type;
reg = T->reg; //TODO check if wrong.
};
};
class Bool : public Exp{
public:
std::string trueLabel;
std::string falseLabel;
//shouldn't actually be used probably
Bool() : Exp(T_BOOL, "bool_reg"), trueLabel("UNDEFINED_TRUE"), falseLabel("UNDEFINED_FALSE")
{};
Bool(const std::string& trueLabel, const std::string& falseLabel) : Exp(T_BOOL, "BOOL_REG"), trueLabel(trueLabel), falseLabel(falseLabel)
{};
Bool(Bool* T) : Exp(T)
{
trueLabel = T->trueLabel;
falseLabel = T->falseLabel;
}
};
Exp* ExpParentheses(Exp* exp);
Exp* ExpFromBinop(Exp* a, Exp* b, BinopEnum binop); //Exp + Exp, we need to store the type and ensure it's legal.
Bool* BoolFromLogic(Bool* a, Bool* b, LogicEnum op); //not Bool.
Bool* BoolFromNot(Bool* a);
Exp* CastExp(Type* cast, Exp* exp); // (int)5b allowed. (string)true not allowed.
Exp* ExpFromID(Node* id);
Exp* ExpFromPrimitive(TypesEnum t, Node* value);
Exp* ExpFromString(Node* value);
Bool* BoolFromPrimitive(Node* lexeme);
Bool* BoolFromRelop(Exp* a, Exp* b, RelopEnum op); //Exp and Exp.
void logicAnd(Bool* b1);
void logicOr(Bool* b1);
#define YYSTYPE Node*
class Call : public Exp {
public:
Call(Node* id, Exp* variable);
};
// STATEMENTS useful functions
void StatementCall(Call* call);
void StatementVariable(TypesEnum type, Node* id);
void StatementVariableExp(TypesEnum type, Node* id, Exp* exp);
void StatementAssignVariable(Node* id, Exp* exp);
void StatementReturn();
void verifyBool(Node* exp);
void verifyByte(Node* exp);
void StatementBreak();
void StatementContinue();
void statementStartWhile(Bool* b, Node* value);
void statementExitWhile(Bool* b);
void jumpToLabel(Node* value);
Node* WhileBoolFlag();
void IfTrue(Bool*);
void IfFalse(Bool*);
void IfElse(Bool*);
void IfDone(Node*);
Node* IfNextFlag();
void startCode();
void endCode();