Search in sources :

Example 1 with VariableExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken 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 2 with VariableExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken in project jphp by jphp-compiler.

the class GlobalDefinitionCompiler method write.

@Override
public void write(GlobalStmtToken token) {
    for (ValueExprToken variable : token.getVariables()) {
        if (variable instanceof VariableExprToken) {
            LocalVariable local = method.getLocalVariable(((VariableExprToken) variable).getName());
            assert local != null;
            expr.writePushEnv();
            expr.writePushConstString(local.name);
            expr.writeSysDynamicCall(Environment.class, "getOrCreateGlobal", Memory.class, String.class);
            expr.writeVarStore(local, false, false);
        } else if (variable instanceof GetVarExprToken) {
            BaseExprCompiler<GetVarExprToken> compiler = (BaseExprCompiler<GetVarExprToken>) expr.getCompiler(GetVarExprToken.class);
            if (compiler == null)
                throw new CriticalException("Cannot find a valid compiler for " + GetVarExprToken.class.getName());
            compiler.write((GetVarExprToken) variable, true);
            expr.writePushEnv();
            Memory name = expr.writeExpression(((GetVarExprToken) variable).getName(), true, true, true);
            if (name != null) {
                expr.writePushConstString(name.toString());
            } else {
                expr.writePopString();
            }
            expr.writeSysDynamicCall(Environment.class, "getOrCreateGlobal", Memory.class, String.class);
            expr.writeSysDynamicCall(Memory.class, "assign", Memory.class, Memory.class);
            expr.writePopAll(1);
        }
    }
}
Also used : 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) CriticalException(php.runtime.exceptions.CriticalException) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)

Example 3 with VariableExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken 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());
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) LineNumberNode(org.objectweb.asm.tree.LineNumberNode)

Example 4 with VariableExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken 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);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ForeachIterator(php.runtime.lang.ForeachIterator) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) FatalException(php.runtime.exceptions.FatalException) ListCompiler(org.develnext.jphp.core.compiler.jvm.statement.expr.value.ListCompiler) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) DynamicAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken) ForeachStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ForeachStmtToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ListExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken) DynamicAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) ListExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken)

Example 5 with VariableExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken in project jphp by jphp-compiler.

the class TryCatchGenerator method processCatch.

public void processCatch(CatchStmtToken result, ListIterator<Token> iterator) {
    Token next = nextToken(iterator);
    if (!isOpenedBrace(next, BraceExprToken.Kind.SIMPLE))
        unexpectedToken(next, "(");
    next = nextToken(iterator);
    if (!(next instanceof NameToken))
        unexpectedToken(next, TokenType.T_STRING);
    FulledNameToken exception = analyzer.getRealName((NameToken) next);
    result.setException(exception);
    next = nextToken(iterator);
    if (!(next instanceof VariableExprToken))
        unexpectedToken(next, TokenType.T_VARIABLE);
    VariableExprToken variable = (VariableExprToken) next;
    result.setVariable(variable);
    if (analyzer.getFunction() != null) {
        analyzer.getFunction().variable(variable).setUnstable(true);
    }
    analyzer.getScope().addVariable(variable);
    next = nextToken(iterator);
    if (!isClosedBrace(next, BraceExprToken.Kind.SIMPLE))
        unexpectedToken(next, ")");
    BodyStmtToken body = analyzer.generator(BodyGenerator.class).getToken(nextToken(iterator), iterator);
    result.setBody(body);
}
Also used : BodyGenerator(org.develnext.jphp.core.syntax.generators.manually.BodyGenerator) Token(org.develnext.jphp.core.tokenizer.token.Token) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) FinallyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.FinallyStmtToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) TryStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.TryStmtToken) CatchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)

Aggregations

VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)5 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)4 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)3 Token (org.develnext.jphp.core.tokenizer.token.Token)2 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)2 GetVarExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.GetVarExprToken)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 Environment (php.runtime.env.Environment)2 ListCompiler (org.develnext.jphp.core.compiler.jvm.statement.expr.value.ListCompiler)1 BodyGenerator (org.develnext.jphp.core.syntax.generators.manually.BodyGenerator)1 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)1 DynamicAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken)1 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)1 ListExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 CatchStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken)1 FinallyStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.FinallyStmtToken)1