Compare commits
10 Commits
code-gener
...
bd61a0e595
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd61a0e595 | ||
|
|
24407a5c1c | ||
| 195440e9d9 | |||
| 6fa57cf319 | |||
| 1de6add080 | |||
| c315966219 | |||
| c2c4974c76 | |||
|
|
85e0cf3807 | ||
|
|
6f3fb02666 | ||
|
|
435697053a |
15
README.md
15
README.md
@@ -95,14 +95,6 @@ Tests and execution:
|
||||
- Typing and Type checking -> (src/test/java/semantic/EndToTypedAstTest)
|
||||
|
||||
|
||||
|
||||
## Used Tools
|
||||
|
||||
- [Maven 4.0](https://maven.apache.org/index.html)
|
||||
- Used for automating the build process and managing dependencies.
|
||||
- [ANTLR4 v.13.1](https://www.antlr.org/)
|
||||
- Used to parse the input Java code into the Abstract Syntax Tree.
|
||||
|
||||
## Used Tools
|
||||
|
||||
- [Maven 4.0](https://maven.apache.org/index.html)
|
||||
@@ -140,10 +132,3 @@ Example (jar needs to be in the target directory)
|
||||
```
|
||||
DgenClass=true
|
||||
```
|
||||
|
||||
## How to run tests
|
||||
|
||||
```bash
|
||||
mvn test
|
||||
```
|
||||
Or start them manually in your IDE
|
||||
@@ -14,7 +14,6 @@ public class ConstructorNode extends MethodNode implements Visitable {
|
||||
public AccessModifierNode accessType;
|
||||
public String identifier;
|
||||
public List<ParameterNode> parameters = new ArrayList<>();
|
||||
public BlockNode block;
|
||||
|
||||
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
||||
this.accessType = new AccessModifierNode(accessType);
|
||||
|
||||
@@ -135,12 +135,12 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(CalculationNode calculationNode) {
|
||||
if (calculationNode.dotExpression != null) {
|
||||
calculationNode.dotExpression.accept(this);
|
||||
}
|
||||
if (calculationNode.calculationExpression != null) {
|
||||
calculationNode.calculationExpression.accept(this);
|
||||
}
|
||||
if (calculationNode.dotExpression != null) {
|
||||
calculationNode.dotExpression.accept(this);
|
||||
}
|
||||
if (calculationNode.operator != null) {
|
||||
switch (calculationNode.operator) {
|
||||
case PLUS:
|
||||
|
||||
@@ -15,7 +15,6 @@ import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
/**
|
||||
* Start miniCompiler using make:
|
||||
* <p> <code> cd .\src\test\ </code>
|
||||
@@ -40,6 +39,14 @@ public class Main {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/endabgabeTests/Person.java"));
|
||||
compileFile(codeCharStream, "src/test/resources/input/endabgabeTests");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -66,13 +66,13 @@ public class MiniCompilerLogger {
|
||||
logger.addHandler(consoleHandler);
|
||||
|
||||
// Configure file handler
|
||||
Handler fileHandler = new FileHandler("src/main/resources/logs/miniCompiler.log");
|
||||
//Handler fileHandler = new FileHandler("src/main/resources/logs/miniCompiler.log");
|
||||
// Toggle file logging on/off
|
||||
fileHandler.setLevel(Level.ALL);
|
||||
fileHandler.setFormatter(new CustomFormatter());
|
||||
logger.addHandler(fileHandler);
|
||||
//fileHandler.setLevel(Level.ALL);
|
||||
//fileHandler.setFormatter(new CustomFormatter());
|
||||
//logger.addHandler(fileHandler);
|
||||
|
||||
} catch (SecurityException | IOException e) {
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,10 +114,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(MethodNode methodNode) {
|
||||
if (methodNode instanceof ConstructorNode) {
|
||||
return new TypeCheckResult(true, new BaseType(TypeEnum.VOID));
|
||||
} else {
|
||||
|
||||
var valid = true;
|
||||
|
||||
for (var otherMethod : currentClass.getMethods()) {
|
||||
@@ -159,8 +155,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
|
||||
return new TypeCheckResult(valid, resultType);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -177,8 +171,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
currentFields.put(toCheck.identifier, toCheck.type);
|
||||
}
|
||||
return new TypeCheckResult(true, null);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -317,9 +309,18 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(IfElseNode toCheck) {
|
||||
var resultIf = toCheck.ifStatement.accept(this);
|
||||
var validElseIf = true;
|
||||
|
||||
if(toCheck.elseIfStatements.size() != 0) {
|
||||
for(IfNode ifNode : toCheck.elseIfStatements) {
|
||||
var resultIfFor = ifNode.accept(this);
|
||||
validElseIf = validElseIf && resultIfFor.isValid();
|
||||
}
|
||||
}
|
||||
|
||||
if(toCheck.elseStatement != null){
|
||||
var resultElse = toCheck.elseStatement.accept(this);
|
||||
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID));
|
||||
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid() && validElseIf, new BaseType(TypeEnum.VOID));
|
||||
}
|
||||
|
||||
|
||||
@@ -575,6 +576,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
var type = currentFields.get(unary.identifier);
|
||||
unary.setType(type);
|
||||
return new TypeCheckResult(valid,type );
|
||||
} else if (unary.value != null) {
|
||||
var result = unary.value.accept(this);
|
||||
unary.setType(result.getType());
|
||||
return new TypeCheckResult(result.isValid(), result.getType());
|
||||
} else if (unary.statement != null) {
|
||||
var result = unary.statement.accept(this);
|
||||
unary.setType(result.getType());
|
||||
|
||||
0
src/main/resources/logs/test
Normal file
0
src/main/resources/logs/test
Normal file
File diff suppressed because it is too large
Load Diff
@@ -438,9 +438,7 @@ class AstBuilderTest {
|
||||
BlockNode testMethod3Block = new BlockNode();
|
||||
testMethod3Block.addStatement(new LocalVariableDeclarationNode(new ReferenceType("SelfReference"),"selfReference1", "=", new UnaryNode(new NewDeclarationNode("SelfReference")))); // Assing einfach "=" ?
|
||||
MemberAccessNode methodAccess = new MemberAccessNode(false);
|
||||
methodAccess.addIdentifier("selfReference1");
|
||||
methodAccess.addIdentifier("selfReference");
|
||||
TargetNode methodTarget = new TargetNode(methodAccess);
|
||||
TargetNode methodTarget = new TargetNode("selfReference1");
|
||||
testMethod3Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(methodTarget,"testMethod1"))));
|
||||
MethodNode testMethod3 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod3", testMethod3Block);
|
||||
|
||||
|
||||
@@ -4,13 +4,13 @@ public class ControlStructures {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public cahr checkNumber(int num) {
|
||||
public char checkNumber(int num) {
|
||||
if (num > 0) {
|
||||
return "p";
|
||||
return 'p';
|
||||
} else if (num < 0) {
|
||||
return "n";
|
||||
return 'n';
|
||||
} else {
|
||||
return "z";
|
||||
return 'z';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class ControlStructures {
|
||||
int uneven = 0;
|
||||
int i = 0;
|
||||
while (i < limit) {
|
||||
if (i % 2 == 0) {
|
||||
if ((i % 2) == 0) {
|
||||
even++;
|
||||
} else {
|
||||
uneven = uneven + 1;
|
||||
|
||||
5
src/test/resources/input/endabgabeTests/Main.java
Normal file
5
src/test/resources/input/endabgabeTests/Main.java
Normal file
@@ -0,0 +1,5 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Person testPerson = new Person(5);
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
17
src/test/resources/input/endabgabeTests/working/Loops.java
Normal file
17
src/test/resources/input/endabgabeTests/working/Loops.java
Normal file
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
class Null{
|
||||
|
||||
Null a;
|
||||
int a;
|
||||
|
||||
public Null(){
|
||||
this.a = null;
|
||||
this.a = 1;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
class SelfReference{
|
||||
class SelfReference {
|
||||
|
||||
SelfReference selfReference;
|
||||
|
||||
@@ -10,9 +10,9 @@ class SelfReference{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int testMethod3(){
|
||||
int testMethod3() {
|
||||
SelfReference selfReference1 = new SelfReference();
|
||||
return selfReference1.selfReference.testMethod1();
|
||||
return selfReference1.testMethod1();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
public class Compiler {
|
||||
Node node;
|
||||
public int add(int i, int j) {
|
||||
node = new Node();
|
||||
node.x = 1;
|
||||
return i+j;
|
||||
}
|
||||
}
|
||||
|
||||
public class Node {
|
||||
public int x;
|
||||
public void main() {
|
||||
Compiler compiler = new Compiler();
|
||||
int i = compiler.add(5, 8);
|
||||
public class If {
|
||||
public If() {
|
||||
int intValue = 5;
|
||||
if(intValue == 5) {
|
||||
intValue--;
|
||||
}
|
||||
}
|
||||
}
|
||||
0
src/test/resources/output/javac/test
Normal file
0
src/test/resources/output/javac/test
Normal file
Reference in New Issue
Block a user