Skip to content

Commit

Permalink
more complex tests (2h)
Browse files Browse the repository at this point in the history
  • Loading branch information
danschnurp committed Dec 14, 2022
1 parent 1e161bc commit 1499719
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 62 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ todo
[//]: # (})

- boolean
- closures (lambda expressions)



Expand Down
9 changes: 9 additions & 0 deletions sample_input/for_in_func.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var a : Int = 52;
func someOtherFunction(c: Int) -> Int{
for(var j: Int = 0; j < 32*42; j+= 1;){
c += 1;
}
return c;
}

a = someOtherFunction(a);
8 changes: 8 additions & 0 deletions sample_input/not_tested/closure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var some_param: Int = 1000;
let something = { (some_param) -> Int in
var number = some_param;
repeat {
number /= 10;
} while number > 0
return number;
}
8 changes: 0 additions & 8 deletions sample_input/not_tested/for_in_func.swift

This file was deleted.

54 changes: 0 additions & 54 deletions sample_input/not_tested/program.swift

This file was deleted.

49 changes: 49 additions & 0 deletions sample_input/program.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

func someOtherFunction(d: Int) -> Int{
var b: Int = 20;
if( 52 > 43){
var f: Int = d;
f = 20;
b *= 32;
return b;
}
else {
return b;
}

}

func someOtherFunction2(a:Int) -> Int{
for(var j: Int = 1; j < 21; j += 1;) {
a = j + 5;
}
return a;
}
func someOtherFunction3(a:Int, b:Int) -> Int{
for(var i: Int = 1; i < 20; i += 1;) {
a += 1;
b += 2;
}
return a;

}

func someComplexFunction(A: Int, B: Int, C: Int, D: Int)->Int {
var someResult : Int = 42;
for(var i: Int = 1; i < 3; i += 1;) {
for(var j: Int = 1; j < 3; j += 1;) {
someResult += A * B - C - D + i + j;
}
}
return someResult;
}


var a : Int = 0;

var b : Int = 0;
b = someOtherFunction(43);
a = someOtherFunction3(a,b);
// a = someOtherFunction(52);

// b = someComplexFunction(42,42,42,42);
2 changes: 2 additions & 0 deletions src/pl0_code_generator/pl0.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ def gen_expression(self, sub_tree, index, symbol_table=None, level=0):

elif sub_tree[index].name == "expression_term":
self.expressions[sub_tree[index].name](leaf_names[0], symbol_table=symbol_table)
if sub_tree[index].children[0].name == "const_expression_term":
index += len(sub_tree)
else:
self.expressions[sub_tree[index].name](leaf_names[0], leaf_names[1], symbol_table=symbol_table)
index += 2
Expand Down
182 changes: 182 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,185 @@ def test_ternary_operator(self):
15 STO 0 3
16 RET 0 0
""", code, "ternary_operator")

def test_for_in_func(self):
code = main("../sample_input/for_in_func.swift")
self.assertEqual("""0 INT 0 3
1 INT 0 1
2 LIT 0 52
3 STO 0 3
4 JMP 0 26
5 INT 0 3
6 LOD 0 -1
7 INT 0 1
8 LIT 0 0
9 STO 0 3
10 LOD 0 3
11 LIT 0 1344
12 OPR 0 10
13 JMC 0 23
14 LIT 0 1
15 LOD 0 2
16 OPR 0 2
17 STO 0 2
18 LIT 0 1
19 LOD 0 3
20 OPR 0 2
21 STO 0 3
22 JMP 0 10
23 LOD 0 2
24 STO 0 -2
25 RET 0 0
26 INT 0 1
27 LOD 0 3
28 CAL 0 5
29 INT 0 -1
30 STO 0 3
31 RET 0 0
""", code, "for_in_func")

def test_program(self):
code = main("../sample_input/program.swift")
self.assertEqual("""0 INT 0 3
1 JMP 0 25
2 INT 0 3
3 LOD 0 -1
4 INT 0 1
5 LIT 0 20
6 STO 0 7
7 LIT 0 52
8 LIT 0 43
9 OPR 0 12
10 JMC 0 22
11 INT 0 1
12 LOD 0 6
13 STO 0 8
14 LIT 0 20
15 STO 0 8
16 LIT 0 32
17 LOD 0 7
18 OPR 0 4
19 STO 0 7
20 LOD 0 7
21 JMP 0 23
22 LOD 0 7
23 STO 0 -2
24 RET 0 0
25 JMP 0 47
26 INT 0 3
27 LOD 0 -1
28 INT 0 1
29 LIT 0 1
30 STO 0 7
31 LOD 0 7
32 LIT 0 21
33 OPR 0 10
34 JMC 0 44
35 LOD 0 7
36 LIT 0 5
37 OPR 0 2
38 STO 0 6
39 LIT 0 1
40 LOD 0 7
41 OPR 0 2
42 STO 0 7
43 JMP 0 31
44 LOD 0 6
45 STO 0 -2
46 RET 0 0
47 JMP 0 74
48 INT 0 3
49 LOD 0 -2
50 LOD 0 -1
51 INT 0 1
52 LIT 0 1
53 STO 0 8
54 LOD 0 8
55 LIT 0 20
56 OPR 0 10
57 JMC 0 71
58 LIT 0 1
59 LOD 0 6
60 OPR 0 2
61 STO 0 6
62 LIT 0 2
63 LOD 0 7
64 OPR 0 2
65 STO 0 7
66 LIT 0 1
67 LOD 0 8
68 OPR 0 2
69 STO 0 8
70 JMP 0 54
71 LOD 0 6
72 STO 0 -3
73 RET 0 0
74 JMP 0 124
75 INT 0 3
76 LOD 0 -4
77 LOD 0 -3
78 LOD 0 -2
79 LOD 0 -1
80 INT 0 1
81 LIT 0 42
82 STO 0 10
83 INT 0 1
84 LIT 0 1
85 STO 0 11
86 LOD 0 11
87 LIT 0 3
88 OPR 0 10
89 JMC 0 121
90 INT 0 1
91 LIT 0 1
92 STO 0 12
93 LOD 0 12
94 LIT 0 3
95 OPR 0 10
96 JMC 0 116
97 LOD 0 6
98 LOD 0 7
99 OPR 0 4
100 LOD 0 8
101 OPR 0 3
102 LOD 0 9
103 OPR 0 3
104 LOD 0 11
105 OPR 0 2
106 LOD 0 12
107 OPR 0 2
108 LOD 0 10
109 OPR 0 2
110 STO 0 10
111 LIT 0 1
112 LOD 0 12
113 OPR 0 2
114 STO 0 12
115 JMP 0 93
116 LIT 0 1
117 LOD 0 11
118 OPR 0 2
119 STO 0 11
120 JMP 0 86
121 LOD 0 10
122 STO 0 -5
123 RET 0 0
124 INT 0 1
125 LIT 0 0
126 STO 0 7
127 INT 0 1
128 LIT 0 0
129 STO 0 8
130 INT 0 1
131 LIT 0 43
132 CAL 0 2
133 INT 0 -1
134 STO 0 8
135 INT 0 1
136 LOD 0 7
137 LOD 0 8
138 CAL 0 48
139 INT 0 -2
140 STO 0 7
141 RET 0 0
""", code, "program")

0 comments on commit 1499719

Please sign in to comment.