Search in sources :

Example 1 with ConstStmtToken

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

the class ConstTest method testSimple.

@Test
public void testSimple() {
    List<Token> tree = getSyntaxTree("const my_CONST = 1;");
    Assert.assertTrue(tree.size() == 1);
    Assert.assertTrue(tree.get(0) instanceof ConstStmtToken);
    ConstStmtToken constant = (ConstStmtToken) tree.get(0);
    Assert.assertEquals("my_CONST", constant.items.get(0).getFulledName());
    Assert.assertNull(constant.getClazz());
    Assert.assertNotNull(constant.items.get(0).value);
    Assert.assertTrue(constant.items.get(0).value.getTokens().size() == 1);
    Assert.assertTrue(constant.items.get(0).value.getTokens().get(0) instanceof IntegerExprToken);
}
Also used : ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) IntegerExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.IntegerExprToken) IntegerExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.IntegerExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) Test(org.junit.Test)

Example 2 with ConstStmtToken

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

the class ClassStmtCompiler method writeConstant.

protected void writeConstant(ConstStmtToken constant) {
    MethodStmtCompiler methodStmtCompiler = new MethodStmtCompiler(this, (MethodStmtToken) null);
    ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(methodStmtCompiler, null);
    DocumentComment documentComment = null;
    if (constant.getDocComment() != null)
        documentComment = new DocumentComment(constant.getDocComment().getComment());
    for (ConstStmtToken.Item el : constant.items) {
        Memory value = expressionStmtCompiler.writeExpression(el.value, true, true, false);
        ConstantEntity constantEntity = new ConstantEntity(el.getFulledName(), value, true);
        constantEntity.setTrace(el.name.toTraceInfo(compiler.getContext()));
        constantEntity.setDocComment(documentComment);
        if (value != null && !value.isArray()) {
            ConstantEntity c = entity.findConstant(el.getFulledName());
            if (c != null && c.getClazz().getId() == entity.getId()) {
                compiler.getEnvironment().error(constant.toTraceInfo(compiler.getContext()), ErrorType.E_ERROR, Messages.ERR_CANNOT_REDEFINE_CLASS_CONSTANT, entity.getName() + "::" + el.getFulledName());
                return;
            }
            entity.addConstant(constantEntity);
        } else {
            if (ValueExprToken.isConstable(el.value.getSingle(), false)) {
                dynamicConstants.add(el);
                entity.addConstant(constantEntity);
            } else
                compiler.getEnvironment().error(constant.toTraceInfo(compiler.getContext()), Messages.ERR_EXPECTED_CONST_VALUE.fetch(entity.getName() + "::" + el.getFulledName()));
        }
    }
}
Also used : Memory(php.runtime.Memory) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken)

Example 3 with ConstStmtToken

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

the class ClassStmtCompiler method compile.

@Override
public ClassEntity compile() {
    entity = new ClassEntity(compiler.getContext());
    entity.setId(compiler.getScope().nextClassIndex());
    entity.setFinal(statement.isFinal());
    entity.setAbstract(statement.isAbstract());
    entity.setName(statement.getFulledName());
    if (statement.getDocComment() != null)
        entity.setDocComment(new DocumentComment(statement.getDocComment().getComment()));
    entity.setTrace(statement.toTraceInfo(compiler.getContext()));
    entity.setType(statement.getClassType());
    List<ClassEntity> traits = fetchTraits();
    for (ClassEntity e : traits) entity.addTrait(e);
    checkAliasAndReplacementsTraits();
    if (statement.getExtend() != null) {
        ClassEntity parent = fetchClass(statement.getExtend().getName().getName());
        if (parent == null)
            compiler.getEnvironment().error(statement.getExtend().toTraceInfo(compiler.getContext()), Messages.ERR_CLASS_NOT_FOUND.fetch(statement.getExtend().getName().toName()));
        ClassEntity.ExtendsResult result = entity.setParent(parent, false);
        if (isInterfaceCheck) {
            result.check(compiler.getEnvironment());
        }
    }
    if (!isSystem) {
        if (entity.isUseJavaLikeNames()) {
            entity.setInternalName(entity.getName().replace('\\', '/'));
        } else {
            entity.setInternalName(compiler.getModule().getInternalName() + "_class" + compiler.getModule().getClasses().size());
        }
    }
    if (compiler.getModule().findClass(entity.getLowerName()) != null || compiler.getEnvironment().isLoadedClass(entity.getLowerName())) {
        throw new FatalException(Messages.ERR_CANNOT_REDECLARE_CLASS.fetch(entity.getName()), statement.getName().toTraceInfo(compiler.getContext()));
    }
    if (!statement.isInterface()) {
        node.access = ACC_SUPER + ACC_PUBLIC;
        node.name = !isSystem ? /*&& !statement.isTrait()*/
        entity.getCompiledInternalName() : statement.getFulledName(Constants.NAME_DELIMITER);
        node.superName = entity.getParent() == null ? Type.getInternalName(BaseObject.class) : entity.getParent().getInternalName();
        node.sourceFile = compiler.getSourceFile();
        /*if (!isSystem) {
                AnnotationNode annotationNode = new AnnotationNode(Type.getInternalName(Reflection.Name.class));
                annotationNode.values = Arrays.asList("value", entity.getName());

                node.visibleAnnotations.add(annotationNode);
            } */
        writeSystemInfo();
        writeConstructor();
        writeDefaultConstructors();
    }
    // constants
    if (statement.getConstants() != null)
        for (ConstStmtToken constant : statement.getConstants()) {
            writeConstant(constant);
        }
    if (statement.getMethods() != null) {
        for (MethodStmtToken method : statement.getMethods()) {
            ClassEntity.SignatureResult result = entity.addMethod(compiler.compileMethod(this, method, external, generatorEntity), null);
            result.check(compiler.getEnvironment());
        }
    }
    writeTraits(traits);
    ClassEntity.SignatureResult result = entity.updateParentMethods();
    if (isInterfaceCheck) {
        result.check(compiler.getEnvironment());
    }
    writeImplements();
    entity.doneDeclare();
    if (!statement.isInterface()) {
        writeDestructor();
        if (entity.getType() != ClassEntity.Type.INTERFACE) {
            writeInitEnvironment();
        }
        writeInitStatic();
        cw = new JPHPClassWriter(entity.isTrait());
        node.accept(cw);
        entity.setData(cw.toByteArray());
    }
    return entity;
}
Also used : JPHPClassWriter(org.develnext.jphp.core.compiler.jvm.JPHPClassWriter) FatalException(php.runtime.exceptions.FatalException) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) MethodStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)

Example 4 with ConstStmtToken

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

the class ClassDescription method parse.

@Override
protected void parse() {
    methods = new LinkedHashMap<String, MethodDescription>();
    for (MethodStmtToken el : token.getMethods()) {
        methods.put(el.getName().getName().toLowerCase(), new MethodDescription(el));
    }
    properties = new LinkedHashMap<String, PropertyDescription>();
    for (ClassVarStmtToken el : token.getProperties()) {
        properties.put(el.getVariable().getName(), new PropertyDescription(el));
    }
    constants = new LinkedHashMap<String, ConstantDescription>();
    for (ConstStmtToken el : token.getConstants()) {
        constants.put(el.items.get(0).getFulledName(), new ConstantDescription(el));
    }
    if (token.getDocComment() != null) {
        DocAnnotations annotations = new DocAnnotations(token.getDocComment().getComment());
        description = annotations.getDescription();
    }
}
Also used : ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) MethodStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken) ClassVarStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken) DocAnnotations(org.develnext.jphp.genapi.DocAnnotations)

Aggregations

ConstStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken)4 MethodStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)2 JPHPClassWriter (org.develnext.jphp.core.compiler.jvm.JPHPClassWriter)1 Token (org.develnext.jphp.core.tokenizer.token.Token)1 IntegerExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.IntegerExprToken)1 ClassVarStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken)1 DocAnnotations (org.develnext.jphp.genapi.DocAnnotations)1 Test (org.junit.Test)1 Memory (php.runtime.Memory)1 FatalException (php.runtime.exceptions.FatalException)1