mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 09:48:03 +00:00
wip update records
This commit is contained in:
parent
e14af08678
commit
70ae37d4c9
@ -20,7 +20,7 @@ public class ASTGenerator {
|
||||
}
|
||||
|
||||
public static Class generateClass(DecafParser.ClassContext ctx) {
|
||||
List<Variable> vars = new ArrayList<>();
|
||||
List<LocalVariable> vars = new ArrayList<>();
|
||||
if(ctx.var() != null){
|
||||
vars = ctx.var().stream().map(ASTGenerator::generateVariable).toList();
|
||||
}
|
||||
@ -32,7 +32,7 @@ public class ASTGenerator {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -4,14 +4,14 @@ import de.maishai.antlr.DecafBaseVisitor;
|
||||
import de.maishai.antlr.DecafParser;
|
||||
import de.maishai.ast.Statement;
|
||||
import de.maishai.ast.records.Block;
|
||||
import de.maishai.ast.records.Variable;
|
||||
import de.maishai.ast.records.LocalVariable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BlockGenerator extends DecafBaseVisitor<Block> {
|
||||
@Override
|
||||
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();
|
||||
return new Block(vars, statements);
|
||||
}
|
||||
|
@ -5,16 +5,16 @@ import de.maishai.antlr.DecafBaseVisitor;
|
||||
import de.maishai.antlr.DecafParser;
|
||||
import de.maishai.ast.Type;
|
||||
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
|
||||
public Variable visitVar(DecafParser.VarContext ctx) {
|
||||
public LocalVariable visitVar(DecafParser.VarContext ctx) {
|
||||
Id id = new Id(ctx.id().getText());
|
||||
Type type = ASTGenerator.getType(ctx.type());
|
||||
if(ctx.expr() != null)
|
||||
return new Variable(id, type, new ExpressionGenerator().visit(ctx.expr()));
|
||||
return new Variable(id, type, null);
|
||||
return new LocalVariable(id, type, new ExpressionGenerator().visit(ctx.expr()));
|
||||
return new LocalVariable(id, type, null);
|
||||
}
|
||||
}
|
||||
|
@ -7,5 +7,5 @@ import de.maishai.ast.Statement;
|
||||
|
||||
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 {
|
||||
}
|
||||
|
@ -4,5 +4,5 @@ import de.maishai.ast.Node;
|
||||
|
||||
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) {
|
||||
}
|
@ -5,5 +5,5 @@ import de.maishai.ast.Expression;
|
||||
import de.maishai.ast.Node;
|
||||
import de.maishai.ast.Type;
|
||||
|
||||
public record Variable(Id name, Type type, Expression expr) implements Node {
|
||||
public record LocalVariable(Id name, Type type) implements Node {
|
||||
}
|
@ -5,5 +5,5 @@ import de.maishai.ast.Node;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record Program(List<Class> variables) implements Node {
|
||||
public record Program(List<Class> classes) implements Node {
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user