Search in sources :

Example 1 with ValueExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken 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 ValueExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken 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 ValueExprToken

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

the class ExpressionStmtCompiler method writePopAll.

public void writePopAll(int count) {
    int i = 0;
    while (method.getStackCount() > 0 && i < count) {
        i++;
        StackItem o = stackPop();
        ValueExprToken token = o.getToken();
        StackItem.Type type = o.type;
        if (token == null) {
            switch(type.size()) {
                case 2:
                    code.add(new InsnNode(POP2));
                    break;
                case 1:
                    code.add(new InsnNode(POP));
                    break;
                default:
                    throw new IllegalArgumentException("Invalid of size StackItem: " + type.size());
            }
        } else
            /* if (o.isInvalidForOperations())*/
            unexpectedToken(token);
    }
}
Also used : StackItem(org.develnext.jphp.core.compiler.common.misc.StackItem) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)

Example 4 with ValueExprToken

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

the class ExpressionStmtCompiler method writeExpression.

@SuppressWarnings("unchecked")
public Memory writeExpression(ExprStmtToken expression, boolean returnValue, boolean returnMemory, boolean writeOpcode) {
    int initStackSize = method.getStackCount();
    exprStackInit.push(initStackSize);
    if (!expression.isStmtList()) {
        if (expression.getAsmExpr() == null) {
            throw new CriticalException("Invalid expression token without asm expr, on line " + expression.getMeta().getStartLine() + ", expr = " + expression.getWord());
        }
        // new ASMExpression(compiler.getEnvironment(), compiler.getContext(), expression).getResult();
        expression = expression.getAsmExpr();
    }
    List<Token> tokens = expression.getTokens();
    int operatorCount = 0;
    for (Token token : tokens) {
        if (token instanceof OperatorExprToken)
            operatorCount++;
    }
    boolean invalid = false;
    for (Token token : tokens) {
        if (token == null)
            continue;
        writeTickTrigger(token);
        if (writeOpcode) {
            if (token instanceof StmtToken) {
                if (!(token instanceof ReturnStmtToken))
                    method.entity.setImmutable(false);
            }
            BaseStatementCompiler cmp = getCompiler(token.getClass());
            if (cmp != null && !(cmp instanceof BaseExprCompiler)) {
                cmp.write(token);
                continue;
            }
        }
        if (token instanceof ValueExprToken) {
            // mixed, calls, numbers, strings, vars, etc.
            if (token instanceof CallExprToken && ((CallExprToken) token).getName() instanceof OperatorExprToken) {
                if (writeOpcode) {
                    writePush((ValueExprToken) token, true, true);
                    method.entity.setImmutable(false);
                } else
                    break;
            } else
                stackPush((ValueExprToken) token);
        } else if (token instanceof OperatorExprToken) {
            // + - * / % && || or ! and == > < etc.
            operatorCount--;
            if (operatorCount >= 0) {
                Memory result;
                if (operatorCount == 0) {
                    result = writeOperator((OperatorExprToken) token, returnValue, writeOpcode);
                } else
                    result = writeOperator((OperatorExprToken) token, true, writeOpcode);
                if (!writeOpcode && result == null) {
                    invalid = true;
                    break;
                }
                if (result == null)
                    method.entity.setImmutable(false);
            }
        } else
            break;
    }
    Memory result = null;
    if (!invalid && returnMemory && returnValue && !stackEmpty(false) && stackPeek().isConstant()) {
        result = stackPop().memory;
        invalid = true;
    }
    if (!invalid) {
        if (returnValue && !stackEmpty(false) && stackPeek().isKnown()) {
            if (returnMemory)
                result = tryWritePush(stackPop(), writeOpcode, returnValue, true);
            else
                writePush(stackPop());
        } else if (method.getStackCount() > 0) {
            if (stackPeekToken() instanceof CallableExprToken) {
                if (returnMemory)
                    result = tryWritePush(stackPopToken(), returnValue, writeOpcode, true);
                else
                    writePush(stackPopToken(), returnValue, true);
            } else if (stackPeek().isConstant()) {
                stackPop();
            }
        }
    }
    if (!returnValue && writeOpcode) {
        writePopAll(method.getStackCount() - initStackSize);
    } else if (!writeOpcode) {
        int count = method.getStackCount() - initStackSize;
        for (int i = 0; i < count; i++) {
            stackPop();
        }
    }
    exprStackInit.pop();
    return result;
}
Also used : UndefinedMemory(php.runtime.memory.helper.UndefinedMemory) Memory(php.runtime.Memory) OperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) OperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ClassExprToken(org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken) OpenEchoTagToken(org.develnext.jphp.core.tokenizer.token.OpenEchoTagToken) CriticalException(php.runtime.exceptions.CriticalException) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)

Example 5 with ValueExprToken

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

the class ExpressionStmtCompiler method writePushCall.

Memory writePushCall(CallExprToken function, boolean returnValue, boolean writeOpcode, PushCallStatistic statistic) {
    Token name = function.getName();
    if (name instanceof NameToken) {
        String realName = ((NameToken) name).getName();
        CompileFunction compileFunction = compiler.getScope().findCompileFunction(realName);
        // try find system function, like max, sin, cos, etc.
        if (compileFunction == null && name instanceof FulledNameToken && compiler.getEnvironment().fetchFunction(realName) == null && compiler.findFunction(realName) == null) {
            String tryName = ((FulledNameToken) name).getLastName().getName();
            compileFunction = compiler.getScope().findCompileFunction(tryName);
        }
        if (compileFunction != null) {
            return writePushCompileFunction(function, compileFunction, returnValue, writeOpcode, statistic);
        } else {
            if (!writeOpcode)
                return null;
            method.entity.setImmutable(false);
            int index = method.clazz.getAndIncCallFuncCount();
            writePushEnv();
            writePushTraceInfo(function);
            writePushString(realName.toLowerCase());
            writePushString(realName);
            writePushParameters(function.getParameters());
            writeGetStatic("$CALL_FUNC_CACHE", FunctionCallCache.class);
            writePushConstInt(index);
            writeSysStaticCall(InvokeHelper.class, "call", Memory.class, Environment.class, TraceInfo.class, String.class, String.class, Memory[].class, FunctionCallCache.class, Integer.TYPE);
            if (!returnValue)
                writePopAll(1);
        }
    } else if (name instanceof StaticAccessExprToken) {
        method.entity.setImmutable(false);
        if (((StaticAccessExprToken) name).isAsParent())
            return writePushParentDynamicMethod(function, returnValue, writeOpcode, statistic);
        else
            return writePushStaticMethod(function, returnValue, writeOpcode, statistic);
    } else if (name instanceof DynamicAccessExprToken) {
        method.entity.setImmutable(false);
        return writePushDynamicMethod(function, returnValue, writeOpcode, statistic);
    } else {
        if (!writeOpcode)
            return null;
        method.entity.setImmutable(false);
        writeLineNumber(function);
        writePush((ValueExprToken) function.getName(), true, false);
        writePopBoxing();
        writePushParameters(function.getParameters());
        writePushEnv();
        writePushTraceInfo(function);
        writeSysStaticCall(InvokeHelper.class, "callAny", Memory.class, Memory.class, Memory[].class, Environment.class, TraceInfo.class);
        if (!returnValue)
            writePopAll(1);
    }
    return null;
}
Also used : UndefinedMemory(php.runtime.memory.helper.UndefinedMemory) Memory(php.runtime.Memory) CompileFunction(php.runtime.ext.support.compile.CompileFunction) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) OperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ClassExprToken(org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken) OpenEchoTagToken(org.develnext.jphp.core.tokenizer.token.OpenEchoTagToken)

Aggregations

ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)12 Memory (php.runtime.Memory)8 UndefinedMemory (php.runtime.memory.helper.UndefinedMemory)6 StackItem (org.develnext.jphp.core.compiler.common.misc.StackItem)5 Token (org.develnext.jphp.core.tokenizer.token.Token)5 ClassExprToken (org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken)5 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)4 OperatorExprToken (org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken)4 OpenEchoTagToken (org.develnext.jphp.core.tokenizer.token.OpenEchoTagToken)3 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)3 Environment (php.runtime.env.Environment)3 GetVarExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.GetVarExprToken)2 VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)2 TraceInfo (php.runtime.env.TraceInfo)2 CriticalException (php.runtime.exceptions.CriticalException)2 CompileClass (php.runtime.ext.support.compile.CompileClass)2 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)1 ExprToken (org.develnext.jphp.core.tokenizer.token.expr.ExprToken)1 CallExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.CallExprToken)1 StringBuilderExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StringBuilderExprToken)1