use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.
the class ForCompiler method write.
@Override
public void write(ForStmtToken token) {
expr.writeDefineVariables(token.getInitLocal());
for (ExprStmtToken expr : token.getInitExpr()) {
this.expr.writeExpression(expr, false, false);
}
expr.writeUndefineVariables(token.getInitLocal());
expr.writeDefineVariables(token.getLocal());
for (VariableExprToken variable : token.getIterationLocal()) {
// TODO optimize this for Dynamic Values of variables
LocalVariable local = method.getLocalVariable(variable.getName());
local.setValue(null);
}
LabelNode start = expr.writeLabel(node, token.getMeta().getStartLine());
LabelNode iter = new LabelNode();
LabelNode end = new LabelNode();
for (Iterator<ExprStmtToken> i = token.getConditionExpr().iterator(); i.hasNext(); ) {
ExprStmtToken expr = i.next();
if (i.hasNext()) {
this.expr.writeExpression(expr, false, false);
} else {
this.expr.writeExpression(expr, true, false);
this.expr.writePopBoolean();
add(new JumpInsnNode(IFEQ, end));
this.expr.stackPop();
}
}
method.pushJump(end, iter);
expr.write(BodyStmtToken.class, token.getBody());
method.popJump();
add(iter);
for (ExprStmtToken expr : token.getIterationExpr()) {
this.expr.writeExpression(expr, false, false);
}
add(new JumpInsnNode(GOTO, start));
add(end);
add(new LineNumberNode(token.getMeta().getEndLine(), end));
expr.writeUndefineVariables(token.getLocal());
}
use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.
the class ForeachCompiler method write.
@Override
public void write(ForeachStmtToken token) {
expr.writeDefineVariables(token.getLocal());
LabelNode start = new LabelNode();
LabelNode end = new LabelNode();
LabelNode l = new LabelNode();
add(l);
expr.writePushEnv();
expr.writePushTraceInfo(token);
expr.writeExpression(token.getIterator(), true, false, true);
expr.writePopBoxing();
expr.writePushConstBoolean(token.isValueReference());
expr.writePushConstBoolean(token.isKeyReference());
expr.writeSysDynamicCall(Environment.class, "__getIterator", ForeachIterator.class, TraceInfo.class, Memory.class, Boolean.TYPE, Boolean.TYPE);
String name = "~foreach~" + method.nextStatementIndex(ForeachIterator.class);
LocalVariable foreachVariable = method.getLocalVariable(name);
if (foreachVariable == null)
foreachVariable = method.addLocalVariable(name, l, ForeachIterator.class);
/*LocalVariable foreachVariable = method.addLocalVariable(
"~foreach~" + method.nextStatementIndex(ForeachIterator.class), l, ForeachIterator.class
);*/
foreachVariable.setEndLabel(end);
expr.writeVarStore(foreachVariable, false, false);
method.pushJump(end, start);
add(start);
expr.writeVarLoad(foreachVariable);
expr.writeSysDynamicCall(ForeachIterator.class, "next", Boolean.TYPE);
add(new JumpInsnNode(IFEQ, end));
expr.stackPop();
// $key
if (token.getKey() != null) {
LocalVariable key = method.getLocalVariable(token.getKey().getName());
expr.checkAssignableVar(token.getKey());
expr.writeVarLoad(foreachVariable);
expr.writeSysDynamicCall(ForeachIterator.class, "getMemoryKey", Memory.class);
if (token.isKeyReference()) {
throw new FatalException("Key element cannot be a reference", token.getKey().toTraceInfo(compiler.getContext()));
// writeVarStore(key, false, false);
} else
expr.writeVarAssign(key, null, false, false);
}
// $var
//LocalVariable variable = method.getLocalVariable(token.getValue().getName());
Token last = token.getValue().getLast();
VariableExprToken var = null;
if (last instanceof DynamicAccessExprToken) {
DynamicAccessExprToken setter = (DynamicAccessExprToken) last;
ExprStmtToken value = new ExprStmtToken(this.env, this.compiler.getContext(), token.getValue().getTokens());
value.getTokens().remove(value.getTokens().size() - 1);
value.updateAsmExpr(this.env, this.compiler.getContext());
expr.writeExpression(value, true, false);
expr.writeVarLoad(foreachVariable);
expr.writeSysDynamicCall(ForeachIterator.class, "getValue", Memory.class);
if (!token.isValueReference())
expr.writePopImmutable();
expr.writeDynamicAccessInfo(setter, false);
expr.writeGetStatic("$CALL_PROP_CACHE", PropertyCallCache.class);
expr.writePushConstInt(method.clazz.getAndIncCallPropCount());
expr.writeSysStaticCall(ObjectInvokeHelper.class, "assignProperty", Memory.class, Memory.class, Memory.class, String.class, Environment.class, TraceInfo.class, PropertyCallCache.class, int.class);
} else {
if (token.getValue().getSingle() instanceof VariableExprToken)
expr.checkAssignableVar(var = (VariableExprToken) token.getValue().getSingle());
expr.writeVarLoad(foreachVariable);
expr.writeSysDynamicCall(ForeachIterator.class, "getValue", Memory.class);
if (!token.isValueReference())
expr.writePopImmutable();
ExprStmtToken value = token.getValue();
if (value.isSingle() && value.getSingle() instanceof ListExprToken) {
ListCompiler listCompiler = (ListCompiler) expr.getCompiler(ListExprToken.class);
listCompiler.write((ListExprToken) value.getSingle(), false, false);
} else {
expr.writeExpression(value, true, false);
if (expr.stackPeek().immutable)
expr.unexpectedToken(value.getLast());
expr.writeSysStaticCall(Memory.class, token.isValueReference() ? "assignRefRight" : "assignRight", Memory.class, Memory.class, Memory.class);
}
}
expr.writePopAll(1);
/*
if (token.isValueReference())
writeVarStore(variable, false, false);
else
writeVarAssign(variable, false, true); */
// body
expr.write(BodyStmtToken.class, token.getBody());
add(new JumpInsnNode(GOTO, start));
add(end);
/*if (compiler.getLangMode() == LangMode.JPHP){
if (token.isValueReference() && var != null){
expr.writeVarLoad(var.getName());
expr.writeSysDynamicCall(Memory.class, "unset", void.class);
}
}*/
method.popJump();
expr.writeUndefineVariables(token.getLocal());
method.prevStatementIndex(ForeachIterator.class);
}
use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.
the class ASMExpressionTest method testWithBraces.
@Test
public void testWithBraces() {
ExprStmtToken expression = getASMExpression("(1 + 2) * 3").getResult();
Assert.assertEquals(5, expression.getTokens().size());
Assert.assertEquals("12+3*", expression.getWord());
}
use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.
the class SimpleExprTest method testVarVar.
@Test
public void testVarVar() {
List<Token> tokens = getSyntaxTree("$$foobar;");
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 GetVarExprToken);
ExprStmtToken name = ((GetVarExprToken) tokens.get(0)).getName();
Assert.assertTrue(name.getTokens().size() == 1);
Assert.assertTrue(name.getTokens().get(0) instanceof VariableExprToken);
tokens = getSyntaxTree("$$$foobar;");
Assert.assertTrue(tokens.size() == 1);
Assert.assertTrue(tokens.get(0) instanceof ExprStmtToken);
expr = (ExprStmtToken) tokens.get(0);
tokens = expr.getTokens();
Assert.assertTrue(tokens.size() == 1);
Assert.assertTrue(tokens.get(0) instanceof GetVarExprToken);
name = ((GetVarExprToken) tokens.get(0)).getName();
Assert.assertTrue(name.getTokens().size() == 1);
Assert.assertTrue(name.getTokens().get(0) instanceof GetVarExprToken);
tokens = getSyntaxTree("${'foobar' . 'x'};");
Assert.assertTrue(tokens.size() == 1);
Assert.assertTrue(tokens.get(0) instanceof ExprStmtToken);
expr = (ExprStmtToken) tokens.get(0);
tokens = expr.getTokens();
Assert.assertTrue(tokens.size() == 1);
Assert.assertTrue(tokens.get(0) instanceof GetVarExprToken);
name = ((GetVarExprToken) tokens.get(0)).getName();
Assert.assertTrue(name.getTokens().size() == 3);
Assert.assertTrue(name.getTokens().get(0) instanceof StringExprToken);
}
use of org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken in project jphp by jphp-compiler.
the class SimpleExprTest method testVarCall.
@Test
public void testVarCall() {
List<Token> tokens = getSyntaxTree("$x - $func(1, 3, 4);");
Assert.assertEquals(1, tokens.size());
Assert.assertTrue(tokens.get(0) instanceof ExprStmtToken);
ExprStmtToken expr = (ExprStmtToken) tokens.get(0);
tokens = expr.getTokens();
Assert.assertTrue(tokens.size() == 3);
Assert.assertTrue(tokens.get(0) instanceof VariableExprToken);
Assert.assertTrue(tokens.get(1) instanceof MinusExprToken);
Assert.assertTrue(tokens.get(2) instanceof CallExprToken);
}
Aggregations