Add mainmeth

This commit is contained in:
Boolean-True 2024-05-29 19:40:34 +02:00
parent 73102c9897
commit fd35b4583e
10 changed files with 442 additions and 434 deletions

View File

@ -1,7 +1,7 @@
grammar Decaf;
class : PUBLIC 'class' id '{' mainmeth? (field | meth | constructor)* '}';
class : PUBLIC 'class' id '{' (field | meth | mainmeth | constructor)* '}';
field : PUBLIC? type id ';';
assignSign : ASSIGN | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN | MOD_ASSIGN | DIV_ASSIGN;

View File

@ -27,11 +27,18 @@ public class ASTGenerator {
else {
constructors.add(new Constructor(ctx.id().IDENTIFIER().getText(), List.of(), new Block(List.of())));
}
Block mainmeth = null;
if (ctx.mainmeth() != null) {
if (ctx.mainmeth().size() > 1) {
throw new RuntimeException("Only one main method allowed!");
}
mainmeth = new BlockGenerator().visit(ctx.mainmeth().get(0).block());
}
List<Method> meths = new ArrayList<>();
if (ctx.meth() != null) {
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
}
return new Class(ctx.id().IDENTIFIER().getText(), declarations, meths, constructors);
return new Class(ctx.id().IDENTIFIER().getText(), mainmeth, declarations, meths, constructors);
}
public static Declaration generateFieldVariable(DecafParser.FieldContext ctx) {

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Uni/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.ParserRuleContext;

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Uni/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Uni/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Uni/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.tree.ParseTreeListener;

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Uni/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;

View File

@ -2,5 +2,8 @@ package de.maishai.ast.records;
import java.util.List;
public record Class(String classname , List<Declaration> fieldDeclarations, List<Method> methods, List<Constructor> constructors) implements Node {
public record Class(String classname, Block mainmeth, List<Declaration> fieldDeclarations, List<Method> methods, List<Constructor> constructors) implements Node {
public Class(String classname, List<Declaration> fieldDeclarations, List<Method> methods, List<Constructor> constructors) {
this(classname, null, fieldDeclarations, methods, constructors);
}
}