annotate python/runtests.py @ 192:6cd6260789a1

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