Search in sources :

Example 6 with ExprStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.

the class ASMExpressionTest method testSimple.

@Test
public void testSimple() {
    ExprStmtToken expression = getASMExpression("1 + 2").getResult();
    Assert.assertEquals(3, expression.getTokens().size());
    Assert.assertEquals("12+", expression.getWord());
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Test(org.junit.Test)

Example 7 with ExprStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.

the class ASMExpressionTest method getASMExpression.

private ASMExpression getASMExpression(String expr) {
    environment.scope.setLangMode(LangMode.DEFAULT);
    Tokenizer tokenizer = null;
    try {
        tokenizer = new Tokenizer(context = new Context(expr + ";"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    SyntaxAnalyzer analyzer = new SyntaxAnalyzer(environment, tokenizer);
    Assert.assertTrue(analyzer.getTree().size() == 1);
    Assert.assertTrue(analyzer.getTree().get(0) instanceof ExprStmtToken);
    return new ASMExpression(null, context, (ExprStmtToken) analyzer.getTree().get(0));
}
Also used : Context(php.runtime.env.Context) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) IOException(java.io.IOException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 8 with ExprStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.

the class ListCompiler method write.

public void write(ListExprToken list, boolean returnValue, boolean writeValue) {
    if (writeValue) {
        expr.writeExpression(list.getValue(), true, false);
        expr.writePopBoxing();
    }
    int i, length = list.getVariables().size();
    for (i = length - 1; i >= 0; i--) {
        // desc order as in PHP
        ListExprToken.Variable v = list.getVariables().get(i);
        expr.writePushDup();
        if (v.indexes != null) {
            for (int index : v.indexes) {
                expr.writePushConstLong(index);
                expr.writeSysStaticCall(MemoryUtils.class, "valueForList", Memory.class, Memory.class, expr.stackPeek().type.toClass());
            //expr.writeSysDynamicCall(Memory.class, "valueOfIndex", Memory.class, expr.stackPeek().type.toClass());
            }
        }
        expr.writePushConstLong(v.index);
        expr.writeSysStaticCall(MemoryUtils.class, "valueForList", Memory.class, Memory.class, expr.stackPeek().type.toClass());
        if (v.isVariable()) {
            LocalVariable variable = method.getLocalVariable(v.getVariableName());
            expr.writeVarAssign(variable, (VariableExprToken) v.var.getSingle(), false, true);
        } else if (v.isArray() || v.isStaticProperty() || v.isArrayPush()) {
            expr.writeExpression(v.var, true, false);
            if (expr.stackPeek().immutable || expr.stackPeek().isConstant())
                expr.unexpectedToken(v.var.getSingle());
            expr.writeSysStaticCall(Memory.class, "assignRight", Memory.class, Memory.class, Memory.class);
            expr.writePopAll(1);
        } else if (v.isDynamicProperty()) {
            DynamicAccessExprToken dynamic = (DynamicAccessExprToken) v.var.getLast();
            ExprStmtToken var = new ExprStmtToken(this.env, this.compiler.getContext(), v.var.getTokens());
            var.getTokens().remove(var.getTokens().size() - 1);
            var.updateAsmExpr(this.env, this.compiler.getContext());
            expr.writeDynamicAccessInfo(dynamic, false);
            expr.writeExpression(var, true, false);
            expr.writePopBoxing(false);
            expr.writeGetStatic("$CALL_PROP_CACHE", PropertyCallCache.class);
            expr.writePushConstInt(method.clazz.getAndIncCallPropCount());
            expr.writeSysStaticCall(ObjectInvokeHelper.class, "assignPropertyRight", Memory.class, Memory.class, String.class, Environment.class, TraceInfo.class, Memory.class, PropertyCallCache.class, int.class);
            expr.writePopAll(1);
        }
    }
    if (!returnValue)
        expr.writePopAll(1);
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Memory(php.runtime.Memory) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) DynamicAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken) ListExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken)

Example 9 with ExprStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.

the class NamespaceSyntaxTest method testSimple.

@Test
public void testSimple() {
    List<Token> tree = getSyntaxTree("namespace; 123;");
    Assert.assertTrue(tree.size() == 2);
    Assert.assertTrue(tree.get(0) instanceof NamespaceStmtToken);
    Assert.assertTrue(tree.get(1) instanceof ExprStmtToken);
    NamespaceStmtToken token = (NamespaceStmtToken) tree.get(0);
    Assert.assertNull(token.getTree());
    Assert.assertNull(token.getName());
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) Test(org.junit.Test)

Example 10 with ExprStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.

the class SimpleExprTest method testSimpleCall.

@Test
public void testSimpleCall() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("myCall(1 * 2, func(3, 2), 4);"));
    SyntaxAnalyzer analyzer = new SyntaxAnalyzer(environment, tokenizer);
    List<Token> tokens = analyzer.getTree();
    Assert.assertTrue(tokens.size() == 1);
    Assert.assertTrue(tokens.get(0) instanceof ExprStmtToken);
    ExprStmtToken expr = (ExprStmtToken) tokens.get(0);
    tokens = expr.getTokens();
    Assert.assertTrue(tokens.size() == 1);
    Assert.assertTrue(tokens.get(0) instanceof CallExprToken);
    CallExprToken call = (CallExprToken) expr.getTokens().get(0);
    Assert.assertTrue(call.getName() instanceof NameToken);
    Assert.assertEquals("myCall", ((NameToken) call.getName()).getName());
    Assert.assertTrue(call.getParameters().size() == 3);
    Assert.assertTrue(call.getParameters().get(0).getTokens().size() == 3);
    Assert.assertTrue(call.getParameters().get(2).getTokens().size() == 1);
    ExprStmtToken param = call.getParameters().get(1);
    Assert.assertTrue(param.getTokens().size() == 1);
    Assert.assertTrue(param.getTokens().get(0) instanceof CallExprToken);
    CallExprToken subCall = (CallExprToken) param.getTokens().get(0);
    Assert.assertTrue(subCall.getParameters().size() == 2);
}
Also used : Context(php.runtime.env.Context) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) MinusExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.MinusExprToken) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Aggregations

ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)25 Test (org.junit.Test)15 Token (org.develnext.jphp.core.tokenizer.token.Token)12 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)4 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)4 MinusExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.MinusExprToken)4 ValueIfElseToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken)4 IfStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.IfStmtToken)4 Context (php.runtime.env.Context)4 VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)3 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)2 DynamicAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken)2 ListExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken)2 BodyStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)2 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)2 LabelNode (org.objectweb.asm.tree.LabelNode)2 Memory (php.runtime.Memory)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 StackItem (org.develnext.jphp.core.compiler.common.misc.StackItem)1