100
|
1 #!/usr/bin/python
|
|
2
|
4
|
3 import unittest
|
8
|
4 import os
|
4
|
5
|
|
6 from compiler.compiler import Compiler
|
|
7 from compiler.errors import CompilerException, printError
|
|
8 from compiler import lexer
|
|
9 from compiler.parser import Parser
|
|
10 from compiler import assembler
|
|
11 from compiler.codegenerator import CodeGenerator
|
7
|
12 from project import Project
|
4
|
13
|
|
14 class CompilerTestCase(unittest.TestCase):
|
|
15 """ test methods start with 'test*' """
|
|
16 def testSource1(self):
|
|
17 source = """
|
|
18 module lcfos;
|
|
19 var
|
|
20 a : integer;
|
|
21
|
|
22 procedure putchar(num : integer);
|
|
23 begin
|
|
24 end putchar;
|
|
25
|
|
26 procedure WriteNum( num: integer);
|
|
27 var
|
|
28 d, base : integer;
|
|
29 dgt : integer;
|
|
30 begin
|
|
31 d := 1;
|
|
32 base := 10;
|
|
33 while num div d >= base do
|
|
34 d := d * base
|
|
35 end;
|
|
36 while d <> 0 do
|
|
37 dgt := num div d;
|
|
38 num := num mod d;
|
|
39 d := d div base;
|
|
40 putchar(48 + dgt)
|
|
41 end
|
|
42 end WriteNum;
|
|
43
|
|
44 begin
|
|
45 a := 1;
|
|
46 while a < 26
|
|
47 do
|
|
48 putchar(65+a);
|
|
49 a := a * 2
|
|
50 end;
|
|
51 end lcfos.
|
|
52 """
|
|
53 pc = Compiler()
|
|
54 pc.compilesource(source)
|
|
55 def testSource2(self):
|
|
56 source = """
|
|
57 module lcfos;
|
|
58 var
|
|
59 a, b : integer;
|
|
60 arr: array 30 of integer;
|
|
61 arr2: array 10, 12 of integer;
|
|
62 procedure t2*() : integer;
|
|
63 begin
|
|
64 a := 2;
|
|
65 while a < 5 do
|
|
66 b := arr[a-1] + arr[a-2];
|
|
67 arr2[a,2] := b;
|
|
68 arr2[a,3] := arr2[a,2] + arr2[a,2]*3 + b;
|
|
69 arr[a] := b;
|
|
70 a := a + 1;
|
|
71 end;
|
|
72 return b
|
|
73 end t2;
|
|
74 begin
|
|
75 b := 12;
|
|
76 arr[0] := 1;
|
|
77 arr[1] := 1;
|
|
78 end lcfos.
|
|
79 """
|
|
80 pc = Compiler()
|
|
81 mod = pc.compilesource(source)
|
|
82 def testSource5(self):
|
|
83 source = """
|
|
84 module lcfos;
|
|
85 procedure WriteLn() : integer;
|
|
86 const zzz = 13;
|
|
87 var
|
|
88 a, b, c: integer;
|
|
89 begin
|
|
90 a := 2;
|
|
91 b := 7;
|
|
92 c := 10 * a + b*10*a;
|
|
93 return c
|
|
94 end WriteLn;
|
|
95 begin end lcfos.
|
|
96 """
|
|
97 pc = Compiler()
|
|
98 pc.compilesource(source)
|
5
|
99 def tstForStatement(self):
|
4
|
100 source = """
|
|
101 module fortest;
|
|
102 var
|
|
103 a,b,c : integer;
|
|
104 begin
|
|
105 c := 0;
|
|
106 for a := 1 to 10 by 1 do
|
|
107 b := a + 15;
|
|
108 c := c + b * a;
|
|
109 end;
|
|
110 end fortest.
|
|
111 """
|
|
112 pc = Compiler()
|
|
113 pc.compilesource(source)
|
|
114 def testSourceIfAndWhilePattern(self):
|
|
115 source = """
|
|
116 module lcfos;
|
|
117 procedure WriteLn() : integer;
|
|
118 const zzz = 13;
|
|
119 var
|
|
120 a, b, c: integer;
|
|
121 begin
|
|
122 a := 1;
|
|
123 b := 2;
|
|
124 if a * 3 > b then
|
|
125 c := 10*a + b*10*a*a*a*b;
|
|
126 else
|
|
127 c := 13;
|
|
128 end;
|
|
129 while a < 101 do
|
|
130 a := a + 1;
|
|
131 c := c + 2;
|
|
132 end;
|
|
133 return c
|
|
134 end WriteLn;
|
|
135 begin end lcfos.
|
|
136 """
|
|
137 pc = Compiler()
|
|
138 pc.compilesource(source)
|
|
139
|
|
140 def testPattern1(self):
|
|
141 """ Test if expression can be compiled into byte code """
|
|
142 src = "12*13+33-12*2*3"
|
|
143 tokens = lexer.tokenize(src)
|
|
144 ast = Parser(tokens).parseExpression()
|
|
145 code = CodeGenerator().genexprcode(ast)
|
|
146
|
7
|
147 def testProject(self):
|
15
|
148 p = Project('test.xml', isnew=True)
|
7
|
149 p.name = "Test project"
|
|
150 p.files.append('main.mod')
|
|
151 p.files.append('test.mod')
|
|
152 p.save('test.xml')
|
|
153
|
15
|
154 q = Project('test.xml')
|
7
|
155
|
|
156 assert(p.name == q.name)
|
|
157 assert(p.files == q.files)
|
|
158 # TODO: remove test.xml test file
|
8
|
159 os.remove('test.xml')
|
4
|
160
|
|
161 if __name__ == '__main__':
|
|
162 unittest.main()
|
|
163
|