diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java index dd90516..b93dda1 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java @@ -90,7 +90,12 @@ public TypeDictionary createAST(String src) { private void populateDefaultTypes(Map types) { + // Pre-create int, [int] and *[int] types types.put(typeDictionary.INT.name(), SONTypeInteger.BOT); + var intArrayType = SONTypeStruct.makeAry(SONTypeInteger.U32, _code.getALIAS(), SONTypeInteger.BOT, _code.getALIAS()); + var ptrIntArrayType = SONTypeMemPtr.make(intArrayType); + types.put("[" + typeDictionary.INT.name() + "]", ptrIntArrayType); + // Also get the types created by default for (SONType t: SONType.gather()) { types.put(t.str(), t); } @@ -145,18 +150,23 @@ private void createSONStructType(Map structTypes, String typeNa Type type = typeStruct.getField(name); // FIXME fs.push(new Field(name,getSONType(structTypes,type),_code.getALIAS(),false)); } + // A Struct type may have been created before because of + // reference from itself; in which case we need to update that SONType fref = structTypes.get(typeName); if (fref != null) { - if (fref instanceof SONTypeStruct ts) { + if (fref instanceof SONTypeMemPtr ptr && + ptr._obj instanceof SONTypeStruct ts) { + assert ts._fields.length == 0; + // Add the fields to the existing type ts._fields = fs.asAry(); } - else throw new CompilerException(""); + else throw new CompilerException("Expected struct type " + typeName + " but got " + fref); } else { - structTypes.put(typeName, new SONTypeStruct(typeName, fs.asAry())); + var ts = SONTypeStruct.make(typeName, fs.asAry()); + var ptr = SONTypeMemPtr.make((byte)2,ts); + structTypes.put(typeName,ptr); } -// SONTypeStruct ts = SONTypeStruct.make(typeName, fs.asAry()); -// TYPES.put(typeName, SONTypeMemPtr.make(ts)); } private String getSONTypeName(Type type) { @@ -164,7 +174,7 @@ private String getSONTypeName(Type type) { return typeFunction.name; } else if (type instanceof Type.TypeArray typeArray) { - return typeArray.name(); + return "*[" + getSONTypeName(typeArray.getElementType()) + "]"; } else if (type instanceof Type.TypeStruct typeStruct) { return "*" + typeStruct.name; @@ -183,36 +193,25 @@ else if (type instanceof Type.TypeNullable typeNullable) { } private SONType getSONType(Map structTypes, Type type) { - String tname = getSONTypeName(type); - SONType t = structTypes.get(tname); + SONType t = structTypes.get(type.name()); if (t != null) return t; if (type instanceof Type.TypeStruct) { - SONType existing = structTypes.get(type.name); - SONTypeStruct ts; - if (existing instanceof SONTypeStruct tsExisting) - ts = tsExisting; - else { - ts = new SONTypeStruct(type.name, new Field[0]); - structTypes.put(ts.str(), ts); - } - SONTypeMemPtr ptr = new SONTypeMemPtr((byte)2,ts); - if (type.name().equals(ts.str())) { - structTypes.put(ptr.str(), ptr); - return ptr; - } - else throw new CompilerException("Unexpected error"); + // For struct types in EeZee language a reference + // to T means *T in SoN + // Create SON struct type + SONTypeStruct ts = SONTypeStruct.make(type.name, new Field[0]); + // Now create *T + SONTypeMemPtr ptr = SONTypeMemPtr.make((byte)2,ts); + // EeZee T maps to SoN *T + structTypes.put(type.name(), ptr); + return ptr; } else if (type instanceof Type.TypeArray typeArray) { + // A reference to array in EeZee means + // *array in SoN SONType elementType = getSONType(structTypes,typeArray.getElementType()); - SONTypeStruct ts = null; - SONType existing = structTypes.get(typeArray.name()); - if (existing instanceof SONTypeStruct tsExisting) - ts = tsExisting; - else { - ts = SONTypeStruct.makeArray(SONTypeInteger.U32, _code.getALIAS(), elementType, _code.getALIAS()); - structTypes.put(ts.str(), ts); - } - SONTypeMemPtr ptr = new SONTypeMemPtr((byte)2,ts); + SONTypeStruct ts = SONTypeStruct.makeArray(SONTypeInteger.U32, _code.getALIAS(), elementType, _code.getALIAS()); + SONTypeMemPtr ptr = SONTypeMemPtr.make((byte)2,ts); structTypes.put(typeArray.name(), ptr); // Array type name is not same as ptr str() return ptr; } @@ -223,11 +222,11 @@ else if (type instanceof Type.TypeNullable typeNullable) { if (ptr1.nullable()) ptr = ptr1; else - ptr = new SONTypeMemPtr((byte)3,ptr1._obj); + ptr = SONTypeMemPtr.make((byte)3,ptr1._obj); } else - ptr = new SONTypeMemPtr((byte)2,(SONTypeStruct) baseType); - structTypes.put(ptr.str(), ptr); + ptr = SONTypeMemPtr.make((byte)2,(SONTypeStruct) baseType); + structTypes.put(typeNullable.name(), ptr); return ptr; } else if (type instanceof Type.TypeVoid) { @@ -241,13 +240,13 @@ else if (type instanceof Type.TypeVoid) { // TODO because first two slots are MEM and RPC private int REGNUM = 2; - private void setVarIds(Scope scope, ScopeNode scopeNode, FunNode fun) { + private void defineScopedVars(Scope scope, ScopeNode scopeNode, FunNode fun) { for (Symbol symbol: scope.getLocalSymbols()) { if (symbol instanceof Symbol.VarSymbol varSymbol) { varSymbol.regNumber = REGNUM++; - String sonTypeName = getSONTypeName(varSymbol.type); - SONType sonType = TYPES.get(sonTypeName); - if (sonType == null) throw new CompilerException("Unknown SON Type "+sonTypeName); + SONType sonType = TYPES.get(varSymbol.type.name()); + if (sonType == null) + throw new CompilerException("Unknown SON Type "+varSymbol.type.name()); Node init = null; if (varSymbol instanceof Symbol.ParameterSymbol) { @@ -256,9 +255,6 @@ private void setVarIds(Scope scope, ScopeNode scopeNode, FunNode fun) { scopeNode.define(makeVarName(varSymbol), sonType, false, init); } } - for (Scope childScope: scope.children) { - setVarIds(childScope, scopeNode, fun); - } } private void generateFunction(Symbol.FunctionTypeSymbol functionTypeSymbol) { @@ -338,7 +334,8 @@ private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSy _scope.mem(mem); // All args, "as-if" called externally AST.FuncDecl funcDecl = (AST.FuncDecl) functionTypeSymbol.functionDecl; - setVarIds(funcDecl.scope,_scope,fun); + REGNUM = 2; + defineScopedVars(funcDecl.scope,_scope,fun); // Parse the body Node last = compileStatement(funcDecl.block); @@ -556,12 +553,12 @@ private Node newStruct( SONTypeStruct obj, Node size) { private Node compileNewExpr(AST.NewExpr newExpr) { Type type = newExpr.type; if (type instanceof Type.TypeArray typeArray) { - SONTypeMemPtr tarray = (SONTypeMemPtr) TYPES.get(getSONTypeName(typeArray)); + SONTypeMemPtr tarray = (SONTypeMemPtr) TYPES.get(typeArray.name()); return newArray(tarray._obj,ZERO); } else if (type instanceof Type.TypeStruct typeStruct) { - SONTypeStruct tstruct = (SONTypeStruct) TYPES.get(typeStruct.name()); - return newStruct(tstruct,con(tstruct.offset(tstruct._fields.length))); + SONTypeMemPtr tptr = (SONTypeMemPtr) TYPES.get(typeStruct.name()); + return newStruct(tptr._obj,con(tptr._obj.offset(tptr._obj._fields.length))); } else throw new CompilerException("Unexpected type: " + type); @@ -701,7 +698,7 @@ private Node compileCallExpr(AST.CallExpr callExpr) { } private String makeVarName(Symbol.VarSymbol varSymbol) { - return varSymbol.name + "$" + varSymbol.regNumber; + return varSymbol.name; // + "$" + varSymbol.regNumber; } private Node compileStatement(AST.Stmt statement) { @@ -955,9 +952,12 @@ private Node compileReturn(AST.ReturnStmt returnStmt) { private Node compileBlock(AST.BlockStmt block) { Node last = ZERO; + _scope.push(ScopeNode.Kind.Block); + defineScopedVars(block.scope, _scope, _fun); for (AST.Stmt stmt: block.stmtList) { last = compileStatement(stmt); } + _scope.pop(); return last; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFunPtr.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFunPtr.java index 2d81a60..0b41493 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFunPtr.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFunPtr.java @@ -41,10 +41,10 @@ public SONTypeFunPtr(byte nil, SONTypeTuple sig, SONType ret, long fidxs) { public SONTypeFunPtr makeFrom(int fidx ) { return make((byte)2, _sig,_ret,1L< ts) { ts.add(TEST); ts.add(TEST0); ts.add(BOT); ts.add(MAIN); } + public static void gather(ArrayList ts) { /* ts.add(TEST); ts.add(TEST0); */ ts.add(BOT); ts.add(MAIN); } @Override SONType xmeet(SONType t) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMemPtr.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMemPtr.java index 7b0f1c8..1d74de0 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMemPtr.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMemPtr.java @@ -31,7 +31,7 @@ public SONTypeMemPtr(byte nil, SONTypeStruct obj) { assert obj!=null; _obj = obj; } - static SONTypeMemPtr make(byte nil, SONTypeStruct obj) { return new SONTypeMemPtr(nil, obj).intern(); } + public static SONTypeMemPtr make(byte nil, SONTypeStruct obj) { return new SONTypeMemPtr(nil, obj).intern(); } public static SONTypeMemPtr makeNullable(SONTypeStruct obj) { return make((byte)3, obj); } public static SONTypeMemPtr make(SONTypeStruct obj) { return new SONTypeMemPtr((byte)2, obj).intern(); } public SONTypeMemPtr makeFrom(SONTypeStruct obj) { return obj==_obj ? this : make(_nil, obj); } @@ -47,8 +47,8 @@ public SONTypeMemPtr(byte nil, SONTypeStruct obj) { public static SONTypeMemPtr TOP = BOT.dual(); public static SONTypeMemPtr NOTBOT = make((byte)2, SONTypeStruct.BOT); - public static SONTypeMemPtr TEST= make((byte)2, SONTypeStruct.TEST); - public static void gather(ArrayList ts) { ts.add(NOTBOT); ts.add(BOT); ts.add(TEST); } + //public static SONTypeMemPtr TEST= make((byte)2, SONTypeStruct.TEST); + public static void gather(ArrayList ts) { ts.add(NOTBOT); ts.add(BOT); /* ts.add(TEST); */ } @Override public SONTypeNil xmeet(SONType t) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeStruct.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeStruct.java index f1f15ea..256ddfa 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeStruct.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeStruct.java @@ -35,7 +35,7 @@ public SONTypeStruct(String name, Field[] fields) { public static SONTypeStruct make(String name, Field... fields) { return new SONTypeStruct(name, fields).intern(); } public static final SONTypeStruct TOP = make("$TOP",new Field[0]); public static final SONTypeStruct BOT = make("$BOT",new Field[0]); - public static final SONTypeStruct TEST = make("test",new Field[]{Field.TEST}); + //public static final SONTypeStruct TEST = make("test",new Field[]{Field.TEST}); // Forward-ref version public static SONTypeStruct makeFRef(String name) { return make(name, (Field[])null); } // Make a read-only version @@ -66,14 +66,14 @@ public static SONTypeStruct makeArray(SONTypeInteger len, int lenAlias, SONType } // A pair of self-cyclic types - private static final SONTypeStruct S1F = makeFRef("S1"); - private static final SONTypeStruct S2F = makeFRef("S2"); - public static final SONTypeStruct S1 = make("S1", Field.make("a", SONTypeInteger.BOT, -1, false), Field.make("s2", SONTypeMemPtr.make((byte)2,S2F),-2, false) ); - private static final SONTypeStruct S2 = make("S2", Field.make("b", SONTypeFloat.F64, -3, false), Field.make("s1", SONTypeMemPtr.make((byte)2,S1F),-4, false) ); +// private static final SONTypeStruct S1F = makeFRef("S1"); +// private static final SONTypeStruct S2F = makeFRef("S2"); +// public static final SONTypeStruct S1 = make("S1", Field.make("a", SONTypeInteger.BOT, -1, false), Field.make("s2", SONTypeMemPtr.make((byte)2,S2F),-2, false) ); +// private static final SONTypeStruct S2 = make("S2", Field.make("b", SONTypeFloat.F64, -3, false), Field.make("s1", SONTypeMemPtr.make((byte)2,S1F),-4, false) ); +// +// private static final SONTypeStruct ARY = makeAry(SONTypeInteger.U32,-1, SONTypeInteger.BOT,-2); - private static final SONTypeStruct ARY = makeAry(SONTypeInteger.U32,-1, SONTypeInteger.BOT,-2); - - public static void gather(ArrayList ts) { ts.add(TEST); ts.add(BOT); ts.add(S1); ts.add(S2); ts.add(ARY); } + public static void gather(ArrayList ts) { /* ts.add(TEST); ts.add(BOT); ts.add(S1); ts.add(S2); ts.add(ARY); */ } // Find field index by name public int find(String fname) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeTuple.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeTuple.java index 4ea6539..f3acceb 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeTuple.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeTuple.java @@ -13,7 +13,7 @@ public class SONTypeTuple extends SONType { public static final SONTypeTuple BOT = new SONTypeTuple(new SONType[0]).intern(); public static final SONTypeTuple TOP = new SONTypeTuple(null).intern(); - public static final SONTypeTuple TEST = make(SONTypeInteger.BOT, SONTypeMemPtr.TEST); + //public static final SONTypeTuple TEST = make(SONTypeInteger.BOT, SONTypeMemPtr.TEST); public static final SONTypeTuple START= make(SONType.CONTROL, SONTypeMem.TOP, SONTypeInteger.BOT); public static final SONTypeTuple MAIN = make(SONTypeInteger.BOT); public static final SONTypeTuple RET = make(SONType.CONTROL, SONTypeMem.BOT, SONType.BOTTOM); @@ -23,7 +23,7 @@ public class SONTypeTuple extends SONType { public static final SONTypeTuple IF_TRUE = make(new SONType[]{SONType. CONTROL, SONType.XCONTROL}); public static final SONTypeTuple IF_FALSE = make(new SONType[]{SONType.XCONTROL, SONType. CONTROL}); - public static void gather(ArrayList ts) { ts.add(BOT); ts.add(TEST); ts.add(START); ts.add(MAIN); ts.add(IF_TRUE); } + public static void gather(ArrayList ts) { ts.add(BOT); /* ts.add(TEST); */ ts.add(START); ts.add(MAIN); ts.add(IF_TRUE); } @Override SONType xmeet(SONType other) { diff --git a/seaofnodes/src/test/cases/fib/fib.ez b/seaofnodes/src/test/cases/fib/fib.ez new file mode 100644 index 0000000..a30b6d9 --- /dev/null +++ b/seaofnodes/src/test/cases/fib/fib.ez @@ -0,0 +1,18 @@ +func fib(n: Int)->Int { + var i: Int; + var temp=0; + var f1=1; + var f2=1; + i=n; + while( i>1 ){ + temp = f1+f2; + f1=f2; + f2=temp; + i=i-1; + } + return f2; +} + +func foo()->Int { + return fib(10); +} \ No newline at end of file diff --git a/seaofnodes/src/test/cases/fib/fib.ir b/seaofnodes/src/test/cases/fib/fib.ir new file mode 100644 index 0000000..b0821d1 --- /dev/null +++ b/seaofnodes/src/test/cases/fib/fib.ir @@ -0,0 +1,46 @@ +START: [[ ]] + 1 Start ____ ____ ____ [[ 3 4 9 16 18 8 5 46 ]] [ Ctrl, #TOP, int] + +L8: [[ START ]] + 8 fib ____ 1 [[ 10 17 19 20 22 ]] Ctrl + 10 $rpc 8 9 [[ 14 ]] $[ALL] + 17 $mem 8 16 [[ 14 ]] #BOT + 19 n$2 8 18 [[ 25 ]] int + 20 #1 8 [[ 37 39 26 41 ]] 1 + +L46: [[ START ]] + 46 foo ____ 1 [[ 48 55 56 57 59 ]] Ctrl + 48 $rpc 46 9 [[ 52 ]] $[ALL] + 55 $mem 46 16 [[ 59 ]] #BOT + 57 #10 46 [[ 59 ]] 10 + 56 #{ int 46 [[ 59 ]] { int -> int #1} + 59 Call 46 55 57 56 [[ 60 ]] Ctrl + +LOOP22: [[ L8 L29 ]] + 22 Loop ____ 8 29 [[ 39 25 37 26 28 ]] Ctrl + 39 Phi_f2$ 22 20 38 [[ 37 14 38 ]] int + 25 Phi_i$3 22 19 41 [[ 26 41 ]] int + 37 Phi_f1$ 22 20 39 [[ 38 ]] int + 26 LT 22 20 25 [[ 28 ]] bool + 28 If 22 26 [[ 29 30 ]] [ Ctrl, Ctrl] + +L60: [[ L46 ]] + 60 CallEnd 59 [[ 61 63 64 ]] [ Ctrl, #BOT, int] + 63 $mem 60 [[ 52 ]] #BOT + 64 #2 60 [[ 52 ]] int + +L30: [[ LOOP22 ]] + 30 False 28 [[ 14 ]] Ctrl + 14 Return 30 17 39 10 [[ 2 ]] [ Ctrl, #BOT, int] + +L29: [[ LOOP22 ]] + 29 True 28 [[ 38 41 22 ]] Ctrl + 41 Sub 29 25 20 [[ 25 ]] int + 38 Add 29 39 37 [[ 39 ]] int + +L61: [[ L60 ]] + 61 $ctrl 60 [[ 52 ]] Ctrl + 52 Return 61 63 64 48 [[ 2 ]] [ Ctrl, #BOT, int] + +L2: [[ ]] + 2 Stop 14 52 [[ ]] Bot diff --git a/seaofnodes/src/test/cases/fib/fib.simple b/seaofnodes/src/test/cases/fib/fib.simple new file mode 100644 index 0000000..5746da4 --- /dev/null +++ b/seaofnodes/src/test/cases/fib/fib.simple @@ -0,0 +1,15 @@ +val fib = {int n -> + int temp=0; + int f1=1; + int f2=1; + int i=n; + while( i>1 ){ + temp = f1+f2; + f1=f2; + f2=temp; + i=i-1; + } + return f2; +}; + +fib(10); \ No newline at end of file diff --git a/seaofnodes/src/test/cases/fib/fib.simple_ir b/seaofnodes/src/test/cases/fib/fib.simple_ir new file mode 100644 index 0000000..d45c9a8 --- /dev/null +++ b/seaofnodes/src/test/cases/fib/fib.simple_ir @@ -0,0 +1,46 @@ +START: [[ ]] + 1 Start ____ ____ ____ [[ 4 5 9 16 18 8 6 20 ]] [ Ctrl, #TOP, int] + +L8: [[ START ]] + 8 main ____ 1 [[ 10 17 57 58 60 ]] Ctrl + 10 $rpc 8 9 [[ 14 ]] $[ALL] + 17 $mem 8 16 [[ 60 ]] #BOT + 58 #10 8 [[ 60 ]] 10 + 57 #{ int 8 [[ 60 ]] { int -> int #1} + 60 Call 8 17 58 57 [[ 61 ]] Ctrl + +L20: [[ START ]] + 20 fib ____ 1 [[ 22 29 31 33 35 ]] Ctrl + 22 $rpc 20 9 [[ 26 ]] $[ALL] + 29 $mem 20 16 [[ 26 ]] #BOT + 31 n 20 18 [[ 38 ]] int + 33 #1 20 [[ 50 52 39 54 ]] 1 + +LOOP35: [[ L20 L42 ]] + 35 Loop ____ 20 42 [[ 52 38 50 39 41 ]] Ctrl + 52 Phi_f2 35 33 51 [[ 50 26 51 ]] int + 38 Phi_i 35 31 54 [[ 39 54 ]] int + 50 Phi_f1 35 33 52 [[ 51 ]] int + 39 LT 35 33 38 [[ 41 ]] bool + 41 If 35 39 [[ 42 43 ]] [ Ctrl, Ctrl] + +L61: [[ L8 ]] + 61 CallEnd 60 [[ 63 65 66 ]] [ Ctrl, #BOT, int] + 65 $mem 61 [[ 14 ]] #BOT + 66 #2 61 [[ 14 ]] int + +L63: [[ L61 ]] + 63 $ctrl 61 [[ 14 ]] Ctrl + 14 Return 63 65 66 10 [[ 2 ]] [ Ctrl, #BOT, int] + +L43: [[ LOOP35 ]] + 43 False 41 [[ 26 ]] Ctrl + 26 Return 43 29 52 22 [[ 2 ]] [ Ctrl, #BOT, int] + +L42: [[ LOOP35 ]] + 42 True 41 [[ 51 54 35 ]] Ctrl + 54 Sub 42 38 33 [[ 38 ]] int + 51 Add 42 52 50 [[ 52 ]] int + +L2: [[ ]] + 2 Stop 14 26 [[ ]] Bot diff --git a/seaofnodes/src/test/cases/hello/hello.ez b/seaofnodes/src/test/cases/hello/hello.ez new file mode 100644 index 0000000..4814721 --- /dev/null +++ b/seaofnodes/src/test/cases/hello/hello.ez @@ -0,0 +1,4 @@ +func main()->Int +{ + return 1 +} \ No newline at end of file diff --git a/seaofnodes/src/test/cases/mergsort/sort.ez b/seaofnodes/src/test/cases/mergsort/sort.ez new file mode 100644 index 0000000..8ad506d --- /dev/null +++ b/seaofnodes/src/test/cases/mergsort/sort.ez @@ -0,0 +1,78 @@ +// based on the top-down version from https://en.wikipedia.org/wiki/Merge_sort +// via https://github.com/SeaOfNodes/Simple +func merge_sort(a: [Int], b: [Int], n: Int) +{ + copy_array(a, 0, n, b) + split_merge(a, 0, n, b) +} + +func split_merge(b: [Int], begin: Int, end: Int, a: [Int]) +{ + if (end - begin <= 1) + return; + var middle = (end + begin) / 2 + split_merge(a, begin, middle, b) + split_merge(a, middle, end, b) + merge(b, begin, middle, end, a) +} + +func merge(b: [Int], begin: Int, middle: Int, end: Int, a: [Int]) +{ + var i = begin + var j = middle + var k = begin + while (k < end) { + // && and || + var cond = 0 + if (i < middle) { + if (j >= end) cond = 1; + else if (a[i] <= a[j]) cond = 1; + } + if (cond) + { + b[k] = a[i] + i = i + 1 + } + else + { + b[k] = a[j] + j = j + 1 + } + k = k + 1 + } +} + +func copy_array(a: [Int], begin: Int, end: Int, b: [Int]) +{ + var k = begin + while (k < end) + { + b[k] = a[k] + k = k + 1 + } +} + +func eq(a: [Int], b: [Int], n: Int)->Int +{ + var result = 1 + var i = 0 + while (i < n) + { + if (a[i] != b[i]) + { + result = 0 + break + } + i = i + 1 + } + return result +} + +func main()->Int +{ + var a = new [Int]{10,9,8,7,6,5,4,3,2,1} + var b = new [Int]{ 0,0,0,0,0,0,0,0,0,0} + var expect = new [Int]{1,2,3,4,5,6,7,8,9,10} + merge_sort(a, b, 10) + return eq(a,expect,10) +} diff --git a/seaofnodes/src/test/java/com/compilerprogramming/ezlang/compiler/TestSONTypes.java b/seaofnodes/src/test/java/com/compilerprogramming/ezlang/compiler/TestSONTypes.java index 6584c71..78c7e87 100644 --- a/seaofnodes/src/test/java/com/compilerprogramming/ezlang/compiler/TestSONTypes.java +++ b/seaofnodes/src/test/java/com/compilerprogramming/ezlang/compiler/TestSONTypes.java @@ -1,6 +1,7 @@ package com.compilerprogramming.ezlang.compiler; import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; +import org.junit.Before; import org.junit.Test; import static com.compilerprogramming.ezlang.compiler.Main.PORTS; @@ -25,7 +26,7 @@ public void test1() { @Test public void test2() { String src = """ -struct T { var i: S } +struct T1 { var i: S } struct S { var i: Int } """; compileSrc(src); @@ -34,7 +35,7 @@ public void test2() { @Test public void test3() { String src = """ -struct T { var next: T? } +struct T2 { var next: T2? } """; compileSrc(src); } @@ -42,7 +43,7 @@ public void test3() { @Test public void test4() { String src = """ -struct T { var arr: [Int] } +struct T3 { var arr: [Int] } """; compileSrc(src); } @@ -131,9 +132,9 @@ func foo()->[Int] { @Test public void test14() { String src = """ -struct T { var i: Int } -func foo()->T { - return new T{i=1}; +struct T4 { var i: Int } +func foo()->T4 { + return new T4{i=1}; } """; compileSrc(src); @@ -142,9 +143,9 @@ func foo()->T { @Test public void test15() { String src = """ -struct T { var i: Int; var j: Int } -func foo()->T { - return new T{i=23, j=32}; +struct T5 { var i: Int; var j: Int } +func foo()->T5 { + return new T5{i=23, j=32}; } """; compileSrc(src); @@ -163,9 +164,9 @@ func foo()->Int { @Test public void test17() { String src = """ -struct T { var i: Int; var j: Int } +struct T5 { var i: Int; var j: Int } func foo()->Int { - return new T{i=23, j=32}.j; + return new T5{i=23, j=32}.j; } """; compileSrc(src);