Bläddra i källkod

code generation for returning dynamic array

Vladislav Folts 10 år sedan
förälder
incheckning
61ac5b4baf

BIN
bin/compiled.zip


+ 13 - 0
src/eberon/eberon_context.js

@@ -1084,6 +1084,18 @@ var Repeat = Context.Repeat.extend({
     }
 });
 
+var Return = Context.Return.extend({
+    init: function EberonContext$Return(context){
+        Context.Return.prototype.init.call(this, context);
+    },
+    handleExpression: function(e){
+        var type = e.type();
+        if (type instanceof Type.Array)
+            e = Code.makeSimpleExpression(this.language().rtl.clone(e.code()), type);
+        Context.Return.prototype.handleExpression.call(this, e);
+    }
+});
+
 var For = Context.For.extend({
     init: function EberonContext$Repeat(context){
         Context.For.prototype.init.call(this, context);
@@ -1234,6 +1246,7 @@ exports.ProcOrMethodId = ProcOrMethodId;
 exports.ProcOrMethodDecl = ProcOrMethodDecl;
 exports.RecordDecl = RecordDecl;
 exports.Repeat = Repeat;
+exports.Return = Return;
 exports.SimpleExpression = SimpleExpression;
 exports.InPlaceVariableInit = InPlaceVariableInit;
 exports.InPlaceVariableInitFor = InPlaceVariableInitFor;

+ 1 - 0
src/eberon/eberon_grammar.js

@@ -114,6 +114,7 @@ exports.language = {
             If:                 EbContext.If,
             CaseLabel:          EbContext.CaseLabel,
             Repeat:             EbContext.Repeat,
+            Return:             EbContext.Return,
             ModuleDeclaration:  EbContext.ModuleDeclaration
         },
         Grammar.reservedWords + " SELF SUPER"

+ 1 - 1
src/grammar.js

@@ -210,7 +210,7 @@ result.declarationSequence
 result.procedureBody
     = and(result.declarationSequence,
           optional(and("BEGIN", statementSequence)),
-          optional(context(and("RETURN", expression), Context.Return)),
+          optional(context(and("RETURN", expression), contexts.Return)),
           required("END", "END expected (PROCEDURE)"));
 result.module
     = context(and("MODULE", ident, ";",

+ 1 - 0
src/oberon/oberon_grammar.js

@@ -94,6 +94,7 @@ exports.language = {
             If:                 Context.If,
             CaseLabel:          Context.CaseLabel,
             Repeat:             Context.Repeat,
+            Return:             Context.Return,
             ModuleDeclaration:  Context.ModuleDeclaration
         },
         Grammar.reservedWords

+ 4 - 0
test/expected/eberon/dynamic_array.js

@@ -106,6 +106,10 @@ function assignDynamicArrayFromStatic(){
 	var dynamic = [];
 	dynamic = RTL$.clone(static$);
 }
+
+function returnOuterArray(){
+	return RTL$.clone(a);
+}
 dynamicInt.push(3);
 dynamicInt.push(i);
 dynamicInt.push(byte);

+ 5 - 0
test/input/eberon/dynamic_array.ob

@@ -5,6 +5,7 @@ TYPE
     END;
 
     A = ARRAY 3 OF INTEGER;
+    DynamicInt = ARRAY * OF INTEGER;
 VAR
     r: T;
     a: A;
@@ -26,6 +27,10 @@ BEGIN
     dynamic := static;
 END assignDynamicArrayFromStatic;
 
+PROCEDURE returnOuterArray(): DynamicInt;
+    RETURN a
+END returnOuterArray;
+
 BEGIN
     dynamicInt.add(3);
     dynamicInt.add(i);

+ 22 - 0
test/input/eberon/run/dynamic_array.ob

@@ -24,6 +24,27 @@ BEGIN
     ASSERT(a[1] = 3);
 END testAddRemove;
 
+PROCEDURE testArrayReturn();
+TYPE
+    A = ARRAY * OF INTEGER;
+VAR
+    a: A;
+
+    PROCEDURE returnA(): A;
+        RETURN a
+    END returnA;
+BEGIN
+    a.add(1);
+    a.add(2);
+    a.add(3);
+    
+    a2 <- returnA();
+    ASSERT(LEN(a) = 3);
+    ASSERT(a[0] = 1);
+    ASSERT(a[1] = 2);
+    ASSERT(a[2] = 3);
+END testArrayReturn;
+
 BEGIN
     static[0] := 1;
     static[1] := 2;
@@ -37,4 +58,5 @@ BEGIN
     ASSERT(dynamic[2] = 3);
 
     testAddRemove();
+    testArrayReturn();
 END m.