Permalink
Browse files

Fix stack error, pasen runs

  • Loading branch information...
1 parent e98a55f commit 2a5809b988d5eb83659fa976a79092786334f79c @markflorisson committed Jul 5, 2011
Showing with 100 additions and 93 deletions.
  1. +4 −4 g-files/SELMAChecker.g
  2. +32 −32 g-files/SELMACompiler.g
  3. +1 −0 pasen/pasen.SELMA
  4. +63 −57 pasen/pasen.jasmin
View
8 g-files/SELMAChecker.g
@@ -31,7 +31,7 @@ options {
public SymbolTable<CheckerEntry> st = new SymbolTable<CheckerEntry>();
// Keep track of whether we are assigning to an identifier
int assigning = 0;
-
+
public void matchType(Tree expectedType, SR_Type exprType) {
matchType(((SELMATree) expectedType).getSelmaType(), exprType);
}
@@ -126,7 +126,7 @@ declaration
{
SELMATree type = (SELMATree) node.getChild(0);
st.retrieve($funcname).type = type.getSelmaType();
- } compoundexpression expression
+ } compoundexpression expression
{
SELMATree expr = (SELMATree) node.getChild(2);
matchType(type, expr.SR_type);
@@ -346,7 +346,7 @@ for (int i=1; i<func.getChildCount(); i++){
CheckerEntry ident = st.retrieve(e1);
ident.initialized = true;
-
+
if (ident.kind!=SR_Kind.VAR)
throw new SELMAException(e1,"Must be a variable");
if (ident.type!=e2.SR_type)
@@ -385,7 +385,7 @@ for (int i=1; i<func.getChildCount(); i++){
{
CheckerEntry entry = st.retrieve($node);
if (assigning == 0 && !entry.isInitialized(st))
- throw new SELMAException($node,
+ throw new SELMAException($node,
"Variable " + $node.text + " is not initialized yet.");
$node.SR_type=entry.type;
$node.SR_kind=entry.kind;
View
64 g-files/SELMACompiler.g
@@ -13,7 +13,7 @@ options {
import SELMA.SELMATree.SR_Type;
import SELMA.SELMATree.SR_Kind;
import SELMA.SELMATree.SR_Func;
-
+
import java.lang.StringBuilder;
}
@@ -25,9 +25,9 @@ options {
@members {
public SymbolTable<CompilerEntry> st = new SymbolTable<CompilerEntry>();
-
- int curStackDepth;
- int maxStackDepth;
+
+ int curStackDepth = 0;
+ int maxStackDepth = 0;
int labelNum = 0;
class StackDepthLabelCounter {
@@ -41,7 +41,7 @@ options {
Stack<StackDepthLabelCounter> stack = new Stack<StackDepthLabelCounter>();
private void incrStackDepth() {
- if (curStackDepth > maxStackDepth)
+ if (++curStackDepth > maxStackDepth)
maxStackDepth = curStackDepth;
}
@@ -53,35 +53,35 @@ options {
o.nextAddr = st.nextAddr;
o.localCount = st.localCount;
- stack.push(o);
-
+ stack.push(o);
+
st.enterFuncScope();
curStackDepth = maxStackDepth = labelNum = st.localCount = 0;
st.nextAddr = 0;
}
-
+
private void leaveFuncScope() {
StackDepthLabelCounter o = stack.pop();
st.leaveFuncScope();
curStackDepth = o.curStackDepth;
maxStackDepth = o.maxStackDepth;
labelNum = o.labelNum;
st.nextAddr = o.nextAddr;
- st.localCount = o.localCount;
+ st.localCount = o.localCount;
}
private String getTypeDenoter(SR_Type type) {
return st.getTypeDenoter(type, false);
}
-
+
private String getTypeDenoter(SR_Type type, boolean printing) {
return st.getTypeDenoter(type, printing);
}
}
program
: ^(node=BEGIN {st.openScope();} compoundexpression END)
- { SELMATree expr = (SELMATree) $node.getChild(0);
+ { SELMATree expr = (SELMATree) $node.getChild(0);
int localsCount = st.getLocalsCount();
st.closeScope();
}
@@ -122,7 +122,7 @@ declaration
| ^(node=CONST CHAR val=CHARV (id=ID)+)
{ char c = $val.text.charAt(1);
- st.enter($id, new CompilerEntry(SR_Type.CHAR, SR_Kind.CONST, 0).setChar(c));
+ st.enter($id, new CompilerEntry(SR_Type.CHAR, SR_Kind.CONST, 0).setChar(c));
}
//-> declareConst(id={$id.text}, val={(int) c}, type={"character"}, addr={st.nextAddr()-1})
@@ -142,37 +142,37 @@ declaration
//paramTypeDenoters.add(getTypeDenoter(type1.getSelmaType()));
st.addParamToFunc($funcname, param, type1);
})*
- ( ^(return_node=FUNCRETURN (INT|BOOL|CHAR)
+ ( ^(return_node=FUNCRETURN (INT|BOOL|CHAR)
{
SELMATree returnTypeNode = (SELMATree) $return_node.getChild(0);
-
+
signatureBuilder.append(")");
signatureBuilder.append(getTypeDenoter(returnTypeNode.getSelmaType()));
funcentry.signature = signatureBuilder.toString();
-
+
hasReturnType = true;
}
-
+
(body+=compoundexpression) retexpr=expression)
| ({funcentry.signature = signatureBuilder.toString() + ")V";} body+=compoundexpression)
)
{
SELMATree funcbody;
int stackLimit = maxStackDepth + 3;
int localsLimit = st.getLocalsCount();
-
+
if ($return_node == null)
- funcbody = (SELMATree) $node.getChild(paramCount * 2 + 1);
+ funcbody = (SELMATree) $node.getChild(paramCount * 2 + 1);
else
funcbody = (SELMATree) $return_node.getChild(1);
-
+
leaveFuncScope();
}
)
- -> function(funcname={$funcname.text},
+ -> function(funcname={$funcname.text},
body={$body},
signature={funcentry.signature},
- return_expression={$retexpr.st},
+ return_expression={$retexpr.st},
is_void={funcentry.signature.endsWith("V")},
pop={funcbody.SR_type != SR_Type.VOID},
stack_limit={stackLimit},
@@ -244,7 +244,7 @@ expression
label_num1={labelNum++}, label_num2={labelNum++})
//CONDITIONAL
- | ^(node=IF { st.openScope(); } ec1=compoundexpression { st.closeScope(); } THEN
+ | ^(node=IF { st.openScope(); } ec1=compoundexpression { st.closeScope(); } THEN
{ st.openScope(); } ec2=compoundexpression { st.closeScope(); }
(ELSE { st.openScope(); } ec3=compoundexpression { st.closeScope(); })?)
{ boolean ec3NotEmpty = $ec3.st != null;
@@ -258,15 +258,15 @@ expression
pop1={$node.SR_type == SR_Type.VOID && expr2.SR_type != SR_Type.VOID},
pop2={ec3NotEmpty && $node.SR_type == SR_Type.VOID && expr3.SR_type != SR_Type.VOID})
- | ^(node=WHILE
- { st.openScope(); } ec1=compoundexpression { st.closeScope(); } DO
+ | ^(node=WHILE
+ { st.openScope(); } ec1=compoundexpression { st.closeScope(); } DO
{ st.openScope(); } ec2=compoundexpression { st.closeScope(); } OD)
{ SELMATree expr2 = (SELMATree) node.getChild(2);
- boolean pop = expr2.SR_type != SR_Type.VOID;
- if (pop)
- curStackDepth--;
+ boolean pop = expr2.SR_type != SR_Type.VOID;
+ if (pop)
+ curStackDepth--;
}
- -> while(ec1={$ec1.st}, ec2={$ec2.st}, pop={pop},
+ -> while(ec1={$ec1.st}, ec2={$ec2.st}, pop={pop},
label_num1={labelNum++}, label_num2={labelNum++})
//IO
@@ -338,16 +338,16 @@ expression
-> print(exprs={$exprs}, type_denoters={typeDenoters}, dup_top={isExpr},
expr_is_bool={exprIsBool},
label_nums1={labelNums1}, label_nums2={labelNums2}, line={$node.getLine()})
-
+
| ^(node=FUNCTION id=ID (exprs+=expression)*)
-> funccall(id={$id.text}, signature={st.retrieve($id).signature}, exprs={$exprs})
//ASSIGN
- | ^(BECOMES node=ID e1=expression)
- {
+ | ^(BECOMES node=ID e1=expression)
+ {
CompilerEntry entry = st.retrieve(node);
String ident = entry.getIdentifier($node.text);
boolean isConst = entry.kind == SR_Kind.CONST;
-
+
String typeDenoter = getTypeDenoter(entry.type);
// System.err.println("assign " + ident + " " + entry + " " + typeDenoter);
}
View
1 pasen/pasen.SELMA
@@ -46,6 +46,7 @@ if @checkjaar(jaar,); then
const twaalf: integer = 12;
function nest(stap,w: integer;): integer {
var returnvalue: integer;
+ returnvalue := 0;
if stap==1; then
returnvalue := 3*w;
else
View
120 pasen/pasen.jasmin
@@ -21,7 +21,7 @@
return
.end method
.method public static main([Ljava/lang/String;)V
-.limit stack 3
+.limit stack 48
.limit locals 20
new java/util/Scanner
dup ; dup for <init>
@@ -295,7 +295,7 @@
dup
putstatic Main/C_1 I
pop
-.line 64
+.line 65
ldc 1 ; load constant stap_1
iconst_1
iadd
@@ -306,7 +306,7 @@
dup
putstatic Main/X_1 I
pop
-.line 69
+.line 70
getstatic Main/jaar_1 I ; load global jaar_1
ldc 1900
if_icmpge L0 ; e1 >= e2
@@ -316,14 +316,14 @@ L0:
iconst_1
L1:
ifeq L2 ; e1 is false
-.line 70
+.line 71
iconst_1
; e1 right hand for assignment
dup
putstatic Main/Y_1 I
goto L3
L2:
-.line 72
+.line 73
bipush 8
getstatic Main/C_1 I ; load global C_1
imul
@@ -335,14 +335,14 @@ L2:
putstatic Main/Y_1 I
L3:
pop
-.line 89
+.line 90
invokestatic Main/nested()I ; e1 right hand for assignment
dup
putstatic Main/Z_1 I
pop
-.line 94
+.line 95
ldc 11 ; load constant elf_1
-.line 94
+.line 95
getstatic Main/G_1 I ; load global G_1
bipush 20
iadd
@@ -359,7 +359,7 @@ L3:
dup
putstatic Main/E_1 I
pop
-.line 96
+.line 97
getstatic Main/EE_1 I ; load global EE_1
bipush 24
if_icmpeq L4 ; e1 = e2
@@ -368,7 +368,7 @@ L3:
L4:
iconst_1
L5:
-.line 96
+.line 97
getstatic Main/E_1 I ; load global E_1
bipush 25
if_icmpeq L6 ; e1 = e2
@@ -377,7 +377,7 @@ L5:
L6:
iconst_1
L7:
-.line 96
+.line 97
getstatic Main/G_1 I ; load global G_1
bipush 11
if_icmpgt L8 ; e1 > e2
@@ -391,22 +391,22 @@ L9:
ldc 0 ; load constant mnope_1
ior
ifeq L10 ; e1 is false
-.line 97
+.line 98
iconst_1
getstatic Main/E_1 I ; load global E_1
iadd ; e1 right hand for assignment
dup
putstatic Main/E_1 I
pop
L10:
-.line 104
+.line 105
ldc 44 ; load constant minus2_1
getstatic Main/E_1 I ; load global E_1
isub ; e1 right hand for assignment
dup
putstatic Main/N_1 I
pop
-.line 106
+.line 107
getstatic Main/N_1 I ; load global N_1
bipush 21
if_icmplt L11 ; e1 < e2
@@ -418,27 +418,27 @@ L12: ; e1 right hand for assignment
dup
putstatic Main/tosmall_1 I
pop
-.line 107
- getstatic Main/N_1 I ; load global N_1
.line 108
+ getstatic Main/N_1 I ; load global N_1
+.line 109
getstatic Main/tosmall_1 I ; load global tosmall_1
ifeq L13 ; e1 is false
-.line 109
+.line 110
bipush 30
goto L14
L13:
-.line 111
+.line 112
iconst_0
L14:
iadd ; e1 right hand for assignment
dup
putstatic Main/N_1 I
pop
-.line 117
+.line 118
getstatic Main/N_1 I ; load global N_1
bipush 7
iadd
-.line 117
+.line 118
getstatic Main/Z_1 I ; load global Z_1
getstatic Main/N_1 I ; load global N_1
iadd
@@ -448,7 +448,7 @@ L14:
dup
putstatic Main/P_1 I
pop
-.line 122
+.line 123
getstatic Main/P_1 I ; load global P_1
bipush 31
if_icmpgt L15 ; e1 > e2
@@ -458,41 +458,41 @@ L15:
iconst_1
L16:
ifeq L17 ; e1 is false
-.line 123
+.line 124
iconst_4
; e1 right hand for assignment
dup
putstatic Main/month_1 I
pop
-.line 124
+.line 125
getstatic Main/P_1 I ; load global P_1
bipush 31
irem ; e1 right hand for assignment
dup
putstatic Main/day_1 I
goto L18
L17:
-.line 126
+.line 127
iconst_3
; e1 right hand for assignment
dup
putstatic Main/month_1 I
pop
-.line 127
+.line 128
getstatic Main/P_1 I ; load global P_1
; e1 right hand for assignment
dup
putstatic Main/day_1 I
L18:
pop
-.line 141
+.line 142
invokestatic Main/printdatum()V
; e2 if true expression
L19:
return
.end method
.method public static checkjaar(I)I
-.limit stack 3
+.limit stack 26
.limit locals 3
.line 9
.line 10
@@ -719,11 +719,17 @@ L10:
ireturn
.end method
.method public static nest(II)I
-.limit stack 3
+.limit stack 5
.limit locals 5
.line 47
.line 48
.line 49
+ iconst_0
+ ; e1 right hand for assignment
+ dup
+ istore 2 ; store e1 in returnvalue_1
+ pop
+.line 50
iload 0 ; load stap_1 from 0
iconst_1
if_icmpeq L0 ; e1 = e2
@@ -733,7 +739,7 @@ L0:
iconst_1
L1:
ifeq L9 ; e1 is false
-.line 50
+.line 51
iconst_3
iload 1 ; load w_1 from 1
imul ; e1 right hand for assignment
@@ -742,7 +748,7 @@ L1:
pop
goto L10
L9:
-.line 52
+.line 53
iload 0 ; load stap_1 from 0
iconst_2
if_icmpeq L2 ; e1 = e2
@@ -752,22 +758,22 @@ L2:
iconst_1
L3:
ifeq L8 ; e1 is false
-.line 53
+.line 54
iload 1 ; load w_1 from 1
iconst_4
idiv ; e1 right hand for assignment
dup
istore 2 ; store e1 in returnvalue_1
pop
-.line 55
+.line 56
iconst_1
; e1 right hand for assignment
dup
istore 3 ; store e1 in loop_1
pop
-.line 56
+.line 57
L6:
-.line 56
+.line 57
iload 3 ; load loop_1 from 3
ldc 12 ; load constant twaalf_1
if_icmple L4 ; e1 <= e2
@@ -777,14 +783,14 @@ L4:
iconst_1
L5:
ifeq L7
-.line 57
+.line 58
iload 2 ; load returnvalue_1 from 2
iconst_1
isub ; e1 right hand for assignment
dup
istore 2 ; store e1 in returnvalue_1
pop
-.line 58
+.line 59
iload 3 ; load loop_1 from 3
iconst_1
iadd ; e1 right hand for assignment
@@ -801,36 +807,36 @@ L10:
ireturn
.end method
.method public static copy(I)I
-.limit stack 3
+.limit stack 4
.limit locals 2
-.line 75
.line 76
+.line 77
bipush 42
pop
iload 0 ; load jaar_1 from 0
ireturn
.end method
.method public static nested()I
-.limit stack 3
+.limit stack 5
.limit locals 2
-.line 82
.line 83
.line 84
+.line 85
getstatic Main/jaar_1 I ; load global jaar_1
iconst_5
imul
iconst_4
idiv
getstatic Main/X_1 I ; load global X_1
isub
-.line 84 ; - bipush 10
+.line 85 ; - bipush 10
bipush 10
ineg
iadd ; e1 right hand for assignment
dup
istore 0 ; store e1 in Z_1
pop
-.line 85
+.line 86
iload 0 ; load Z_1 from 0
invokestatic Main/copy(I)I ; e1 right hand for assignment
dup
@@ -840,17 +846,17 @@ L10:
ireturn
.end method
.method public static printdatum()V
-.limit stack 3
+.limit stack 8
.limit locals 2
-.line 130
.line 131
+.line 132
getstatic Main/day_1 I ; load global day_1
dup
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(I)V
pop
-.line 133
+.line 134
bipush 95 ; ldc '_'
dup
getstatic java/lang/System/out Ljava/io/PrintStream;
@@ -860,7 +866,7 @@ L10:
dup
istore 0 ; store e1 in under_1
pop
-.line 134
+.line 135
getstatic Main/month_1 I ; load global month_1
iconst_3
if_icmpeq L0 ; e1 = e2
@@ -870,62 +876,62 @@ L0:
iconst_1
L1:
ifeq L2 ; e1 is false
-.line 135
+.line 136
bipush 77 ; ldc 'M'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
- .line 135
+ .line 136
bipush 97 ; ldc 'a'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
- .line 135
+ .line 136
bipush 97 ; ldc 'a'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
- .line 135
+ .line 136
bipush 114 ; ldc 'r'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
- .line 135
+ .line 136
bipush 116 ; ldc 't'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
; e2 if true expression
goto L3
L2:
-.line 137
+.line 138
bipush 65 ; ldc 'A'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
- .line 137
+ .line 138
bipush 112 ; ldc 'p'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
- .line 137
+ .line 138
bipush 114 ; ldc 'r'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
- .line 137
+ .line 138
bipush 105 ; ldc 'i'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
- .line 137
+ .line 138
bipush 108 ; ldc 'l'
getstatic java/lang/System/out Ljava/io/PrintStream;
swap
invokevirtual java/io/PrintStream/println(C)V
; e3 if false expression
L3:
-.line 139
+.line 140
iload 0 ; load under_1 from 0
getstatic java/lang/System/out Ljava/io/PrintStream;
swap

0 comments on commit 2a5809b

Please sign in to comment.