mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-29 01:08:02 +00:00
Merge branch 'main' of https://github.com/JonathanFleischmann/CompilerULTIMATE
This commit is contained in:
commit
1a73384825
@ -20,7 +20,7 @@ public class ASTGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Class generateClass(DecafParser.ClassContext ctx) {
|
public static Class generateClass(DecafParser.ClassContext ctx) {
|
||||||
List<Variable> vars = new ArrayList<>();
|
List<LocalVariable> vars = new ArrayList<>();
|
||||||
if(ctx.var() != null){
|
if(ctx.var() != null){
|
||||||
vars = ctx.var().stream().map(ASTGenerator::generateVariable).toList();
|
vars = ctx.var().stream().map(ASTGenerator::generateVariable).toList();
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ public class ASTGenerator {
|
|||||||
return new Class(classId, vars, meths);
|
return new Class(classId, vars, meths);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Variable generateVariable(DecafParser.VarContext ctx) {
|
public static LocalVariable generateVariable(DecafParser.VarContext ctx) {
|
||||||
return new VariableGenerator().visitVar(ctx);
|
return new VariableGenerator().visitVar(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,14 +4,14 @@ import de.maishai.antlr.DecafBaseVisitor;
|
|||||||
import de.maishai.antlr.DecafParser;
|
import de.maishai.antlr.DecafParser;
|
||||||
import de.maishai.ast.records.Statement;
|
import de.maishai.ast.records.Statement;
|
||||||
import de.maishai.ast.records.Block;
|
import de.maishai.ast.records.Block;
|
||||||
import de.maishai.ast.records.Variable;
|
import de.maishai.ast.records.LocalVariable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class BlockGenerator extends DecafBaseVisitor<Block> {
|
public class BlockGenerator extends DecafBaseVisitor<Block> {
|
||||||
@Override
|
@Override
|
||||||
public Block visitBlock(DecafParser.BlockContext ctx) {
|
public Block visitBlock(DecafParser.BlockContext ctx) {
|
||||||
List<Variable> vars = ctx.var().stream().map(var -> new VariableGenerator().visit(var)).toList();
|
List<LocalVariable> vars = ctx.var().stream().map(var -> new VariableGenerator().visit(var)).toList();
|
||||||
List<Statement> statements = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).toList();
|
List<Statement> statements = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).toList();
|
||||||
return new Block(vars, statements);
|
return new Block(vars, statements);
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,16 @@ import de.maishai.antlr.DecafBaseVisitor;
|
|||||||
import de.maishai.antlr.DecafParser;
|
import de.maishai.antlr.DecafParser;
|
||||||
import de.maishai.ast.Type;
|
import de.maishai.ast.Type;
|
||||||
import de.maishai.ast.records.Id;
|
import de.maishai.ast.records.Id;
|
||||||
import de.maishai.ast.records.Variable;
|
import de.maishai.ast.records.LocalVariable;
|
||||||
|
|
||||||
public class VariableGenerator extends DecafBaseVisitor<Variable> {
|
public class VariableGenerator extends DecafBaseVisitor<LocalVariable> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Variable visitVar(DecafParser.VarContext ctx) {
|
public LocalVariable visitVar(DecafParser.VarContext ctx) {
|
||||||
Id id = new Id(ctx.id().getText());
|
Id id = new Id(ctx.id().getText());
|
||||||
Type type = ASTGenerator.getType(ctx.type());
|
Type type = ASTGenerator.getType(ctx.type());
|
||||||
if(ctx.expr() != null)
|
if(ctx.expr() != null)
|
||||||
return new Variable(id, type, new ExpressionGenerator().visit(ctx.expr()));
|
return new LocalVariable(id, type, new ExpressionGenerator().visit(ctx.expr()));
|
||||||
return new Variable(id, type, null);
|
return new LocalVariable(id, type, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,5 @@ package de.maishai.ast.records;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public record Block(List<Variable> vars, List<Statement> stmts) implements Node {
|
public record Block(List<LocalVariable> vars, List<Statement> stmts) implements Node {
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,5 @@ package de.maishai.ast.records;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public record Class(Id id ,List<Variable> variables, List<Method> methods) implements Node {
|
public record Class(Id id , List<LocalVariable> variables, List<Method> methods) implements Node {
|
||||||
}
|
}
|
||||||
|
6
src/main/java/de/maishai/ast/records/Field.java
Normal file
6
src/main/java/de/maishai/ast/records/Field.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package de.maishai.ast.records;
|
||||||
|
|
||||||
|
import de.maishai.ast.Type;
|
||||||
|
|
||||||
|
public record Field(Id name, Type type) {
|
||||||
|
}
|
7
src/main/java/de/maishai/ast/records/LocalVariable.java
Normal file
7
src/main/java/de/maishai/ast/records/LocalVariable.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package de.maishai.ast.records;
|
||||||
|
|
||||||
|
|
||||||
|
import de.maishai.ast.Type;
|
||||||
|
|
||||||
|
public record LocalVariable(Id name, Type type) implements Node {
|
||||||
|
}
|
@ -3,5 +3,5 @@ package de.maishai.ast.records;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public record Program(List<Class> variables) implements Node {
|
public record Program(List<Class> classes) implements Node {
|
||||||
}
|
}
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package de.maishai.ast.records;
|
|
||||||
|
|
||||||
|
|
||||||
import de.maishai.ast.Type;
|
|
||||||
|
|
||||||
public record Variable(Id name, Type type, Expression expr) implements Node {
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user