Compare commits

..

No commits in common. "3de54afa935c97b1eeac80e4adac4d02fa19192c" and "efa1a2165538eb37116753819e1cc854c6902f5e" have entirely different histories.

16 changed files with 163 additions and 109 deletions

View File

@ -4,6 +4,5 @@ public enum TypeEnum {
VOID, VOID,
INT, INT,
CHAR, CHAR,
BOOL, BOOL
NULL
} }

View File

@ -723,10 +723,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(MethodCallNode methodCallNode) { public void visit(MethodCallNode methodCallNode) {
List<ParameterNode> parameterNodes = new ArrayList<>(); List<ParameterNode> parameterNodes = new ArrayList<>();
for (IExpressionNode expressionNode : methodCallNode.parameters) {
expressionNode.accept(this);
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
}
int localVarIndex = -1; int localVarIndex = -1;
if (methodCallNode.target.memberAccess.identifiers.size() > 1) { if (methodCallNode.target.memberAccess.identifiers.size() > 1) {
localVarIndex = localVariables.indexOf(methodCallNode.target.memberAccess.identifiers.get(1)); localVarIndex = localVariables.indexOf(methodCallNode.target.memberAccess.identifiers.get(1));

View File

@ -41,8 +41,8 @@ public class Main {
} }
else { else {
try { try {
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/finalTest/Car.java")); CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/endabgabeTests/working/Loops.java"));
compileFile(codeCharStream, "src/test/resources/input/finalTest"); compileFile(codeCharStream, "src/test/resources/input/endabgabeTests");
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage()); System.err.println("Error reading the file: " + e.getMessage());
} }

View File

@ -254,11 +254,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) { } else if (currentScope.getLocalVar(toCheck.identifier) != null) {
var type = currentScope.getLocalVar(toCheck.identifier); var type = currentScope.getLocalVar(toCheck.identifier);
toCheck.setTypeNode(type);
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} }
} }
return new TypeCheckResult(true, null); return new TypeCheckResult(true, null);
} }
@Override @Override
@ -429,10 +430,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(NewDeclarationNode toCheck) { public TypeCheckResult analyze(NewDeclarationNode toCheck) {
if (context.containsClass(toCheck.identifier)) { if (context.containsClass(toCheck.identifier)) {
for(var t : toCheck.expressions) {
t.accept(this);
}
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier)); return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
} else { } else {
throw new RuntimeException("Cannot find class " + toCheck.identifier); 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)); return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
} }
case CHAR_VALUE -> { case CHAR_VALUE -> {
valueNode.value = valueNode.value.replace("'", "");
return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR)); return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR));
} }
case BOOLEAN_VALUE -> { case BOOLEAN_VALUE -> {

View File

@ -403,7 +403,7 @@ class AstBuilderTest {
MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference"); MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference");
BlockNode testMethod1Block = new BlockNode(); 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); MethodNode testMethod1 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod1", testMethod1Block);
BlockNode testMethod2Block = new BlockNode(); BlockNode testMethod2Block = new BlockNode();

View File

@ -352,6 +352,34 @@ public class SemanticTest {
assertNotNull(typedAst); 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 @Test
@DisplayName("Self Reference Test") @DisplayName("Self Reference Test")
public void selfReferenceTest() { public void selfReferenceTest() {

View 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;
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Loops loop = new Loops();
System.out.println(loop.For(6));
}
}

View File

@ -0,0 +1,11 @@
public class Person {
private int age;
public Person(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1 @@
public class EmptyClass {}

View 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;
}
}

View File

@ -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;
}
}

View File

@ -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));
}
}