no programm, forced public in classes and method

This commit is contained in:
laurenz 2024-05-08 10:08:28 +02:00
parent b61af71076
commit 25d539cf49
13 changed files with 411 additions and 542 deletions

View File

@ -1,8 +1,7 @@
grammar Decaf; grammar Decaf;
program : (class)+;
class : PUBLIC? 'class' id '{' mainmeth? (field | meth | constructor)* '}'; class : PUBLIC 'class' id '{' mainmeth? (field | meth | constructor)* '}';
field : type id ';'; field : type id ';';
localVar : type id ';'; localVar : type id ';';
@ -10,7 +9,7 @@ assignSign : ASSIGN | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN;
returntype : type | VOID; returntype : type | VOID;
type : INT | BOOL | CHAR | id; type : INT | BOOL | CHAR | id;
meth : PUBLIC? returntype id '(' params? ')' block; meth : PUBLIC returntype id '(' params? ')' block;
mainmeth : PUBLIC 'static' 'void' 'main' '(' 'String[] args' ')' block; mainmeth : PUBLIC 'static' 'void' 'main' '(' 'String[] args' ')' block;
constructor: PUBLIC? id '(' params? ')' block; constructor: PUBLIC? id '(' params? ')' block;
params : param (',' param)*; params : param (',' param)*;

View File

@ -11,16 +11,8 @@ import java.util.List;
public class ASTGenerator { public class ASTGenerator {
public static Program generateAST(DecafParser.ProgramContext parseTree) {
List<Class> classes = new ArrayList<>();
for (DecafParser.ClassContext cctx : parseTree.class_()) {
classes.add(generateClass(cctx));
}
return new Program(classes);
}
public static Class generateClass(DecafParser.ClassContext ctx) { public static Class generateAST(DecafParser.ClassContext ctx) {
Boolean isPublic = ctx.PUBLIC() != null;
List<Field> fields = new ArrayList<>(); List<Field> fields = new ArrayList<>();
if (ctx.field() != null) { if (ctx.field() != null) {
fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList(); fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList();
@ -38,7 +30,7 @@ public class ASTGenerator {
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList(); meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
} }
Id classId = new Id(ctx.id().getText()); Id classId = new Id(ctx.id().getText());
return new Class(isPublic, classId, fields, meths, mainMethod, constructors); return new Class(classId, fields, meths, mainMethod, constructors);
} }
public static Field generateFieldVariable(DecafParser.FieldContext ctx) { public static Field generateFieldVariable(DecafParser.FieldContext ctx) {
@ -52,14 +44,13 @@ public class ASTGenerator {
} }
public static Method generateMethod(DecafParser.MethContext ctx) { public static Method generateMethod(DecafParser.MethContext ctx) {
Boolean isPublic = ctx.PUBLIC() != null;
List<Parameter> params = new ArrayList<>(); List<Parameter> params = new ArrayList<>();
if (ctx.params() != null) { if (ctx.params() != null) {
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList(); params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
} }
Block block = new BlockGenerator().visit(ctx.block()); Block block = new BlockGenerator().visit(ctx.block());
Id methId = new Id(ctx.id().getText()); Id methId = new Id(ctx.id().getText());
return new Method(isPublic, getReturnType(ctx.returntype()), methId, params, block); return new Method(getReturnType(ctx.returntype()), methId, params, block);
} }
public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) { public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) {

View File

@ -2,7 +2,7 @@ package de.maishai;
import de.maishai.antlr.DecafLexer; import de.maishai.antlr.DecafLexer;
import de.maishai.antlr.DecafParser; import de.maishai.antlr.DecafParser;
import de.maishai.ast.records.Program; import de.maishai.ast.records.Class;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.CommonTokenStream;
@ -12,13 +12,20 @@ import org.antlr.v4.runtime.CommonTokenStream;
*/ */
public class Compiler { public class Compiler {
public static Program generateAST(String fromSource) { public static void main(String[] args) {
generateAST("""
class Test {
}""");
}
public static Class generateAST(String fromSource) {
CharStream input = CharStreams.fromString(fromSource); CharStream input = CharStreams.fromString(fromSource);
DecafLexer lexer = new DecafLexer(input); DecafLexer lexer = new DecafLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
DecafParser parser = new DecafParser(tokens); DecafParser parser = new DecafParser(tokens);
DecafParser.ProgramContext tree = parser.program(); //Parsen DecafParser.ClassContext tree = parser.class_(); //Parsen
return ASTGenerator.generateAST(tree); Class ast = ASTGenerator.generateAST(tree);
return ast;
} }

File diff suppressed because one or more lines are too long

View File

@ -12,18 +12,6 @@ import org.antlr.v4.runtime.tree.TerminalNode;
*/ */
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public class DecafBaseListener implements DecafListener { public class DecafBaseListener implements DecafListener {
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterProgram(DecafParser.ProgramContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitProgram(DecafParser.ProgramContext ctx) { }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

View File

@ -12,13 +12,6 @@ import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
*/ */
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public class DecafBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements DecafVisitor<T> { public class DecafBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements DecafVisitor<T> {
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitProgram(DecafParser.ProgramContext ctx) { return visitChildren(ctx); }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

View File

@ -7,16 +7,6 @@ import org.antlr.v4.runtime.tree.ParseTreeListener;
* {@link DecafParser}. * {@link DecafParser}.
*/ */
public interface DecafListener extends ParseTreeListener { public interface DecafListener extends ParseTreeListener {
/**
* Enter a parse tree produced by {@link DecafParser#program}.
* @param ctx the parse tree
*/
void enterProgram(DecafParser.ProgramContext ctx);
/**
* Exit a parse tree produced by {@link DecafParser#program}.
* @param ctx the parse tree
*/
void exitProgram(DecafParser.ProgramContext ctx);
/** /**
* Enter a parse tree produced by {@link DecafParser#class}. * Enter a parse tree produced by {@link DecafParser#class}.
* @param ctx the parse tree * @param ctx the parse tree

File diff suppressed because it is too large Load Diff

View File

@ -10,12 +10,6 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor;
* operations with no return type. * operations with no return type.
*/ */
public interface DecafVisitor<T> extends ParseTreeVisitor<T> { public interface DecafVisitor<T> extends ParseTreeVisitor<T> {
/**
* Visit a parse tree produced by {@link DecafParser#program}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitProgram(DecafParser.ProgramContext ctx);
/** /**
* Visit a parse tree produced by {@link DecafParser#class}. * Visit a parse tree produced by {@link DecafParser#class}.
* @param ctx the parse tree * @param ctx the parse tree

View File

@ -2,5 +2,5 @@ package de.maishai.ast.records;
import java.util.List; import java.util.List;
public record Class(Boolean isPublic, Id id , List<Field> fields, List<Method> methods, MainMethod mainMethod, List<Constructor> constructors) implements Node { public record Class(Id id , List<Field> fields, List<Method> methods, MainMethod mainMethod, List<Constructor> constructors) implements Node {
} }

View File

@ -6,5 +6,5 @@ import de.maishai.ast.ReturnType;
import java.util.List; import java.util.List;
public record Method(Boolean isPublic, ReturnType type, Id id, List<Parameter> params, Block block) implements Node { public record Method(ReturnType type, Id id, List<Parameter> params, Block block) implements Node {
} }

View File

@ -1,4 +1,4 @@
package de.maishai.ast.records; package de.maishai.ast.records;
public sealed interface Node permits Block, Class, Constructor, Expression, Field, LocalVariable, MainMethod, Method, Parameter, Program, Statement { public sealed interface Node permits Block, Class, Constructor, Expression, Field, LocalVariable, MainMethod, Method, Parameter, Statement {
} }

View File

@ -1,7 +0,0 @@
package de.maishai.ast.records;
import java.util.List;
public record Program(List<Class> classes) implements Node {
}