浏览代码

FOREACH -> FOR

Vladislav Folts 10 年之前
父节点
当前提交
cc0e895902

二进制
bin/compiled.zip


+ 4 - 4
src/eberon/EberonRecord.ob

@@ -112,7 +112,7 @@ PROCEDURE ensureMethodDefinitions(r: PRecord; reasons: MapOfMethodIds);
 VAR
 VAR
     result: ARRAY * OF STRING;
     result: ARRAY * OF STRING;
 BEGIN
 BEGIN
-    FOREACH v, k IN reasons DO
+    FOR k, v IN reasons DO
         ensureMethodDefinitionsForEach(k, v.ids, r, result);
         ensureMethodDefinitionsForEach(k, v.ids, r, result);
     END;
     END;
     IF LEN(result) # 0 THEN
     IF LEN(result) # 0 THEN
@@ -159,7 +159,7 @@ END;
 PROCEDURE ensureNonAbstract(r: PRecord);
 PROCEDURE ensureNonAbstract(r: PRecord);
     PROCEDURE require(declaredMethods: MapOfFields; base: PRecord);
     PROCEDURE require(declaredMethods: MapOfFields; base: PRecord);
     BEGIN
     BEGIN
-        FOREACH v, k IN declaredMethods DO
+        FOR k, v IN declaredMethods DO
             IF ~hasMethodDefinition(r, k) THEN
             IF ~hasMethodDefinition(r, k) THEN
                 requireMethodDefinition(base, k, cannotInstantiateErrMsg(r^));
                 requireMethodDefinition(base, k, cannotInstantiateErrMsg(r^));
             END;
             END;
@@ -410,7 +410,7 @@ VAR
     VAR
     VAR
         result: Strings;
         result: Strings;
     BEGIN
     BEGIN
-        FOREACH v, k IN m DO
+        FOR k, v IN m DO
             result.add(k);
             result.add(k);
         END;
         END;
         RETURN result;
         RETURN result;
@@ -497,7 +497,7 @@ VAR
     code: STRING;
     code: STRING;
     result: STRING;
     result: STRING;
 BEGIN
 BEGIN
-    FOREACH f, key IN r.fields DO
+    FOR key, f IN r.fields DO
         type <- f.type()(Types.PStorageType);
         type <- f.type()(Types.PStorageType);
         IF key IN r.fieldsInit THEN
         IF key IN r.fieldsInit THEN
             code := r.fieldsInit[key];
             code := r.fieldsInit[key];

+ 7 - 8
src/eberon/eberon_context.js

@@ -166,7 +166,7 @@ var ForEachVariable = TypeNarrowVariable.extend({
     init: function(type){
     init: function(type){
         TypeNarrowVariable.prototype.init.call(this, type, false, true);
         TypeNarrowVariable.prototype.init.call(this, type, false, true);
     },
     },
-    idType: function(){return "FOREACH variable";}
+    idType: function(){return "FOR variable";}
 });
 });
 
 
 var Identdef = Context.Identdef.extend({
 var Identdef = Context.Identdef.extend({
@@ -296,8 +296,7 @@ var Designator = Context.Designator.extend({
         else if (s == "POINTER"){
         else if (s == "POINTER"){
             var typeId = new Type.TypeId(this.handleMessage(getSelfAsPointerMsg));
             var typeId = new Type.TypeId(this.handleMessage(getSelfAsPointerMsg));
             var pointerType = new Type.Pointer("", typeId);
             var pointerType = new Type.Pointer("", typeId);
-            var info = new SelfAsPointer();
-            this._advance(pointerType, info, "");
+            this._advance(pointerType, new SelfAsPointer(), "");
         }
         }
         else if (s == "SUPER"){
         else if (s == "SUPER"){
             var ms = this.handleMessage(getMethodSuper);
             var ms = this.handleMessage(getMethodSuper);
@@ -1235,16 +1234,16 @@ var ForEach = Context.Chained.extend({
         this.__codeGenerator = CodeGenerator.nullGenerator();
         this.__codeGenerator = CodeGenerator.nullGenerator();
     },
     },
     handleIdent: function(id){
     handleIdent: function(id){
-        if (!this.__valueId)
-            this.__valueId = id;
-        else
-            this.__keyId = id;
+        if (!this.__keyId)
+                this.__keyId = id;
+            else
+                this.__valueId = id;
     },
     },
     codeGenerator: function(){return this.__codeGenerator;},
     codeGenerator: function(){return this.__codeGenerator;},
     handleExpression: function(e){
     handleExpression: function(e){
         var type = e.type();
         var type = e.type();
         if (!(type instanceof EberonMap.Type))
         if (!(type instanceof EberonMap.Type))
-            throw new Errors.Error("expression of type MAP is expected in FOREACH, got '" 
+            throw new Errors.Error("expression of type MAP is expected in FOR, got '" 
                                  + type.description() + "'");
                                  + type.description() + "'");
 
 
         var scope = EberonScope.makeOperator(
         var scope = EberonScope.makeOperator(

+ 6 - 5
src/eberon/eberon_grammar.js

@@ -25,10 +25,11 @@ function makeStrucType(base, type){
 }
 }
 
 
 function makeStatement(base, statementSequence, ident, expression){
 function makeStatement(base, statementSequence, ident, expression){
-    return or(base, 
-              context(and("FOREACH", ident, ",", ident, "IN", expression, "DO", 
-                          statementSequence, required("END", "END expected (FOREACH)")), 
-                      EbContext.ForEach));
+    return or(context(and("FOR", ident, ",", ident, "IN", expression, "DO", 
+                          statementSequence, required("END", "END expected (FOR)")), 
+                      EbContext.ForEach),
+              base
+              );
 }
 }
 
 
 function makeProcedureHeading(ident, identdef, formalParameters){
 function makeProcedureHeading(ident, identdef, formalParameters){
@@ -158,7 +159,7 @@ exports.language = {
             Return:             EbContext.Return,
             Return:             EbContext.Return,
             ModuleDeclaration:  EbContext.ModuleDeclaration
             ModuleDeclaration:  EbContext.ModuleDeclaration
         },
         },
-        Grammar.reservedWords + " SELF SUPER MAP FOREACH"
+        Grammar.reservedWords + " SELF SUPER MAP"
         ),
         ),
     stdSymbols: Symbols.makeStd(),
     stdSymbols: Symbols.makeStd(),
     types: {
     types: {

+ 2 - 2
src/ob/Code.ob

@@ -279,7 +279,7 @@ PROCEDURE genCommaList(m: StringsMap; import: BOOLEAN): STRING;
 VAR
 VAR
     result: STRING;
     result: STRING;
 BEGIN
 BEGIN
-    FOREACH alias, name IN m DO;
+    FOR name, alias IN m DO;
         IF LEN(result) # 0 THEN
         IF LEN(result) # 0 THEN
             result := result + ", ";
             result := result + ", ";
         END;
         END;
@@ -300,7 +300,7 @@ PROCEDURE ModuleGenerator.epilog(exports: Symbols.Map): STRING;
 VAR
 VAR
     result: STRING;
     result: STRING;
 BEGIN
 BEGIN
-    FOREACH s, k IN exports DO
+    FOR k, s IN exports DO
         code <- genExport(s^);
         code <- genExport(s^);
         IF LEN(code) # 0 THEN
         IF LEN(code) # 0 THEN
             IF LEN(result) # 0 THEN
             IF LEN(result) # 0 THEN

+ 1 - 1
src/ob/Scope.ob

@@ -209,7 +209,7 @@ END;
 
 
 PROCEDURE defineExports*(VAR m: CompiledModule; exports: Symbols.Map);
 PROCEDURE defineExports*(VAR m: CompiledModule; exports: Symbols.Map);
 BEGIN
 BEGIN
-    FOREACH k, id IN exports DO
+    FOR id, k IN exports DO
         symbol <- k;
         symbol <- k;
         info <- symbol.info();
         info <- symbol.info();
         IF info IS Types.PVariable THEN
         IF info IS Types.PVariable THEN

+ 1 - 1
src/ob/Types.ob

@@ -971,7 +971,7 @@ BEGIN
     IF type.base # NIL THEN
     IF type.base # NIL THEN
         result := dumpRecordFields(type.base);
         result := dumpRecordFields(type.base);
     END;
     END;
-    FOREACH v, k IN type.fields DO
+    FOR k, v IN type.fields DO
         IF LEN(result) # 0 THEN
         IF LEN(result) # 0 THEN
             result := result + ", ";
             result := result + ", ";
         END;
         END;

+ 10 - 10
test/input/eberon/map.ob

@@ -25,7 +25,7 @@ PROCEDURE ForEach();
 VAR
 VAR
     m: MapOfInteger;
     m: MapOfInteger;
 BEGIN
 BEGIN
-    FOREACH v, k IN m DO
+    FOR k, v IN m DO
         ASSERT(v = 0);
         ASSERT(v = 0);
         ASSERT(k # "");
         ASSERT(k # "");
     END;
     END;
@@ -40,7 +40,7 @@ END;
 
 
 PROCEDURE ForEachWithExpression();
 PROCEDURE ForEachWithExpression();
 BEGIN
 BEGIN
-    FOREACH v, k IN makeMap() DO
+    FOR k, v IN makeMap() DO
     END;
     END;
 END;
 END;
 
 
@@ -50,18 +50,18 @@ VAR
 
 
     PROCEDURE inner();
     PROCEDURE inner();
     BEGIN
     BEGIN
-        FOREACH v, k IN m DO
-            FOREACH v2, k2 IN m DO
+        FOR k, v IN m DO
+            FOR k2, v2 IN m DO
             END;
             END;
         END;
         END;
     END;
     END;
 BEGIN
 BEGIN
-    FOREACH v, k IN m DO
-        FOREACH v2, k2 IN m DO
+    FOR k, v IN m DO
+        FOR k2, v2 IN m DO
         END;
         END;
     END;
     END;
-    FOREACH v3, k3 IN m DO
-        FOREACH v, k IN m DO
+    FOR k3, v3 IN m DO
+        FOR k, v IN m DO
         END;
         END;
     END;
     END;
 END;
 END;
@@ -196,8 +196,8 @@ PROCEDURE passMapArrayElementByRef(VAR a: ARRAY * OF INTEGER);
 END;
 END;
 
 
 BEGIN
 BEGIN
-    FOREACH v, k IN m DO
-        FOREACH v2, k2 IN m DO
+    FOR k, v IN m DO
+        FOR k2, v2 IN m DO
         END;
         END;
     END;
     END;
 
 

+ 2 - 2
test/input/eberon/run/map.ob

@@ -4,7 +4,7 @@ PROCEDURE testEmptyForEach();
 VAR
 VAR
     m: MAP OF INTEGER;
     m: MAP OF INTEGER;
 BEGIN
 BEGIN
-    FOREACH v, k IN m DO
+    FOR k, v IN m DO
         ASSERT(FALSE);
         ASSERT(FALSE);
     END;
     END;
 END;
 END;
@@ -15,7 +15,7 @@ VAR
 BEGIN
 BEGIN
     m["abc"] := 1;
     m["abc"] := 1;
     count <- 0;
     count <- 0;
-    FOREACH v, k IN m DO
+    FOR k, v IN m DO
         ASSERT(v = 1);
         ASSERT(v = 1);
         ASSERT(k = "abc");
         ASSERT(k = "abc");
         INC(count);
         INC(count);

+ 14 - 14
test/test_unit_eberon.js

@@ -1432,29 +1432,29 @@ exports.suite = {
         pass(),
         pass(),
         fail(["PROCEDURE p(m: M); BEGIN m[\"abc\"] := 123; END;", "cannot assign to read-only MAP's element"])
         fail(["PROCEDURE p(m: M); BEGIN m[\"abc\"] := 123; END;", "cannot assign to read-only MAP's element"])
         ),
         ),
-    "FOREACH": testWithContext(
+    "FOR": testWithContext(
         context(grammar.statement,
         context(grammar.statement,
                 "TYPE T = RECORD END;"
                 "TYPE T = RECORD END;"
               + "VAR m: MAP OF INTEGER; r: T;"),
               + "VAR m: MAP OF INTEGER; r: T;"),
-        pass("FOREACH v, k IN m DO END",
-             "FOREACH v, k IN m DO ASSERT(k # \"abc\"); END",
-             "FOREACH v, k IN m DO ASSERT(v # 123); END"
+        pass("FOR k, v IN m DO END",
+             "FOR k, v IN m DO ASSERT(k # \"abc\"); END",
+             "FOR k, v IN m DO ASSERT(v # 123); END"
             ),
             ),
-        fail(["FOREACH k, k IN m DO END", "'k' already declared"],
-             ["FOREACH m, k IN m DO END", "'m' already declared in module scope"],
-             ["FOREACH v, m IN m DO END", "'m' already declared in module scope"],
-             ["FOREACH v, k IN m DO k := \"\"; END", "cannot assign to FOREACH variable"],
-             ["FOREACH v, k IN m DO v := 0; END", "cannot assign to FOREACH variable"],
-             ["FOREACH v, k IN r DO END", "expression of type MAP is expected in FOREACH, got 'T'"],
-             ["FOREACH v, k IN T DO END", "expression of type MAP is expected in FOREACH, got 'type T'"]
+        fail(["FOR k, k IN m DO END", "'k' already declared"],
+             ["FOR m, v IN m DO END", "'m' already declared in module scope"],
+             ["FOR k, m IN m DO END", "'m' already declared in module scope"],
+             ["FOR k, v IN m DO k := \"\"; END", "cannot assign to FOR variable"],
+             ["FOR k, v IN m DO v := 0; END", "cannot assign to FOR variable"],
+             ["FOR k, v IN r DO END", "expression of type MAP is expected in FOR, got 'T'"],
+             ["FOR k, v IN T DO END", "expression of type MAP is expected in FOR, got 'type T'"]
             )
             )
         ),
         ),
-    "FOREACH scope": testWithContext(
+    "FOR scope": testWithContext(
         context(grammar.declarationSequence,
         context(grammar.declarationSequence,
                 "VAR m: MAP OF INTEGER;"),
                 "VAR m: MAP OF INTEGER;"),
         pass(),
         pass(),
-        fail(["PROCEDURE p(); BEGIN FOREACH k, v IN m DO END; ASSERT(k # \"abc\"); END;", "undeclared identifier: 'k'"],
-             ["PROCEDURE p(); BEGIN FOREACH k, v IN m DO END; ASSERT(v # 123); END;", "undeclared identifier: 'v'"]
+        fail(["PROCEDURE p(); BEGIN FOR k, v IN m DO END; ASSERT(k # \"abc\"); END;", "undeclared identifier: 'k'"],
+             ["PROCEDURE p(); BEGIN FOR k, v IN m DO END; ASSERT(v # 123); END;", "undeclared identifier: 'v'"]
              )
              )
         ),
         ),
     "remove": testWithContext(
     "remove": testWithContext(