Compare commits
No commits in common. "3de54afa935c97b1eeac80e4adac4d02fa19192c" and "efa1a2165538eb37116753819e1cc854c6902f5e" have entirely different histories.
3de54afa93
...
efa1a21655
@ -4,6 +4,5 @@ public enum TypeEnum {
|
||||
VOID,
|
||||
INT,
|
||||
CHAR,
|
||||
BOOL,
|
||||
NULL
|
||||
BOOL
|
||||
}
|
||||
|
@ -723,10 +723,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
@Override
|
||||
public void visit(MethodCallNode methodCallNode) {
|
||||
List<ParameterNode> parameterNodes = new ArrayList<>();
|
||||
for (IExpressionNode expressionNode : methodCallNode.parameters) {
|
||||
expressionNode.accept(this);
|
||||
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
|
||||
}
|
||||
int localVarIndex = -1;
|
||||
if (methodCallNode.target.memberAccess.identifiers.size() > 1) {
|
||||
localVarIndex = localVariables.indexOf(methodCallNode.target.memberAccess.identifiers.get(1));
|
||||
|
@ -41,8 +41,8 @@ public class Main {
|
||||
}
|
||||
else {
|
||||
try {
|
||||
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/finalTest/Car.java"));
|
||||
compileFile(codeCharStream, "src/test/resources/input/finalTest");
|
||||
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/endabgabeTests/working/Loops.java"));
|
||||
compileFile(codeCharStream, "src/test/resources/input/endabgabeTests");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
|
Binary file not shown.
@ -254,11 +254,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
return new TypeCheckResult(true, type);
|
||||
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
|
||||
var type = currentScope.getLocalVar(toCheck.identifier);
|
||||
toCheck.setTypeNode(type);
|
||||
return new TypeCheckResult(true, type);
|
||||
}
|
||||
}
|
||||
|
||||
return new TypeCheckResult(true, null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -429,10 +430,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(NewDeclarationNode toCheck) {
|
||||
|
||||
if (context.containsClass(toCheck.identifier)) {
|
||||
for(var t : toCheck.expressions) {
|
||||
t.accept(this);
|
||||
}
|
||||
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
|
||||
} else {
|
||||
throw new RuntimeException("Cannot find class " + toCheck.identifier);
|
||||
@ -684,7 +683,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
|
||||
}
|
||||
case CHAR_VALUE -> {
|
||||
valueNode.value = valueNode.value.replace("'", "");
|
||||
return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR));
|
||||
}
|
||||
case BOOLEAN_VALUE -> {
|
||||
|
@ -403,7 +403,7 @@ class AstBuilderTest {
|
||||
MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference");
|
||||
|
||||
BlockNode testMethod1Block = new BlockNode();
|
||||
testMethod1Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(null, "testMethod2"))));
|
||||
testMethod1Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(new TargetNode(true), "testMethod2"))));
|
||||
MethodNode testMethod1 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod1", testMethod1Block);
|
||||
|
||||
BlockNode testMethod2Block = new BlockNode();
|
||||
|
@ -352,6 +352,34 @@ public class SemanticTest {
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Null Test")
|
||||
public void nullTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
|
||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "Null", blockCon);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "Null");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Self Reference Test")
|
||||
public void selfReferenceTest() {
|
||||
|
25
src/test/resources/input/endabgabeTests/Car.java
Normal file
25
src/test/resources/input/endabgabeTests/Car.java
Normal file
@ -0,0 +1,25 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Car myCar = new Car(2020);
|
||||
int tires = 0;
|
||||
int year = myCar.getYear();
|
||||
|
||||
if (year == 2020) {
|
||||
tires = 4;
|
||||
} else {
|
||||
tires = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Car {
|
||||
private int year;
|
||||
|
||||
public Car(int year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return this.year;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
public class ControlStructures {
|
||||
|
||||
public int sum(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public char checkNumber(int num) {
|
||||
if (num > 0) {
|
||||
return 'p';
|
||||
} else if (num < 0) {
|
||||
return 'n';
|
||||
} else {
|
||||
return 'z';
|
||||
}
|
||||
}
|
||||
|
||||
public void printNumbersUpTo(int limit) {
|
||||
int even = 0;
|
||||
int uneven = 0;
|
||||
int i = 0;
|
||||
while (i < limit) {
|
||||
if ((i % 2) == 0) {
|
||||
even++;
|
||||
} else {
|
||||
uneven = uneven + 1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ControlStructures cs = new ControlStructures();
|
||||
cs.printNumbersUpTo(5);
|
||||
int result = cs.sum(5, 5);
|
||||
}
|
||||
}
|
6
src/test/resources/input/endabgabeTests/Main.java
Normal file
6
src/test/resources/input/endabgabeTests/Main.java
Normal file
@ -0,0 +1,6 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Loops loop = new Loops();
|
||||
System.out.println(loop.For(6));
|
||||
}
|
||||
}
|
11
src/test/resources/input/endabgabeTests/Person.java
Normal file
11
src/test/resources/input/endabgabeTests/Person.java
Normal file
@ -0,0 +1,11 @@
|
||||
public class Person {
|
||||
private int age;
|
||||
|
||||
public Person(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return this.age;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
public class Calculation {
|
||||
public int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public int sub(int a, int b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
public int mul(int a, int b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
public int div(int a, int b) {
|
||||
return a / b;
|
||||
}
|
||||
|
||||
public int mod(int a, int b) {
|
||||
return a % b;
|
||||
}
|
||||
|
||||
public int complexCalculation() {
|
||||
return 3 - 2 * 2 + 5;
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
public class EmptyClass {}
|
24
src/test/resources/input/endabgabeTests/working/Loops.java
Normal file
24
src/test/resources/input/endabgabeTests/working/Loops.java
Normal file
@ -0,0 +1,24 @@
|
||||
public class Loops {
|
||||
public boolean If(int a, int b) {
|
||||
if(a == b) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int While(int a) {
|
||||
int count = 0;
|
||||
while(count < a) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public int For(int a) {
|
||||
for(int i = 0; i < a; i++) {
|
||||
|
||||
}
|
||||
return i + 2;
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
public class Car {
|
||||
public int ps;
|
||||
public char brand;
|
||||
public int tires;
|
||||
public int drivenKilometers;
|
||||
|
||||
public Car(int horsePower) {
|
||||
this.ps = horsePower;
|
||||
this.tires = 4;
|
||||
}
|
||||
|
||||
public int getTires() {
|
||||
return this.tires;
|
||||
}
|
||||
|
||||
public boolean isSuperCar() {
|
||||
if(this.ps > 300) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void tune(int horsePower) {
|
||||
this.ps = this.ps + horsePower;
|
||||
}
|
||||
|
||||
public void tune() {
|
||||
this.ps = this.ps + 50;
|
||||
}
|
||||
|
||||
public void driveCircels(int circels) {
|
||||
for(int i = 0; i < circels; i++) {
|
||||
this.drivenKilometers = this.drivenKilometers + 5;
|
||||
}
|
||||
}
|
||||
|
||||
public void isTunable(boolean hasEngine, boolean hasTires) {
|
||||
if(hasEngine && hasTires) {
|
||||
this.tune(5);
|
||||
}
|
||||
}
|
||||
|
||||
public char race() {
|
||||
int enemyHorsePower = 200;
|
||||
char win = 'W';
|
||||
char lose = 'L';
|
||||
Car enemy = new Car(ps);
|
||||
if(this.ps > enemyHorsePower) {
|
||||
return win;
|
||||
} else {
|
||||
return lose;
|
||||
}
|
||||
}
|
||||
|
||||
public int refuel(int currentTank, int maxTank){
|
||||
int tank = currentTank;
|
||||
do{
|
||||
tank++;
|
||||
}while(tank<maxTank);
|
||||
return tank;
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Car vw = new Car(50);
|
||||
System.out.println(vw.getTires());
|
||||
System.out.println(vw.isSuperCar());
|
||||
|
||||
System.out.println("--------------------------------------");
|
||||
|
||||
vw.tune(300);
|
||||
System.out.println(vw.ps);
|
||||
System.out.println(vw.isSuperCar());
|
||||
|
||||
System.out.println("--------------------------------------");
|
||||
|
||||
System.out.println(vw.drivenKilometers);
|
||||
vw.driveCircels(25);
|
||||
System.out.println(vw.drivenKilometers);
|
||||
|
||||
System.out.println("--------------------------------------");
|
||||
|
||||
vw.isTunable(true, true);
|
||||
System.out.println(vw.ps);
|
||||
|
||||
System.out.println("--------------------------------------");
|
||||
|
||||
System.out.println(vw.race());
|
||||
|
||||
System.out.println("--------------------------------------");
|
||||
|
||||
System.out.println(vw.refuel(10, 20));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user