-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.h
106 lines (84 loc) · 2.22 KB
/
compiler.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
/*
* Copyright (C) Rida Bazzi, 2017
*
* Do not share this file with anyone
*/
#ifndef _COMPILER_H_
#define _COMPILER_H_
#include <string>
#include <vector>
extern int mem[1000];
extern int next_available;
extern std::vector<int> inputs;
extern int next_input;
enum ArithmeticOperatorType {
OPERATOR_NONE = 123,
OPERATOR_PLUS,
OPERATOR_MINUS,
OPERATOR_MULT,
OPERATOR_DIV
};
enum ConditionalOperatorType {
CONDITION_GREATER = 345,
CONDITION_LESS,
CONDITION_NOTEQUAL
};
enum InstructionType
{
NOOP = 1000,
IN,
OUT,
ASSIGN,
CJMP,
JMP
};
struct InstructionNode
{
InstructionType type;
union
{
struct
{
int left_hand_side_index;
int operand1_index;
int operand2_index;
/*
* If op == OPERATOR_NONE then only operand1 is meaningful.
* Otherwise both operands are meaningful
*/
ArithmeticOperatorType op;
} assign_inst;
struct
{
int var_index;
} input_inst;
struct
{
int var_index;
} output_inst;
struct {
ConditionalOperatorType condition_op;
int operand1_index;
int operand2_index;
struct InstructionNode * target;
} cjmp_inst;
struct {
struct InstructionNode * target;
} jmp_inst;
};
struct InstructionNode * next; // next statement in the list or NULL
};
void debug(const char* format, ...);
//---------------------------------------------------------
// You should write the following function:
struct InstructionNode * parse_generate_intermediate_representation();
/*
NOTE:
You need to write a function with the above signature. This function
is supposed to parse the input program and generate an intermediate
representation for it. The output of this function is passed to the
execute_program function in main().
Write your code in a separate file and include this header file in
your code.
*/
#endif /* _COMPILER_H_ */