Search in sources :

Example 1 with StatementList

use of dyvilx.tools.compiler.ast.statement.StatementList in project Dyvil by Dyvil.

the class VarDirectiveParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case KEYWORD:
            AttributeList attributes = new AttributeList();
            switch(type) {
                case GenSrcSymbols.VAR:
                    break;
                case GenSrcSymbols.CONST:
                case GenSrcSymbols.LET:
                    attributes.addFlag(Modifiers.FINAL);
                    break;
                default:
                    pm.report(token, "var.declarator");
                    return;
            }
            this.mode = OPEN_PAREN;
            return;
        case OPEN_PAREN:
            if (type != BaseSymbols.OPEN_PARENTHESIS) {
                this.directives.add(this.varDirective);
                pm.popParser(true);
                return;
            }
            pm.pushParser(new DataMemberParser<>(this).withFlags(DataMemberParser.PARSE_VALUE));
            this.mode = CLOSE_PAREN;
            return;
        case CLOSE_PAREN:
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "var.close_paren");
                return;
            }
            this.mode = BODY;
            return;
        case BODY:
            if (type != BaseSymbols.OPEN_CURLY_BRACKET) {
                this.directives.add(this.varDirective);
                pm.popParser(true);
                return;
            }
            final StatementList body = new StatementList();
            pm.pushParser(new BlockParser(body));
            this.varDirective.setBlock(body);
            this.mode = BODY_END;
            return;
        case BODY_END:
            assert type == BaseSymbols.CLOSE_CURLY_BRACKET;
            this.directives.add(this.varDirective);
            pm.popParser();
    }
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) DataMemberParser(dyvilx.tools.compiler.parser.classes.DataMemberParser) StatementList(dyvilx.tools.compiler.ast.statement.StatementList)

Example 2 with StatementList

use of dyvilx.tools.compiler.ast.statement.StatementList in project Dyvil by Dyvil.

the class Template method parse.

@Override
public void parse() {
    // class NAME { }
    final CodeClass theClass = new CodeClass(null, this.name, AttributeList.of(Modifiers.PUBLIC));
    final ClassBody classBody = new ClassBody(theClass);
    theClass.setBody(classBody);
    this.addClass(theClass);
    this.templateClass = theClass;
    // func generate(spec, writer) -> void = { ... }
    final CodeMethod genMethod = new CodeMethod(theClass, Name.fromRaw("generate"), Types.VOID, AttributeList.of(Modifiers.PUBLIC | Modifiers.OVERRIDE));
    final StatementList directives = new StatementList();
    genMethod.setValue(directives);
    classBody.addMethod(genMethod);
    this.genMethod = genMethod;
    // func main(args) -> void
    final CodeMethod mainMethod = new CodeMethod(theClass, Name.fromRaw("main"), Types.VOID, AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC));
    classBody.addMethod(mainMethod);
    this.mainMethod = mainMethod;
    // Parse
    new ParserManager(DyvilSymbols.INSTANCE, this.tokens.iterator(), this.markers).parse(new BlockParser(this, directives));
}
Also used : ParserManager(dyvilx.tools.parsing.ParserManager) BlockParser(dyvilx.tools.gensrc.parser.BlockParser) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) CodeClass(dyvilx.tools.compiler.ast.classes.CodeClass) ClassBody(dyvilx.tools.compiler.ast.classes.ClassBody)

Example 3 with StatementList

use of dyvilx.tools.compiler.ast.statement.StatementList in project Dyvil by Dyvil.

the class Template method toString.

@Override
public void toString(@NonNull String indent, @NonNull StringBuilder buffer) {
    for (int i = 0; i < this.importCount; i++) {
        buffer.append(indent);
        appendImport(indent, buffer, this.importDeclarations[i]);
        buffer.append('\n');
    }
    final IValue directives = this.genMethod.getValue();
    if (!(directives instanceof StatementList)) {
        directives.toString(indent, buffer);
        return;
    }
    final StatementList statements = (StatementList) directives;
    for (int i = 0, count = statements.size(); i < count; i++) {
        statements.get(i).toString(indent, buffer);
        buffer.append('\n');
    }
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) StatementList(dyvilx.tools.compiler.ast.statement.StatementList)

Example 4 with StatementList

use of dyvilx.tools.compiler.ast.statement.StatementList in project Dyvil by Dyvil.

the class CallDirectiveParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case NAME:
            if (Tokens.isIdentifier(type)) {
                this.mode = OPEN_PAREN;
                this.directive = new CallDirective(token.raw(), token.nameValue());
                return;
            }
            pm.report(token, "directive.identifier");
            return;
        case OPEN_PAREN:
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                pm.pushParser(new ArgumentListParser(this.directive));
                this.mode = CLOSE_PAREN;
                return;
            }
        // Fallthrough
        case BODY:
            if (type == BaseSymbols.OPEN_CURLY_BRACKET) {
                final StatementList closure = new StatementList();
                pm.pushParser(new BlockParser(closure));
                this.directive.setBlock(closure);
                this.mode = BODY_END;
                return;
            }
            this.list.add(this.directive);
            pm.popParser(true);
            return;
        case CLOSE_PAREN:
            if (type == BaseSymbols.CLOSE_PARENTHESIS) {
                this.mode = BODY;
                return;
            }
            pm.report(token, "directive.close_paren");
            return;
        case BODY_END:
            assert type == BaseSymbols.CLOSE_CURLY_BRACKET;
            this.list.add(this.directive);
            pm.popParser();
    }
}
Also used : CallDirective(dyvilx.tools.gensrc.ast.directive.CallDirective) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) ArgumentListParser(dyvilx.tools.compiler.parser.expression.ArgumentListParser)

Example 5 with StatementList

use of dyvilx.tools.compiler.ast.statement.StatementList in project Dyvil by Dyvil.

the class FuncDirectiveParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case KEYWORD:
            if (type != GenSrcSymbols.FUNC) {
                pm.report(token, "func.declarator");
                return;
            }
            this.mode = OPEN_PAREN;
            return;
        case OPEN_PAREN:
            if (type != BaseSymbols.OPEN_PARENTHESIS) {
                this.directives.add(this.funcDirective);
                pm.popParser(true);
                return;
            }
            pm.pushParser(new MethodParser(this));
            this.mode = CLOSE_PAREN;
            return;
        case CLOSE_PAREN:
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "func.close_paren");
                return;
            }
            this.mode = BODY;
            return;
        case BODY:
            if (type != BaseSymbols.OPEN_CURLY_BRACKET) {
                this.directives.add(this.funcDirective);
                pm.popParser(true);
                return;
            }
            final StatementList body = new StatementList();
            pm.pushParser(new BlockParser(body));
            this.funcDirective.setBlock(body);
            this.mode = BODY_END;
            return;
        case BODY_END:
            assert type == BaseSymbols.CLOSE_CURLY_BRACKET;
            this.directives.add(this.funcDirective);
            pm.popParser();
    }
}
Also used : StatementList(dyvilx.tools.compiler.ast.statement.StatementList) MethodParser(dyvilx.tools.compiler.parser.classes.MethodParser)

Aggregations

StatementList (dyvilx.tools.compiler.ast.statement.StatementList)12 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)4 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)3 Variable (dyvilx.tools.compiler.ast.field.Variable)3 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)2 IValue (dyvilx.tools.compiler.ast.expression.IValue)2 VariableStatement (dyvilx.tools.compiler.ast.statement.VariableStatement)2 SourcePosition (dyvil.source.position.SourcePosition)1 ClassBody (dyvilx.tools.compiler.ast.classes.ClassBody)1 CodeClass (dyvilx.tools.compiler.ast.classes.CodeClass)1 CodeConstructor (dyvilx.tools.compiler.ast.constructor.CodeConstructor)1 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)1 ThisExpr (dyvilx.tools.compiler.ast.expression.ThisExpr)1 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1 FieldAssignment (dyvilx.tools.compiler.ast.expression.access.FieldAssignment)1 InitializerCall (dyvilx.tools.compiler.ast.expression.access.InitializerCall)1 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)1 IVariable (dyvilx.tools.compiler.ast.field.IVariable)1 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1