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();
}
}
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));
}
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');
}
}
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();
}
}
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();
}
}
Aggregations