Search in sources :

Example 6 with NameToken

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

the class InstanceOfCompiler method pushName.

protected void pushName(StackItem R) {
    NameToken name = null;
    if (R.getToken() instanceof NameToken)
        name = this.compiler.getAnalyzer().getRealName((NameToken) R.getToken());
    if (name == null) {
        expr.writePush(R);
        expr.writePopString();
        expr.writePushDupLowerCase();
    } else {
        expr.writePushConstString(name.getName());
        expr.writePushConstString(name.getName().toLowerCase());
    }
}
Also used : NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)

Example 7 with NameToken

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

the class MethodStmtCompiler method compile.

@Override
public MethodEntity compile() {
    if (statement != null) {
        if (external)
            statement.setDynamicLocal(true);
        if (statement.getDocComment() != null)
            entity.setDocComment(new DocumentComment(statement.getDocComment().getComment()));
        entity.setAbstract(statement.isAbstract());
        entity.setAbstractable(statement.getBody() == null);
        entity.setFinal(statement.isFinal());
        entity.setStatic(statement.isStatic());
        entity.setModifier(statement.getModifier());
        entity.setReturnReference(statement.isReturnReference());
        entity.setTrace(statement.toTraceInfo(compiler.getContext()));
        entity.setImmutable(statement.getArguments().isEmpty());
        entity.setGeneratorEntity(generatorEntity);
        if (clazz.isSystem())
            entity.setInternalName(entity.getName());
        else
            entity.setInternalName(entity.getName() + "$" + clazz.entity.nextMethodIndex());
        ParameterEntity[] parameters = new ParameterEntity[statement.getArguments().size()];
        int i = 0;
        for (ArgumentStmtToken argument : statement.getArguments()) {
            parameters[i] = new ParameterEntity(compiler.getContext());
            ParameterEntity parameter = parameters[i];
            parameter.setReference(argument.isReference());
            parameter.setName(argument.getName().getName());
            parameter.setTrace(argument.toTraceInfo(compiler.getContext()));
            parameter.setMutable(statement.isDynamicLocal() || statement.variable(argument.getName()).isMutable());
            parameter.setUsed(!statement.isUnusedVariable(argument.getName()));
            parameter.setVariadic(argument.isVariadic());
            parameter.setType(argument.getHintType());
            if (argument.getHintTypeClass() != null) {
                parameter.setTypeClass(argument.getHintTypeClass().getName());
            }
            ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(compiler);
            ExprStmtToken value = argument.getValue();
            if (value != null) {
                Memory defaultValue = expressionStmtCompiler.writeExpression(value, true, true, false);
                // try detect constant
                if (value.isSingle()) {
                    if (value.getSingle() instanceof NameToken) {
                        parameter.setDefaultValueConstName(((NameToken) value.getSingle()).getName());
                        if (defaultValue == null) {
                            defaultValue = (new ConstantMemory(((NameToken) value.getSingle()).getName()));
                            parameter.setMutable(true);
                        }
                    } else if (value.getSingle() instanceof StaticAccessExprToken) {
                        StaticAccessExprToken access = (StaticAccessExprToken) value.getSingle();
                        if (access.getClazz() instanceof NameToken && access.getField() instanceof NameToken) {
                            if (defaultValue == null)
                                defaultValue = (new ClassConstantMemory(((NameToken) access.getClazz()).getName(), ((NameToken) access.getField()).getName()));
                            parameter.setDefaultValueConstName(((NameToken) access.getClazz()).getName() + "::" + ((NameToken) access.getField()).getName());
                            parameter.setMutable(true);
                        }
                    }
                }
                if (defaultValue == null)
                    compiler.getEnvironment().error(argument.toTraceInfo(compiler.getContext()), ErrorType.E_COMPILE_ERROR, Messages.ERR_EXPECTED_CONST_VALUE, "$" + argument.getName().getName());
                parameter.setDefaultValue(defaultValue);
            }
            i++;
        }
        entity.setParameters(parameters);
    }
    if (statement != null && clazz.statement.isInterface()) {
        if (!statement.isInterfacable()) {
            compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_INTERFACE_FUNCTION_CANNOT_CONTAIN_BODY.fetch(entity.getSignatureString(false)));
        }
        if (statement.isAbstract() || statement.isFinal()) {
            compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_ACCESS_TYPE_FOR_INTERFACE_METHOD.fetch(entity.getSignatureString(false)));
        }
    } else {
        writeHeader();
        if (statement.isGenerator()) {
            entity.setEmpty(false);
            entity.setImmutable(false);
            GeneratorStmtCompiler generatorStmtCompiler = new GeneratorStmtCompiler(compiler, statement);
            entity.setGeneratorEntity(generatorStmtCompiler.compile());
            ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
            expr.makeUnknown(new TypeInsnNode(NEW, entity.getGeneratorEntity().getInternalName()));
            expr.stackPush(Memory.Type.REFERENCE);
            expr.writePushDup();
            // env
            expr.writePushEnv();
            // classEntity
            expr.writePushDup();
            expr.writePushConstString(compiler.getModule().getInternalName());
            expr.writePushConstInt((int) entity.getGeneratorEntity().getId());
            expr.writeSysDynamicCall(Environment.class, "__getGenerator", ClassEntity.class, String.class, Integer.TYPE);
            // self
            expr.writePushThis();
            // uses
            expr.writeVarLoad("~args");
            expr.writeSysCall(entity.getGeneratorEntity().getInternalName(), INVOKESPECIAL, Constants.INIT_METHOD, void.class, Environment.class, ClassEntity.class, Memory.class, Memory[].class);
            expr.writeSysStaticCall(ObjectMemory.class, "valueOf", Memory.class, IObject.class);
            expr.makeUnknown(new InsnNode(Opcodes.ARETURN));
            expr.stackPop();
        } else {
            ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
            entity.setEmpty(true);
            if (statement != null && statement.getBody() != null) {
                expr.writeDefineVariables(statement.getLocal());
                expr.write(statement.getBody());
                if (!statement.getBody().getInstructions().isEmpty()) {
                    entity.setEmpty(false);
                }
            }
            if (generatorEntity != null) {
                expr.writeVarLoad("~this");
                expr.writePushConstBoolean(false);
                expr.writeSysDynamicCall(null, "_setValid", void.class, Boolean.TYPE);
            }
            ReturnStmtToken token = new ReturnStmtToken(new TokenMeta("", 0, 0, 0, 0));
            token.setValue(null);
            expr.getCompiler(ReturnStmtToken.class).write(token);
        }
        writeFooter();
    }
    return entity;
}
Also used : TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) ArrayMemory(php.runtime.memory.ArrayMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) Memory(php.runtime.Memory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) ObjectMemory(php.runtime.memory.ObjectMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) DocumentComment(php.runtime.reflection.DocumentComment) ParameterEntity(php.runtime.reflection.ParameterEntity) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory)

Example 8 with NameToken

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

the class SyntaxAnalyzer method getRealName.

public static FulledNameToken getRealName(NameToken what, NamespaceStmtToken namespace, NamespaceUseStmtToken.UseType useType) {
    if (what instanceof FulledNameToken) {
        FulledNameToken fulledNameToken = (FulledNameToken) what;
        if (fulledNameToken.isProcessed(useType)) {
            return fulledNameToken;
        } else {
            if (fulledNameToken.isAnyProcessed()) {
                what = (fulledNameToken).getLastName();
            }
        }
    }
    String name = what.getName();
    // check name in uses
    if (namespace != null) {
        for (NamespaceUseStmtToken use : namespace.getUses()) {
            if (use.getUseType() != useType) {
                continue;
            }
            if (use.getAs() == null) {
                String string = use.getName().getLastName().getName();
                if ((useType == CONSTANT && name.equals(string)) || (useType != CONSTANT && name.equalsIgnoreCase(string))) {
                    FulledNameToken t = new FulledNameToken(use.getName());
                    t.setProcessed(useType);
                    return t;
                }
            } else {
                String string = use.getAs().getName();
                if ((useType == CONSTANT && name.equals(string)) || (useType != CONSTANT && name.equalsIgnoreCase(string))) {
                    FulledNameToken t = new FulledNameToken(use.getName());
                    t.setProcessed(useType);
                    return t;
                }
            }
        }
    }
    List<NameToken> names = namespace == null || namespace.getName() == null ? new ArrayList<NameToken>() : new ArrayList<NameToken>(namespace.getName().getNames());
    if (what instanceof FulledNameToken)
        names.addAll(((FulledNameToken) what).getNames());
    else
        names.add(what);
    FulledNameToken t = new FulledNameToken(what.getMeta(), names);
    t.setProcessed(useType);
    return t;
}
Also used : 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)

Example 9 with NameToken

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

the class JvmCompiler method compile.

@Override
public ModuleEntity compile(boolean autoRegister) {
    this.classes = new ArrayList<>();
    module.setInternalName("$php_module_m" + UUID.randomUUID().toString().replace("-", ""));
    List<ExprStmtToken> externalCode = process(tokens, NamespaceStmtToken.getDefault());
    NamespaceStmtToken namespace = NamespaceStmtToken.getDefault();
    ClassStmtToken token = new ClassStmtToken(TokenMeta.of(module.getName()));
    token.setFinal(true);
    token.setName(new NameToken(TokenMeta.of(module.getInternalName())));
    token.setNamespace(namespace);
    MethodStmtToken methodToken = new MethodStmtToken(token.getMeta());
    methodToken.setFinal(true);
    methodToken.setClazz(token);
    methodToken.setModifier(Modifier.PUBLIC);
    methodToken.setStatic(true);
    methodToken.setDynamicLocal(true);
    methodToken.setName((NameToken) Token.of("__include"));
    methodToken.setArguments(new ArrayList<ArgumentStmtToken>());
    methodToken.setLocal(analyzer.getScope().getVariables());
    methodToken.setLabels(analyzer.getScope().getLabels());
    methodToken.setBody(BodyStmtToken.of(externalCode));
    token.setMethods(Arrays.asList(methodToken));
    ClassStmtCompiler classStmtCompiler = new ClassStmtCompiler(this, token);
    classStmtCompiler.setExternal(true);
    classStmtCompiler.setSystem(true);
    module.setData(classStmtCompiler.compile().getData());
    if (autoRegister)
        scope.addUserModule(module);
    return module;
}
Also used : NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)

Example 10 with NameToken

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

the class MethodStmtToken method of.

public static MethodStmtToken of(String name, ClassStmtToken clazz) {
    MethodStmtToken mToken = new MethodStmtToken(clazz.getMeta());
    mToken.setClazz(clazz);
    mToken.setName(new NameToken(TokenMeta.of(name, clazz)));
    mToken.setNamespace(null);
    return mToken;
}
Also used : NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)

Aggregations

NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)12 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)6 Token (org.develnext.jphp.core.tokenizer.token.Token)4 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)2 CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)2 OperatorExprToken (org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken)2 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)2 StaticAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken)2 ClassStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken)2 MethodStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)2 Memory (php.runtime.Memory)2 Environment (php.runtime.env.Environment)2 ArrayList (java.util.ArrayList)1 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)1 MethodNodeImpl (org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl)1 BodyGenerator (org.develnext.jphp.core.syntax.generators.manually.BodyGenerator)1 SimpleExprGenerator (org.develnext.jphp.core.syntax.generators.manually.SimpleExprGenerator)1 TokenMeta (org.develnext.jphp.core.tokenizer.TokenMeta)1 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)1 BackslashExprToken (org.develnext.jphp.core.tokenizer.token.expr.BackslashExprToken)1