Add java code and test cases
This commit is contained in:
parent
d5f1b9a23a
commit
c00decd11b
23
compiler/pom.xml
Normal file
23
compiler/pom.xml
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>antlr-compiler</artifactId>
|
||||
<groupId>org.example</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>compiler</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>parser</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,15 @@
|
||||
package de.letsbuildacompiler.compiler;
|
||||
|
||||
public enum DataType {
|
||||
INT("I"), STRING("Ljava/lang/String;");
|
||||
|
||||
public final String jvmType;
|
||||
|
||||
DataType(String jvmType) {
|
||||
this.jvmType = jvmType;
|
||||
}
|
||||
|
||||
public String getJvmType() {
|
||||
return jvmType;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package de.letsbuildacompiler.compiler;
|
||||
|
||||
import de.letsbuildacompiler.compiler.exceptions.FunctionAlreadyDefinedException;
|
||||
import de.letsbuildacompiler.parser.DemoBaseVisitor;
|
||||
import de.letsbuildacompiler.parser.DemoParser;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class FunctionDefinitionFinder {
|
||||
|
||||
public static FunctionList findFunctions(ParseTree tree) {
|
||||
final FunctionList definedFunctions = new FunctionList();
|
||||
|
||||
new DemoBaseVisitor<Void>() {
|
||||
@Override
|
||||
public Void visitFunctionDefinition(DemoParser.FunctionDefinitionContext ctx) {
|
||||
String functionName = ctx.funcName.getText();
|
||||
int numberOfArguments = ctx.params.declarations.size();
|
||||
if (definedFunctions.contains(functionName, numberOfArguments)) {
|
||||
throw new FunctionAlreadyDefinedException(ctx.funcName);
|
||||
}
|
||||
definedFunctions.add(functionName, numberOfArguments);
|
||||
return null;
|
||||
}
|
||||
}.visit(tree);
|
||||
|
||||
return definedFunctions;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package de.letsbuildacompiler.compiler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class FunctionList {
|
||||
|
||||
private Collection<FunctionDefinition> definitions;
|
||||
|
||||
FunctionList() {
|
||||
this.definitions = new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean contains(String functionName, int parameterCount) {
|
||||
for (FunctionDefinition definition : definitions) {
|
||||
if (definition.functionName.equals(functionName) && definition.parameterCount == parameterCount) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void add (String functionName, int parameterCount) {
|
||||
definitions.add(new FunctionDefinition(functionName, parameterCount));
|
||||
}
|
||||
|
||||
private static final class FunctionDefinition {
|
||||
private final String functionName;
|
||||
private final int parameterCount;
|
||||
|
||||
private FunctionDefinition(String functionName, int parameterCount) {
|
||||
this.functionName = functionName;
|
||||
this.parameterCount = parameterCount;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,304 @@
|
||||
package de.letsbuildacompiler.compiler;
|
||||
|
||||
import de.letsbuildacompiler.compiler.exceptions.UndeclaredVariableException;
|
||||
import de.letsbuildacompiler.compiler.exceptions.UndefinedFunctionException;
|
||||
import de.letsbuildacompiler.compiler.exceptions.VariableAlreadyDefinedException;
|
||||
import de.letsbuildacompiler.parser.DemoBaseVisitor;
|
||||
import de.letsbuildacompiler.parser.DemoParser;
|
||||
import de.letsbuildacompiler.parser.DemoParser.MainStatementContext;
|
||||
import de.letsbuildacompiler.parser.DemoParser.NumberContext;
|
||||
import de.letsbuildacompiler.parser.DemoParser.PlusContext;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JasminVisitor extends DemoBaseVisitor<String> {
|
||||
|
||||
// Symbol table
|
||||
private Map<String, Integer> variables = new HashMap<>();
|
||||
private JvmStack jvmStack = new JvmStack();
|
||||
private final FunctionList definedFunctions;
|
||||
private int branchCounter = 0;
|
||||
private int compareCount = 0;
|
||||
private int andCounter = 0;
|
||||
private int orCounter = 0;
|
||||
|
||||
JasminVisitor(FunctionList definedFunctions) {
|
||||
if (definedFunctions == null) {
|
||||
throw new NullPointerException("definedFunctions");
|
||||
}
|
||||
this.definedFunctions = definedFunctions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitNumber(NumberContext ctx) {
|
||||
jvmStack.push(DataType.INT);
|
||||
return "ldc " + ctx.number.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitString(DemoParser.StringContext ctx) {
|
||||
jvmStack.push(DataType.STRING);
|
||||
return "ldc " + ctx.txt.getText();
|
||||
}
|
||||
|
||||
private String visitBinaryOperation(String name, DemoParser.ExpressionContext ctx) {
|
||||
String instructions = visitChildren(ctx) + "\n" + name;
|
||||
jvmStack.pop();
|
||||
jvmStack.pop();
|
||||
jvmStack.push(DataType.INT);
|
||||
return instructions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPlus(PlusContext ctx) {
|
||||
return visitBinaryOperation("iadd", ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitMinus(DemoParser.MinusContext ctx) {
|
||||
return visitBinaryOperation("isub", ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitDiv(DemoParser.DivContext ctx) {
|
||||
return visitBinaryOperation("idiv", ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitMult(DemoParser.MultContext ctx) {
|
||||
return visitBinaryOperation("imul", ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitRelational(DemoParser.RelationalContext ctx) {
|
||||
int compareNum = compareCount++;
|
||||
String jumpInstruction;
|
||||
|
||||
switch (ctx.operator.getText()) {
|
||||
case "<":
|
||||
jumpInstruction = "if_icmplt";
|
||||
break;
|
||||
case "<=":
|
||||
jumpInstruction = "if_icmple";
|
||||
break;
|
||||
case ">":
|
||||
jumpInstruction = "if_icmpgt";
|
||||
break;
|
||||
case ">=":
|
||||
jumpInstruction = "if_icmpge";
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown operator : <" + ctx.operator.getText() + ">");
|
||||
}
|
||||
|
||||
final String instructions = visitChildren(ctx) + "\n" +
|
||||
jumpInstruction + " onTrue" + compareNum + "\n" +
|
||||
"ldc 0\n" +
|
||||
"goto onFalse" + compareNum + "\n" +
|
||||
"onTrue" + compareNum + ":\n" +
|
||||
"ldc 1\n" +
|
||||
"onFalse" + compareNum + ":";
|
||||
jvmStack.pop();
|
||||
jvmStack.pop();
|
||||
jvmStack.push(DataType.INT);
|
||||
return instructions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitAnd(DemoParser.AndContext ctx) {
|
||||
String left = visit(ctx.left);
|
||||
String right = visit(ctx.right);
|
||||
int andNum = andCounter++;
|
||||
jvmStack.pop();
|
||||
jvmStack.pop();
|
||||
jvmStack.push(DataType.INT);
|
||||
|
||||
return left + "\n" +
|
||||
"ifeq onAndFalse" + andNum + "\n" +
|
||||
right + "\n" +
|
||||
"ifeq onAndFalse" + andNum + "\n" +
|
||||
"ldc 1\n" +
|
||||
"goto andEnd" + andNum + "\n" +
|
||||
"onAndFalse" + andNum + ":\n" +
|
||||
"ldc 0\n" +
|
||||
"andEnd" + andNum + ":";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitOr(DemoParser.OrContext ctx) {
|
||||
String left = visit(ctx.left);
|
||||
String right = visit(ctx.right);
|
||||
int orNum = orCounter++;
|
||||
jvmStack.pop();
|
||||
jvmStack.pop();
|
||||
jvmStack.push(DataType.INT);
|
||||
|
||||
return left + "\n" +
|
||||
"ifne onOrTrue" + orNum + "\n" +
|
||||
right + "\n" +
|
||||
"ifne onOrTrue" + orNum + "\n" +
|
||||
"ldc 0\n" +
|
||||
"goto orEnd" + orNum + "\n" +
|
||||
"onOrTrue" + orNum + ":\n" +
|
||||
"ldc 1\n" +
|
||||
"orEnd" + orNum + ":";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPrintln(DemoParser.PrintlnContext ctx) {
|
||||
String arg = visit(ctx.argument);
|
||||
DataType type = jvmStack.pop();
|
||||
return "getstatic java/lang/System/out Ljava/io/PrintStream;\n" +
|
||||
arg + "\n" +
|
||||
"invokevirtual java/io/PrintStream/println(" + type.getJvmType() + ")V\n";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPrint(DemoParser.PrintContext ctx) {
|
||||
String arg = visit(ctx.argument);
|
||||
DataType type = jvmStack.pop();
|
||||
return "getstatic java/lang/System/out Ljava/io/PrintStream;\n" +
|
||||
arg + "\n" +
|
||||
"invokevirtual java/io/PrintStream/print(" + type.getJvmType() + ")V\n";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitVariable(DemoParser.VariableContext ctx) {
|
||||
jvmStack.push(DataType.INT);
|
||||
return "iload " + requireVariableIndex(ctx.varName);
|
||||
}
|
||||
|
||||
private int requireVariableIndex(Token varName) {
|
||||
Integer varIndex = variables.get(varName.getText());
|
||||
if (varIndex == null) {
|
||||
throw new UndeclaredVariableException(varName);
|
||||
}
|
||||
return varIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitVarDeclaration(DemoParser.VarDeclarationContext ctx) {
|
||||
String key = ctx.varName.getText();
|
||||
if (variables.containsKey(key)) {
|
||||
throw new VariableAlreadyDefinedException(ctx.varName);
|
||||
}
|
||||
variables.put(key, variables.size());
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitBranch(DemoParser.BranchContext ctx) {
|
||||
String condition = visit(ctx.condition);
|
||||
jvmStack.pop();
|
||||
String onTrue = visit(ctx.onTrue);
|
||||
String onFalse = visit(ctx.onFalse);
|
||||
int branchNum = branchCounter++;
|
||||
|
||||
return condition + "\n" +
|
||||
"ifne ifTrue" + branchNum + "\n" +
|
||||
onFalse + "\n" +
|
||||
"goto endIf" + branchNum + "\n" +
|
||||
"ifTrue" + branchNum + ":\n" +
|
||||
onTrue + "\n" +
|
||||
"endIf" + branchNum + ":\n";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitAssignment(DemoParser.AssignmentContext ctx) {
|
||||
final String instructions = visit(ctx.expr) + "\n"
|
||||
+ "istore " + requireVariableIndex(ctx.varName) + "\n";
|
||||
jvmStack.pop();
|
||||
return instructions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitFunctionDefinition(DemoParser.FunctionDefinitionContext ctx) {
|
||||
Map<String, Integer> oldVars = this.variables;
|
||||
JvmStack oldJvmStack = jvmStack;
|
||||
jvmStack = new JvmStack();
|
||||
variables = new HashMap<>();
|
||||
visit(ctx.params);
|
||||
String statements = visit(ctx.statements);
|
||||
String instructions = ".method public static " + ctx.funcName.getText() + "(";
|
||||
int numberOfParams = ctx.params.declarations.size();
|
||||
instructions += stringRepeat("I", numberOfParams);
|
||||
instructions += ")I\n" +
|
||||
".limit locals 100\n" +
|
||||
".limit stack 100\n" +
|
||||
(statements == null ? "" : statements + "\n") +
|
||||
visit(ctx.retValue) + "\n" +
|
||||
"ireturn\n" +
|
||||
".end method\n";
|
||||
jvmStack.pop();
|
||||
variables = oldVars;
|
||||
jvmStack = oldJvmStack;
|
||||
return instructions;
|
||||
}
|
||||
|
||||
private String stringRepeat(String string, int numberOfParams) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < numberOfParams; i++) {
|
||||
result.append(string);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitFunctionCall(DemoParser.FunctionCallContext ctx) {
|
||||
if (!definedFunctions.contains(ctx.funcName.getText(), ctx.args.expressions.size() )) {
|
||||
throw new UndefinedFunctionException(ctx.funcName);
|
||||
}
|
||||
|
||||
String instructions = "";
|
||||
String args = visit(ctx.args);
|
||||
if (args != null) {
|
||||
instructions += args + "\n";
|
||||
}
|
||||
instructions += "invokestatic HelloWorld/" + ctx.funcName.getText() + "(";
|
||||
int numberOfParams = ctx.args.expressions.size();
|
||||
instructions += stringRepeat("I", numberOfParams);
|
||||
instructions +=")I\n";
|
||||
for (int i = 0; i < numberOfParams; i++) {
|
||||
jvmStack.pop();
|
||||
}
|
||||
jvmStack.push(DataType.INT);
|
||||
return instructions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitProgram(DemoParser.ProgramContext ctx) {
|
||||
StringBuilder mainCode = new StringBuilder();
|
||||
StringBuilder functions = new StringBuilder();
|
||||
|
||||
for (ParseTree child : ctx.children) {
|
||||
final String instructions = visit(child);
|
||||
if (child instanceof MainStatementContext) {
|
||||
mainCode.append(instructions);
|
||||
} else {
|
||||
functions.append(instructions);
|
||||
}
|
||||
}
|
||||
final String instructions = ".method public static main([Ljava/lang/String;)V\n" +
|
||||
".limit stack 100\n" +
|
||||
".limit locals 100\n" +
|
||||
mainCode + "\n" +
|
||||
"return\n" +
|
||||
".end method\n";
|
||||
|
||||
return functions.append(instructions).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String aggregateResult(String aggregate, String nextResult) {
|
||||
if (aggregate == null) {
|
||||
return nextResult;
|
||||
}
|
||||
if (nextResult == null) {
|
||||
return aggregate;
|
||||
}
|
||||
return aggregate + "\n" + nextResult;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package de.letsbuildacompiler.compiler;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class JvmStack {
|
||||
|
||||
private Deque<DataType> typesOnStack = new LinkedList<>();
|
||||
|
||||
public void push(DataType type) {
|
||||
typesOnStack.push(type);
|
||||
}
|
||||
|
||||
public DataType pop() {
|
||||
return typesOnStack.pop();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package de.letsbuildacompiler.compiler;
|
||||
|
||||
import de.letsbuildacompiler.parser.DemoLexer;
|
||||
import de.letsbuildacompiler.parser.DemoParser;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CodePointCharStream;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
CodePointCharStream input = CharStreams.fromString("print(2 < 2);");
|
||||
System.out.println(compile(input));
|
||||
}
|
||||
|
||||
public static String compile(CharStream input) {
|
||||
DemoLexer lexer = new DemoLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
DemoParser parser = new DemoParser(tokens);
|
||||
|
||||
ParseTree tree = parser.program();
|
||||
FunctionList definedFunctions = FunctionDefinitionFinder.findFunctions(tree);
|
||||
return createJasminFile(new JasminVisitor(definedFunctions).visit(tree));
|
||||
}
|
||||
|
||||
private static String createJasminFile(String instructions) {
|
||||
return ".class public HelloWorld\n" +
|
||||
".super java/lang/Object\n" +
|
||||
"\n" +
|
||||
instructions;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package de.letsbuildacompiler.compiler.exceptions;
|
||||
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
class CompileException extends RuntimeException {
|
||||
int line;
|
||||
int col;
|
||||
|
||||
CompileException(Token token) {
|
||||
line = token.getLine();
|
||||
col = token.getCharPositionInLine();
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package de.letsbuildacompiler.compiler.exceptions;
|
||||
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
public class FunctionAlreadyDefinedException extends CompileException{
|
||||
|
||||
private String funcName;
|
||||
|
||||
public FunctionAlreadyDefinedException(Token token) {
|
||||
super(token);
|
||||
funcName = token.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return line + ":" + col + " function already defined : <" + funcName + ">";
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package de.letsbuildacompiler.compiler.exceptions;
|
||||
|
||||
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
public class UndeclaredVariableException extends CompileException {
|
||||
|
||||
private final String varName;
|
||||
|
||||
public UndeclaredVariableException(Token token) {
|
||||
super(token);
|
||||
varName = token.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return line + ":" + col + " undeclared variable <" + varName + ">";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package de.letsbuildacompiler.compiler.exceptions;
|
||||
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
public class UndefinedFunctionException extends CompileException {
|
||||
|
||||
private String funcName;
|
||||
|
||||
public UndefinedFunctionException(Token token) {
|
||||
super(token);
|
||||
funcName = token.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return line + ":" + col + " call to undefined function : <" + funcName + ">";
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package de.letsbuildacompiler.compiler.exceptions;
|
||||
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
public class VariableAlreadyDefinedException extends CompileException {
|
||||
|
||||
private String varName;
|
||||
|
||||
public VariableAlreadyDefinedException(Token token) {
|
||||
super(token);
|
||||
varName = token.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return line + ":" + col + " variable <" + varName + "> already defined";
|
||||
}
|
||||
}
|
@ -0,0 +1,173 @@
|
||||
package de.letsbuildacompiler.compiler;
|
||||
|
||||
import de.letsbuildacompiler.compiler.exceptions.FunctionAlreadyDefinedException;
|
||||
import de.letsbuildacompiler.compiler.exceptions.UndeclaredVariableException;
|
||||
import de.letsbuildacompiler.compiler.exceptions.UndefinedFunctionException;
|
||||
import de.letsbuildacompiler.compiler.exceptions.VariableAlreadyDefinedException;
|
||||
import jasmin.ClassFile;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
|
||||
public class CompilerTest {
|
||||
|
||||
private Path tempDir;
|
||||
|
||||
@BeforeMethod
|
||||
public void createTempDir() throws IOException {
|
||||
tempDir = Files.createTempDirectory("compilerTest");
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void deleteTempDir() {
|
||||
deleteRecursive(tempDir.toFile());
|
||||
}
|
||||
|
||||
private void deleteRecursive(File file) {
|
||||
if (file.isDirectory()) {
|
||||
for (File f : Objects.requireNonNull(file.listFiles())) {
|
||||
deleteRecursive(f);
|
||||
}
|
||||
}
|
||||
|
||||
if (!file.delete()) {
|
||||
throw new Error("Could not delete file <" + file + ">");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provider")
|
||||
@SuppressWarnings("unused")
|
||||
public void runningCode_outputsExpectedText(String description, String code, String expectedText) throws Exception {
|
||||
String actual = compileAndRun(code);
|
||||
assertEquals(expectedText, actual);
|
||||
}
|
||||
|
||||
@DataProvider(name = "provider")
|
||||
public Object[][] provide_code_expectedText() {
|
||||
return new Object[][] {
|
||||
{"plus", "println(1+2);", "3" + System.lineSeparator()},
|
||||
{"chained plus", "println(1+4+42);", "47" + System.lineSeparator()},
|
||||
{"multiple statements", "println(1); println(2);", "1" + System.lineSeparator() + "2" + System.lineSeparator()},
|
||||
{"minus", "println(3-2);", "1" + System.lineSeparator()},
|
||||
{"times", "println(3*3);", "9" + System.lineSeparator()},
|
||||
{"division", "println(6/3);", "2" + System.lineSeparator()},
|
||||
{"truncated division", "println(7/2);", "3" + System.lineSeparator()},
|
||||
{"operator precedence times and divide", "println(8/2*4);", "16" + System.lineSeparator()},
|
||||
{"operator precedence times and plus", "println(2+3*3);", "11" + System.lineSeparator()},
|
||||
{"operator precedence times and minus", "println(9-2*3);", "3" + System.lineSeparator()},
|
||||
{"operator precedence minus and plus", "println(8-2+5);", "11" + System.lineSeparator()},
|
||||
{"int variable", "int foo; foo = 42; println(foo);", "42" + System.lineSeparator()},
|
||||
{"add variable and constant parameters",
|
||||
"int foo; foo = 42; println(foo+2);", "44" + System.lineSeparator()},
|
||||
{"add two variables parameters",
|
||||
"int foo; int bar; foo = 42; bar = 43; println(foo+bar);", "85" + System.lineSeparator()},
|
||||
|
||||
example("functions/simple_function", "4" + System.lineSeparator()),
|
||||
example("functions/scopes", "4" + System.lineSeparator() + "42" + System.lineSeparator()),
|
||||
example("functions/int_parameters", "13" + System.lineSeparator()),
|
||||
example("branch/if_int_false", "42" + System.lineSeparator()),
|
||||
example("branch/if_int_true", "81" + System.lineSeparator()),
|
||||
|
||||
{"lower than true", "println(1 < 2);", "1" + System.lineSeparator()},
|
||||
{"lower than false", "println(2 < 2);", "0" + System.lineSeparator()},
|
||||
{"lower or equal true", "println(1 <= 1);", "1" + System.lineSeparator()},
|
||||
{"lower or equal false", "println(3 <= 2);", "0" + System.lineSeparator()},
|
||||
|
||||
{"greater than true", "println(3 > 2);", "1" + System.lineSeparator()},
|
||||
{"greater than false", "println(3 > 3);", "0" + System.lineSeparator()},
|
||||
{"greater or equal true", "println(3 >= 3);", "1" + System.lineSeparator()},
|
||||
{"greater or equal false", "println(2 >= 3);", "0" + System.lineSeparator()},
|
||||
|
||||
{"and true", "println(1 && 1);", "1" + System.lineSeparator()},
|
||||
{"and left false", "println(0 && 1);", "0" + System.lineSeparator()},
|
||||
{"and right false", "println(1 && 0);", "0" + System.lineSeparator()},
|
||||
example("operators/and-skip-right", "0" + System.lineSeparator() + "0" + System.lineSeparator()),
|
||||
|
||||
{"or false", "println(0 || 0);", "0" + System.lineSeparator()},
|
||||
{"or left true", "println(1 || 0);", "1" + System.lineSeparator()},
|
||||
{"or right true", "println(0 || 1);", "1" + System.lineSeparator()},
|
||||
example("operators/or-skip-right", "1" + System.lineSeparator() + "1" + System.lineSeparator()),
|
||||
|
||||
{"print", "print(42);", "42"},
|
||||
{"print string litteral", "print(\"Hello world\");", "Hello world"},
|
||||
};
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = UndeclaredVariableException.class,
|
||||
expectedExceptionsMessageRegExp = "1:8 undeclared variable <x>")
|
||||
public void compilingCode_throwsUndeclaredVariableException_ifReadingUndefinedVariable() throws Exception {
|
||||
compileAndRun("println(x);");
|
||||
// evaluation performed by expected exception.
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = UndeclaredVariableException.class,
|
||||
expectedExceptionsMessageRegExp = "1:0 undeclared variable <x>")
|
||||
public void compilingCode_throwsUndeclaredVariableException_ifWritingUndefinedVariable() throws Exception {
|
||||
compileAndRun("x = 5;");
|
||||
// evaluation performed by expected exception.
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = VariableAlreadyDefinedException.class,
|
||||
expectedExceptionsMessageRegExp = "2:4 variable <x> already defined")
|
||||
public void compilingCode_throwsAlreadyDefinedException_whenDefiningAlreadyDefinedVariable() throws Exception {
|
||||
compileAndRun("int x;" + System.lineSeparator() +
|
||||
"int x;");
|
||||
// evaluation performed by expected exception.
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = UndefinedFunctionException.class,
|
||||
expectedExceptionsMessageRegExp = "1:8 call to undefined function : <undefinedFunction>")
|
||||
public void compilingCode_throwsUndefinedFunctionException_whenCallingUndefinedFunction() throws Exception {
|
||||
compileAndRun("println(undefinedFunction());");
|
||||
// evaluation performed by expected exception.
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = FunctionAlreadyDefinedException.class,
|
||||
expectedExceptionsMessageRegExp = "2:4 function already defined : <x>")
|
||||
public void compilingCode_throwsAlreadyDefinedFunctionException_whenDefiningFunctionTwice() throws Exception {
|
||||
compileAndRun("int x() { return 42; }\n" +
|
||||
"int x() { return 45; }");
|
||||
// evaluation performed by expected exception.
|
||||
}
|
||||
|
||||
private String compileAndRun(String code) throws Exception {
|
||||
code = Main.compile(CharStreams.fromString(code));
|
||||
ClassFile classFile = new ClassFile();
|
||||
classFile.readJasmin(new StringReader(code), "", false);
|
||||
Path outputPath = tempDir.resolve(classFile.getClassName() + ".class");
|
||||
try (OutputStream outputStream = Files.newOutputStream(outputPath)) {
|
||||
classFile.write(outputStream);
|
||||
}
|
||||
return runJavaClass(tempDir, classFile.getClassName());
|
||||
}
|
||||
|
||||
private String runJavaClass(Path dir, String className) throws IOException {
|
||||
Process process = Runtime.getRuntime().exec(new String[]{"java", "-cp", dir.toString(), className});
|
||||
try (InputStream in = process.getInputStream()) {
|
||||
return new Scanner(in).useDelimiter("\\A").next();
|
||||
}
|
||||
}
|
||||
|
||||
private String[] example(String name, String expectedResult) throws IllegalArgumentException {
|
||||
try (InputStream in = CompilerTest.class.getClassLoader().getResourceAsStream("examples/" + name)) {
|
||||
if (in == null) {
|
||||
throw new IllegalArgumentException("No such example " + name);
|
||||
}
|
||||
String code = new Scanner(in, "UTF-8").useDelimiter("\\A").next();
|
||||
|
||||
return new String[] {name, code, expectedResult};
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("No such example " + name);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
if (0) {
|
||||
println(81);
|
||||
} else {
|
||||
println(42);
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
if(1){
|
||||
println(81);
|
||||
} else{
|
||||
println(42);
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
println(add(5, 8));
|
@ -0,0 +1,9 @@
|
||||
int i;
|
||||
i = 42;
|
||||
int randomNumber() {
|
||||
int i;
|
||||
i = 4;
|
||||
return i;
|
||||
}
|
||||
println(randomNumber());
|
||||
println(i);
|
@ -0,0 +1,5 @@
|
||||
int number() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
println(number());
|
@ -0,0 +1,6 @@
|
||||
int x(int i) {
|
||||
println(i);
|
||||
return i;
|
||||
}
|
||||
|
||||
println(x(0) && x(1));
|
@ -0,0 +1,6 @@
|
||||
int x(int i) {
|
||||
println(i);
|
||||
return i;
|
||||
}
|
||||
|
||||
println(x(1) || x(0));
|
@ -0,0 +1,5 @@
|
||||
if (0) {
|
||||
println(81);
|
||||
} else {
|
||||
println(42);
|
||||
}
|
5
compiler/target/test-classes/examples/branch/if_int_true
Normal file
5
compiler/target/test-classes/examples/branch/if_int_true
Normal file
@ -0,0 +1,5 @@
|
||||
if(1){
|
||||
println(81);
|
||||
} else{
|
||||
println(42);
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
println(add(5, 8));
|
9
compiler/target/test-classes/examples/functions/scopes
Normal file
9
compiler/target/test-classes/examples/functions/scopes
Normal file
@ -0,0 +1,9 @@
|
||||
int i;
|
||||
i = 42;
|
||||
int randomNumber() {
|
||||
int i;
|
||||
i = 4;
|
||||
return i;
|
||||
}
|
||||
println(randomNumber());
|
||||
println(i);
|
@ -0,0 +1,5 @@
|
||||
int number() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
println(number());
|
@ -0,0 +1,6 @@
|
||||
int x(int i) {
|
||||
println(i);
|
||||
return i;
|
||||
}
|
||||
|
||||
println(x(0) && x(1));
|
@ -0,0 +1,6 @@
|
||||
int x(int i) {
|
||||
println(i);
|
||||
return i;
|
||||
}
|
||||
|
||||
println(x(1) || x(0));
|
15
parser/pom.xml
Normal file
15
parser/pom.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>compiler</artifactId>
|
||||
<groupId>org.example</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>parser</artifactId>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,52 @@
|
||||
token literal names:
|
||||
null
|
||||
';'
|
||||
'/'
|
||||
'*'
|
||||
'-'
|
||||
'+'
|
||||
'int'
|
||||
'='
|
||||
'print('
|
||||
')'
|
||||
'('
|
||||
'{'
|
||||
'return'
|
||||
'}'
|
||||
null
|
||||
null
|
||||
null
|
||||
|
||||
token symbolic names:
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
IDENTIFIER
|
||||
NUMBER
|
||||
WHITESPACE
|
||||
|
||||
rule names:
|
||||
program
|
||||
programPart
|
||||
statement
|
||||
expression
|
||||
varDeclaration
|
||||
assignment
|
||||
print
|
||||
functionDefinition
|
||||
functionCall
|
||||
|
||||
|
||||
atn:
|
||||
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 18, 85, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 3, 2, 6, 2, 22, 10, 2, 13, 2, 14, 2, 23, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 30, 10, 3, 3, 4, 3, 4, 3, 4, 5, 4, 35, 10, 4, 3, 5, 3, 5, 3, 5, 3, 5, 5, 5, 41, 10, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 7, 5, 55, 10, 5, 12, 5, 14, 5, 58, 11, 5, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 2, 3, 8, 11, 2, 4, 6, 8, 10, 12, 14, 16, 18, 2, 2, 2, 85, 2, 21, 3, 2, 2, 2, 4, 29, 3, 2, 2, 2, 6, 34, 3, 2, 2, 2, 8, 40, 3, 2, 2, 2, 10, 59, 3, 2, 2, 2, 12, 62, 3, 2, 2, 2, 14, 66, 3, 2, 2, 2, 16, 70, 3, 2, 2, 2, 18, 80, 3, 2, 2, 2, 20, 22, 5, 4, 3, 2, 21, 20, 3, 2, 2, 2, 22, 23, 3, 2, 2, 2, 23, 21, 3, 2, 2, 2, 23, 24, 3, 2, 2, 2, 24, 3, 3, 2, 2, 2, 25, 26, 5, 6, 4, 2, 26, 27, 7, 3, 2, 2, 27, 30, 3, 2, 2, 2, 28, 30, 5, 16, 9, 2, 29, 25, 3, 2, 2, 2, 29, 28, 3, 2, 2, 2, 30, 5, 3, 2, 2, 2, 31, 35, 5, 14, 8, 2, 32, 35, 5, 10, 6, 2, 33, 35, 5, 12, 7, 2, 34, 31, 3, 2, 2, 2, 34, 32, 3, 2, 2, 2, 34, 33, 3, 2, 2, 2, 35, 7, 3, 2, 2, 2, 36, 37, 8, 5, 1, 2, 37, 41, 7, 17, 2, 2, 38, 41, 7, 16, 2, 2, 39, 41, 5, 18, 10, 2, 40, 36, 3, 2, 2, 2, 40, 38, 3, 2, 2, 2, 40, 39, 3, 2, 2, 2, 41, 56, 3, 2, 2, 2, 42, 43, 12, 9, 2, 2, 43, 44, 7, 4, 2, 2, 44, 55, 5, 8, 5, 10, 45, 46, 12, 8, 2, 2, 46, 47, 7, 5, 2, 2, 47, 55, 5, 8, 5, 9, 48, 49, 12, 7, 2, 2, 49, 50, 7, 6, 2, 2, 50, 55, 5, 8, 5, 8, 51, 52, 12, 6, 2, 2, 52, 53, 7, 7, 2, 2, 53, 55, 5, 8, 5, 7, 54, 42, 3, 2, 2, 2, 54, 45, 3, 2, 2, 2, 54, 48, 3, 2, 2, 2, 54, 51, 3, 2, 2, 2, 55, 58, 3, 2, 2, 2, 56, 54, 3, 2, 2, 2, 56, 57, 3, 2, 2, 2, 57, 9, 3, 2, 2, 2, 58, 56, 3, 2, 2, 2, 59, 60, 7, 8, 2, 2, 60, 61, 7, 16, 2, 2, 61, 11, 3, 2, 2, 2, 62, 63, 7, 16, 2, 2, 63, 64, 7, 9, 2, 2, 64, 65, 5, 8, 5, 2, 65, 13, 3, 2, 2, 2, 66, 67, 7, 10, 2, 2, 67, 68, 5, 8, 5, 2, 68, 69, 7, 11, 2, 2, 69, 15, 3, 2, 2, 2, 70, 71, 7, 8, 2, 2, 71, 72, 7, 16, 2, 2, 72, 73, 7, 12, 2, 2, 73, 74, 7, 11, 2, 2, 74, 75, 7, 13, 2, 2, 75, 76, 7, 14, 2, 2, 76, 77, 5, 8, 5, 2, 77, 78, 7, 3, 2, 2, 78, 79, 7, 15, 2, 2, 79, 17, 3, 2, 2, 2, 80, 81, 7, 16, 2, 2, 81, 82, 7, 12, 2, 2, 82, 83, 7, 11, 2, 2, 83, 19, 3, 2, 2, 2, 8, 23, 29, 34, 40, 54, 56]
|
@ -0,0 +1,50 @@
|
||||
T__0=1
|
||||
T__1=2
|
||||
T__2=3
|
||||
T__3=4
|
||||
T__4=5
|
||||
T__5=6
|
||||
T__6=7
|
||||
T__7=8
|
||||
T__8=9
|
||||
T__9=10
|
||||
T__10=11
|
||||
T__11=12
|
||||
T__12=13
|
||||
T__13=14
|
||||
T__14=15
|
||||
T__15=16
|
||||
T__16=17
|
||||
T__17=18
|
||||
T__18=19
|
||||
T__19=20
|
||||
T__20=21
|
||||
T__21=22
|
||||
T__22=23
|
||||
IDENTIFIER=24
|
||||
NUMBER=25
|
||||
WHITESPACE=26
|
||||
STRING=27
|
||||
';'=1
|
||||
'if'=2
|
||||
'('=3
|
||||
')'=4
|
||||
'else'=5
|
||||
'{'=6
|
||||
'}'=7
|
||||
'/'=8
|
||||
'*'=9
|
||||
'-'=10
|
||||
'+'=11
|
||||
'<'=12
|
||||
'<='=13
|
||||
'>'=14
|
||||
'>='=15
|
||||
'&&'=16
|
||||
'||'=17
|
||||
'int'=18
|
||||
'='=19
|
||||
'println('=20
|
||||
'print('=21
|
||||
'return'=22
|
||||
','=23
|
@ -0,0 +1,196 @@
|
||||
// Generated from /home/thibaud/Programmation/Java/antlr-compiler/Demo.g4 by ANTLR 4.7
|
||||
package de.letsbuildacompiler.parser;
|
||||
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
||||
|
||||
/**
|
||||
* This class provides an empty implementation of {@link DemoVisitor},
|
||||
* which can be extended to create a visitor which only needs to handle a subset
|
||||
* of the available methods.
|
||||
*
|
||||
* @param <T> The return type of the visit operation. Use {@link Void} for
|
||||
* operations with no return type.
|
||||
*/
|
||||
public class DemoBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements DemoVisitor<T> {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitProgram(DemoParser.ProgramContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitMainStatement(DemoParser.MainStatementContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitProgPartFunctionDefinition(DemoParser.ProgPartFunctionDefinitionContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitStatement(DemoParser.StatementContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitBranch(DemoParser.BranchContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitBlock(DemoParser.BlockContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitDiv(DemoParser.DivContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitVariable(DemoParser.VariableContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitFuncCallExpression(DemoParser.FuncCallExpressionContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitOr(DemoParser.OrContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitNumber(DemoParser.NumberContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitMult(DemoParser.MultContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitAnd(DemoParser.AndContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitRelational(DemoParser.RelationalContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitString(DemoParser.StringContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitPlus(DemoParser.PlusContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitMinus(DemoParser.MinusContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitVarDeclaration(DemoParser.VarDeclarationContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitAssignment(DemoParser.AssignmentContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitPrintln(DemoParser.PrintlnContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitPrint(DemoParser.PrintContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitFunctionDefinition(DemoParser.FunctionDefinitionContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitStatementList(DemoParser.StatementListContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitParameters(DemoParser.ParametersContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitFunctionCall(DemoParser.FunctionCallContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitExpressionList(DemoParser.ExpressionListContext ctx) { return visitChildren(ctx); }
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
token literal names:
|
||||
null
|
||||
';'
|
||||
'/'
|
||||
'*'
|
||||
'-'
|
||||
'+'
|
||||
'int'
|
||||
'='
|
||||
'print('
|
||||
')'
|
||||
'('
|
||||
'{'
|
||||
'return'
|
||||
'}'
|
||||
null
|
||||
null
|
||||
null
|
||||
|
||||
token symbolic names:
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
IDENTIFIER
|
||||
NUMBER
|
||||
WHITESPACE
|
||||
|
||||
rule names:
|
||||
T__0
|
||||
T__1
|
||||
T__2
|
||||
T__3
|
||||
T__4
|
||||
T__5
|
||||
T__6
|
||||
T__7
|
||||
T__8
|
||||
T__9
|
||||
T__10
|
||||
T__11
|
||||
T__12
|
||||
IDENTIFIER
|
||||
NUMBER
|
||||
WHITESPACE
|
||||
|
||||
channel names:
|
||||
DEFAULT_TOKEN_CHANNEL
|
||||
HIDDEN
|
||||
|
||||
mode names:
|
||||
DEFAULT_MODE
|
||||
|
||||
atn:
|
||||
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 18, 92, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 12, 3, 12, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 14, 3, 14, 3, 15, 3, 15, 7, 15, 76, 10, 15, 12, 15, 14, 15, 79, 11, 15, 3, 16, 6, 16, 82, 10, 16, 13, 16, 14, 16, 83, 3, 17, 6, 17, 87, 10, 17, 13, 17, 14, 17, 88, 3, 17, 3, 17, 2, 2, 18, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 29, 16, 31, 17, 33, 18, 3, 2, 6, 4, 2, 67, 92, 99, 124, 5, 2, 50, 59, 67, 92, 99, 124, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 94, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 2, 29, 3, 2, 2, 2, 2, 31, 3, 2, 2, 2, 2, 33, 3, 2, 2, 2, 3, 35, 3, 2, 2, 2, 5, 37, 3, 2, 2, 2, 7, 39, 3, 2, 2, 2, 9, 41, 3, 2, 2, 2, 11, 43, 3, 2, 2, 2, 13, 45, 3, 2, 2, 2, 15, 49, 3, 2, 2, 2, 17, 51, 3, 2, 2, 2, 19, 58, 3, 2, 2, 2, 21, 60, 3, 2, 2, 2, 23, 62, 3, 2, 2, 2, 25, 64, 3, 2, 2, 2, 27, 71, 3, 2, 2, 2, 29, 73, 3, 2, 2, 2, 31, 81, 3, 2, 2, 2, 33, 86, 3, 2, 2, 2, 35, 36, 7, 61, 2, 2, 36, 4, 3, 2, 2, 2, 37, 38, 7, 49, 2, 2, 38, 6, 3, 2, 2, 2, 39, 40, 7, 44, 2, 2, 40, 8, 3, 2, 2, 2, 41, 42, 7, 47, 2, 2, 42, 10, 3, 2, 2, 2, 43, 44, 7, 45, 2, 2, 44, 12, 3, 2, 2, 2, 45, 46, 7, 107, 2, 2, 46, 47, 7, 112, 2, 2, 47, 48, 7, 118, 2, 2, 48, 14, 3, 2, 2, 2, 49, 50, 7, 63, 2, 2, 50, 16, 3, 2, 2, 2, 51, 52, 7, 114, 2, 2, 52, 53, 7, 116, 2, 2, 53, 54, 7, 107, 2, 2, 54, 55, 7, 112, 2, 2, 55, 56, 7, 118, 2, 2, 56, 57, 7, 42, 2, 2, 57, 18, 3, 2, 2, 2, 58, 59, 7, 43, 2, 2, 59, 20, 3, 2, 2, 2, 60, 61, 7, 42, 2, 2, 61, 22, 3, 2, 2, 2, 62, 63, 7, 125, 2, 2, 63, 24, 3, 2, 2, 2, 64, 65, 7, 116, 2, 2, 65, 66, 7, 103, 2, 2, 66, 67, 7, 118, 2, 2, 67, 68, 7, 119, 2, 2, 68, 69, 7, 116, 2, 2, 69, 70, 7, 112, 2, 2, 70, 26, 3, 2, 2, 2, 71, 72, 7, 127, 2, 2, 72, 28, 3, 2, 2, 2, 73, 77, 9, 2, 2, 2, 74, 76, 9, 3, 2, 2, 75, 74, 3, 2, 2, 2, 76, 79, 3, 2, 2, 2, 77, 75, 3, 2, 2, 2, 77, 78, 3, 2, 2, 2, 78, 30, 3, 2, 2, 2, 79, 77, 3, 2, 2, 2, 80, 82, 9, 4, 2, 2, 81, 80, 3, 2, 2, 2, 82, 83, 3, 2, 2, 2, 83, 81, 3, 2, 2, 2, 83, 84, 3, 2, 2, 2, 84, 32, 3, 2, 2, 2, 85, 87, 9, 5, 2, 2, 86, 85, 3, 2, 2, 2, 87, 88, 3, 2, 2, 2, 88, 86, 3, 2, 2, 2, 88, 89, 3, 2, 2, 2, 89, 90, 3, 2, 2, 2, 90, 91, 8, 17, 2, 2, 91, 34, 3, 2, 2, 2, 6, 2, 77, 83, 88, 3, 8, 2, 2]
|
160
parser/src/main/java/de/letsbuildacompiler/parser/DemoLexer.java
Normal file
160
parser/src/main/java/de/letsbuildacompiler/parser/DemoLexer.java
Normal file
@ -0,0 +1,160 @@
|
||||
// Generated from /home/thibaud/Programmation/Java/antlr-compiler/Demo.g4 by ANTLR 4.7
|
||||
package de.letsbuildacompiler.parser;
|
||||
import org.antlr.v4.runtime.Lexer;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.TokenStream;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.atn.*;
|
||||
import org.antlr.v4.runtime.dfa.DFA;
|
||||
import org.antlr.v4.runtime.misc.*;
|
||||
|
||||
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
|
||||
public class DemoLexer extends Lexer {
|
||||
static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); }
|
||||
|
||||
protected static final DFA[] _decisionToDFA;
|
||||
protected static final PredictionContextCache _sharedContextCache =
|
||||
new PredictionContextCache();
|
||||
public static final int
|
||||
T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9,
|
||||
T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17,
|
||||
T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, IDENTIFIER=24,
|
||||
NUMBER=25, WHITESPACE=26, STRING=27;
|
||||
public static String[] channelNames = {
|
||||
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
|
||||
};
|
||||
|
||||
public static String[] modeNames = {
|
||||
"DEFAULT_MODE"
|
||||
};
|
||||
|
||||
public static final String[] ruleNames = {
|
||||
"T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8",
|
||||
"T__9", "T__10", "T__11", "T__12", "T__13", "T__14", "T__15", "T__16",
|
||||
"T__17", "T__18", "T__19", "T__20", "T__21", "T__22", "IDENTIFIER", "NUMBER",
|
||||
"WHITESPACE", "STRING"
|
||||
};
|
||||
|
||||
private static final String[] _LITERAL_NAMES = {
|
||||
null, "';'", "'if'", "'('", "')'", "'else'", "'{'", "'}'", "'/'", "'*'",
|
||||
"'-'", "'+'", "'<'", "'<='", "'>'", "'>='", "'&&'", "'||'", "'int'", "'='",
|
||||
"'println('", "'print('", "'return'", "','"
|
||||
};
|
||||
private static final String[] _SYMBOLIC_NAMES = {
|
||||
null, null, null, null, null, null, null, null, null, null, null, null,
|
||||
null, null, null, null, null, null, null, null, null, null, null, null,
|
||||
"IDENTIFIER", "NUMBER", "WHITESPACE", "STRING"
|
||||
};
|
||||
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #VOCABULARY} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String[] tokenNames;
|
||||
static {
|
||||
tokenNames = new String[_SYMBOLIC_NAMES.length];
|
||||
for (int i = 0; i < tokenNames.length; i++) {
|
||||
tokenNames[i] = VOCABULARY.getLiteralName(i);
|
||||
if (tokenNames[i] == null) {
|
||||
tokenNames[i] = VOCABULARY.getSymbolicName(i);
|
||||
}
|
||||
|
||||
if (tokenNames[i] == null) {
|
||||
tokenNames[i] = "<INVALID>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public String[] getTokenNames() {
|
||||
return tokenNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public Vocabulary getVocabulary() {
|
||||
return VOCABULARY;
|
||||
}
|
||||
|
||||
|
||||
public DemoLexer(CharStream input) {
|
||||
super(input);
|
||||
_interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGrammarFileName() { return "Demo.g4"; }
|
||||
|
||||
@Override
|
||||
public String[] getRuleNames() { return ruleNames; }
|
||||
|
||||
@Override
|
||||
public String getSerializedATN() { return _serializedATN; }
|
||||
|
||||
@Override
|
||||
public String[] getChannelNames() { return channelNames; }
|
||||
|
||||
@Override
|
||||
public String[] getModeNames() { return modeNames; }
|
||||
|
||||
@Override
|
||||
public ATN getATN() { return _ATN; }
|
||||
|
||||
public static final String _serializedATN =
|
||||
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\35\u009e\b\1\4\2"+
|
||||
"\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4"+
|
||||
"\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+
|
||||
"\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+
|
||||
"\t\31\4\32\t\32\4\33\t\33\4\34\t\34\3\2\3\2\3\3\3\3\3\3\3\4\3\4\3\5\3"+
|
||||
"\5\3\6\3\6\3\6\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3\t\3\n\3\n\3\13\3\13\3\f\3"+
|
||||
"\f\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\20\3\20\3\20\3\21\3\21\3\21\3\22"+
|
||||
"\3\22\3\22\3\23\3\23\3\23\3\23\3\24\3\24\3\25\3\25\3\25\3\25\3\25\3\25"+
|
||||
"\3\25\3\25\3\25\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\27\3\27"+
|
||||
"\3\27\3\27\3\27\3\30\3\30\3\31\3\31\7\31\u0085\n\31\f\31\16\31\u0088\13"+
|
||||
"\31\3\32\6\32\u008b\n\32\r\32\16\32\u008c\3\33\6\33\u0090\n\33\r\33\16"+
|
||||
"\33\u0091\3\33\3\33\3\34\3\34\7\34\u0098\n\34\f\34\16\34\u009b\13\34\3"+
|
||||
"\34\3\34\3\u0099\2\35\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27"+
|
||||
"\r\31\16\33\17\35\20\37\21!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63\33"+
|
||||
"\65\34\67\35\3\2\6\4\2C\\c|\5\2\62;C\\c|\3\2\62;\5\2\13\f\17\17\"\"\2"+
|
||||
"\u00a1\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2"+
|
||||
"\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3"+
|
||||
"\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2"+
|
||||
"\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2\2\2\2)\3\2\2\2\2+\3\2\2\2\2-\3\2\2\2\2"+
|
||||
"/\3\2\2\2\2\61\3\2\2\2\2\63\3\2\2\2\2\65\3\2\2\2\2\67\3\2\2\2\39\3\2\2"+
|
||||
"\2\5;\3\2\2\2\7>\3\2\2\2\t@\3\2\2\2\13B\3\2\2\2\rG\3\2\2\2\17I\3\2\2\2"+
|
||||
"\21K\3\2\2\2\23M\3\2\2\2\25O\3\2\2\2\27Q\3\2\2\2\31S\3\2\2\2\33U\3\2\2"+
|
||||
"\2\35X\3\2\2\2\37Z\3\2\2\2!]\3\2\2\2#`\3\2\2\2%c\3\2\2\2\'g\3\2\2\2)i"+
|
||||
"\3\2\2\2+r\3\2\2\2-y\3\2\2\2/\u0080\3\2\2\2\61\u0082\3\2\2\2\63\u008a"+
|
||||
"\3\2\2\2\65\u008f\3\2\2\2\67\u0095\3\2\2\29:\7=\2\2:\4\3\2\2\2;<\7k\2"+
|
||||
"\2<=\7h\2\2=\6\3\2\2\2>?\7*\2\2?\b\3\2\2\2@A\7+\2\2A\n\3\2\2\2BC\7g\2"+
|
||||
"\2CD\7n\2\2DE\7u\2\2EF\7g\2\2F\f\3\2\2\2GH\7}\2\2H\16\3\2\2\2IJ\7\177"+
|
||||
"\2\2J\20\3\2\2\2KL\7\61\2\2L\22\3\2\2\2MN\7,\2\2N\24\3\2\2\2OP\7/\2\2"+
|
||||
"P\26\3\2\2\2QR\7-\2\2R\30\3\2\2\2ST\7>\2\2T\32\3\2\2\2UV\7>\2\2VW\7?\2"+
|
||||
"\2W\34\3\2\2\2XY\7@\2\2Y\36\3\2\2\2Z[\7@\2\2[\\\7?\2\2\\ \3\2\2\2]^\7"+
|
||||
"(\2\2^_\7(\2\2_\"\3\2\2\2`a\7~\2\2ab\7~\2\2b$\3\2\2\2cd\7k\2\2de\7p\2"+
|
||||
"\2ef\7v\2\2f&\3\2\2\2gh\7?\2\2h(\3\2\2\2ij\7r\2\2jk\7t\2\2kl\7k\2\2lm"+
|
||||
"\7p\2\2mn\7v\2\2no\7n\2\2op\7p\2\2pq\7*\2\2q*\3\2\2\2rs\7r\2\2st\7t\2"+
|
||||
"\2tu\7k\2\2uv\7p\2\2vw\7v\2\2wx\7*\2\2x,\3\2\2\2yz\7t\2\2z{\7g\2\2{|\7"+
|
||||
"v\2\2|}\7w\2\2}~\7t\2\2~\177\7p\2\2\177.\3\2\2\2\u0080\u0081\7.\2\2\u0081"+
|
||||
"\60\3\2\2\2\u0082\u0086\t\2\2\2\u0083\u0085\t\3\2\2\u0084\u0083\3\2\2"+
|
||||
"\2\u0085\u0088\3\2\2\2\u0086\u0084\3\2\2\2\u0086\u0087\3\2\2\2\u0087\62"+
|
||||
"\3\2\2\2\u0088\u0086\3\2\2\2\u0089\u008b\t\4\2\2\u008a\u0089\3\2\2\2\u008b"+
|
||||
"\u008c\3\2\2\2\u008c\u008a\3\2\2\2\u008c\u008d\3\2\2\2\u008d\64\3\2\2"+
|
||||
"\2\u008e\u0090\t\5\2\2\u008f\u008e\3\2\2\2\u0090\u0091\3\2\2\2\u0091\u008f"+
|
||||
"\3\2\2\2\u0091\u0092\3\2\2\2\u0092\u0093\3\2\2\2\u0093\u0094\b\33\2\2"+
|
||||
"\u0094\66\3\2\2\2\u0095\u0099\7$\2\2\u0096\u0098\13\2\2\2\u0097\u0096"+
|
||||
"\3\2\2\2\u0098\u009b\3\2\2\2\u0099\u009a\3\2\2\2\u0099\u0097\3\2\2\2\u009a"+
|
||||
"\u009c\3\2\2\2\u009b\u0099\3\2\2\2\u009c\u009d\7$\2\2\u009d8\3\2\2\2\7"+
|
||||
"\2\u0086\u008c\u0091\u0099\3\b\2\2";
|
||||
public static final ATN _ATN =
|
||||
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
|
||||
static {
|
||||
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
|
||||
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
|
||||
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
T__0=1
|
||||
T__1=2
|
||||
T__2=3
|
||||
T__3=4
|
||||
T__4=5
|
||||
T__5=6
|
||||
T__6=7
|
||||
T__7=8
|
||||
T__8=9
|
||||
T__9=10
|
||||
T__10=11
|
||||
T__11=12
|
||||
T__12=13
|
||||
T__13=14
|
||||
T__14=15
|
||||
T__15=16
|
||||
T__16=17
|
||||
T__17=18
|
||||
T__18=19
|
||||
T__19=20
|
||||
T__20=21
|
||||
T__21=22
|
||||
T__22=23
|
||||
IDENTIFIER=24
|
||||
NUMBER=25
|
||||
WHITESPACE=26
|
||||
STRING=27
|
||||
';'=1
|
||||
'if'=2
|
||||
'('=3
|
||||
')'=4
|
||||
'else'=5
|
||||
'{'=6
|
||||
'}'=7
|
||||
'/'=8
|
||||
'*'=9
|
||||
'-'=10
|
||||
'+'=11
|
||||
'<'=12
|
||||
'<='=13
|
||||
'>'=14
|
||||
'>='=15
|
||||
'&&'=16
|
||||
'||'=17
|
||||
'int'=18
|
||||
'='=19
|
||||
'println('=20
|
||||
'print('=21
|
||||
'return'=22
|
||||
','=23
|
1317
parser/src/main/java/de/letsbuildacompiler/parser/DemoParser.java
Normal file
1317
parser/src/main/java/de/letsbuildacompiler/parser/DemoParser.java
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,182 @@
|
||||
// Generated from /home/thibaud/Programmation/Java/antlr-compiler/Demo.g4 by ANTLR 4.7
|
||||
package de.letsbuildacompiler.parser;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
|
||||
/**
|
||||
* This interface defines a complete generic visitor for a parse tree produced
|
||||
* by {@link DemoParser}.
|
||||
*
|
||||
* @param <T> The return type of the visit operation. Use {@link Void} for
|
||||
* operations with no return type.
|
||||
*/
|
||||
public interface DemoVisitor<T> extends ParseTreeVisitor<T> {
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#program}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitProgram(DemoParser.ProgramContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code MainStatement}
|
||||
* labeled alternative in {@link DemoParser#programPart}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitMainStatement(DemoParser.MainStatementContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code ProgPartFunctionDefinition}
|
||||
* labeled alternative in {@link DemoParser#programPart}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitProgPartFunctionDefinition(DemoParser.ProgPartFunctionDefinitionContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#statement}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitStatement(DemoParser.StatementContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#branch}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitBranch(DemoParser.BranchContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#block}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitBlock(DemoParser.BlockContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code Div}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitDiv(DemoParser.DivContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code Variable}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitVariable(DemoParser.VariableContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code FuncCallExpression}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitFuncCallExpression(DemoParser.FuncCallExpressionContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code Or}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitOr(DemoParser.OrContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code Number}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitNumber(DemoParser.NumberContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code Mult}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitMult(DemoParser.MultContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code And}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitAnd(DemoParser.AndContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code Relational}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitRelational(DemoParser.RelationalContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code String}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitString(DemoParser.StringContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code Plus}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitPlus(DemoParser.PlusContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by the {@code Minus}
|
||||
* labeled alternative in {@link DemoParser#expression}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitMinus(DemoParser.MinusContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#varDeclaration}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitVarDeclaration(DemoParser.VarDeclarationContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#assignment}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitAssignment(DemoParser.AssignmentContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#println}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitPrintln(DemoParser.PrintlnContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#print}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitPrint(DemoParser.PrintContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#functionDefinition}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitFunctionDefinition(DemoParser.FunctionDefinitionContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#statementList}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitStatementList(DemoParser.StatementListContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#parameters}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitParameters(DemoParser.ParametersContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#functionCall}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitFunctionCall(DemoParser.FunctionCallContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DemoParser#expressionList}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitExpressionList(DemoParser.ExpressionListContext ctx);
|
||||
}
|
Loading…
Reference in New Issue
Block a user