Search in sources :

Example 1 with ExprStmtToken

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

the class ThrowGenerator method getToken.

@Override
public ThrowStmtToken getToken(Token current, ListIterator<Token> iterator) {
    if (current instanceof ThrowStmtToken) {
        ThrowStmtToken result = (ThrowStmtToken) current;
        ExprStmtToken exception = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, Separator.SEMICOLON, null);
        if (exception == null)
            unexpectedToken(iterator.previous());
        result.setException(exception);
        return result;
    }
    return null;
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) SimpleExprGenerator(org.develnext.jphp.core.syntax.generators.manually.SimpleExprGenerator) ThrowStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ThrowStmtToken)

Example 2 with ExprStmtToken

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

the class BodyGenerator method getToken.

@SuppressWarnings("unchecked")
public BodyStmtToken getToken(Token current, ListIterator<Token> iterator, boolean absolute, boolean returnNull, Class<? extends Token>... endTokens) {
    boolean alternativeSyntax = false;
    List<ExprStmtToken> instructions = new ArrayList<ExprStmtToken>();
    if (isOpenedBrace(current, BraceExprToken.Kind.BLOCK)) /*|| current instanceof SemicolonToken*/
    {
        //if (!returnNull)
        returnNull = false;
        while ((current = nextToken(iterator)) != null) {
            ExprStmtToken expr = analyzer.generator(ExprGenerator.class).getToken(current, iterator, BraceExprToken.class);
            if (expr == null) {
                break;
            }
            instructions.add(expr);
        }
    } else if (current instanceof ColonToken || (absolute && endTokens != null)) {
        if (endTokens == null)
            unexpectedToken(current);
        if (!(current instanceof ColonToken))
            iterator.previous();
        else
            alternativeSyntax = true;
        while (iterator.hasNext()) {
            current = nextToken(iterator);
            ExprStmtToken expr = analyzer.generator(ExprGenerator.class).getToken(current, iterator, endTokens);
            if (expr == null) {
                iterator.previous();
                break;
            } else if (expr.getTokens().size() == 1 && expr.getTokens().get(0) instanceof SemicolonToken) {
            // nop break;
            } else {
                instructions.add(expr);
            }
        }
    } else {
        ExprStmtToken expr = analyzer.generator(ExprGenerator.class).getToken(current, iterator);
        if (expr != null) {
            if (expr.getTokens().size() == 1 && expr.getTokens().get(0) instanceof SemicolonToken) {
            // nop
            } else {
                instructions.add(expr);
            }
        }
    }
    if (instructions.isEmpty() && returnNull)
        return null;
    BodyStmtToken result = new BodyStmtToken(TokenMeta.of(instructions));
    result.setInstructions(instructions);
    result.setAlternativeSyntax(alternativeSyntax);
    return result;
}
Also used : SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ArrayList(java.util.ArrayList) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) ExprGenerator(org.develnext.jphp.core.syntax.generators.ExprGenerator) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)

Example 3 with ExprStmtToken

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

the class UnsetCompiler method write.

@Override
public void write(UnsetExprToken token, boolean returnValue) {
    method.getEntity().setImmutable(false);
    for (ExprStmtToken param : token.getParameters()) {
        if (param.isSingle() && param.getSingle() instanceof VariableExprToken) {
            VariableExprToken variable = (VariableExprToken) param.getSingle();
            expr.checkAssignableVar(variable);
            LocalVariable local = method.getLocalVariable(variable.getName());
            expr.writeVarLoad(local);
            expr.writePushEnv();
            expr.writeSysDynamicCall(Memory.class, "manualUnset", void.class, Environment.class);
            if (!local.isReference()) {
                expr.writePushNull();
                expr.writeVarAssign(local, null, false, false);
            }
            local.setValue(null);
        } else if (param.isSingle() && param.getSingle() instanceof GetVarExprToken) {
            expr.writeValue((ValueExprToken) param.getSingle(), true);
            expr.writePushEnv();
            expr.writeSysDynamicCall(Memory.class, "manualUnset", void.class, Environment.class);
        } else {
            expr.writeExpression(param, false, false, true);
        }
    }
    if (returnValue)
        expr.writePushNull();
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Memory(php.runtime.Memory) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) Environment(php.runtime.env.Environment) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) GetVarExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.GetVarExprToken) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)

Example 4 with ExprStmtToken

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

the class ASMExpressionTest method testCallExpr.

@Test
public void testCallExpr() {
    ExprStmtToken expression = getASMExpression("func(1 + 2, 3) * 3").getResult();
    Assert.assertEquals(3, expression.getTokens().size());
    Assert.assertTrue(expression.getTokens().get(0) instanceof CallExprToken);
    CallExprToken call = (CallExprToken) expression.getTokens().get(0);
    Assert.assertEquals(2, call.getParameters().size());
    Assert.assertEquals("1+2", call.getParameters().get(0).getWord());
    Assert.assertEquals("3", call.getParameters().get(1).getWord());
    Assert.assertTrue(expression.getTokens().get(1) instanceof IntegerExprToken);
    Assert.assertTrue(expression.getTokens().get(2) instanceof MulExprToken);
}
Also used : CallExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.CallExprToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) MulExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.MulExprToken) IntegerExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.IntegerExprToken) Test(org.junit.Test)

Example 5 with ExprStmtToken

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

the class ASMExpressionTest method testPriorities.

@Test
public void testPriorities() {
    Assert.assertEquals("123/+", getASMExpression("1 + 2 / 3").getResult().getWord());
    Assert.assertEquals("123*+", getASMExpression("1 + 2 * 3").getResult().getWord());
    Assert.assertEquals("123%+", getASMExpression("1 + 2 % 3").getResult().getWord());
    Assert.assertEquals("12*3/", getASMExpression("1 * 2 / 3").getResult().getWord());
    Assert.assertEquals("12/3%", getASMExpression("1 / 2 % 3").getResult().getWord());
    Assert.assertEquals("12/3/", getASMExpression("1 / 2 / 3").getResult().getWord());
    Assert.assertEquals("12-3+", getASMExpression("1 - 2 + 3").getResult().getWord());
    ExprStmtToken expr = getASMExpression("1 && 2 + 3").getResult();
    Assert.assertEquals("1&&", expr.getWord());
    Assert.assertTrue(expr.getTokens().get(1) instanceof LogicOperatorExprToken);
    Assert.assertEquals("2+3", ((LogicOperatorExprToken) expr.getTokens().get(1)).getRightValue().getWord());
    Assert.assertEquals("1||", getASMExpression("1 || 2 + 3").getResult().getWord());
    Assert.assertEquals("1||", getASMExpression("1 || 2 && 3").getResult().getWord());
    Assert.assertEquals("1!||", getASMExpression("!1 || 2 && 3").getResult().getWord());
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) LogicOperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.LogicOperatorExprToken) 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