Browse Source

Using FOR..IN
fix code generation for array's FOR..IN

Vladislav Folts 9 years ago
parent
commit
ff61ee91ee

+ 9 - 2
src/eberon/EberonContextLoop.ob

@@ -160,13 +160,20 @@ BEGIN
             keyId := currentScope.generateTempVar("key");
         END;
 
-        code.write("for(var " + keyId + " in " + mapVar + ")");
+        isMap <- type IS EberonMap.PType;
+
+        IF isMap THEN
+            code.write("for(var " + keyId + " in " + mapVar + ")");
+        ELSE
+            code.write("for(var " + keyId + " = 0; " + keyId + " < " + mapVar + ".length; ++" + keyId);
+        END;
+
         code.openScope();
         code.write("var " + valueId + " = " + mapVar + "[" + keyId + "];" + Chars.ln);
         SELF.code := code;
 
         keyType <- Types.basic.integer;
-        IF type IS EberonMap.PType THEN
+        IF isMap THEN
             keyType := EberonString.string;
         END;
 

+ 2 - 2
src/eberon/EberonOperatorScopes.ob

@@ -77,8 +77,8 @@ END;
 PROCEDURE Type.reset();
 BEGIN
     SELF.context.root().popScope();
-    FOR i <- 0 TO LEN(SELF.typePromotions) - 1 DO
-        SELF.typePromotions[i].reset();
+    FOR p IN SELF.typePromotions DO
+        p.reset();
     END;
 END;
 

+ 5 - 8
src/eberon/EberonRecord.ob

@@ -108,8 +108,7 @@ PROCEDURE ensureMethodDefinitionsForEach(key: STRING; ids: ARRAY OF STRING; r: P
 VAR
     report: ARRAY * OF STRING;
 BEGIN
-    FOR i <- 0 TO LEN(ids) - 1 DO
-        m <- ids[i];
+    FOR m IN ids DO
         IF ~hasMethodDefinition(r, m) THEN
             report.add(m);
         END;
@@ -446,8 +445,7 @@ BEGIN
         methods := selfMethods;
     END;
 
-    FOR i <- 0 TO LEN(methods) - 1 DO
-        m <- methods[i];
+    FOR m IN methods DO
         IF r.definedMethods.indexOf(m) = -1 THEN
             r.abstractMethods.add(m);
         END;
@@ -458,8 +456,7 @@ PROCEDURE checkIfFieldsInited(r: Record);
 VAR
     fieldsWereNotInited: ARRAY * OF STRING;
 BEGIN
-    FOR i <- 0 TO LEN(r.customInitedfields) - 1 DO
-        f <- r.customInitedfields[i];
+    FOR f IN r.customInitedfields DO
         IF ~(f IN r.fieldsInit) THEN
             fieldsWereNotInited.add(f);
         END;
@@ -490,8 +487,8 @@ BEGIN
     
     ensureMethodDefinitions(SELF(POINTER), SELF.lazyDefinitions);
 
-    FOR i <- 0 TO LEN(SELF.nonExportedMethods) - 1 DO
-        SELF.declaredMethods.remove(SELF.nonExportedMethods[i]);
+    FOR m IN SELF.nonExportedMethods DO
+        SELF.declaredMethods.remove(m);
     END;
 
     checkIfFieldsInited(SELF);

+ 2 - 2
src/eberon/EberonTypePromotion.ob

@@ -129,8 +129,8 @@ END;
 
 PROCEDURE applyForAll(p: Combined);
 BEGIN
-    FOR i <- 0 TO LEN(p.promotions) - 1 DO
-        p.op(p.promotions[i]^);
+    FOR pr IN p.promotions DO
+        p.op(pr^);
     END;
 END;
 

+ 1 - 1
src/ob/Code.ob

@@ -124,7 +124,7 @@ PROCEDURE ModuleGenerator.epilog(exports: Symbols.Map): STRING;
 VAR
     result: STRING;
 BEGIN
-    FOR k, s IN exports DO
+    FOR s IN exports DO
         code <- genExport(s^);
         IF LEN(code) # 0 THEN
             IF LEN(result) # 0 THEN

+ 2 - 2
src/ob/CodeGenerator.ob

@@ -163,8 +163,8 @@ PROCEDURE Generator.result(): STRING;
 VAR
     result: STRING;
 BEGIN
-    FOR i <- 0 TO LEN(SELF.indents) - 1 DO
-        result := result + SELF.indents[i].result;
+    FOR indent IN SELF.indents DO
+        result := result + indent.result;
     END;
     RETURN result;
 END;

+ 1 - 2
src/ob/ContextModule.ob

@@ -51,8 +51,7 @@ VAR
 BEGIN
     root <- SELF.root();
     scope <- root.currentScope();
-    FOR i <- 0 TO LEN(modules) - 1 DO
-        s <- modules[i];
+    FOR s IN modules DO
         name <- s.info()(Types.PModule).name;
         SELF.imports[name] := s;
         scope.addSymbol(s, FALSE);

+ 1 - 2
src/ob/ContextProcedure.ob

@@ -257,8 +257,7 @@ END;
 
 PROCEDURE DefinedParameters.setType(type: Types.PStorageType);
 BEGIN
-    FOR i <- 0 TO LEN(SELF.argNamesForType) - 1 DO
-        name <- SELF.argNamesForType[i];
+    FOR name IN SELF.argNamesForType DO
         void <- SELF.handleMessage(
             NEW AddArgumentMsg(name, NEW Types.ProcedureArgument(type, SELF.isVar))^);
     END;

+ 4 - 5
src/ob/ContextType.ob

@@ -236,7 +236,7 @@ BEGIN
         IF isTypeRecursive(type.base, base) THEN
             result := TRUE;
         ELSE
-            FOR name, field IN type.fields DO
+            FOR field IN type.fields DO
                 IF ~result & isTypeRecursive(field.type(), base) THEN
                     result := TRUE;
                 END;
@@ -256,8 +256,7 @@ END;
 
 PROCEDURE checkIfFieldCanBeExported*(name: STRING; idents: ARRAY OF Context.PIdentdefInfo; hint: STRING);
 BEGIN
-    FOR i <- 0 TO LEN(idents) - 1 DO
-        id <- idents[i];
+    FOR id IN idents DO
         IF ~id.exported() THEN
             Errors.raise(
                 "field '" + name + "' can be exported only if " + hint + " '" +
@@ -455,8 +454,8 @@ END;
 PROCEDURE FieldList.endParse(): BOOLEAN;
 BEGIN
     parent <- SELF.parent()(PRecord);
-    FOR i <- 0 TO LEN(SELF.idents) - 1 DO
-        parent.addField(SELF.idents[i], SELF.type);
+    FOR id IN SELF.idents DO
+        parent.addField(id, SELF.type);
     END;
     RETURN TRUE;
 END;

+ 1 - 2
src/ob/ContextVar.ob

@@ -56,8 +56,7 @@ END;
 PROCEDURE Declaration.endParse(): BOOLEAN;
 BEGIN
     gen <- SELF.codeGenerator();
-    FOR i <- 0 TO LEN(SELF.idents) - 1 DO
-        id <-SELF.idents[i];
+    FOR id IN SELF.idents DO
         varName <- id.id();
         IF id.exported() THEN
             SELF.doCheckExport(varName);

+ 2 - 2
src/ob/Module.ob

@@ -118,8 +118,8 @@ END AnyTypeProc.callGenerator;
 PROCEDURE AnyProcCall.make(args: ARRAY OF Expression.PType; cx: LanguageContext.PType): Expression.PType;
 BEGIN
     argCode <- Procedure.makeArgumentsCode(cx);
-    FOR i <- 0 TO LEN(args) - 1 DO
-        argCode.write(args[i], NIL, NIL);
+    FOR a IN args DO
+        argCode.write(a, NIL, NIL);
     END;
     RETURN Expression.makeSimple("(" + argCode.result() + ")", any)
 END AnyProcCall.make;

+ 11 - 12
src/ob/Procedure.ob

@@ -134,7 +134,11 @@ BEGIN
     FOR i <- 0 TO LEN(actual) - 1 DO
         checkArgument(actual[i], expected[i], i, code, types);
     END;
-END checkArgumentsType;
+    (*
+    FOR i, a IN actual DO
+        checkArgument(a, expected[i], i, code, types);
+    END;*)
+END;
 
 PROCEDURE checkArgumentsCount*(actual, expected: INTEGER);
 BEGIN
@@ -143,7 +147,7 @@ BEGIN
             String.fromInt(expected) + " argument(s) expected, got " 
             + String.fromInt(actual));
     END;
-END checkArgumentsCount;
+END;
 
 PROCEDURE processArguments*(
     actual: ARRAY OF Expression.PType;
@@ -154,7 +158,7 @@ PROCEDURE processArguments*(
 BEGIN
     checkArgumentsCount(LEN(actual), LEN(expected));
     checkArgumentsType(actual, expected, code, types);
-END processArguments;
+END;
 
 PROCEDURE checkArguments(actual: ARRAY OF Expression.PType; expected: ARRAY OF Types.PProcedureArgument; types: LanguageContext.PTypes);
 BEGIN
@@ -790,18 +794,16 @@ PROCEDURE dumpProcArgs(proc: Defined): STRING;
 VAR
     result: STRING;
 BEGIN
-    len <- LEN(proc.mArgs);
-    IF len = 0 THEN
+    IF LEN(proc.mArgs) = 0 THEN
         IF proc.mResult # NIL THEN
             result := "()";
         END;
     ELSE
         result := "(";
-        FOR i <- 0 TO len - 1 DO
+        FOR i, arg IN proc.mArgs DO
             IF i # 0 THEN
                 result := result + ", ";
             END;
-            arg <- proc.mArgs[i];
             ASSERT(arg.type # NIL);
             result := result + arg.type.description();
         END;
@@ -830,13 +832,10 @@ END;
 
 PROCEDURE Defined.define(args: ARRAY OF Types.PProcedureArgument; result: Types.PType);
 BEGIN
-    FOR i <- 0 TO LEN(args) - 1 DO
-        ASSERT(args[i].type # NIL);
+    FOR a IN args DO
+        ASSERT(a.type # NIL);
     END;
     SELF.mArgs := args;
-    FOR i <- 0 TO LEN(SELF.mArgs) - 1 DO
-        ASSERT(SELF.mArgs[i].type # NIL);
-    END;
     SELF.mResult := result;
 END;
 

+ 2 - 2
src/ob/Record.ob

@@ -63,8 +63,8 @@ END;
 
 PROCEDURE Type.finalize();
 BEGIN
-    FOR i <- 0 TO LEN(SELF.notExported) - 1 DO
-        SELF.fields.remove(SELF.notExported[i])
+    FOR f IN SELF.notExported DO
+        SELF.fields.remove(f)
     END;
     SELF.notExported.clear();
 END Type.finalize;

+ 2 - 4
src/ob/Scope.ob

@@ -75,8 +75,7 @@ BEGIN
     addSymbol(Types.basic.real);
     addSymbol(Types.basic.set); 
 
-    FOR i <- 0 TO LEN(Procedures.predefined) - 1 DO
-        proc <- Procedures.predefined[i];
+    FOR proc IN Procedures.predefined DO
         result[proc.id()] := proc;
     END;
     RETURN result
@@ -132,8 +131,7 @@ END;
 
 PROCEDURE Type.close();
 BEGIN
-    FOR i <- 0 TO LEN(SELF.finalizers) - 1 DO
-        finalizer <- SELF.finalizers[i];
+    FOR finalizer IN SELF.finalizers DO
         finalizer.proc(finalizer.closure);
     END;
 

+ 2 - 2
test/expected/eberon/for_in.js

@@ -4,7 +4,7 @@ var test = function (){
 function array(){
 	var a = RTL$.makeArray(3, false);
 	var $seq1 = a;
-	for(var i in $seq1){
+	for(var i = 0; i < $seq1.length; ++i{
 		var v = $seq1[i];
 		RTL$.assert(a[i] == v);
 	}
@@ -13,7 +13,7 @@ function array(){
 function arrayWithValueOnly(){
 	var a = RTL$.makeArray(3, false);
 	var $seq1 = a;
-	for(var $key2 in $seq1){
+	for(var $key2 = 0; $key2 < $seq1.length; ++$key2{
 		var v = $seq1[$key2];
 		RTL$.assert(!v);
 	}