Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support recursion, add tests, support redeclaration of globals in inn…

…er scopes
  • Loading branch information...
commit 5c7009a019880ca2bfefcd22dcbd66c2e9d4114a 1 parent f0e859d
@markflorisson authored
View
BIN  Verslag/SELMA.pdf
Binary file not shown
View
11 g-files/SELMAChecker.g
@@ -108,7 +108,7 @@ declaration
break;
}
}
- | ^(FUNCDEF funcname=ID
+ | ^(node=FUNCDEF funcname=ID
{
//enter as void
if (st.funclevel != 0)
@@ -122,12 +122,13 @@ declaration
}
)*
(
- ^(node=FUNCRETURN type compoundexpression expression
+ ^(node=FUNCRETURN type
{
SELMATree type = (SELMATree) node.getChild(0);
- SELMATree expr = (SELMATree) node.getChild(2);
- st.retrieve($funcname).type = expr.SR_type;
-
+ st.retrieve($funcname).type = type.getSelmaType();
+ } compoundexpression expression
+ {
+ SELMATree expr = (SELMATree) node.getChild(2);
matchType(type, expr.SR_type);
})
| (compoundexpression))
View
75 g-files/SELMACompiler.g
@@ -55,14 +55,14 @@ options {
stack.push(o);
- st.openScope();
+ st.enterFuncScope();
curStackDepth = maxStackDepth = labelNum = st.localCount = 0;
st.nextAddr = 0;
}
private void leaveFuncScope() {
StackDepthLabelCounter o = stack.pop();
- st.closeScope();
+ st.leaveFuncScope();
curStackDepth = o.curStackDepth;
maxStackDepth = o.maxStackDepth;
labelNum = o.labelNum;
@@ -83,14 +83,13 @@ program
: ^(node=BEGIN {st.openScope();} compoundexpression END)
{ SELMATree expr = (SELMATree) $node.getChild(0);
int localsCount = st.getLocalsCount();
- List<String> globalVariableFields = st.getAllLocalVariablesWithTypes();
st.closeScope();
}
-> program(instructions={$compoundexpression.st},
source_file={SELMA.inputFilename},
stack_limit={maxStackDepth + 3}, // +3 for print
locals_limit={localsCount + 1}, // +1 for the String[] argv parameter
- fields={globalVariableFields},
+ fields={st.globals},
pop={expr.SR_type != SR_Type.VOID})
;
@@ -135,7 +134,7 @@ declaration
enterFuncScope();
int paramCount = 0;
StringBuilder signatureBuilder = new StringBuilder("(");
- //List<String> paramTypeDenoters = new ArrayList<String>();
+ boolean hasReturnType = false;
} (param=ID typ1=(INT|BOOL|CHAR)
{
SELMATree type1 = (SELMATree) $node.getChild(++paramCount * 2);
@@ -143,34 +142,38 @@ declaration
//paramTypeDenoters.add(getTypeDenoter(type1.getSelmaType()));
st.addParamToFunc($funcname, param, type1);
})*
- ( ^(return_node=FUNCRETURN (INT|BOOL|CHAR) (body+=compoundexpression) retexpr=expression)
- | (body+=compoundexpression)
+ ( ^(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;
+ SELMATree funcbody;
int stackLimit = maxStackDepth + 3;
int localsLimit = st.getLocalsCount();
-
- signatureBuilder.append(")");
-
- if ($return_node == null) {
- funcbody = (SELMATree) $node.getChild(paramCount * 2 + 1);
- signatureBuilder.append("V");
- } else {
+
+ if ($return_node == null)
+ funcbody = (SELMATree) $node.getChild(paramCount * 2 + 1);
+ else
funcbody = (SELMATree) $return_node.getChild(1);
- SELMATree returnType = (SELMATree) $return_node.getChild(0);
- signatureBuilder.append(getTypeDenoter(returnType.getSelmaType()));
- }
+
leaveFuncScope();
-
- String signature = signatureBuilder.toString();
- funcentry.signature = signature;
- })
+ }
+ )
-> function(funcname={$funcname.text},
body={$body},
- signature={signature},
+ signature={funcentry.signature},
return_expression={$retexpr.st},
- is_void={signature.endsWith("V")},
+ is_void={funcentry.signature.endsWith("V")},
pop={funcbody.SR_type != SR_Type.VOID},
stack_limit={stackLimit},
locals_limit={localsLimit + 1},
@@ -293,8 +296,8 @@ expression
addrs.add(entry.addr);
isBool.add(child.SR_type == SR_Type.BOOL);
isInt.add(child.SR_type == SR_Type.INT);
- globals.add(entry.level == 0);
- ids.add(child.getText());
+ globals.add(entry.isGlobal);
+ ids.add(entry.getIdentifier(child.getText()));
}
}
-> read(ids={ids}, addrs={addrs}, dup_top={isExpr},
@@ -337,19 +340,15 @@ expression
| ^(BECOMES node=ID e1=expression)
{
CompilerEntry entry = st.retrieve(node);
+ String ident = entry.getIdentifier($node.text);
boolean isConst = node.SR_kind == SR_Kind.CONST;
- boolean isGlobal = false;
String typeDenoter = getTypeDenoter(entry.type);
-
- if (entry.level == 0) {
- isGlobal = true;
- }
}
- -> assign(id={$node.text},
+ -> assign(id={ident},
type={$node.type},
addr={st.retrieve($node).addr},
e1={$e1.st},
- is_global={isGlobal},
+ is_global={entry.isGlobal},
type_denoter={typeDenoter})
//closedcompound
@@ -372,16 +371,12 @@ expression
{
incrStackDepth();
CompilerEntry entry = st.retrieve(node);
+ String ident = entry.getIdentifier($node.text);
boolean isConst = node.SR_kind == SR_Kind.CONST;
- boolean isGlobal = false;
String typeDenoter = getTypeDenoter(entry.type);
-
- if (entry.level == 0) {
- isGlobal = true;
- }
}
- -> loadVal(id={$node.text}, addr={entry.addr}, val={entry.val}, is_const={isConst},
- is_global={isGlobal}, type_denoter={typeDenoter})
+ -> loadVal(id={ident}, addr={entry.addr}, val={entry.val}, is_const={isConst},
+ is_global={entry.isGlobal}, type_denoter={typeDenoter})
;
View
25 pasen/pasen.SELMA
@@ -47,12 +47,12 @@ if @checkjaar(jaar,); then
function nest(stap,w: integer;): integer {
var returnvalue: integer;
if stap==1; then
- returnvalue := 3*w;
+ returnvalue := 3*w;
else
if stap == 2; then
returnvalue := w/4;
var loop: integer;
- loop := 1;
+ loop := 1;
while loop <= twaalf; do
returnvalue := returnvalue-1;
loop := loop + 1;
@@ -72,15 +72,16 @@ if @checkjaar(jaar,); then
Y := (8*C)/25 - 5;
fi;
+ function copy(jaar: integer;): integer {
+ 42;
+ return jaar;
+ };
+
// Zoek de zondag:
// Vermenigvuldig het jaartal met 5, geheeldeel de uitkomst door 4, trek er X en 10 vanaf, en noem dit getal Z. Voor 1991 geldt: Z = 2475.
function nested(): integer {
var Z: integer;
Z := (((jaar*5)/4)-X)+-10;
- function copy(jaar: integer;): integer {
- 42;
- return jaar;
- };
Z := @copy(Z,);
return Z;
};
@@ -89,9 +90,9 @@ if @checkjaar(jaar,); then
// Bepaal de epacta:
// 11 maal G + 20 + Y. Trek daarvan X af, geheeldeel het resultaat door 30 en noem de rest E. Als E gelijk is aan 24, of als E gelijk is aan 25 en het gulden getal is groter dan 11, tel dan 1 bij E op. De Epacta voor 1991 is 14.
- var E,EE: integer;
+ var E,EE: integer;
E := EE := ({const elf: integer = 11; elf*(G+20+Y);}-X)/30;
- const mnope: boolean = false;
+ const mnope: boolean = false;
if EE==24 || (E==25&&G>11) || mnope; then
E := 1+E;
fi;
@@ -103,7 +104,7 @@ if @checkjaar(jaar,); then
N := minus2-E;
var tosmall: boolean;
tosmall := N<21;
- N := N+
+ N := N+
if tosmall; then
30;
else
@@ -117,7 +118,7 @@ if @checkjaar(jaar,); then
// Paasdatum: Als P groter is dan 31, trek er dan 31 vanaf, en de paasdatum valt in April. Anders is de paasdag P in Maart. Zo wordt voor 1991 gevonden 31 maart.
var month: integer;
- var day: integer;
+ var day: integer;
if P>31; then
month := 4;
day := P%31;
@@ -128,14 +129,14 @@ if @checkjaar(jaar,); then
function printdatum() {
print(day);
- var under: character;
+ var under: character;
under := print('_');
if month==3; then
print('M','a','a','r','t');
else
print('A','p','r','i','l');
fi;
- print(under,jaar);
+ print(under,jaar);
};
@printdatum();
fi;
View
937 pasen/pasen.jasmin
@@ -0,0 +1,937 @@
+.source pasen/pasen.SELMA
+.class public Main
+.super java/lang/Object
+.field public static scanner_field Ljava/util/Scanner;
+.field public static X I
+.field public static E I
+.field public static C I
+.field public static jaar I
+.field public static EE I
+.field public static tosmall I
+.field public static P I
+.field public static month I
+.field public static Y I
+.field public static G I
+.field public static N I
+.field public static Z I
+.field public static day I
+.method public <init>()V
+ aload_0
+ invokespecial java/lang/Object/<init>()V
+ return
+.end method
+.method public static main([Ljava/lang/String;)V
+.limit stack 3
+.limit locals 20
+ new java/util/Scanner
+ dup ; dup for <init>
+ getstatic java/lang/System/in Ljava/io/InputStream;
+ invokespecial java/util/Scanner/<init>(Ljava/io/InputStream;)V
+ putstatic Main/scanner_field Ljava/util/Scanner;
+.line 7
+.line 22
+ bipush 72 ; ldc 'H'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 108 ; ldc 'l'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 108 ; ldc 'l'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 111 ; ldc 'o'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 118 ; ldc 'v'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 110 ; ldc 'n'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 119 ; ldc 'w'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 108 ; ldc 'l'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 107 ; ldc 'k'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 106 ; ldc 'j'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 114 ; ldc 'r'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 119 ; ldc 'w'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 105 ; ldc 'i'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 108 ; ldc 'l'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 106 ; ldc 'j'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 100 ; ldc 'd'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 112 ; ldc 'p'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 115 ; ldc 's'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 100 ; ldc 'd'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 116 ; ldc 't'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 117 ; ldc 'u'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 109 ; ldc 'm'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 119 ; ldc 'w'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 116 ; ldc 't'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 22
+ bipush 110 ; ldc 'n'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+.line 25
+ getstatic Main/scanner_field Ljava/util/Scanner;
+ invokevirtual java/util/Scanner/nextInt()I
+ dup
+ putstatic Main/jaar I
+ pop
+.line 29
+ getstatic Main/jaar I ; load global jaar
+ invokestatic Main/checkjaar(I)I
+ ifeq L19 ; e1 is false
+.line 32
+.line 33
+ getstatic Main/jaar I ; load global jaar
+ ldc 19 ; load constant negentien
+ idiv ; e1 right hand for assignment
+ dup
+ putstatic Main/G I
+ pop
+.line 34
+ getstatic Main/G I ; load global G
+ iconst_1
+ iadd ; e1 right hand for assignment
+ dup
+ putstatic Main/G I
+ pop
+.line 39
+ getstatic Main/jaar I ; load global jaar
+ ; e1 right hand for assignment
+ dup
+ putstatic Main/C I
+ pop
+.line 40
+ getstatic Main/C I ; load global C
+ bipush 100
+ idiv
+ iconst_1
+ iadd ; e1 right hand for assignment
+ dup
+ putstatic Main/C I
+ pop
+.line 64
+ ldc 1 ; load constant stap
+ iconst_1
+ iadd
+ ldc 1 ; load constant stap
+ getstatic Main/C I ; load global C
+ invokestatic Main/nest(II)I
+ invokestatic Main/nest(II)I ; e1 right hand for assignment
+ dup
+ putstatic Main/X I
+ pop
+.line 69
+ getstatic Main/jaar I ; load global jaar
+ ldc 1900
+ if_icmpge L0 ; e1 >= e2
+ iconst_0
+ goto L1
+L0:
+ iconst_1
+L1:
+ ifeq L2 ; e1 is false
+.line 70
+ iconst_1
+ ; e1 right hand for assignment
+ dup
+ putstatic Main/Y I
+ goto L3
+L2:
+.line 72
+ bipush 8
+ getstatic Main/C I ; load global C
+ imul
+ bipush 25
+ idiv
+ iconst_5
+ isub ; e1 right hand for assignment
+ dup
+ putstatic Main/Y I
+L3:
+ pop
+.line 89
+ invokestatic Main/nested()I ; e1 right hand for assignment
+ dup
+ putstatic Main/Z I
+ pop
+.line 94
+ ldc 11 ; load constant elf
+.line 94
+ getstatic Main/G I ; load global G
+ bipush 20
+ iadd
+ getstatic Main/Y I ; load global Y
+ iadd
+ imul
+ getstatic Main/X I ; load global X
+ isub
+ bipush 30
+ idiv ; e1 right hand for assignment
+ dup
+ putstatic Main/EE I
+ ; e1 right hand for assignment
+ dup
+ putstatic Main/E I
+ pop
+.line 96
+ getstatic Main/EE I ; load global EE
+ bipush 24
+ if_icmpeq L4 ; e1 = e2
+ iconst_0
+ goto L5
+L4:
+ iconst_1
+L5:
+.line 96
+ getstatic Main/E I ; load global E
+ bipush 25
+ if_icmpeq L6 ; e1 = e2
+ iconst_0
+ goto L7
+L6:
+ iconst_1
+L7:
+.line 96
+ getstatic Main/G I ; load global G
+ bipush 11
+ if_icmpgt L8 ; e1 > e2
+ iconst_0
+ goto L9
+L8:
+ iconst_1
+L9:
+ iand
+ ior
+ ldc 0 ; load constant mnope
+ ior
+ ifeq L10 ; e1 is false
+.line 97
+ iconst_1
+ getstatic Main/E I ; load global E
+ iadd ; e1 right hand for assignment
+ dup
+ putstatic Main/E I
+ pop
+L10:
+.line 104
+ ldc 44 ; load constant minus2
+ getstatic Main/E I ; load global E
+ isub ; e1 right hand for assignment
+ dup
+ putstatic Main/N I
+ pop
+.line 106
+ getstatic Main/N I ; load global N
+ bipush 21
+ if_icmplt L11 ; e1 < e2
+ iconst_0
+ goto L12
+L11:
+ iconst_1
+L12: ; e1 right hand for assignment
+ dup
+ putstatic Main/tosmall I
+ pop
+.line 107
+ getstatic Main/N I ; load global N
+.line 108
+ getstatic Main/tosmall I ; load global tosmall
+ ifeq L13 ; e1 is false
+.line 109
+ bipush 30
+ goto L14
+L13:
+.line 111
+ iconst_0
+L14:
+ iadd ; e1 right hand for assignment
+ dup
+ putstatic Main/N I
+ pop
+.line 117
+ getstatic Main/N I ; load global N
+ bipush 7
+ iadd
+.line 117
+ getstatic Main/Z I ; load global Z
+ getstatic Main/N I ; load global N
+ iadd
+ bipush 7
+ irem
+ isub ; e1 right hand for assignment
+ dup
+ putstatic Main/P I
+ pop
+.line 122
+ getstatic Main/P I ; load global P
+ bipush 31
+ if_icmpgt L15 ; e1 > e2
+ iconst_0
+ goto L16
+L15:
+ iconst_1
+L16:
+ ifeq L17 ; e1 is false
+.line 123
+ iconst_4
+ ; e1 right hand for assignment
+ dup
+ putstatic Main/month I
+ pop
+.line 124
+ getstatic Main/P I ; load global P
+ bipush 31
+ irem ; e1 right hand for assignment
+ dup
+ putstatic Main/day I
+ goto L18
+L17:
+.line 126
+ iconst_3
+ ; e1 right hand for assignment
+ dup
+ putstatic Main/month I
+ pop
+.line 127
+ getstatic Main/P I ; load global P
+ ; e1 right hand for assignment
+ dup
+ putstatic Main/day I
+L18:
+ pop
+.line 141
+ invokestatic Main/printdatum()V
+ ; e2 if true expression
+L19:
+ return
+.end method
+.method public static checkjaar(I)I
+.limit stack 3
+.limit locals 3
+.line 9
+.line 10
+.line 11
+ iload 0 ; load jaar from 0
+ iconst_0
+ if_icmplt L0 ; e1 < e2
+ iconst_0
+ goto L1
+L0:
+ iconst_1
+L1:
+ ifeq L5 ; e1 is false
+.line 12
+ bipush 106 ; ldc 'j'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 122 ; ldc 'z'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 117 ; ldc 'u'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 115 ; ldc 's'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 109 ; ldc 'm'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 111 ; ldc 'o'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 116 ; ldc 't'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 122 ; ldc 'z'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 105 ; ldc 'i'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 106 ; ldc 'j'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 110 ; ldc 'n'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 103 ; ldc 'g'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 98 ; ldc 'b'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 111 ; ldc 'o'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 114 ; ldc 'r'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 12
+ bipush 110 ; ldc 'n'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ ; e2 if true expression
+ goto L6
+L5:
+.line 14
+ iload 0 ; load jaar from 0
+ ldc 2099 ; load constant maxyear
+ if_icmpgt L2 ; e1 > e2
+ iconst_0
+ goto L3
+L2:
+ iconst_1
+L3:
+ ifeq L4 ; e1 is false
+.line 15
+ bipush 104 ; ldc 'h'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 98 ; ldc 'b'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 116 ; ldc 't'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 95 ; ldc '_'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 103 ; ldc 'g'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 101 ; ldc 'e'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 100 ; ldc 'd'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 117 ; ldc 'u'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 108 ; ldc 'l'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 15
+ bipush 100 ; ldc 'd'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ ; e2 if true expression
+L4:
+ ; e3 if false expression
+L6:
+.line 18
+ iload 0 ; load jaar from 0
+ iconst_0
+ if_icmpge L7 ; e1 >= e2
+ iconst_0
+ goto L8
+L7:
+ iconst_1
+L8:
+.line 18
+ iload 0 ; load jaar from 0
+ ldc 2099 ; load constant maxyear
+ if_icmple L9 ; e1 <= e2
+ iconst_0
+ goto L10
+L9:
+ iconst_1
+L10:
+ iand ; e1 right hand for assignment
+ dup
+ istore 1 ; store e1 in ok
+ pop
+ iload 1 ; load ok from 1
+ ireturn
+.end method
+.method public static nest(II)I
+.limit stack 3
+.limit locals 5
+.line 47
+.line 48
+.line 49
+ iload 0 ; load stap from 0
+ iconst_1
+ if_icmpeq L0 ; e1 = e2
+ iconst_0
+ goto L1
+L0:
+ iconst_1
+L1:
+ ifeq L9 ; e1 is false
+.line 50
+ iconst_3
+ iload 1 ; load w from 1
+ imul ; e1 right hand for assignment
+ dup
+ istore 2 ; store e1 in returnvalue
+ pop
+ goto L10
+L9:
+.line 52
+ iload 0 ; load stap from 0
+ iconst_2
+ if_icmpeq L2 ; e1 = e2
+ iconst_0
+ goto L3
+L2:
+ iconst_1
+L3:
+ ifeq L8 ; e1 is false
+.line 53
+ iload 1 ; load w from 1
+ iconst_4
+ idiv ; e1 right hand for assignment
+ dup
+ istore 2 ; store e1 in returnvalue
+ pop
+.line 55
+ iconst_1
+ ; e1 right hand for assignment
+ dup
+ istore 3 ; store e1 in loop
+ pop
+.line 56
+L6:
+.line 56
+ iload 3 ; load loop from 3
+ ldc 12 ; load constant twaalf
+ if_icmple L4 ; e1 <= e2
+ iconst_0
+ goto L5
+L4:
+ iconst_1
+L5:
+ ifeq L7
+.line 57
+ iload 2 ; load returnvalue from 2
+ iconst_1
+ isub ; e1 right hand for assignment
+ dup
+ istore 2 ; store e1 in returnvalue
+ pop
+.line 58
+ iload 3 ; load loop from 3
+ iconst_1
+ iadd ; e1 right hand for assignment
+ dup
+ istore 3 ; store e1 in loop
+ pop
+ goto L6
+L7:
+ ; e2 if true expression
+L8:
+ ; e3 if false expression
+L10:
+ iload 2 ; load returnvalue from 2
+ ireturn
+.end method
+.method public static copy(I)I
+.limit stack 3
+.limit locals 2
+.line 75
+.line 76
+ bipush 42
+ pop
+ iload 0 ; load jaar from 0
+ ireturn
+.end method
+.method public static nested()I
+.limit stack 3
+.limit locals 2
+.line 82
+.line 83
+.line 84
+ getstatic Main/jaar I ; load global jaar
+ iconst_5
+ imul
+ iconst_4
+ idiv
+ getstatic Main/X I ; load global X
+ isub
+.line 84 ; - bipush 10
+ bipush 10
+ ineg
+ iadd ; e1 right hand for assignment
+ dup
+ istore 0 ; store e1 in Z
+ pop
+.line 85
+ iload 0 ; load Z from 0
+ invokestatic Main/copy(I)I ; e1 right hand for assignment
+ dup
+ istore 0 ; store e1 in Z
+ pop
+ iload 0 ; load Z from 0
+ ireturn
+.end method
+.method public static printdatum()V
+.limit stack 3
+.limit locals 2
+.line 130
+.line 131
+ getstatic Main/day I ; load global day
+ dup
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(I)V
+ pop
+.line 133
+ bipush 95 ; ldc '_'
+ dup
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ ; e1 right hand for assignment
+ dup
+ istore 0 ; store e1 in under
+ pop
+.line 134
+ getstatic Main/month I ; load global month
+ iconst_3
+ if_icmpeq L0 ; e1 = e2
+ iconst_0
+ goto L1
+L0:
+ iconst_1
+L1:
+ ifeq L2 ; e1 is false
+.line 135
+ bipush 77 ; ldc 'M'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 135
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 135
+ bipush 97 ; ldc 'a'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 135
+ bipush 114 ; ldc 'r'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 135
+ 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
+ bipush 65 ; ldc 'A'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 137
+ bipush 112 ; ldc 'p'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 137
+ bipush 114 ; ldc 'r'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 137
+ bipush 105 ; ldc 'i'
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ .line 137
+ 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
+ iload 0 ; load under from 0
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(C)V
+ getstatic Main/jaar I ; load global jaar
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(I)V
+ return
+.end method
View
17 scanjasmin.py
@@ -0,0 +1,17 @@
+import re
+import sys
+
+stored = set()
+invalid = set()
+for lineno, line in enumerate(open(sys.argv[1])):
+ if line.lstrip().startswith('.method'):
+ stored = set()
+ else:
+ m = re.search('istore (\d+)', line)
+ if m:
+ stored.add(m.group(1))
+
+ m = re.search('iload (\d+)', line)
+ if m and m.group(1) not in stored and line not in invalid:
+ invalid.add(line)
+ print 'Invalid iload on line %d: %s' % (lineno + 1, line.strip())
View
15 src/SELMA/CompilerEntry.java
@@ -8,15 +8,21 @@
public int addr;
public int val;
public String signature;
+ public boolean isGlobal;
+
+ int _mangleID;
+ static int mangleId = 0;
public CompilerEntry(SR_Type type, SR_Kind kind, int addr) {
super(type, kind);
this.addr = addr;
+ _mangleID++;
}
public CompilerEntry(SR_Type type, SR_Kind kind, int addr, SR_Func func) {
super(type, kind, func);
this.addr = addr;
+ _mangleID++;
}
public CompilerEntry setVal(String intval) {
@@ -34,4 +40,13 @@ public CompilerEntry setChar(int c) {
// System.err.println("Setting " + c + " on " + addr + " = " + val);
return this;
}
+
+ public String getIdentifier(String ident) {
+ return ident + "_" + _mangleID;
+ /*
+ String result = ident;
+ for (int i = 0; i < level; i++)
+ result = "_" + result;
+ return result; */
+ }
}
View
31 src/SELMA/SymbolTable.java
@@ -14,6 +14,9 @@
public int nextAddr = 1;
public int funclevel = 0;
+ // Set of global variables along with their type denoters that should become fields
+ public Set<String> globals = new HashSet<String>();
+
private int currentLevel;
private Map<String, Stack<Entry>> entries;
@@ -97,6 +100,13 @@ public void enter(Tree tree, Entry entry) throws SymbolTableException {
} else {
throw new SymbolTableException(tree, "Entry "+id+" already exists in current scope.");
}
+
+ if (entry instanceof CompilerEntry) {
+ CompilerEntry e = (CompilerEntry) entry;
+ e.isGlobal = funclevel == 0;
+ if (e.isGlobal && e.kind == SR_Kind.VAR && e.func != SR_Func.YES)
+ globals.add(e.getIdentifier(id) + " " + getTypeDenoter(e.type, false));
+ }
}
@@ -164,27 +174,6 @@ public String getTypeDenoter(SR_Type type, boolean printing) {
}
}
- /**
- * Get a list of strings for each local variable along with its type denoter.
- * This is used for global variables that are to become fields
- * @return
- */
- public List<String> getAllLocalVariablesWithTypes() {
- List<String> localVars = new ArrayList<String>();
-
- for (Map.Entry<String, Stack<Entry>> entry: entries.entrySet()){
- Stack<Entry> stack = entry.getValue();
- if (stack != null && !stack.isEmpty()) {
- CheckerEntry e = (CheckerEntry) stack.peek();
- if (e.kind != SR_Kind.CONST && e.func != SR_Func.YES) {
- localVars.add(entry.getKey() + " " + getTypeDenoter(e.type, false));
- }
- }
- }
-
- return localVars;
- }
-
public void enterFuncScope() {
openScope();
funclevel++;
View
18 test/test_functions/error_nested.selma
@@ -0,0 +1,18 @@
+<output>
+ ERROR: inner (9:13) Cannot nest functions
+</output>
+
+const c : character = 'c';
+var v1, v2 : integer;
+
+function outer(outerparam : character;) {
+ function inner(innerparam : integer;) : character {
+ print(innerparam, outerparam, v1, v2);
+ return 'e';
+ };
+ print(@inner(10,));
+};
+
+v1 := 1;
+v2 := 2;
+@outer('z',);
View
0  test/test_functions/nested.jasmin
No changes.
View
65 test/test_functions/recursion.jasmin
@@ -0,0 +1,65 @@
+.source test/test_functions/recursion.selma
+.class public Main
+.super java/lang/Object
+.field public static scanner_field Ljava/util/Scanner;
+.method public <init>()V
+ aload_0
+ invokespecial java/lang/Object/<init>()V
+ return
+.end method
+.method public static main([Ljava/lang/String;)V
+.limit stack 3
+.limit locals 3
+ new java/util/Scanner
+ dup ; dup for <init>
+ getstatic java/lang/System/in Ljava/io/InputStream;
+ invokespecial java/util/Scanner/<init>(Ljava/io/InputStream;)V
+ putstatic Main/scanner_field Ljava/util/Scanner;
+.line 1
+.line 12
+ bipush 10
+ invokestatic Main/fac(I)I
+ dup
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(I)V
+ pop
+ return
+.end method
+.method public static fac(I)I
+.limit stack 3
+.limit locals 3
+.line 1
+.line 2
+.line 3
+ iload 0 ; load n from 0
+ iconst_1
+ if_icmpeq L0 ; e1 = e2
+ iconst_0
+ goto L1
+L0:
+ iconst_1
+L1:
+ ifeq L2 ; e1 is false
+.line 4
+ iconst_1
+ ; e1 right hand for assignment
+ dup
+ istore 1 ; store e1 in result
+ goto L3
+L2:
+.line 6
+ iload 0 ; load n from 0
+.line 6
+ iload 0 ; load n from 0
+ iconst_1
+ isub
+ invokestatic Main/fac(I)I
+ imul ; e1 right hand for assignment
+ dup
+ istore 1 ; store e1 in result
+L3:
+ pop
+ iload 1 ; load result from 1
+ ireturn
+.end method
View
28 test/test_functions/recursion.selma
@@ -0,0 +1,28 @@
+function fac(n : integer;) : integer {
+ var result : integer;
+ if n == 1; then
+ result := 1;
+ else
+ result := n * @fac(n - 1,);
+ fi;
+
+ return result;
+};
+
+<output>3628800</output>
+print(@fac(10,));
+
+var result : integer;
+
+function fac2(n : integer;) {
+ if n <> 1; then
+ result := result * n;
+ @fac2(n - 1,);
+ fi;
+};
+
+result := 1;
+@fac2(10,);
+
+<output>3628800</output>
+print(result);
Please sign in to comment.
Something went wrong with that request. Please try again.