-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathREADME
More file actions
168 lines (129 loc) · 6.04 KB
/
README
File metadata and controls
168 lines (129 loc) · 6.04 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
###############################################################################
PyCS - A C# Compiler written in Python
Authors: Pranshu Gupta, Divyanshu Shende, Prashant Kumar, Rahul Tudu
###############################################################################
# About:
This compiler was built as a course project in CS335 - Compiler Design at Indian
Institute of Technology - Kanpur, India
This is a simple compiler for C# that was built using PLY(Python Lex-Yacc) and
implements the features given below:
If you wish to contribute or extend it, feel free to send a pull request.
# Language Features:
a. Data Types - int, one dimensional arrays of int
b. Operators
int:
- Unary -> !,-,~,+
- Relational -> <, >, ==, !=
- Arithmetic -> +,-,/,%
- Bitwise -> <<,>>,&,|,^
- Logical ops -> &&,||
- Assignment -> =
bool (realized as int 0 and 1):
- Unary-> !
- Logical -> &&,||
- Assignment -> =
Arrays:
- Same as int
c. Loops -> for, while
d. Selection Statements -> if, if-else
e. Namespaces
- Only one namespace allowed in the program
f. Arrays -> 1-D arrays of types - {int}
- Format allowed for initialization, int[] a = {1,2,3,4};
g. Classes
- Only one class allowed in the program
- Data members can only be of simple or array of simple types.
h. Functions
- Allowed return types -> int
- Allowed argument types -> int
i. Scoping
- Imlemented as a tree of symbol tables
j. boolean expressions realized as integer 0 and 1 wit relational operators
k. pre increment/decrement
l. read from stdin
m. Multiple Declarations and Sequential Assignments
- Multiple Declarations -> int a,b,c=3;
- Sequential Assignments -> a=b=c=3;
# Features not supported:
a. post increment/decrement
b. structs and enums, pointers
c. comments
d. switch case and do while
e. float and double type
f. multidimensional arrays
g. OOP
h. op/func overloading
NOTE:
Braces required in blocks (even if single statement) for if, else, while, for etc.
Otherwise scope might be misunterstood by the compiler
# Components:
A1 - This was the first assignment. Building a Lexer for C#
A2 - IR to Assembly Code Generator.
A3 - Parser for C#.
A4 - Semantic Analysis and IR Generation.
PyCS - An end-to-end compiler for C#.
###############################################################################
# Intermediate Code Specification
The generated IR code begins with a call to main function followed by a
call to exit. So, the input program must have a main and only one main
function in it.
- print statement -> 1, print, var
var will be printed on the standard output
- assignment statement -> 1, =, a, b
value of b will be assigned to a
- addition -> 1, +, a, b, c
the sum of a and b will be stored in c
- subtraction -> 1, -, a, b, c
b-a will be stored in c
- multiplication -> 1, *, a, b, c
the product of a and b will be stored in c
- division -> 1, /, a, b, c
the value of a/b will be stored in c
- modulo -> 1, %, a, b, c
the value of a%b will be stored in c
- relops -> 1, <, res, a, b
the truth value (0 or 1) will be stored in variable res
- function parameters -> 1, param, val
the function parameter val will be pushed on the stack. This ir will
be followed by a function call or another param ir
- function call -> 1, call, func, arg_num, ret
the function func will be called and the value returned by it will be
stored in ret. arg_num is the number of arguments that the function
expects. The arguments must have already been pushed on the stack
- pop -> 1, pop, var
store the value on stack into var, to be used as function parameter value
- label -> 1, label, l
a label is introduced
- ifgoto -> 1, ifgoto, relop, arg1, arg2, l
if the arg1 relop arg2 is true then the control will jump to label l.
otherwise to the next line of the IR
- goto -> 1, goto, l
control will jump to label l
- exit -> 1, exit
exit syscall will be made
- function definition -> 1, function, func
marks the beginning of the definition of func
- return -> 1, return, val
returns the value of val from a function
- shift expressions -> 1, >>, result, num, count
num will be shifted count places and the result will be stored in the
variable 'result'.
Similarly for left shift
- logical and -> 1, &&, result, op1, op2
- logical or -> 1, ||, result, op1, op2
- bitwise not -> 1, ~, var
perform bitwise not on var
- logical not -> 1, !, var
perform logical not on var
- array element -> 1, member, var, array_name, offset
store the value at offset distance from the address of the array
array_name into var
- declaration -> 1, int, var
- array declaration -> 1, array, int, len, name
- retval
- arg -> 1, arg, 1, x
move the first argument on the stack in the variable x
###############################################################################
Usage :
$./compile test/test1.cs
$./a.out