Permalink
Browse files

Merge remote-tracking branch 'jingweno/for_step_expr'

  • Loading branch information...
2 parents a18fe7e + 0532e51 commit afd0810660d3e9a1219fd46c6c988b7e97181577 @qmx qmx committed Jul 20, 2012
@@ -29,6 +29,7 @@
import org.dynjs.parser.statement.DoWhileStatement;
import org.dynjs.parser.statement.EqualsOperationStatement;
import org.dynjs.parser.statement.ExpressionListStatement;
+import org.dynjs.parser.statement.ForStepExprStatement;
import org.dynjs.parser.statement.ForStepVarStatement;
import org.dynjs.parser.statement.FunctionStatement;
import org.dynjs.parser.statement.IfStatement;
@@ -358,8 +359,8 @@ public Statement forStepVar(final Tree tree, final Statement varDef, final State
return new ForStepVarStatement(labelStack, tree, varDef, expr1, expr2, statement);
}
- public Statement forStepExpr(final Tree tree, final Statement expr1, final Statement expr2, final Statement expr3, Statement statement) {
- throw new ParserException("not implemented yet", tree);
+ public Statement forStepExpr(final Tree tree, final Statement initialize, final Statement test, final Statement increment, Statement statement) {
+ return new ForStepExprStatement(labelStack, tree, initialize, test, increment, statement);
}
public Statement forIterVar(final Tree tree, final Statement varDef, final Statement expr1, final Statement statement) {
@@ -0,0 +1,67 @@
+/**
+ * Copyright 2012 Douglas Campos, and individual contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.dynjs.parser.statement;
+
+import me.qmx.jitescript.CodeBlock;
+import org.antlr.runtime.tree.Tree;
+import org.dynjs.parser.Statement;
+import org.dynjs.runtime.RT;
+import org.objectweb.asm.tree.LabelNode;
+
+import java.util.Stack;
+
+import static me.qmx.jitescript.util.CodegenUtils.p;
+import static me.qmx.jitescript.util.CodegenUtils.sig;
+
+public class ForStepExprStatement extends BaseStatement implements Statement {
+
+ private final Stack<LabelNode> labelStack;
+ private final Statement initialize;
+ private final Statement test;
+ private final Statement increment;
+ private final BlockStatement statement;
+ private final LabelNode preIncrement = new LabelNode();
+
+ public ForStepExprStatement(Stack<LabelNode> labelStack, final Tree tree, final Statement initialize, final Statement test, final Statement increment, final Statement statement) {
+ super(tree);
+ this.labelStack = labelStack;
+ this.initialize = initialize;
+ this.test = test;
+ this.increment = increment;
+ this.statement = (BlockStatement) statement;
+ }
+
+ @Override
+ public CodeBlock getCodeBlock() {
+ return new CodeBlock() {{
+ labelStack.push(preIncrement);
+ if (initialize != null) {
+ append(initialize.getCodeBlock());
+ }
+ label(statement.getBeginLabel());
+ append(test.getCodeBlock());
+ invokedynamic("dynjs:convert:to_boolean", sig(Boolean.class, Object.class), RT.BOOTSTRAP, RT.BOOTSTRAP_ARGS);
+ invokevirtual(p(Boolean.class), "booleanValue", sig(boolean.class));
+ iffalse(statement.getEndLabel());
+ append(statement.getCodeBlock());
+ label(preIncrement);
+ append(increment.getCodeBlock());
+ go_to(statement.getBeginLabel());
+ label(statement.getEndLabel());
+ labelStack.pop();
+ }};
+ }
+}
@@ -171,6 +171,8 @@ public void testLoop() {
public void testFor() {
check("var x = 0; for (var i =0;i < 10; i+=1){ x+=1;}; var result = x == 10");
check("var x = 0; var i =0; for (var w = 0;i < 10; i+=1){ x+=1;}; var result = i == 10");
+ check("var x = 0; for (; x < 10; x += 1) { x += 1; }; var result = x == 10");
+ check("var i = 0; var x = 33; for(; i < 10; i += 1) { x -= 1} var result = x == 23");
}
@Test
@@ -336,5 +338,4 @@ public void tryFinallyBlock() {
public void testDeleteOper() {
check("var x = {a:'lol'}; var result = delete x.a;", false);
}
-
}

0 comments on commit afd0810

Please sign in to comment.