Skip to content

Commit 324d7a1

Browse files
author
Ash Cripps
committed
Final touches
1 parent e1cc137 commit 324d7a1

19 files changed

+461
-134
lines changed

BuggySemanticInput.mj

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Test input for checking symbol table handling
2+
//**************************************************
3+
// invoke as: java MJ.Compiler BuggySemanticInput.mj
4+
//**************************************************
5+
6+
program P
7+
final int c1 = 'x';
8+
//----------------| integer constant expected
9+
final char cc = 3;
10+
//----------------| character constant expected
11+
12+
class C {
13+
int f;
14+
}
15+
16+
int i;
17+
char ch;
18+
C c, c2;
19+
char[] a;
20+
21+
ch x;
22+
//---| type expected
23+
ch[] y;
24+
//---| type expected
25+
char i;
26+
//-----| i already declared
27+
28+
{
29+
void foo (int x) {
30+
return 3;
31+
//---------| void method must not return a value
32+
}
33+
34+
int bar (char c) {
35+
if (c > 'x') return; else return c;
36+
//---------------------| return expression expected
37+
//-----------------------------------| return type must match method type
38+
}
39+
40+
int main()
41+
//| must be void
42+
{
43+
i = 1;
44+
int = 3;
45+
//--| type identifier not allowed here
46+
ch = 3;
47+
//--------| incompatible types in assignment
48+
i = 3 * v + 1;
49+
//----------| v not found
50+
x(3, ch);
51+
//--| called object is not a method
52+
read(foo);
53+
//--------| read object must be a variable
54+
read(c);
55+
//-------| can only read int or char variables
56+
print(c);
57+
//--------| can only print int or char variables
58+
foo(3, i);
59+
//----------| more actual than formal parameters
60+
foo();
61+
//-------| fewer actual than formal parameters
62+
foo('x');
63+
//------| parameter type mismatch
64+
if (i == 'a') i = i + ch; else i = i * ch;
65+
//------------| type mismatch
66+
//-------------------------| operands must be of type int
67+
//------------------------------------------| operands must be on type int
68+
if (c > c2) c = - c2;
69+
//------------| invalid compare
70+
//---------------------| integer operand required
71+
i = ch();
72+
//------| called object is not a method
73+
i = new int;
74+
//------------| class type expected
75+
a = new ch[3];
76+
//-----------| type expected
77+
a = new char['3'];
78+
//---------------| array size must be an integer
79+
i = a.f + 3 * c.g;
80+
//------| dereferenced object is not a class
81+
//------------------| g not found
82+
i = foo[2];
83+
//-------| bad method call
84+
i = ch[2];
85+
//--------| indexed object is not an array
86+
i = a['2'];
87+
//-------| indexed object is not an array
88+
//----------| index must be an int
89+
}
90+
}

InvalidScanner.mj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Program P
2+
{
3+
void main()
4+
int x, i;
5+
char c;
6+
{
7+
c = 'ab';
8+
x = 15;
9+
i = 0;
10+
@j = int[];
11+
while (i < 15){
12+
print(i);
13+
i = i + 1;
14+
}
15+
}
16+
}

MJ/CodeGen/Code.class

0 Bytes
Binary file not shown.

MJ/CodeGen/Code.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/* MicroJava Code Generator (HM 06-12-28)
2-
========================
3-
This class holds the code buffer with its access primitives get* and put*.
4-
It also holds methods to load operands and to generate complex instructions
5-
such as assignments and jumps.
6-
*/
71
package MJ.CodeGen;
82

93
import java.io.*;
@@ -113,18 +107,18 @@ public static int get(int pos) {
113107
public static void load(Operand x) {
114108
switch (x.kind) {
115109
case Operand.Con:
116-
if (0 <= x.val && x.val <= 5) put(const0+ x.val);
110+
if (0 <= x.val && x.val <= 5) put(const0+ x.val); //More efficent put
117111
else if (x.val == -1) put(const_m1);
118112
else {
119113
put(const_);
120114
put4(x.val);
121115
}
122116
break;
123-
case Operand.Static:
117+
case Operand.Static: //Static operand
124118
put(getstatic);
125119
put2(x.adr);
126120
break;
127-
case Operand.Local:
121+
case Operand.Local: //Local vars
128122
if (0 <= x.adr && x.adr <= 3) put(load0+ x.adr);
129123
else {
130124
put(load);
@@ -176,8 +170,8 @@ public static void assign(Operand x, Operand y) {
176170

177171
// Unconditional jump
178172
public static void putJump(int adr) {
179-
put(jmp);
180-
put2(adr);
173+
put(jmp); //Put jump instruction in Buffer
174+
put2(adr); //Put location of where to jump too
181175
}
182176

183177
// Conditional jump if op is false

MJ/Parser.class

699 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)