mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 01:38:03 +00:00
no programm, forced public in classes and method
This commit is contained in:
parent
b61af71076
commit
25d539cf49
@ -1,8 +1,7 @@
|
||||
grammar Decaf;
|
||||
|
||||
program : (class)+;
|
||||
|
||||
class : PUBLIC? 'class' id '{' mainmeth? (field | meth | constructor)* '}';
|
||||
class : PUBLIC 'class' id '{' mainmeth? (field | meth | constructor)* '}';
|
||||
|
||||
field : type id ';';
|
||||
localVar : type id ';';
|
||||
@ -10,7 +9,7 @@ assignSign : ASSIGN | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN;
|
||||
returntype : type | VOID;
|
||||
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;
|
||||
constructor: PUBLIC? id '(' params? ')' block;
|
||||
params : param (',' param)*;
|
||||
|
@ -11,16 +11,8 @@ import java.util.List;
|
||||
|
||||
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) {
|
||||
Boolean isPublic = ctx.PUBLIC() != null;
|
||||
public static Class generateAST(DecafParser.ClassContext ctx) {
|
||||
List<Field> fields = new ArrayList<>();
|
||||
if (ctx.field() != null) {
|
||||
fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList();
|
||||
@ -38,7 +30,7 @@ public class ASTGenerator {
|
||||
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
|
||||
}
|
||||
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) {
|
||||
@ -52,14 +44,13 @@ public class ASTGenerator {
|
||||
}
|
||||
|
||||
public static Method generateMethod(DecafParser.MethContext ctx) {
|
||||
Boolean isPublic = ctx.PUBLIC() != null;
|
||||
List<Parameter> params = new ArrayList<>();
|
||||
if (ctx.params() != null) {
|
||||
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
|
||||
}
|
||||
Block block = new BlockGenerator().visit(ctx.block());
|
||||
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) {
|
||||
|
@ -2,7 +2,7 @@ package de.maishai;
|
||||
|
||||
import de.maishai.antlr.DecafLexer;
|
||||
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.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
@ -12,13 +12,20 @@ import org.antlr.v4.runtime.CommonTokenStream;
|
||||
*/
|
||||
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);
|
||||
DecafLexer lexer = new DecafLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
DecafParser parser = new DecafParser(tokens);
|
||||
DecafParser.ProgramContext tree = parser.program(); //Parsen
|
||||
return ASTGenerator.generateAST(tree);
|
||||
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
||||
Class ast = ASTGenerator.generateAST(tree);
|
||||
return ast;
|
||||
}
|
||||
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@ -12,18 +12,6 @@ import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
*/
|
||||
@SuppressWarnings("CheckReturnValue")
|
||||
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}
|
||||
*
|
||||
|
@ -12,13 +12,6 @@ import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
||||
*/
|
||||
@SuppressWarnings("CheckReturnValue")
|
||||
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}
|
||||
*
|
||||
|
@ -7,16 +7,6 @@ import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||
* {@link DecafParser}.
|
||||
*/
|
||||
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}.
|
||||
* @param ctx the parse tree
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -10,12 +10,6 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
* operations with no return type.
|
||||
*/
|
||||
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}.
|
||||
* @param ctx the parse tree
|
||||
|
@ -2,5 +2,5 @@ package de.maishai.ast.records;
|
||||
|
||||
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 {
|
||||
}
|
||||
|
@ -6,5 +6,5 @@ import de.maishai.ast.ReturnType;
|
||||
|
||||
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 {
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
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 {
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package de.maishai.ast.records;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record Program(List<Class> classes) implements Node {
|
||||
}
|
Loading…
Reference in New Issue
Block a user