forked from stan/AntlrAssignment3Template
all done
This commit is contained in:
parent
ae22e8ab82
commit
d6b4b41d3c
2
.idea/.gitignore
generated
vendored
2
.idea/.gitignore
generated
vendored
@ -6,3 +6,5 @@
|
|||||||
# Datasource local storage ignored files
|
# Datasource local storage ignored files
|
||||||
/dataSources/
|
/dataSources/
|
||||||
/dataSources.local.xml
|
/dataSources.local.xml
|
||||||
|
# GitHub Copilot persisted chat sessions
|
||||||
|
/copilot/chatSessions
|
||||||
|
BIN
.idea/copilot/chatSessions/00000000000.xd
generated
BIN
.idea/copilot/chatSessions/00000000000.xd
generated
Binary file not shown.
4
.idea/copilot/chatSessions/xd.lck
generated
4
.idea/copilot/chatSessions/xd.lck
generated
@ -1,4 +1,4 @@
|
|||||||
Private property of Exodus: 36669@Simon-Lenovo-Ubuntu
|
Private property of Exodus: 41264@Simon-Lenovo-Ubuntu
|
||||||
|
|
||||||
jetbrains.exodus.io.LockingManager.lock(LockingManager.kt:88)
|
jetbrains.exodus.io.LockingManager.lock(LockingManager.kt:88)
|
||||||
jetbrains.exodus.io.LockingManager.lock(LockingManager.kt:39)
|
jetbrains.exodus.io.LockingManager.lock(LockingManager.kt:39)
|
||||||
@ -33,7 +33,7 @@ com.intellij.util.messages.impl.MessageBusImplKt.deliverMessage(MessageBusImpl.k
|
|||||||
com.intellij.util.messages.impl.MessageBusImplKt.pumpWaiting(MessageBusImpl.kt:402)
|
com.intellij.util.messages.impl.MessageBusImplKt.pumpWaiting(MessageBusImpl.kt:402)
|
||||||
com.intellij.util.messages.impl.MessageBusImplKt.access$pumpWaiting(MessageBusImpl.kt:1)
|
com.intellij.util.messages.impl.MessageBusImplKt.access$pumpWaiting(MessageBusImpl.kt:1)
|
||||||
com.intellij.util.messages.impl.MessagePublisher.invoke(MessageBusImpl.kt:461)
|
com.intellij.util.messages.impl.MessagePublisher.invoke(MessageBusImpl.kt:461)
|
||||||
jdk.proxy7/jdk.proxy7.$Proxy158.onCopilotStatus(Unknown Source)
|
jdk.proxy6/jdk.proxy6.$Proxy166.onCopilotStatus(Unknown Source)
|
||||||
com.github.copilot.status.CopilotStatusService.notifyApplication(CopilotStatusService.java:76)
|
com.github.copilot.status.CopilotStatusService.notifyApplication(CopilotStatusService.java:76)
|
||||||
com.github.copilot.status.CopilotStatusService.notifyApplication(CopilotStatusService.java:64)
|
com.github.copilot.status.CopilotStatusService.notifyApplication(CopilotStatusService.java:64)
|
||||||
com.github.copilot.github.GitHubAuthStartupActivity.handleAuthNotifications(GitHubAuthStartupActivity.java:54)
|
com.github.copilot.github.GitHubAuthStartupActivity.handleAuthNotifications(GitHubAuthStartupActivity.java:54)
|
||||||
|
@ -1,47 +1,28 @@
|
|||||||
package de.dhbw.horb;
|
package de.dhbw.horb;
|
||||||
|
|
||||||
import de.dhbw.horb.antlr.DecafParser;
|
import de.dhbw.horb.antlr.DecafParser;
|
||||||
import de.dhbw.horb.ast.Function;
|
import de.dhbw.horb.ast.*;
|
||||||
import de.dhbw.horb.ast.Program;
|
|
||||||
import de.dhbw.horb.ast.Type;
|
|
||||||
import de.dhbw.horb.ast.Variable;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ASTGenerator {
|
public class ASTGenerator {
|
||||||
|
|
||||||
public static Program generateAST(DecafParser.ProgramContext parseTree){
|
public static Program generateAST(DecafParser.ProgramContext parseTree) {
|
||||||
List<Variable> variables = new ArrayList<>();
|
List<Variable> variables = parseTree.var().stream().map(var -> new Variable(var.id().getText(), Type.mapType(var.type().getText()))).toList();
|
||||||
for(DecafParser.VarContext varCtx : parseTree.var()){
|
List<Function> funcs = parseTree.func().stream().map(ASTGenerator::generateFunc).toList();
|
||||||
variables.add(generateVariable(varCtx));
|
|
||||||
}
|
|
||||||
List<Function> funcs = new ArrayList<>();
|
|
||||||
for(DecafParser.FuncContext fctx : parseTree.func()){
|
|
||||||
funcs.add(generateFunc(fctx));
|
|
||||||
}
|
|
||||||
return new Program(variables, funcs);
|
return new Program(variables, funcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Variable generateVariable(DecafParser.VarContext ctx) {
|
|
||||||
return new Variable(ctx.id().getText(), getType(ctx.type()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Variable generateVariable(DecafParser.ParamContext ctx) {
|
|
||||||
return new Variable(ctx.id().getText(), getType(ctx.type()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Function generateFunc(DecafParser.FuncContext ctx) {
|
public static Function generateFunc(DecafParser.FuncContext ctx) {
|
||||||
throw new RuntimeException("TODO");
|
Type t = Type.mapType(ctx.type().getText());
|
||||||
}
|
String id = ctx.id().getText();
|
||||||
|
List<Variable> params = Optional.ofNullable(ctx.params())
|
||||||
public static Type getType(DecafParser.TypeContext ctx){
|
.map(p -> p.param().stream().map(param -> new Variable(param.getText(), Type.mapType(param.type().getText()))).collect(Collectors.toList()))
|
||||||
if(ctx.INT() != null)
|
.orElseGet(ArrayList::new);
|
||||||
return Type.INT;
|
Block block = new BlockGenerator().visit(ctx.block());
|
||||||
if(ctx.BOOL() != null)
|
return new Function(t, id, params, block);
|
||||||
return Type.BOOL;
|
|
||||||
if(ctx.VOID() != null)
|
|
||||||
return Type.VOID;
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
src/main/java/de/dhbw/horb/BlockGenerator.java
Normal file
21
src/main/java/de/dhbw/horb/BlockGenerator.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package de.dhbw.horb;
|
||||||
|
|
||||||
|
import de.dhbw.horb.antlr.DecafBaseVisitor;
|
||||||
|
import de.dhbw.horb.antlr.DecafParser;
|
||||||
|
import de.dhbw.horb.ast.Block;
|
||||||
|
import de.dhbw.horb.ast.Statement;
|
||||||
|
import de.dhbw.horb.ast.Type;
|
||||||
|
import de.dhbw.horb.ast.Variable;
|
||||||
|
|
||||||
|
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 Variable(var.id().getText(), Type.mapType(var.type().getText()))).toList();
|
||||||
|
List<Statement> stmts = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).toList();
|
||||||
|
return new Block(vars, stmts);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,42 +2,47 @@ package de.dhbw.horb;
|
|||||||
|
|
||||||
import de.dhbw.horb.antlr.DecafBaseVisitor;
|
import de.dhbw.horb.antlr.DecafBaseVisitor;
|
||||||
import de.dhbw.horb.antlr.DecafParser;
|
import de.dhbw.horb.antlr.DecafParser;
|
||||||
import de.dhbw.horb.ast.Statement;
|
import de.dhbw.horb.ast.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
||||||
@Override
|
@Override
|
||||||
public Statement visitAssign(DecafParser.AssignContext ctx) {
|
public Statement visitAssign(DecafParser.AssignContext ctx) {
|
||||||
throw new RuntimeException("TODO");
|
return new Assignment(new Location(ctx.loc().getText()), new ExpressionGenerator().visit(ctx.expr()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitIf(DecafParser.IfContext ctx) {
|
public Statement visitIf(DecafParser.IfContext ctx) {
|
||||||
throw new RuntimeException("TODO");
|
Block elseBlock = new Block(new ArrayList<>(), new ArrayList<>());
|
||||||
|
if (ctx.block().size() > 1)
|
||||||
|
elseBlock = new BlockGenerator().visit(ctx.block(1));
|
||||||
|
return new IfElse(new ExpressionGenerator().visit(ctx.expr()), new BlockGenerator().visit(ctx.block(0)), elseBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitWhile(DecafParser.WhileContext ctx) {
|
public Statement visitWhile(DecafParser.WhileContext ctx) {
|
||||||
throw new RuntimeException("TODO");
|
return new While(new ExpressionGenerator().visit(ctx.expr()), new BlockGenerator().visit(ctx.block()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitReturn(DecafParser.ReturnContext ctx) {
|
public Statement visitReturn(DecafParser.ReturnContext ctx) {
|
||||||
throw new RuntimeException("TODO");
|
return new Return(new ExpressionGenerator().visit(ctx.expr()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitReturnVoid(DecafParser.ReturnVoidContext ctx) {
|
public Statement visitReturnVoid(DecafParser.ReturnVoidContext ctx) {
|
||||||
throw new RuntimeException("TODO");
|
return new ReturnVoid();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitBreak(DecafParser.BreakContext ctx) {
|
public Statement visitBreak(DecafParser.BreakContext ctx) {
|
||||||
throw new RuntimeException("TODO");
|
return new Break();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitContinue(DecafParser.ContinueContext ctx) {
|
public Statement visitContinue(DecafParser.ContinueContext ctx) {
|
||||||
throw new RuntimeException("TODO");
|
return new Continue();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,14 @@ package de.dhbw.horb.ast;
|
|||||||
public enum Type {
|
public enum Type {
|
||||||
INT,
|
INT,
|
||||||
BOOL,
|
BOOL,
|
||||||
VOID
|
VOID;
|
||||||
|
|
||||||
|
public static Type mapType(String type){
|
||||||
|
return switch (type) {
|
||||||
|
case "int" -> Type.INT;
|
||||||
|
case "bool" -> Type.BOOL;
|
||||||
|
case "void" -> Type.VOID;
|
||||||
|
default -> throw new RuntimeException("Unknown type: " + type);
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package de.dhbw.horb;
|
package de.dhbw.horb;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import de.dhbw.horb.ast.Program;
|
import de.dhbw.horb.ast.Program;
|
||||||
@ -8,14 +9,12 @@ import org.junit.Test;
|
|||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
*/
|
*/
|
||||||
public class CompilerTest
|
public class CompilerTest {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Rigorous Test :-)
|
* Rigorous Test :-)
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void generateASTTest()
|
public void generateASTTest() {
|
||||||
{
|
|
||||||
String inputString = "def int add(int x, int y)\n" +
|
String inputString = "def int add(int x, int y)\n" +
|
||||||
"{\n" +
|
"{\n" +
|
||||||
"return x + y;\n" +
|
"return x + y;\n" +
|
||||||
@ -27,6 +26,6 @@ public class CompilerTest
|
|||||||
"return add(a, 2);\n" +
|
"return add(a, 2);\n" +
|
||||||
"}";
|
"}";
|
||||||
Program ast = Compiler.generateAST(inputString);
|
Program ast = Compiler.generateAST(inputString);
|
||||||
assertTrue( ast.methods().size() == 2 );
|
assertEquals(2, ast.methods().size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import org.antlr.v4.runtime.CharStreams;
|
|||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,8 +37,8 @@ public class ExpressionGeneratorTest
|
|||||||
{
|
{
|
||||||
Expression mCall = generateExpression("m(x,y)");
|
Expression mCall = generateExpression("m(x,y)");
|
||||||
assertTrue(mCall instanceof FunctionCall);
|
assertTrue(mCall instanceof FunctionCall);
|
||||||
assertTrue(((FunctionCall) mCall).name().equals("m"));
|
assertEquals("m", ((FunctionCall) mCall).name());
|
||||||
assertTrue(((FunctionCall) mCall).args().size() == 2);
|
assertEquals(2, ((FunctionCall) mCall).args().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -8,24 +8,63 @@ import org.antlr.v4.runtime.CharStreams;
|
|||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for AST Generation of Expressions.
|
* Unit test for AST Generation of Expressions.
|
||||||
*/
|
*/
|
||||||
public class StatementGeneratorTest
|
public class StatementGeneratorTest {
|
||||||
{
|
|
||||||
@Test
|
@Test
|
||||||
public void whileTest()
|
public void whileTest() {
|
||||||
{
|
|
||||||
Statement whileStmt = generateStatement("while(true){}");
|
Statement whileStmt = generateStatement("while(true){}");
|
||||||
assertTrue(whileStmt instanceof While);
|
assertTrue(whileStmt instanceof While);
|
||||||
assertTrue( ((While) whileStmt).block().stmts().size() == 0);
|
assertEquals(0, ((While) whileStmt).block().stmts().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Statement generateStatement(String from){
|
@Test
|
||||||
String inputString = from;
|
public void whileTest2() {
|
||||||
CharStream input = CharStreams.fromString(inputString);
|
Statement whileStmt = generateStatement("while(true){int x; x= 3;}");
|
||||||
|
assertTrue(whileStmt instanceof While);
|
||||||
|
assertEquals(1, ((While) whileStmt).block().vars().size());
|
||||||
|
assertEquals(1, ((While) whileStmt).block().stmts().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifTest() {
|
||||||
|
Statement ifStmt = generateStatement("if(true){}");
|
||||||
|
assertTrue(ifStmt instanceof IfElse);
|
||||||
|
assertTrue(((IfElse) ifStmt).ifBlock().stmts().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifTest2() {
|
||||||
|
Statement ifStmt = generateStatement("if(true){bool cool; cool = true;}");
|
||||||
|
assertTrue(ifStmt instanceof IfElse);
|
||||||
|
assertEquals(1, ((IfElse) ifStmt).ifBlock().vars().size());
|
||||||
|
assertEquals(1, ((IfElse) ifStmt).ifBlock().stmts().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifElseTest() {
|
||||||
|
Statement ifElseStmt = generateStatement("if(true){}else{}");
|
||||||
|
assertTrue(ifElseStmt instanceof IfElse);
|
||||||
|
assertTrue(((IfElse) ifElseStmt).ifBlock().stmts().isEmpty());
|
||||||
|
assertTrue(((IfElse) ifElseStmt).elseBlock().stmts().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifElseTest2() {
|
||||||
|
Statement ifElseStmt = generateStatement("if(true){int x; x = 3;}else{int y; y = 4;}");
|
||||||
|
assertTrue(ifElseStmt instanceof IfElse);
|
||||||
|
assertEquals(1, ((IfElse) ifElseStmt).ifBlock().vars().size());
|
||||||
|
assertEquals(1, ((IfElse) ifElseStmt).ifBlock().stmts().size());
|
||||||
|
assertEquals(1, ((IfElse) ifElseStmt).elseBlock().vars().size());
|
||||||
|
assertEquals(1, ((IfElse) ifElseStmt).elseBlock().stmts().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Statement generateStatement(String from) {
|
||||||
|
CharStream input = CharStreams.fromString(from);
|
||||||
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user