Compare commits
252 Commits
50a52a1e87
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
4c4c06f728 | ||
|
30bd5a0b5c | ||
f673612f54 | |||
|
9760e39a29 | ||
|
93e17e5415 | ||
|
b151edc2fd | ||
|
c0d8ab8399 | ||
|
f50bb7efd7 | ||
|
51a390752b | ||
|
3114064038 | ||
|
745db43b34 | ||
|
e0286e0840 | ||
|
ebef717141 | ||
|
3de54afa93 | ||
|
30d3cfa44a | ||
efa1a21655 | |||
3fb11e5d7e | |||
|
c6e61defce | ||
|
dbde4e8047 | ||
5a12d61623 | |||
f5bccab651 | |||
0de5c3e993 | |||
3628a0a4d8 | |||
36f0683240 | |||
|
97aadb9ba8 | ||
|
e5dcbb8f99 | ||
e96f30fe19 | |||
64b15af6ef | |||
4ff6854094 | |||
0a9cc7655a | |||
7e66b5b8e3 | |||
d835a98e6f | |||
240913d422 | |||
4bee432294 | |||
0cf4715782 | |||
|
3c0f7e857a | ||
|
2b7e0d0d42 | ||
|
bd61a0e595 | ||
|
24407a5c1c | ||
195440e9d9 | |||
6fa57cf319 | |||
1de6add080 | |||
c315966219 | |||
c2c4974c76 | |||
|
85e0cf3807 | ||
|
6f3fb02666 | ||
|
435697053a | ||
8eba420d48 | |||
fbff03c3d7 | |||
721e1caa79 | |||
6d3e1f859e | |||
f6b34bf70b | |||
430d551f7d | |||
8e0d627505 | |||
6b6051cad8 | |||
1f8e045b1e | |||
ceb1231632 | |||
|
31929878b0 | ||
|
ea97f34398 | ||
|
2f7b310254 | ||
|
9b8155ebab | ||
|
4775c3f47e | ||
|
2e3a7850a4 | ||
|
d925a3258c | ||
|
fb5372bc8f | ||
|
f29be4fd8c | ||
|
34bb86c7f4 | ||
|
c0e197e2d0 | ||
|
ae872ed906 | ||
91552ad147 | |||
084808c3ab | |||
d19748766f | |||
|
4bdb65a6ce | ||
|
97e0c228d6 | ||
|
d4be77ceb2 | ||
ca539add98 | |||
1bcf396f95 | |||
8ba58d492b | |||
|
ed25868ff7 | ||
879fa08cdc | |||
|
0e512161a0 | ||
|
ba08c6f6b0 | ||
c5aea85965 | |||
2a1d0a0b8a | |||
f1abe5d5a8 | |||
|
a9d7e841f5 | ||
|
b7863d0684 | ||
|
7ccff3208c | ||
|
3e0e6f8327 | ||
|
ab5a51d3b1 | ||
|
a4c1c502ab | ||
|
7d441116bd | ||
|
8afa3b8461 | ||
|
84ecf316cd | ||
|
7ddea8f18d | ||
46154fbb01 | |||
9aac795c07 | |||
e59f4e7190 | |||
|
e6a2b0fe9d | ||
|
44c2f551de | ||
|
bad034acfd | ||
|
ba73e1bd45 | ||
|
b7affd75ae | ||
|
2ef50f93a9 | ||
|
1cf1aaf837 | ||
|
2bf73385af | ||
|
b072af346b | ||
|
27baa429b4 | ||
bd76135895 | |||
|
6cec17eb45 | ||
87be850a0e | |||
72f82ff863 | |||
7419953510 | |||
449b895d20 | |||
f0dd6d5eb6 | |||
8956362033 | |||
|
ed4aa2d59b | ||
|
6760a17f00 | ||
|
671317f28b | ||
|
e862a7427b | ||
233725778f | |||
3227d69fc1 | |||
4ca6972ccd | |||
0047f6c08e | |||
88ce9e52f0 | |||
|
437de74cc6 | ||
|
ca77307f0c | ||
|
5f46130439 | ||
3e1456351c | |||
d26cd0c13a | |||
|
cfde5219a4 | ||
|
f414e278bb | ||
|
741a56cb99 | ||
|
02e5f3a729 | ||
|
f7338a06b3 | ||
|
3996082fa7 | ||
903e87901e | |||
775beb60fe | |||
|
2537051668 | ||
|
82356ec189 | ||
|
561eafbf4c | ||
b58fa00c9a | |||
|
92990e4042 | ||
|
3500ffd377 | ||
|
771b92bfd7 | ||
c94c7a6477 | |||
c7ad574474 | |||
22c3850062 | |||
|
21da31dee3 | ||
|
294df16e89 | ||
3112660880 | |||
c135819ba0 | |||
e94cd9cc2a | |||
|
dcd20a2394 | ||
|
71ffb8bb83 | ||
|
2d455ba197 | ||
|
77fecfa476 | ||
|
36e56fa66e | ||
|
f3e3158460 | ||
|
7b1e6fced4 | ||
|
4d5c0017b4 | ||
093beb9ef1 | |||
2ae0988986 | |||
|
18fc17b707 | ||
|
bea71838ac | ||
dfded3e592 | |||
0298e2cb76 | |||
|
dccd34db73 | ||
90f91d58d1 | |||
612c967023 | |||
f12e553ffc | |||
f24e279596 | |||
c5fc378eed | |||
|
27f50b9c66 | ||
3e901ea254 | |||
85ae06048c | |||
cd845cd91c | |||
|
4d21d8e94e | ||
|
a34c7ded50 | ||
78d5528733 | |||
|
10eb17497e | ||
|
576146c4fc | ||
|
bf365194be | ||
|
35b3e9ee46 | ||
|
88a25207e9 | ||
|
4e56760dd6 | ||
|
347bdcbd94 | ||
|
d594bacb7d | ||
|
eb313464f0 | ||
|
8163d0b61e | ||
|
92e1daab5b | ||
70a3788030 | |||
b562a789a0 | |||
c05ad761d7 | |||
|
34d17660ef | ||
|
cc6d26e17d | ||
|
f9743efddc | ||
|
b37e065857 | ||
4c5473827e | |||
d631b01c82 | |||
|
8cd22c6e4c | ||
ef0bc99bbf | |||
99948fef07 | |||
66c9481b3e | |||
fd8d451ba7 | |||
f27a7471bc | |||
a15f684c04 | |||
4a78ab62a1 | |||
|
8d3602eda3 | ||
|
fcaeb850ba | ||
|
864ddbaeee | ||
fb3f822160 | |||
fd6da5cad2 | |||
|
c44118c872 | ||
|
0020f582a1 | ||
8b5a0d528e | |||
|
1b91e25d17 | ||
|
9edee73705 | ||
|
5bf9a4fc73 | ||
6d36eb109c | |||
|
7b41c45cd5 | ||
|
cf41babcb7 | ||
|
b9ada16dd1 | ||
|
b5738034b0 | ||
b9f6014f59 | |||
|
0ec65af9f9 | ||
|
fd8c3b066a | ||
af64f88198 | |||
837e751094 | |||
|
8cc67080ec | ||
|
cfcb61d49e | ||
|
8f742191bb | ||
|
102961bccc | ||
|
f59d7e9918 | ||
|
0732712e61 | ||
57c2023215 | |||
|
12ab402746 | ||
|
b6cc925e02 | ||
|
6a971345d4 | ||
|
8d6190b130 | ||
09e36a84dc | |||
|
e395c4d96b | ||
|
8a5f307947 | ||
514f7d724a | |||
|
9f40949f5a | ||
|
de5c2a5002 | ||
|
1132ff015c | ||
|
2a20a91d35 | ||
407b03620e | |||
|
b2e1745d51 | ||
|
a0e55d7b27 | ||
|
5a28d88f6a |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -77,3 +77,10 @@ fabric.properties
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
/target
|
||||
src/main/resources/logs/miniCompilerLog.log
|
||||
src/test/java/main/CompilerInput.class
|
||||
src/test/resources/output/javac/CompilerInput$Test.class
|
||||
src/test/resources/output/javac/CompilerInput.class
|
||||
src/test/resources/output/miniCompiler/CompilerInput.class
|
||||
src/test/resources/output/miniCompiler/CompilerInput$Test.class
|
||||
.idea/inspectionProfiles/Project_Default.xml
|
||||
|
19
.idea/ANTLRv4ToolGrammarProperties.xml
generated
Normal file
19
.idea/ANTLRv4ToolGrammarProperties.xml
generated
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ANTLRv4ToolGrammarProperties">
|
||||
<option name="perGrammarGenerationSettings">
|
||||
<list>
|
||||
<PerGrammarGenerationSettings>
|
||||
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/grammar/SimpleJava.g4" />
|
||||
<option name="autoGen" value="true" />
|
||||
<option name="outputDir" value="C:\Users\janni\Desktop\NichtHaskell2.0\src\main\java" />
|
||||
<option name="libDir" value="" />
|
||||
<option name="encoding" value="" />
|
||||
<option name="pkg" value="parser.generated" />
|
||||
<option name="language" value="" />
|
||||
<option name="generateVisitor" value="true" />
|
||||
</PerGrammarGenerationSettings>
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
5
.idea/jarRepositories.xml
generated
5
.idea/jarRepositories.xml
generated
@@ -6,6 +6,11 @@
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="maven_central" />
|
||||
<option name="name" value="Maven Central" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
|
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -6,7 +6,7 @@
|
||||
<PerGrammarGenerationSettings>
|
||||
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/SimpleJava.g4" />
|
||||
<option name="autoGen" value="true" />
|
||||
<option name="outputDir" value="C:\Users\Johannes\Documents\Github\JavaCompiler\src\main\java" />
|
||||
<option name="outputDir" value="C:\Users\ARB00075\Documents\DH\Compilerbau\NichtHaskell2.0\src\main\java" />
|
||||
<option name="libDir" value="" />
|
||||
<option name="encoding" value="" />
|
||||
<option name="pkg" value="parser.generated" />
|
||||
|
BIN
JavaCompiler-1.0-jar-with-dependencies.jar
Normal file
BIN
JavaCompiler-1.0-jar-with-dependencies.jar
Normal file
Binary file not shown.
37
Makefile
Normal file
37
Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
### IntelliJs play buttons do not work. Run in "src/test" folder with "make" command to run all
|
||||
### Or run only parts with "make compile-javac", "make clean" etc.
|
||||
|
||||
all: compile-javac compile-miniCompiler
|
||||
|
||||
compile-javac:
|
||||
#javac -d src/test/resources/output/javac src/test/resources/input/CompilerInput.java
|
||||
|
||||
compile-miniCompiler:
|
||||
cd ../.. ; mvn -DskipTests install
|
||||
cd ../.. ; mvn exec:java -DgenJar=true -DgenClass=true -Dexec.mainClass="main.Main" -Dexec.args="'src/main/resources/input/CompilerInput.java' 'src/main/resources/output'"
|
||||
# cp src/main/resources/output/CompilerInput.class src/test/resources/output/miniCompiler
|
||||
|
||||
test-miniCompiler:
|
||||
# move the compiled class to the test/main folder
|
||||
mv src/main/resources/output/CompilerInput.class src/test/java/main/
|
||||
# compile the test class
|
||||
javac src/test/java/main/EndToEndTester.java
|
||||
# run the test class
|
||||
java -cp src/test/java/main EndToEndTester
|
||||
|
||||
clean:
|
||||
# clean main output folders
|
||||
rm -f src/main/resources/output/*.class
|
||||
rm -f src/main/resources/output/*.jar
|
||||
# clean resources output folders
|
||||
rm -f src/test/resources/output/javac/*.class
|
||||
rm -f src/test/resources/output/miniCompiler/*.class
|
||||
rm -f src/test/resources/output/miniCompiler/*.jar
|
||||
# clean logs
|
||||
rm -f src/main/resources/logs/*
|
||||
# clean test/java/main folders from .class files for End-to-End tests
|
||||
rm -f src/test/java/main/*.class
|
||||
# clean javac output from every folder
|
||||
rm -f src/test/resources/input/*/*.class
|
||||
# clean test results from maven surefire plugin
|
||||
rm -f ../../target/surefire-reports/*.txt
|
BIN
Parser_Dokumentation.pdf
Normal file
BIN
Parser_Dokumentation.pdf
Normal file
Binary file not shown.
136
README.md
Normal file
136
README.md
Normal file
@@ -0,0 +1,136 @@
|
||||
# "Nicht Haskel 2.0" Java Compiler
|
||||
|
||||
Realisation of a subset of the Java Standard Compiler in the course Compiler Construction of the 4th semester Computer Science at the Duale Hochschule Stuttgart (Horb).
|
||||
|
||||
This project aims to provide a simplified version of the Java compiler, focusing on key language features and demonstrating the principles of compiler construction.
|
||||
|
||||
## Realised Java syntax
|
||||
|
||||
- **Data types**: `int`, `boolean`, `char`
|
||||
- **Access modifier**: `public`, `protected`, `private`
|
||||
- **Operators**: `=` `+` `-` `*` `%` `/` `>` `<` `>=` `<=` `==` `!=` `!` `&&` `||` `++` `--`
|
||||
- **Keywords**: `class`, `this`, `while`, `do`, `if`, `else`, `for`, `return`, `new`, `switch`, `case`, `break`, `default`, `:`
|
||||
- **Statements**:
|
||||
- `if` ... `if else` ... `else`;
|
||||
- `while` ... ;
|
||||
- `do` ... `while`;
|
||||
- `for`;
|
||||
- **Comments**:
|
||||
- Single line: `// comment`
|
||||
- Multi-line: `/* comment */`
|
||||
- **Further functions**:
|
||||
- All methods are overloadable
|
||||
- High maintainability and expandability through implementation of the visitor pattern
|
||||
- Logging Input and Outputs
|
||||
- Error Handling in the Semantic Check
|
||||
|
||||
## Project Structure
|
||||
|
||||
```plain
|
||||
src/
|
||||
└── main/
|
||||
├── java/
|
||||
│ ├── ast/ -> Defining the structure of the AST
|
||||
│ ├── bytecode/ -> Generate Java bytecode
|
||||
│ ├── main/ -> Running the compiler
|
||||
│ ├── parser/
|
||||
│ │ ├── astBuilder/ -> Builder creating the AST
|
||||
│ │ ├── generated/ -> Antlr generated grammar
|
||||
│ │ └── grammar/ -> Antlr grammar
|
||||
│ ├── semantic/ -> Running the semantic check
|
||||
│ └── visitor/ -> Visitor interface
|
||||
└── resources/
|
||||
test/
|
||||
└── java/
|
||||
│ ├── main/ -> Running E2E tests
|
||||
│ ├── parser/ -> Performs tests on the parser
|
||||
│ ├── semantic/ -> Performs tests on the semantic check
|
||||
└── resources/ -> Ressources for running the Tests
|
||||
```
|
||||
|
||||
## Class-Diagramm AST
|
||||
|
||||

|
||||
|
||||
## Distribution of the realisation
|
||||
|
||||
### i22030 & i22035
|
||||
Ausführliche Beschreibung der Parser Umsetzung:
|
||||
|
||||

|
||||
|
||||
Parser:
|
||||
- Grammar -> (src/main/java/parser/grammar)
|
||||
- Scanner
|
||||
- Parser
|
||||
- Abstract Syntax Tree (AST) -> (src/main/java/ast)
|
||||
- AstBuilder -> (src/main/java/parser/astBuilder)
|
||||
|
||||
Parser tests:
|
||||
- ParserTests -> (src/test/java/parser)
|
||||
- TestCases -> (src/test/resources/input/singeFeatureTests)
|
||||
|
||||
Other:
|
||||
- Documentation -> (README.md)
|
||||
- Ast Class-Diagramm -> (ast.png)
|
||||
- PowerPoint
|
||||
|
||||
### i22005
|
||||
Semantic check:
|
||||
- Set all types and check whether types have been used correctly
|
||||
- Contexts -> (src/main/java/semantic/context)
|
||||
- Exceptions Handling -> (src/main/java/semantic/exceptions)
|
||||
|
||||
Semantic Tests:
|
||||
- Typing and Type checking -> (src/test/java/semantic/EndToTypedAstTest)
|
||||
- Exception and feature test -> (src/test/resources/input/typedAstExceptionsTests)
|
||||
|
||||
### i22007
|
||||
Bytecode generation:
|
||||
- Complete bytecode generation -> (src/mein/java/bytecode)
|
||||
|
||||
### i22011
|
||||
Tests and execution:
|
||||
- Makefile
|
||||
- Running Compiler -> (src/main/main)
|
||||
- Running E2E tests -> (src/test/main)
|
||||
- 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.
|
||||
|
||||
## How to run the compiler
|
||||
### Possibilities
|
||||
### 1. Start miniCompiler using make:
|
||||
Make needs to be installed
|
||||
```bash
|
||||
cd .\src\test\ ; make clean compile-miniCompiler
|
||||
```
|
||||
|
||||
### 2. Start miniCompiler using jar:
|
||||
If you do not have the .jar, download it [here](https://gitea.hb.dhbw-stuttgart.de/i22005/NichtHaskell2.0/src/branch/main/JavaCompiler-1.0-jar-with-dependencies.jar) or compile it using mvn package or make first
|
||||
```
|
||||
java.exe -DgenJar=bool -DgenClass=bool -jar path_to_jar\jarName.jar 'path_to_input_file.java' 'path_to_output_directory'
|
||||
```
|
||||
|
||||
Example (jar needs to be in the target directory)
|
||||
```bash
|
||||
java.exe -DgenJar=true -DgenClass=true -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output'
|
||||
```
|
||||
|
||||
- set DgenJar true, to generate the jar, false for no jar
|
||||
|
||||
```
|
||||
DgenJar=true
|
||||
```
|
||||
|
||||
- set DgenClass true, to generate class files, false for no class files
|
||||
|
||||
```
|
||||
DgenClass=true
|
||||
```
|
64
pom.xml
64
pom.xml
@@ -4,13 +4,14 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<groupId>de.dhbw-stuttgart</groupId>
|
||||
<artifactId>JavaCompiler</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<java.version>22</java.version>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
@@ -19,7 +20,25 @@
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.9.3</version> <!-- Change the version as needed -->
|
||||
<version>5.11.0-M2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-suite-engine</artifactId>
|
||||
<version>1.11.0-M2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.11.0-M2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -52,8 +71,41 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M5</version> <!-- Change the version as needed -->
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Test.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id> <!-- this is used for inheritance merges -->
|
||||
<phase>package</phase> <!-- bind to the packaging phase -->
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>main.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven_central</id>
|
||||
<name>Maven Central</name>
|
||||
<url>https://repo.maven.apache.org/maven2/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
@@ -1,132 +0,0 @@
|
||||
import ast.ASTNode;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import ast.ProgramNode;
|
||||
//import bytecode.ByteCodeGenerator;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import parser.astBuilder.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
//import semantic.SemanticAnalyzer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
if(args.length > 0) {
|
||||
|
||||
} else {
|
||||
try {
|
||||
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/resources/CompilerInput.java"));
|
||||
parseFile(codeCharStream);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void parseFile(CharStream codeCharStream) {
|
||||
/* ------------------------- Scanner -> tokens ------------------------- */
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
|
||||
// Printing the tokens
|
||||
// tokenStream.fill();
|
||||
// List<Token> tokens = tokenStream.getTokens();
|
||||
// System.out.println("-------------------- Scanner -> tokens --------------------");
|
||||
// for (Token token : tokens) {
|
||||
// String tokenType = SimpleJavaLexer.VOCABULARY.getSymbolicName(token.getType());
|
||||
// String tokenText = token.getText();
|
||||
// // System.out.println("Token Type: " + tokenType + ", Token Text: " +
|
||||
// // tokenText);
|
||||
// System.out.println(tokenType + " " + tokenText);
|
||||
// }
|
||||
// System.out.println();
|
||||
|
||||
/* ------------------------- Parser -> Parsetree ------------------------- */
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree parseTree = parser.program(); // parse the input
|
||||
|
||||
// Printing the parse tree
|
||||
// System.out.println("-------------------- Parser -> Parsetree --------------------");
|
||||
// System.out.println(parseTree.toStringTree(parser));
|
||||
// printTree(parseTree, parser, 0);
|
||||
// System.out.println();
|
||||
|
||||
/* ------------------------- AST builder -> AST ------------------------- */
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
||||
System.out.println(abstractSyntaxTree);
|
||||
// Printing the AST
|
||||
// System.out.println("-------------------- AST builder -> AST --------------------");
|
||||
// // System.out.println("AST: " + ast.toString());
|
||||
// printAST(abstractSyntaxTree, 0);
|
||||
// System.out.println();
|
||||
|
||||
/*
|
||||
* ------------------------- Semantic Analyzer -> Tast -------------------------
|
||||
*/
|
||||
//SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
||||
//ProgramNode typedAst = (ProgramNode) semanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
|
||||
// Printing the Tast
|
||||
System.out.println("Tast generated");
|
||||
|
||||
/*
|
||||
* ------------------------- Bytecode Generator -> Bytecode
|
||||
* -------------------------
|
||||
*/
|
||||
//ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||
//byteCodeGenerator.generateByteCode(abstractSyntaxTree);
|
||||
//byteCodeGenerator.visit(typedAst);
|
||||
System.out.println("Bytecode generated");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to print the parse tree in a structured format.
|
||||
* It recursively traverses the tree and prints the rule names and text of the
|
||||
* nodes.
|
||||
*
|
||||
* @param tree The parse tree to be printed.
|
||||
* @param parser The parser used to parse the input. It's used to get the rule
|
||||
* names.
|
||||
* @param indent The current indentation level. It's used to format the output.
|
||||
*/
|
||||
public static void printTree(ParseTree tree, Parser parser, int indent) {
|
||||
// Create an indentation string based on the current indentation level
|
||||
String indentString = " ".repeat(indent * 2);
|
||||
|
||||
// If the tree node is an instance of RuleContext (i.e., it's an internal node),
|
||||
// print the rule name
|
||||
if (tree instanceof RuleContext) {
|
||||
int ruleIndex = ((RuleContext) tree).getRuleIndex();
|
||||
String ruleName = parser.getRuleNames()[ruleIndex];
|
||||
System.out.println(indentString + ruleName);
|
||||
} else {
|
||||
// If the tree node is not an instance of RuleContext (i.e., it's a leaf node),
|
||||
// print the text of the node
|
||||
System.out.println(indentString + tree.getText());
|
||||
}
|
||||
|
||||
// Recursively print the children of the current node, increasing the
|
||||
// indentation level
|
||||
for (int i = 0; i < tree.getChildCount(); i++) {
|
||||
printTree(tree.getChild(i), parser, indent + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void printAST(ASTNode node, int indent) {
|
||||
String indentString = " ".repeat(indent * 2);
|
||||
System.out.println(indentString + node.getClass().toString());
|
||||
|
||||
// for (ASTNode child : node.) {
|
||||
// printAST(child, indent + 1);
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
package ast;
|
||||
|
||||
public interface ASTNode { }
|
||||
public interface ASTNode {
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,42 +1,35 @@
|
||||
package ast;
|
||||
|
||||
import ast.type.AccessModifierNode;
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.MemberNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.members.MemberNode;
|
||||
import ast.members.MethodNode;
|
||||
import bytecode.visitor.ClassVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ClassNode implements ASTNode {
|
||||
public class ClassNode implements ASTNode, Visitable {
|
||||
public AccessModifierNode accessType;
|
||||
public String identifier;
|
||||
public List<MemberNode> members = new ArrayList<>();
|
||||
public boolean hasConstructor;
|
||||
|
||||
public ClassNode() {}
|
||||
public ClassNode() {
|
||||
|
||||
public ClassNode(String accessType, String identifier){
|
||||
}
|
||||
|
||||
public ClassNode(String accessType, String identifier) {
|
||||
this.accessType = new AccessModifierNode(accessType);
|
||||
this.identifier = identifier;
|
||||
hasConstructor = false;
|
||||
}
|
||||
|
||||
public void addMember(MemberNode member) {
|
||||
if (member instanceof ConstructorNode) {
|
||||
this.hasConstructor = true;
|
||||
}
|
||||
members.add(member);
|
||||
}
|
||||
|
||||
public void ensureConstructor(){
|
||||
if(!hasConstructor) {
|
||||
ConstructorNode constructor = new ConstructorNode(new AccessModifierNode("public"), identifier);
|
||||
members.addFirst(constructor);
|
||||
}
|
||||
}
|
||||
|
||||
public List<MethodNode> getMethods(){
|
||||
public List<MethodNode> getMethods() {
|
||||
List<MethodNode> methods = new ArrayList<>();
|
||||
for (MemberNode member : members) {
|
||||
if (member instanceof MethodNode methodNode) {
|
||||
@@ -45,4 +38,15 @@ public class ClassNode implements ASTNode {
|
||||
}
|
||||
return methods;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ClassVisitor classVisitor) {
|
||||
classVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,13 +1,27 @@
|
||||
package ast;
|
||||
|
||||
import bytecode.visitor.ProgramVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ProgramNode implements ASTNode {
|
||||
public class ProgramNode implements ASTNode, Visitable {
|
||||
public List<ClassNode> classes = new ArrayList<>();
|
||||
|
||||
public void addClass(ClassNode classNode) {
|
||||
classes.add(classNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ProgramVisitor programVisitor) {
|
||||
programVisitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
@@ -1,17 +0,0 @@
|
||||
package ast.block;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.statement.IStatementNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockNode implements ASTNode {
|
||||
public List<IStatementNode> statements = new ArrayList<>();
|
||||
|
||||
public BlockNode() {}
|
||||
|
||||
public void addStatement(IStatementNode statement) {
|
||||
statements.add(statement);
|
||||
}
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
package ast.expression;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public interface IExpressionNode extends ASTNode {}
|
@@ -1,7 +0,0 @@
|
||||
package ast.expression.binaryexpression;
|
||||
|
||||
import ast.expression.IExpressionNode;
|
||||
|
||||
import java.beans.Expression;
|
||||
|
||||
public class BinaryExpressionNode implements IExpressionNode {}
|
@@ -1,19 +0,0 @@
|
||||
package ast.expression.binaryexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public class CalculationExpressionNode extends BinaryExpressionNode {
|
||||
public CalculationExpressionNode calculationExpression;
|
||||
public String operator;
|
||||
public DotExpressionNode dotExpression;
|
||||
|
||||
public CalculationExpressionNode(CalculationExpressionNode calculationExpression, String operator, DotExpressionNode dotExpression) {
|
||||
this.calculationExpression = calculationExpression;
|
||||
this.operator = operator;
|
||||
this.dotExpression = dotExpression;
|
||||
}
|
||||
|
||||
public CalculationExpressionNode(DotExpressionNode dotExpression) {
|
||||
this.dotExpression = dotExpression;
|
||||
}
|
||||
}
|
@@ -1,19 +0,0 @@
|
||||
package ast.expression.binaryexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public class DotExpressionNode extends BinaryExpressionNode {
|
||||
public DotExpressionNode dotExpression;
|
||||
public String operator;
|
||||
public DotSubstractionExpressionNode dotSubstractionExpression;
|
||||
|
||||
public DotExpressionNode(DotExpressionNode dotExpression, String operator, DotSubstractionExpressionNode dotSubstractionExpression) {
|
||||
this.dotExpression = dotExpression;
|
||||
this.operator = operator;
|
||||
this.dotSubstractionExpression = dotSubstractionExpression;
|
||||
}
|
||||
|
||||
public DotExpressionNode(DotSubstractionExpressionNode dotSubstractionExpression) {
|
||||
this.dotSubstractionExpression = dotSubstractionExpression;
|
||||
}
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
package ast.expression.binaryexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.unaryexpression.MemberAccessNode;
|
||||
import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode;
|
||||
import ast.type.ValueNode;
|
||||
|
||||
public class DotSubstractionExpressionNode extends BinaryExpressionNode {
|
||||
public ValueNode value;
|
||||
public String identifier;
|
||||
public MemberAccessNode memberAccess;
|
||||
public MethodCallStatementExpressionNode methodCall;
|
||||
public CalculationExpressionNode calculationExpression;
|
||||
|
||||
public DotSubstractionExpressionNode(ValueNode value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public DotSubstractionExpressionNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public DotSubstractionExpressionNode(MemberAccessNode memberAccess) {
|
||||
this.memberAccess = memberAccess;
|
||||
}
|
||||
|
||||
public DotSubstractionExpressionNode(MethodCallStatementExpressionNode methodCall, CalculationExpressionNode calculationExpression) {
|
||||
this.methodCall = methodCall;
|
||||
this.calculationExpression = calculationExpression;
|
||||
}
|
||||
}
|
@@ -1,17 +0,0 @@
|
||||
package ast.expression.binaryexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
import ast.expression.unaryexpression.UnaryExpressionNode;
|
||||
|
||||
public class NonCalculationExpressionNode extends BinaryExpressionNode {
|
||||
public UnaryExpressionNode unaryExpression;
|
||||
public String operator;
|
||||
public IExpressionNode expression;
|
||||
|
||||
public NonCalculationExpressionNode(UnaryExpressionNode unaryExpression, String operator, IExpressionNode expression) {
|
||||
this.unaryExpression = unaryExpression;
|
||||
this.operator = operator;
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
@@ -1,19 +0,0 @@
|
||||
package ast.expression.unaryexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MemberAccessNode implements ASTNode {
|
||||
public Boolean thisExpr;
|
||||
public List<String> identifiers = new ArrayList<>();
|
||||
|
||||
public MemberAccessNode(Boolean thisExpr) {
|
||||
this.thisExpr = thisExpr;
|
||||
}
|
||||
|
||||
public void addIdentifier(String identifier) {
|
||||
identifiers.add(identifier);
|
||||
}
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
package ast.expression.unaryexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
|
||||
public class NotExpressionNode implements ASTNode {
|
||||
public IExpressionNode expression;
|
||||
|
||||
public NotExpressionNode(IExpressionNode expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
@@ -1,45 +0,0 @@
|
||||
package ast.expression.unaryexpression;
|
||||
|
||||
import ast.expression.IExpressionNode;
|
||||
import ast.statement.IStatementNode;
|
||||
import ast.type.ValueNode;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class UnaryExpressionNode implements IExpressionNode {
|
||||
public String thisExp;
|
||||
public String identifier;
|
||||
public MemberAccessNode memberAccess;
|
||||
public ValueNode value;
|
||||
public NotExpressionNode notExpression;
|
||||
public IStatementNode statement;
|
||||
public IExpressionNode expression;
|
||||
|
||||
public UnaryExpressionNode(String value) {
|
||||
if(Objects.equals(value, "this")) {
|
||||
this.thisExp = "this";
|
||||
} else {
|
||||
this.identifier = value;
|
||||
}
|
||||
}
|
||||
|
||||
public UnaryExpressionNode(MemberAccessNode memberAccess) {
|
||||
this.memberAccess = memberAccess;
|
||||
}
|
||||
|
||||
public UnaryExpressionNode(ValueNode value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public UnaryExpressionNode(NotExpressionNode notExpression) {
|
||||
this.notExpression = notExpression;
|
||||
}
|
||||
|
||||
public UnaryExpressionNode(IStatementNode statement) {
|
||||
this.statement = statement;
|
||||
}
|
||||
|
||||
public UnaryExpressionNode(IExpressionNode expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
13
src/main/java/ast/expressions/IExpressionNode.java
Normal file
13
src/main/java/ast/expressions/IExpressionNode.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package ast.expressions;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.type.type.ITypeNode;
|
||||
import visitor.Visitable;
|
||||
|
||||
public interface IExpressionNode extends ASTNode, Visitable {
|
||||
|
||||
ITypeNode getType();
|
||||
|
||||
void setType(ITypeNode type);
|
||||
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package ast.expressions.binaryexpressions;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.type.type.*;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class BinaryNode implements IExpressionNode, Visitable {
|
||||
|
||||
public ITypeNode typeNode;
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITypeNode getType() {
|
||||
return typeNode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setType(ITypeNode type) {
|
||||
this.typeNode = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package ast.expressions.binaryexpressions;
|
||||
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class CalculationNode extends BinaryNode {
|
||||
public CalculationNode calculationExpression;
|
||||
public EnumLineOperator operator;
|
||||
public DotNode dotExpression;
|
||||
|
||||
public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) {
|
||||
this.calculationExpression = calculationExpression;
|
||||
setOperator(operator);
|
||||
this.dotExpression = dotExpression;
|
||||
}
|
||||
|
||||
public CalculationNode(DotNode dotExpression) {
|
||||
this.dotExpression = dotExpression;
|
||||
}
|
||||
|
||||
public void setOperator(String operator) {
|
||||
if (operator != null) {
|
||||
if (operator.equals("+")) {
|
||||
this.operator = EnumLineOperator.PLUS;
|
||||
} else if (operator.equals("-")) {
|
||||
this.operator = EnumLineOperator.MINUS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
40
src/main/java/ast/expressions/binaryexpressions/DotNode.java
Normal file
40
src/main/java/ast/expressions/binaryexpressions/DotNode.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package ast.expressions.binaryexpressions;
|
||||
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class DotNode extends BinaryNode {
|
||||
public DotNode dotExpression;
|
||||
public EnumDotOperator operator;
|
||||
public DotSubtractionNode dotSubtractionExpression;
|
||||
|
||||
public DotNode(DotNode dotExpression, String operator, DotSubtractionNode dotSubtractionExpression) {
|
||||
this.dotExpression = dotExpression;
|
||||
setOperator(operator);
|
||||
this.dotSubtractionExpression = dotSubtractionExpression;
|
||||
}
|
||||
|
||||
public DotNode(DotSubtractionNode dotSubtractionExpression) {
|
||||
this.dotSubtractionExpression = dotSubtractionExpression;
|
||||
}
|
||||
|
||||
public void setOperator(String operator) {
|
||||
switch (operator) {
|
||||
case "*" -> this.operator = EnumDotOperator.MULT;
|
||||
case "/" -> this.operator = EnumDotOperator.DIV;
|
||||
case "%" -> this.operator = EnumDotOperator.MOD;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
package ast.expressions.binaryexpressions;
|
||||
|
||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||
import ast.type.ValueNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class DotSubtractionNode extends BinaryNode {
|
||||
public ValueNode value;
|
||||
public String identifier;
|
||||
public MemberAccessNode memberAccess;
|
||||
public MethodCallNode methodCall;
|
||||
public CalculationNode calculationExpression;
|
||||
|
||||
public DotSubtractionNode(ValueNode value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public DotSubtractionNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public DotSubtractionNode(MemberAccessNode memberAccess) {
|
||||
this.memberAccess = memberAccess;
|
||||
}
|
||||
|
||||
public DotSubtractionNode(MethodCallNode methodCall, CalculationNode calculationExpression) {
|
||||
this.methodCall = methodCall;
|
||||
this.calculationExpression = calculationExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package ast.expressions.binaryexpressions;
|
||||
|
||||
public enum EnumDotOperator {
|
||||
MULT, DIV, MOD
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package ast.expressions.binaryexpressions;
|
||||
|
||||
public enum EnumLineOperator {
|
||||
PLUS, MINUS
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package ast.expressions.binaryexpressions;
|
||||
|
||||
public enum EnumNonCalculationOperator {
|
||||
AND, OR, GREATER, LESS, GREATER_EQUAL, LESS_EQUAL, EQUAL, NOT_EQUAL
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package ast.expressions.binaryexpressions;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.expressions.unaryexpressions.UnaryNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class NonCalculationNode extends BinaryNode {
|
||||
public UnaryNode unaryExpression;
|
||||
public EnumNonCalculationOperator operator;
|
||||
public IExpressionNode expression;
|
||||
|
||||
public NonCalculationNode(UnaryNode unaryExpression, String operator, IExpressionNode expression) {
|
||||
this.unaryExpression = unaryExpression;
|
||||
setOperator(operator);
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public void setOperator(String operator) {
|
||||
switch (operator) {
|
||||
case "&&" -> this.operator = EnumNonCalculationOperator.AND;
|
||||
case "||" -> this.operator = EnumNonCalculationOperator.OR;
|
||||
case ">" -> this.operator = EnumNonCalculationOperator.GREATER;
|
||||
case "<" -> this.operator = EnumNonCalculationOperator.LESS;
|
||||
case ">=" -> this.operator = EnumNonCalculationOperator.GREATER_EQUAL;
|
||||
case "<=" -> this.operator = EnumNonCalculationOperator.LESS_EQUAL;
|
||||
case "==" -> this.operator = EnumNonCalculationOperator.EQUAL;
|
||||
case "!=" -> this.operator = EnumNonCalculationOperator.NOT_EQUAL;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
package ast.expressions.unaryexpressions;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.type.type.ITypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MemberAccessNode implements ASTNode, Visitable {
|
||||
public Boolean thisExpr;
|
||||
public List<String> identifiers = new ArrayList<>();
|
||||
private ITypeNode typeNode;
|
||||
|
||||
public MemberAccessNode(Boolean thisExpr) {
|
||||
this.thisExpr = thisExpr;
|
||||
}
|
||||
|
||||
public void addIdentifier(String identifier) {
|
||||
identifiers.add(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
public ITypeNode getTypeNode() {
|
||||
return typeNode;
|
||||
}
|
||||
|
||||
public void setTypeNode(ITypeNode typeNode) {
|
||||
this.typeNode = typeNode;
|
||||
}
|
||||
|
||||
|
||||
}
|
35
src/main/java/ast/expressions/unaryexpressions/NotNode.java
Normal file
35
src/main/java/ast/expressions/unaryexpressions/NotNode.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package ast.expressions.unaryexpressions;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.type.type.ITypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class NotNode implements IExpressionNode {
|
||||
public IExpressionNode expression;
|
||||
|
||||
public NotNode(IExpressionNode expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITypeNode getType() {
|
||||
return expression.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setType(ITypeNode type) {
|
||||
this.expression.setType(type);
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
package ast.expressions.unaryexpressions;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.statements.IStatementNode;
|
||||
import ast.type.type.*;
|
||||
import ast.type.ValueNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class UnaryNode implements IExpressionNode {
|
||||
public String thisExp;
|
||||
public String identifier;
|
||||
public MemberAccessNode memberAccess;
|
||||
public ValueNode value;
|
||||
public NotNode notExpression;
|
||||
public IStatementNode statement;
|
||||
public IExpressionNode expression;
|
||||
public ITypeNode type;
|
||||
|
||||
public UnaryNode(String value) {
|
||||
if (Objects.equals(value, "this")) {
|
||||
this.thisExp = "this";
|
||||
} else {
|
||||
this.identifier = value;
|
||||
}
|
||||
}
|
||||
|
||||
public UnaryNode(MemberAccessNode memberAccess) {
|
||||
this.memberAccess = memberAccess;
|
||||
}
|
||||
|
||||
public UnaryNode(ValueNode value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public UnaryNode(NotNode notExpression) {
|
||||
this.notExpression = notExpression;
|
||||
}
|
||||
|
||||
public UnaryNode(IStatementNode statement) {
|
||||
this.statement = statement;
|
||||
}
|
||||
|
||||
public UnaryNode(IExpressionNode expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITypeNode getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(ITypeNode type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
package ast.member;
|
||||
|
||||
import ast.block.BlockNode;
|
||||
import ast.parameter.ParameterNode;
|
||||
import ast.type.AccessModifierNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ConstructorNode extends MethodNode {
|
||||
public AccessModifierNode accessType;
|
||||
public String identifier;
|
||||
public List<ParameterNode> parameters = new ArrayList<>();
|
||||
public BlockNode body;
|
||||
|
||||
public ConstructorNode(AccessModifierNode accessType, String identifier) {
|
||||
this.accessType = accessType;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public ConstructorNode(String accessType, String identifier, BlockNode body) {
|
||||
this.accessType = new AccessModifierNode(accessType);
|
||||
this.identifier = identifier;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public void addParameter(ParameterNode parameterNode) {
|
||||
parameters.add(parameterNode);
|
||||
}
|
||||
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
package ast.member;
|
||||
|
||||
import ast.type.AccessModifierNode;
|
||||
import ast.type.TypeNode;
|
||||
|
||||
public class FieldNode implements MemberNode {
|
||||
public AccessModifierNode accessTypeNode;
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
|
||||
public FieldNode(AccessModifierNode accessTypeNode, TypeNode type, String name){
|
||||
this.accessTypeNode = accessTypeNode;
|
||||
this.type = type;
|
||||
this.identifier = name;
|
||||
}
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
package ast.member;
|
||||
|
||||
import ast.block.BlockNode;
|
||||
import ast.parameter.ParameterNode;
|
||||
import ast.type.AccessModifierNode;
|
||||
import ast.type.TypeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MethodNode implements MemberNode {
|
||||
public AccessModifierNode accesModifier;
|
||||
public TypeNode type;
|
||||
public Boolean voidType;
|
||||
public String identifier;
|
||||
public List<ParameterNode> parameters = new ArrayList<>();
|
||||
public BlockNode block;
|
||||
|
||||
public MethodNode() {}
|
||||
|
||||
public MethodNode(BlockNode block){
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public MethodNode(String accessModifier, String type, Boolean voidType, String identifier, BlockNode block){
|
||||
this.accesModifier = new AccessModifierNode(accessModifier);
|
||||
this.type = new TypeNode(type);
|
||||
this.voidType = voidType;
|
||||
this.identifier = identifier;
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public void addParameter(ParameterNode parameter) {
|
||||
this.parameters.add(parameter);
|
||||
}
|
||||
|
||||
/*
|
||||
public boolean isSame(MethodNode methodNode){
|
||||
boolean isSame = false;
|
||||
if(methodNode.identifier.equals(identifier)){
|
||||
if(parameters != null && methodNode.parameters != null){
|
||||
if(parameters.parameters.size() == methodNode.parameters.parameters.size()){
|
||||
for(int i = 0; i < parameters.parameters.size(); i++){
|
||||
if(parameters.parameters.get(i).identifier.equals(methodNode.parameters.parameters.get(i).identifier)){
|
||||
isSame = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return isSame;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
41
src/main/java/ast/members/ConstructorNode.java
Normal file
41
src/main/java/ast/members/ConstructorNode.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package ast.members;
|
||||
|
||||
import ast.parameters.ParameterNode;
|
||||
import ast.statements.BlockNode;
|
||||
import ast.type.AccessModifierNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import visitor.Visitable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ConstructorNode extends MethodNode implements Visitable {
|
||||
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
||||
this.accesModifier = new AccessModifierNode(accessType);
|
||||
this.identifier = identifier;
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public void addParameter(ParameterNode parameterNode) {
|
||||
parameters.add(parameterNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
public boolean isSame(MethodNode methodNode) {
|
||||
if (!(Objects.equals(this.identifier, methodNode.getIdentifier())) || getParameters().size() != methodNode.getParameters().size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.getParameters().size(); i++) {
|
||||
if (!this.getParameters().get(i).type.equals(methodNode.getParameters().get(i).type)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
31
src/main/java/ast/members/FieldNode.java
Normal file
31
src/main/java/ast/members/FieldNode.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ast.members;
|
||||
|
||||
import ast.type.AccessModifierNode;
|
||||
import ast.type.type.ITypeNode;
|
||||
import bytecode.visitor.ClassVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class FieldNode implements MemberNode, Visitable {
|
||||
public AccessModifierNode accessTypeNode;
|
||||
public ITypeNode type;
|
||||
public String identifier;
|
||||
|
||||
public FieldNode(AccessModifierNode accessTypeNode, ITypeNode type, String name) {
|
||||
this.accessTypeNode = accessTypeNode;
|
||||
this.type = type;
|
||||
this.identifier = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ClassVisitor classVisitor) {
|
||||
classVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
17
src/main/java/ast/members/MainMethodNode.java
Normal file
17
src/main/java/ast/members/MainMethodNode.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package ast.members;
|
||||
|
||||
import ast.statements.BlockNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class MainMethodNode extends MethodNode implements Visitable {
|
||||
|
||||
public MainMethodNode(BlockNode block) {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package ast.member;
|
||||
package ast.members;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
@@ -10,7 +10,8 @@ import ast.ASTNode;
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = MethodNode.class, name = "Method"),
|
||||
|
||||
@JsonSubTypes.Type(value = FieldNode.class, name = "Field") }
|
||||
@JsonSubTypes.Type(value = FieldNode.class, name = "Field")}
|
||||
)
|
||||
|
||||
public interface MemberNode extends ASTNode {}
|
||||
public interface MemberNode extends ASTNode {
|
||||
}
|
83
src/main/java/ast/members/MethodNode.java
Normal file
83
src/main/java/ast/members/MethodNode.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package ast.members;
|
||||
|
||||
import ast.parameters.ParameterNode;
|
||||
import ast.statements.BlockNode;
|
||||
import ast.type.AccessModifierNode;
|
||||
import ast.type.type.*;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MethodNode implements MemberNode, Visitable {
|
||||
public AccessModifierNode accesModifier;
|
||||
public ITypeNode type;
|
||||
public Boolean voidType;
|
||||
public String identifier;
|
||||
public List<ParameterNode> parameters = new ArrayList<>();
|
||||
public BlockNode block;
|
||||
|
||||
public MethodNode() {
|
||||
}
|
||||
|
||||
public MethodNode(String accessModifier, ITypeNode type, Boolean voidType, String identifier, BlockNode block) {
|
||||
this.accesModifier = new AccessModifierNode(accessModifier);
|
||||
this.type = type;
|
||||
this.voidType = voidType;
|
||||
this.identifier = identifier;
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public void addParameter(ParameterNode parameter) {
|
||||
this.parameters.add(parameter);
|
||||
}
|
||||
|
||||
public List<ParameterNode> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public boolean isSame(MethodNode methodNode) {
|
||||
if (!(Objects.equals(this.identifier, methodNode.getIdentifier())) || type.equals(methodNode.type)
|
||||
|| getParameters().size() != methodNode.getParameters().size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.getParameters().size(); i++) {
|
||||
if (!this.getParameters().get(i).type.equals(methodNode.getParameters().get(i).type)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public ITypeNode getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(ITypeNode type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package ast.parameter;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.type.TypeNode;
|
||||
|
||||
public class ParameterNode implements ASTNode {
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
|
||||
public ParameterNode(TypeNode type, String identifier) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
}
|
23
src/main/java/ast/parameters/ParameterNode.java
Normal file
23
src/main/java/ast/parameters/ParameterNode.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package ast.parameters;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.type.type.*;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class ParameterNode implements ASTNode, Visitable {
|
||||
public ITypeNode type;
|
||||
public String identifier;
|
||||
|
||||
public ParameterNode(ITypeNode type, String identifier) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
}
|
@@ -1,23 +0,0 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
|
||||
public class ForStatementNode implements IStatementNode {
|
||||
public IExpressionNode statementExpressionInit;
|
||||
public IStatementNode localVariableDeclarationInit;
|
||||
public IExpressionNode expression;
|
||||
public IExpressionNode statementExpression;
|
||||
|
||||
public ForStatementNode(IExpressionNode statementExpressionInit, IExpressionNode expression, IExpressionNode statementExpression) {
|
||||
this.statementExpressionInit = statementExpressionInit;
|
||||
this.expression = expression;
|
||||
this.statementExpression = statementExpression;
|
||||
}
|
||||
|
||||
public ForStatementNode(IStatementNode localVariableDeclarationInit, IExpressionNode expression, IExpressionNode statementExpression) {
|
||||
this.localVariableDeclarationInit = localVariableDeclarationInit;
|
||||
this.expression = expression;
|
||||
this.statementExpression = statementExpression;
|
||||
}
|
||||
}
|
@@ -1,6 +0,0 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public interface IStatementNode extends ASTNode {
|
||||
}
|
@@ -1,19 +0,0 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
import ast.type.TypeNode;
|
||||
|
||||
public class LocalVariableDeclarationNode implements IStatementNode {
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
public String assign;
|
||||
public IExpressionNode expression;
|
||||
|
||||
public LocalVariableDeclarationNode(TypeNode type, String identifier, String assign, IExpressionNode expression) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
this.assign = assign;
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
|
||||
public class ReturnStatementNode implements IStatementNode {
|
||||
public IExpressionNode expression;
|
||||
|
||||
public ReturnStatementNode(IExpressionNode expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.block.BlockNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
|
||||
public class WhileStatementNode implements IStatementNode {
|
||||
public IExpressionNode expression;
|
||||
public BlockNode block;
|
||||
|
||||
public WhileStatementNode(IExpressionNode expression, BlockNode block) {
|
||||
this.expression = expression;
|
||||
this.block = block;
|
||||
}
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
package ast.statement.ifstatement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.block.BlockNode;
|
||||
import ast.statement.IStatementNode;
|
||||
|
||||
public class ElseStatementNode implements IStatementNode {
|
||||
public BlockNode block;
|
||||
|
||||
public ElseStatementNode(BlockNode block) {
|
||||
this.block = block;
|
||||
}
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
package ast.statement.ifstatement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.statement.IStatementNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class IfElseStatementNode implements IStatementNode {
|
||||
public IfStatementNode ifStatement;
|
||||
public List<ElseStatementNode> elseStatements = new ArrayList<>();
|
||||
|
||||
public IfElseStatementNode(IfStatementNode ifStatement) {
|
||||
this.ifStatement = ifStatement;
|
||||
}
|
||||
|
||||
public void addElseStatement(ElseStatementNode elseStatement) {
|
||||
elseStatements.add(elseStatement);
|
||||
}
|
||||
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
package ast.statement.ifstatement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.block.BlockNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
import ast.statement.IStatementNode;
|
||||
|
||||
public class IfStatementNode implements IStatementNode {
|
||||
public IExpressionNode expression;
|
||||
public BlockNode block;
|
||||
|
||||
public IfStatementNode(IExpressionNode expression, BlockNode block) {
|
||||
this.expression = expression;
|
||||
this.block = block;
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package ast.statement.statementexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
|
||||
public class AssignStatementExpressionNode implements IStatementExpressionNode {
|
||||
public AssignableExpressionNode assignable;
|
||||
public IExpressionNode expression;
|
||||
|
||||
public AssignStatementExpressionNode(AssignableExpressionNode assignable, IExpressionNode expression) {
|
||||
this.assignable = assignable;
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
@@ -1,17 +0,0 @@
|
||||
package ast.statement.statementexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.unaryexpression.MemberAccessNode;
|
||||
|
||||
public class AssignableExpressionNode implements IStatementExpressionNode {
|
||||
public String identifier;
|
||||
public MemberAccessNode memberAccess;
|
||||
|
||||
public AssignableExpressionNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public AssignableExpressionNode(MemberAccessNode memberAccess) {
|
||||
this.memberAccess = memberAccess;
|
||||
}
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
package ast.statement.statementexpression;
|
||||
|
||||
import ast.statement.IStatementNode;
|
||||
|
||||
public interface IStatementExpressionNode extends IStatementNode {}
|
@@ -1,20 +0,0 @@
|
||||
package ast.statement.statementexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NewDeclarationStatementExpressionNode implements IStatementExpressionNode {
|
||||
public String identifier;
|
||||
public List<IExpressionNode> expressions = new ArrayList<>();
|
||||
|
||||
public NewDeclarationStatementExpressionNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void addExpression(IExpressionNode expression) {
|
||||
expressions.add(expression);
|
||||
}
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
package ast.statement.statementexpression.crementExpression;
|
||||
|
||||
public enum CrementType {
|
||||
PREFIX, SUFFIX
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package ast.statement.statementexpression.crementExpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.statement.statementexpression.AssignableExpressionNode;
|
||||
import ast.statement.statementexpression.IStatementExpressionNode;
|
||||
|
||||
public class DecrementExpressionNode implements IStatementExpressionNode {
|
||||
public CrementType crementType;
|
||||
public AssignableExpressionNode assignableExpression;
|
||||
|
||||
public DecrementExpressionNode(CrementType crementType, AssignableExpressionNode assignableExpression) {
|
||||
this.assignableExpression = assignableExpression;
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package ast.statement.statementexpression.crementExpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.statement.statementexpression.AssignableExpressionNode;
|
||||
import ast.statement.statementexpression.IStatementExpressionNode;
|
||||
|
||||
public class IncrementExpressionNode implements IStatementExpressionNode {
|
||||
public CrementType crementType;
|
||||
public AssignableExpressionNode assignableExpression;
|
||||
|
||||
public IncrementExpressionNode(CrementType crementType, AssignableExpressionNode assignableExpression) {
|
||||
this.assignableExpression = assignableExpression;
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
package ast.statement.statementexpression.methodcallstatementnexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ChainedMethodNode implements ASTNode {
|
||||
public String identifier;
|
||||
public List<IExpressionNode> expressions = new ArrayList<>();
|
||||
|
||||
public ChainedMethodNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void addExpression(IExpressionNode expression) {
|
||||
expressions.add(expression);
|
||||
}
|
||||
}
|
@@ -1,30 +0,0 @@
|
||||
package ast.statement.statementexpression.methodcallstatementnexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.IExpressionNode;
|
||||
import ast.statement.statementexpression.IStatementExpressionNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MethodCallStatementExpressionNode implements IStatementExpressionNode {
|
||||
public TargetNode target;
|
||||
public List<ChainedMethodNode> chainedMethods = new ArrayList<>();
|
||||
public String identifier;
|
||||
public List<IExpressionNode> expressions = new ArrayList<>();
|
||||
|
||||
public MethodCallStatementExpressionNode(TargetNode target, String identifier) {
|
||||
this.target = target;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void addChainedMethod(ChainedMethodNode chainedMethode) {
|
||||
chainedMethods.add(chainedMethode);
|
||||
}
|
||||
|
||||
public void addExpression(IExpressionNode expression) {
|
||||
expressions.add(expression);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
package ast.statement.statementexpression.methodcallstatementnexpression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expression.unaryexpression.MemberAccessNode;
|
||||
import ast.statement.statementexpression.NewDeclarationStatementExpressionNode;
|
||||
|
||||
public class TargetNode implements ASTNode {
|
||||
public Boolean thisTar;
|
||||
public MemberAccessNode memberAccess;
|
||||
public NewDeclarationStatementExpressionNode newDeclaration;
|
||||
public String identifier;
|
||||
|
||||
public TargetNode(Boolean thisTar) {
|
||||
this.thisTar = thisTar;
|
||||
}
|
||||
|
||||
public TargetNode(MemberAccessNode memberAccess) {
|
||||
this.memberAccess = memberAccess;
|
||||
}
|
||||
|
||||
public TargetNode(NewDeclarationStatementExpressionNode newDeclaration) {
|
||||
this.newDeclaration = newDeclaration;
|
||||
}
|
||||
|
||||
public TargetNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
}
|
27
src/main/java/ast/statementexpressions/AssignNode.java
Normal file
27
src/main/java/ast/statementexpressions/AssignNode.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package ast.statementexpressions;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class AssignNode implements IStatementExpressionNode {
|
||||
public AssignableNode assignable;
|
||||
public IExpressionNode expression;
|
||||
|
||||
public AssignNode(AssignableNode assignable, IExpressionNode expression) {
|
||||
this.assignable = assignable;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
32
src/main/java/ast/statementexpressions/AssignableNode.java
Normal file
32
src/main/java/ast/statementexpressions/AssignableNode.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package ast.statementexpressions;
|
||||
|
||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||
import ast.type.type.ITypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class AssignableNode implements IStatementExpressionNode {
|
||||
public String identifier;
|
||||
public ITypeNode typeNode;
|
||||
|
||||
public MemberAccessNode memberAccess;
|
||||
|
||||
public AssignableNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public AssignableNode(MemberAccessNode memberAccess) {
|
||||
this.memberAccess = memberAccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
public void setTypeNode(ITypeNode typeNode) {
|
||||
this.typeNode = typeNode;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
package ast.statementexpressions;
|
||||
|
||||
import ast.statements.IStatementNode;
|
||||
|
||||
public interface IStatementExpressionNode extends IStatementNode {
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package ast.statementexpressions;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NewDeclarationNode implements IStatementExpressionNode {
|
||||
public String identifier;
|
||||
public List<IExpressionNode> expressions = new ArrayList<>();
|
||||
|
||||
public NewDeclarationNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void addExpression(IExpressionNode expression) {
|
||||
expressions.add(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package ast.statementexpressions.crementexpressions;
|
||||
|
||||
public enum CrementType {
|
||||
PREFIX, SUFFIX
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package ast.statementexpressions.crementexpressions;
|
||||
|
||||
import ast.statementexpressions.AssignableNode;
|
||||
import ast.statementexpressions.IStatementExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class DecrementNode implements IStatementExpressionNode, Visitable {
|
||||
public CrementType crementType;
|
||||
public AssignableNode assignableExpression;
|
||||
|
||||
public DecrementNode(CrementType crementType, AssignableNode assignableExpression) {
|
||||
this.crementType = crementType;
|
||||
this.assignableExpression = assignableExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package ast.statementexpressions.crementexpressions;
|
||||
|
||||
import ast.statementexpressions.AssignableNode;
|
||||
import ast.statementexpressions.IStatementExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import visitor.Visitable;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class IncrementNode implements IStatementExpressionNode, Visitable {
|
||||
public CrementType crementType;
|
||||
public AssignableNode assignableExpression;
|
||||
|
||||
public IncrementNode(CrementType crementType, AssignableNode assignableExpression) {
|
||||
this.crementType = crementType;
|
||||
this.assignableExpression = assignableExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package ast.statementexpressions.methodcallstatementnexpressions;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expressions.IExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ChainedMethodNode implements ASTNode, Visitable {
|
||||
public String identifier;
|
||||
public List<IExpressionNode> expressions = new ArrayList<>();
|
||||
|
||||
public ChainedMethodNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void addExpression(IExpressionNode expression) {
|
||||
expressions.add(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package ast.statementexpressions.methodcallstatementnexpressions;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.statements.IStatementNode;
|
||||
import ast.type.type.ITypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MethodCallNode implements IStatementNode {
|
||||
public TargetNode target;
|
||||
public ITypeNode type;
|
||||
public List<ChainedMethodNode> chainedMethods = new ArrayList<>();
|
||||
public String identifier;
|
||||
public List<IExpressionNode> parameters = new ArrayList<>();
|
||||
|
||||
public MethodCallNode(TargetNode target, String identifier) {
|
||||
this.target = target;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void addChainedMethod(ChainedMethodNode chainedMethode) {
|
||||
chainedMethods.add(chainedMethode);
|
||||
}
|
||||
|
||||
public void addExpression(IExpressionNode parameter) {
|
||||
parameters.add(parameter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package ast.statementexpressions.methodcallstatementnexpressions;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||
import ast.statementexpressions.NewDeclarationNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class TargetNode implements ASTNode, Visitable {
|
||||
public Boolean thisTar;
|
||||
public MemberAccessNode memberAccess;
|
||||
public NewDeclarationNode newDeclaration;
|
||||
public String identifier;
|
||||
|
||||
public TargetNode(Boolean thisTar) {
|
||||
this.thisTar = thisTar;
|
||||
}
|
||||
|
||||
public TargetNode(MemberAccessNode memberAccess) {
|
||||
this.memberAccess = memberAccess;
|
||||
}
|
||||
|
||||
public TargetNode(NewDeclarationNode newDeclaration) {
|
||||
this.newDeclaration = newDeclaration;
|
||||
}
|
||||
|
||||
public TargetNode(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
31
src/main/java/ast/statements/BlockNode.java
Normal file
31
src/main/java/ast/statements/BlockNode.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ast.statements;
|
||||
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockNode implements IStatementNode, Visitable {
|
||||
public List<IStatementNode> statements = new ArrayList<>();
|
||||
|
||||
public BlockNode() {
|
||||
}
|
||||
|
||||
public void addStatement(IStatementNode statement) {
|
||||
statements.add(statement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
19
src/main/java/ast/statements/ElseNode.java
Normal file
19
src/main/java/ast/statements/ElseNode.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ast.statements;
|
||||
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class ElseNode implements IStatementNode {
|
||||
public BlockNode block;
|
||||
|
||||
public ElseNode(BlockNode block) {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
7
src/main/java/ast/statements/IStatementNode.java
Normal file
7
src/main/java/ast/statements/IStatementNode.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package ast.statements;
|
||||
|
||||
import ast.ASTNode;
|
||||
import visitor.Visitable;
|
||||
|
||||
public interface IStatementNode extends ASTNode, Visitable {
|
||||
}
|
33
src/main/java/ast/statements/IfElseNode.java
Normal file
33
src/main/java/ast/statements/IfElseNode.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package ast.statements;
|
||||
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class IfElseNode implements IStatementNode {
|
||||
public IfNode ifStatement;
|
||||
public List<IfNode> elseIfStatements = new ArrayList<>();
|
||||
public ElseNode elseStatement;
|
||||
|
||||
public IfElseNode(IfNode ifStatement, ElseNode elseNode) {
|
||||
this.ifStatement = ifStatement;
|
||||
this.elseStatement = elseNode;
|
||||
}
|
||||
|
||||
public void addElseIfStatement(IfNode elseIfStament) {
|
||||
elseIfStatements.add(elseIfStament);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
20
src/main/java/ast/statements/IfNode.java
Normal file
20
src/main/java/ast/statements/IfNode.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package ast.statements;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class IfNode implements IStatementNode {
|
||||
public IExpressionNode expression;
|
||||
public BlockNode block;
|
||||
|
||||
public IfNode(IExpressionNode expression, BlockNode block) {
|
||||
this.expression = expression;
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package ast.statements;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.type.type.*;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class LocalVariableDeclarationNode implements IStatementNode {
|
||||
public ITypeNode type;
|
||||
public String identifier;
|
||||
public String assign;
|
||||
public IExpressionNode expression;
|
||||
|
||||
public LocalVariableDeclarationNode(ITypeNode type, String identifier, String assign, IExpressionNode expression) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
this.assign = assign;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
30
src/main/java/ast/statements/ReturnNode.java
Normal file
30
src/main/java/ast/statements/ReturnNode.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package ast.statements;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class ReturnNode implements IStatementNode {
|
||||
public IExpressionNode expression;
|
||||
public Boolean voidReturn = false;
|
||||
|
||||
public ReturnNode(IExpressionNode expression) {
|
||||
if (expression != null) {
|
||||
this.expression = expression;
|
||||
} else {
|
||||
voidReturn = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
30
src/main/java/ast/statements/WhileNode.java
Normal file
30
src/main/java/ast/statements/WhileNode.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package ast.statements;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
|
||||
public class WhileNode implements IStatementNode {
|
||||
public IExpressionNode expression;
|
||||
public BlockNode block;
|
||||
|
||||
public WhileNode(IExpressionNode expression, BlockNode block) {
|
||||
this.expression = expression;
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public void test() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
@@ -8,7 +8,7 @@ public class AccessModifierNode {
|
||||
}
|
||||
|
||||
private void setModifier(String accessType) {
|
||||
switch(accessType) {
|
||||
switch (accessType) {
|
||||
case "public":
|
||||
this.accessType = EnumAccessModifierNode.PUBLIC;
|
||||
break;
|
||||
|
@@ -1,5 +0,0 @@
|
||||
package ast.type;
|
||||
|
||||
public enum EnumTypeNode {
|
||||
INT, BOOLEAN, CHAR, IDENTIFIER
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
package ast.type;
|
||||
|
||||
public class TypeNode {
|
||||
public EnumTypeNode type;
|
||||
|
||||
public TypeNode(String type) {
|
||||
setType(type);
|
||||
}
|
||||
|
||||
private void setType(String type) {
|
||||
switch(type) {
|
||||
case "int":
|
||||
this.type = EnumTypeNode.INT;
|
||||
break;
|
||||
case "boolean":
|
||||
this.type = EnumTypeNode.BOOLEAN;
|
||||
break;
|
||||
case "char":
|
||||
this.type = EnumTypeNode.CHAR;
|
||||
break;
|
||||
default:
|
||||
this.type = EnumTypeNode.IDENTIFIER;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,8 +1,12 @@
|
||||
package ast.type;
|
||||
|
||||
import ast.ASTNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import semantic.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class ValueNode implements ASTNode {
|
||||
public class ValueNode implements ASTNode, Visitable {
|
||||
public EnumValueNode valueType;
|
||||
public String value;
|
||||
|
||||
@@ -10,4 +14,14 @@ public class ValueNode implements ASTNode {
|
||||
this.valueType = valueType;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
26
src/main/java/ast/type/type/BaseType.java
Normal file
26
src/main/java/ast/type/type/BaseType.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package ast.type.type;
|
||||
|
||||
public class BaseType implements ITypeNode {
|
||||
|
||||
public final TypeEnum typeEnum;
|
||||
|
||||
public BaseType(TypeEnum typeEnum) {
|
||||
this.typeEnum = typeEnum;
|
||||
}
|
||||
|
||||
public TypeEnum getTypeEnum() {
|
||||
return typeEnum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
BaseType other = (BaseType) obj;
|
||||
return typeEnum == other.typeEnum;
|
||||
}
|
||||
}
|
4
src/main/java/ast/type/type/ITypeNode.java
Normal file
4
src/main/java/ast/type/type/ITypeNode.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package ast.type.type;
|
||||
|
||||
public interface ITypeNode {
|
||||
}
|
30
src/main/java/ast/type/type/ReferenceType.java
Normal file
30
src/main/java/ast/type/type/ReferenceType.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package ast.type.type;
|
||||
|
||||
public class ReferenceType implements ITypeNode {
|
||||
|
||||
public final String identifier;
|
||||
|
||||
public ReferenceType(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
ReferenceType other = (ReferenceType) obj;
|
||||
if (identifier == null) {
|
||||
return other.identifier == null;
|
||||
} else return identifier.equals(other.identifier);
|
||||
}
|
||||
|
||||
|
||||
}
|
9
src/main/java/ast/type/type/TypeEnum.java
Normal file
9
src/main/java/ast/type/type/TypeEnum.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package ast.type.type;
|
||||
|
||||
public enum TypeEnum {
|
||||
VOID,
|
||||
INT,
|
||||
CHAR,
|
||||
BOOL,
|
||||
NULL
|
||||
}
|
@@ -2,15 +2,89 @@ package bytecode;
|
||||
|
||||
import ast.ProgramNode;
|
||||
import ast.ClassNode;
|
||||
import ast.members.MainMethodNode;
|
||||
import ast.members.MemberNode;
|
||||
import bytecode.visitor.ProgramVisitor;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
public class ByteCodeGenerator implements ProgramVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(ProgramNode programNode) {
|
||||
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||
ClassCodeGen classCodeGen = new ClassCodeGen();
|
||||
classDeclarationNode.accept(classCodeGen);
|
||||
private JarOutputStream jarOutputStream;
|
||||
private ByteArrayOutputStream byteArrayOutputStream;
|
||||
private final String outputDirectory;
|
||||
private final boolean generateJar;
|
||||
private final boolean generateClassFiles;
|
||||
|
||||
public ByteCodeGenerator(String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
||||
this.outputDirectory = outputDirectory;
|
||||
this.generateJar = generateJar;
|
||||
this.generateClassFiles = generateClassFiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ProgramNode programNode) {
|
||||
if(generateJar) {
|
||||
byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
try {
|
||||
Manifest manifest = new Manifest();
|
||||
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
|
||||
boolean foundMainClass = false;
|
||||
for (ClassNode classNode : programNode.classes) {
|
||||
if (foundMainClass) {
|
||||
break;
|
||||
}
|
||||
for (MemberNode memberNode : classNode.members) {
|
||||
if (memberNode instanceof MainMethodNode) {
|
||||
manifest.getMainAttributes().putValue("Main-Class", classNode.identifier);
|
||||
foundMainClass = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
jarOutputStream = new JarOutputStream(byteArrayOutputStream, manifest);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||
ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream, outputDirectory, generateJar, generateClassFiles);
|
||||
classDeclarationNode.accept(classCodeGen);
|
||||
}
|
||||
|
||||
try {
|
||||
jarOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
saveJarFile(byteArrayOutputStream.toByteArray(), "output.jar");
|
||||
} else {
|
||||
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||
ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream, outputDirectory, generateJar, generateClassFiles);
|
||||
classDeclarationNode.accept(classCodeGen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void saveJarFile(byte[] jarBytes, String jarFileName) {
|
||||
File directory = new File(outputDirectory);
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
|
||||
File jarFile = new File(directory, jarFileName);
|
||||
try (FileOutputStream fos = new FileOutputStream(jarFile)) {
|
||||
fos.write(jarBytes);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,25 +1,37 @@
|
||||
package bytecode;
|
||||
|
||||
import ast.ClassNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MemberNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.type.BaseTypeNode;
|
||||
import ast.members.FieldNode;
|
||||
import ast.members.MemberNode;
|
||||
import ast.members.MethodNode;
|
||||
import ast.type.type.BaseType;
|
||||
import bytecode.visitor.ClassVisitor;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
|
||||
|
||||
public class ClassCodeGen implements ClassVisitor {
|
||||
private Mapper mapper;
|
||||
private final Mapper mapper;
|
||||
private ClassWriter classWriter;
|
||||
private final JarOutputStream jarOutputStream;
|
||||
private final String outputDirectory;
|
||||
private final boolean generateJar;
|
||||
private final boolean generateClassFiles;
|
||||
|
||||
public ClassCodeGen() {
|
||||
mapper = new Mapper();
|
||||
public ClassCodeGen(JarOutputStream jarOutputStream, String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
||||
this.mapper = new Mapper();
|
||||
this.jarOutputStream = jarOutputStream;
|
||||
this.outputDirectory = outputDirectory;
|
||||
this.generateJar = generateJar;
|
||||
this.generateClassFiles = generateClassFiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,28 +49,31 @@ public class ClassCodeGen implements ClassVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
classWriter.visitEnd();
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier);
|
||||
if (generateJar) {
|
||||
writeToJar(classWriter.toByteArray(), classNode.identifier);
|
||||
}
|
||||
if (generateClassFiles) {
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier, outputDirectory);
|
||||
}
|
||||
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(FieldNode fieldNode) {
|
||||
if(fieldNode.type instanceof BaseTypeNode baseTypeNode){
|
||||
classWriter.visitField(mapper.mapAccessTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, mapper.getTypeChar(baseTypeNode.enumType), null, null );
|
||||
if (fieldNode.type instanceof BaseType) {
|
||||
classWriter.visitField(mapper.mapAccessTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, mapper.getTypeChar((BaseType) fieldNode.type), null, null);
|
||||
}
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
|
||||
private void printIntoClassFile(byte[] byteCode, String name) {
|
||||
String directoryPath = "src/main/java/classFileOutput";
|
||||
File directory = new File(directoryPath);
|
||||
private void printIntoClassFile(byte[] byteCode, String name, String outputDirectory) {
|
||||
File directory = new File(outputDirectory);
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
|
||||
String filePath = directoryPath + "/" + name + ".class";
|
||||
String filePath = outputDirectory + "/" + name + ".class";
|
||||
try {
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||
fileOutputStream.write(byteCode);
|
||||
@@ -67,4 +82,15 @@ public class ClassCodeGen implements ClassVisitor {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void writeToJar(byte[] byteCode, String className) {
|
||||
try {
|
||||
JarEntry jarEntry = new JarEntry(className + ".class");
|
||||
jarOutputStream.putNextEntry(jarEntry);
|
||||
jarOutputStream.write(byteCode);
|
||||
jarOutputStream.closeEntry();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,43 +1,64 @@
|
||||
package bytecode;
|
||||
|
||||
import ast.parameter.ParameterNode;
|
||||
import ast.parameters.ParameterNode;
|
||||
import ast.type.*;
|
||||
import ast.type.type.BaseType;
|
||||
import ast.type.type.ITypeNode;
|
||||
import ast.type.type.ReferenceType;
|
||||
import ast.type.type.TypeEnum;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import ast.type.BaseTypeNode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Mapper {
|
||||
public int mapAccessTypeToOpcode(AccessTypeNode type) {
|
||||
switch (type.enumAccessTypeNode) {
|
||||
case EnumAccessTypeNode.PUBLIC:
|
||||
public int mapAccessTypeToOpcode(AccessModifierNode accessModifierNode) {
|
||||
switch (accessModifierNode.accessType) {
|
||||
case EnumAccessModifierNode.PUBLIC:
|
||||
return Opcodes.ACC_PUBLIC;
|
||||
case EnumAccessTypeNode.PRIVATE:
|
||||
case EnumAccessModifierNode.PRIVATE:
|
||||
return Opcodes.ACC_PRIVATE;
|
||||
case EnumAccessModifierNode.PUBLIC_STATIC:
|
||||
return Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
|
||||
case EnumAccessModifierNode.PRIVATE_STATIC:
|
||||
return Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String generateMethodDescriptor(BaseTypeNode baseTypeNode, ParameterListNode parameterListNode) {
|
||||
public String generateMethodDescriptor(ITypeNode type, List<ParameterNode> parameters) {
|
||||
String descriptor = "(";
|
||||
for(ParameterNode parameterNode : parameterListNode.parameters) {
|
||||
descriptor += getTypeChar(EnumTypeNode.INT);
|
||||
for (ParameterNode parameterNode : parameters) {
|
||||
if(parameterNode.type instanceof BaseType) {
|
||||
descriptor += getTypeChar((BaseType) parameterNode.type);
|
||||
} else {
|
||||
ReferenceType referenceType = (ReferenceType) parameterNode.type;
|
||||
descriptor += "L" + referenceType.getIdentifier() + ";";
|
||||
}
|
||||
}
|
||||
descriptor += ")";
|
||||
descriptor += getTypeChar(baseTypeNode.enumType);
|
||||
if(type instanceof BaseType) {
|
||||
descriptor += getTypeChar((BaseType) type);
|
||||
} else if(type instanceof ReferenceType) {
|
||||
descriptor += "L" + ((ReferenceType) type).getIdentifier() +";";
|
||||
}
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
public String getTypeChar(EnumTypeNode enumTypeNode) {
|
||||
public String getTypeChar(BaseType type) {
|
||||
String typeChar = "";
|
||||
switch (enumTypeNode) {
|
||||
case EnumTypeNode.INT:
|
||||
switch (type.getTypeEnum()) {
|
||||
case TypeEnum.INT:
|
||||
typeChar = "I";
|
||||
break;
|
||||
case EnumTypeNode.CHAR:
|
||||
case TypeEnum.CHAR:
|
||||
typeChar = "C";
|
||||
break;
|
||||
case EnumTypeNode.BOOLEAN:
|
||||
case TypeEnum.BOOL:
|
||||
typeChar = "Z";
|
||||
break;
|
||||
case TypeEnum.VOID:
|
||||
typeChar = "V";
|
||||
break;
|
||||
}
|
||||
return typeChar;
|
||||
}
|
||||
|
@@ -1,9 +1,28 @@
|
||||
package bytecode;
|
||||
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.parameter.ParameterNode;
|
||||
import ast.type.BaseTypeNode;
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.expressions.binaryexpressions.*;
|
||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||
import ast.expressions.unaryexpressions.NotNode;
|
||||
import ast.expressions.unaryexpressions.UnaryNode;
|
||||
import ast.members.ConstructorNode;
|
||||
import ast.members.MainMethodNode;
|
||||
import ast.members.MethodNode;
|
||||
import ast.parameters.ParameterNode;
|
||||
import ast.statementexpressions.AssignNode;
|
||||
import ast.statementexpressions.AssignableNode;
|
||||
import ast.statementexpressions.NewDeclarationNode;
|
||||
import ast.statementexpressions.crementexpressions.CrementType;
|
||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||
import ast.statements.*;
|
||||
import ast.type.AccessModifierNode;
|
||||
import ast.type.EnumAccessModifierNode;
|
||||
import ast.type.ValueNode;
|
||||
import ast.type.type.BaseType;
|
||||
import ast.type.type.ReferenceType;
|
||||
import ast.type.type.TypeEnum;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
@@ -17,82 +36,709 @@ import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
|
||||
private ClassWriter classWriter;
|
||||
private Mapper mapper;
|
||||
private final ClassWriter classWriter;
|
||||
private final Mapper mapper;
|
||||
private MethodVisitor methodVisitor;
|
||||
|
||||
private List<String> localVaribales;
|
||||
private final List<String> localVariables;
|
||||
|
||||
public MethodCodeGen(ClassWriter classWriter) {
|
||||
this.classWriter = classWriter;
|
||||
mapper = new Mapper();
|
||||
localVaribales = new ArrayList<>();
|
||||
localVariables = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
// Method nodes
|
||||
|
||||
@Override
|
||||
public void visit(ConstructorNode constructorNode) {
|
||||
methodVisitor =
|
||||
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.visibility),
|
||||
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accesModifier),
|
||||
"<init>",
|
||||
"()V",
|
||||
mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), constructorNode.parameters),
|
||||
null,
|
||||
null);
|
||||
|
||||
methodVisitor.visitCode();
|
||||
localVariables.add("this");
|
||||
// Add all method parameters to localVariables
|
||||
for (ParameterNode parameterNode : constructorNode.parameters) {
|
||||
localVariables.add(parameterNode.identifier);
|
||||
}
|
||||
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
||||
methodVisitor.visitInsn(RETURN);
|
||||
methodVisitor.visitMaxs(1, 1);
|
||||
|
||||
// Visit all statements
|
||||
for (IStatementNode statementNode : constructorNode.block.statements) {
|
||||
statementNode.accept(this);
|
||||
}
|
||||
|
||||
methodVisitor.visitMaxs(0, 0);
|
||||
methodVisitor.visitEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MainMethodNode mainMethodNode) {
|
||||
AccessModifierNode accessModifierNode = new AccessModifierNode("");
|
||||
accessModifierNode.accessType = EnumAccessModifierNode.PUBLIC_STATIC;
|
||||
methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
|
||||
"main",
|
||||
"([Ljava/lang/String;)V",
|
||||
null,
|
||||
null);
|
||||
|
||||
methodVisitor.visitCode();
|
||||
localVariables.add("this");
|
||||
localVariables.add("args");
|
||||
|
||||
// Visit all statements
|
||||
for (IStatementNode statementNode : mainMethodNode.block.statements) {
|
||||
statementNode.accept(this);
|
||||
}
|
||||
|
||||
methodVisitor.visitMaxs(0, 0);
|
||||
methodVisitor.visitEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MethodNode methodNode) {
|
||||
if (methodNode.type instanceof BaseTypeNode baseTypeNode) {
|
||||
methodVisitor = classWriter.visitMethod(mapper.mapAccessTypeToOpcode(methodNode.visibility),
|
||||
methodNode.identifier,
|
||||
mapper.generateMethodDescriptor(baseTypeNode, methodNode.parameters),
|
||||
null,
|
||||
null);
|
||||
methodVisitor = classWriter.visitMethod(mapper.mapAccessTypeToOpcode(methodNode.accesModifier),
|
||||
methodNode.getIdentifier(),
|
||||
mapper.generateMethodDescriptor(methodNode.getType(), methodNode.parameters),
|
||||
null,
|
||||
null);
|
||||
|
||||
methodVisitor.visitCode();
|
||||
localVaribales.add("this");
|
||||
for (ParameterNode parameterNode : methodNode.parameters.parameters) {
|
||||
localVaribales.add(parameterNode.identifier);
|
||||
methodVisitor.visitCode();
|
||||
localVariables.add("this");
|
||||
// Add all method parameters to localVariables
|
||||
for (ParameterNode parameterNode : methodNode.parameters) {
|
||||
localVariables.add(parameterNode.identifier);
|
||||
}
|
||||
|
||||
// Visit all statements
|
||||
for (IStatementNode statementNode : methodNode.block.statements) {
|
||||
statementNode.accept(this);
|
||||
}
|
||||
|
||||
methodVisitor.visitMaxs(0, 0);
|
||||
methodVisitor.visitEnd();
|
||||
}
|
||||
|
||||
|
||||
// Binary expressions
|
||||
|
||||
@Override
|
||||
public void visit(BinaryNode binaryNode) {
|
||||
binaryNode.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(CalculationNode calculationNode) {
|
||||
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:
|
||||
methodVisitor.visitInsn(IADD);
|
||||
break;
|
||||
case MINUS:
|
||||
methodVisitor.visitInsn(ISUB);
|
||||
break;
|
||||
}
|
||||
|
||||
//test();
|
||||
methodVisitor.visitMaxs(1, localVaribales.size());
|
||||
methodVisitor.visitEnd();
|
||||
}
|
||||
}
|
||||
|
||||
public void test() {
|
||||
Label start = new Label();
|
||||
Label loop = new Label();
|
||||
Label end = new Label();
|
||||
methodVisitor.visitLabel(start);
|
||||
//methodVisitor.visitVarInsn(Opcodes.ICONST_M1, 99);
|
||||
//methodVisitor.visitInsn(Opcodes.ICONST_5);
|
||||
methodVisitor.visitLdcInsn(99);
|
||||
// methodVisitor.visitInsn(Opcodes.ICONST_0);
|
||||
//methodVisitor.visitVarInsn(Opcodes.ILOAD, 2);
|
||||
methodVisitor.visitVarInsn(Opcodes.ISTORE, 1);
|
||||
methodVisitor.visitLabel(loop);
|
||||
methodVisitor.visitVarInsn(Opcodes.ILOAD, 1);
|
||||
methodVisitor.visitInsn(Opcodes.ICONST_5);
|
||||
methodVisitor.visitJumpInsn(Opcodes.IF_ICMPGE, end);
|
||||
methodVisitor.visitFieldInsn(Opcodes.GETSTATIC,
|
||||
"java/lang/System", "out",
|
||||
"Ljava/io/PrintStream;");
|
||||
methodVisitor.visitLdcInsn("Bytecode");
|
||||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
|
||||
"java/io/PrintStream", "println",
|
||||
"(Ljava/lang/String;)V", false);
|
||||
methodVisitor.visitIincInsn(1, 1);
|
||||
methodVisitor.visitJumpInsn(Opcodes.GOTO, loop);
|
||||
methodVisitor.visitLabel(end);
|
||||
methodVisitor.visitVarInsn(Opcodes.ILOAD, 1);
|
||||
methodVisitor.visitInsn(Opcodes.IRETURN);
|
||||
methodVisitor.visitEnd();
|
||||
@Override
|
||||
public void visit(DotNode dotNode) {
|
||||
if (dotNode.dotExpression != null) {
|
||||
dotNode.dotExpression.accept(this);
|
||||
}
|
||||
if (dotNode.dotSubtractionExpression != null) {
|
||||
dotNode.dotSubtractionExpression.accept(this);
|
||||
}
|
||||
if (dotNode.operator != null) {
|
||||
switch (dotNode.operator) {
|
||||
case DIV:
|
||||
methodVisitor.visitInsn(IDIV);
|
||||
break;
|
||||
case MULT:
|
||||
methodVisitor.visitInsn(IMUL);
|
||||
break;
|
||||
case MOD:
|
||||
methodVisitor.visitInsn(IREM);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DotSubtractionNode dotSubtractionNode) {
|
||||
if (dotSubtractionNode.value != null) {
|
||||
dotSubtractionNode.value.accept(this);
|
||||
} else if (dotSubtractionNode.identifier != null) {
|
||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(dotSubtractionNode.identifier));
|
||||
} else if (dotSubtractionNode.memberAccess != null) {
|
||||
dotSubtractionNode.memberAccess.accept(this);
|
||||
} else if (dotSubtractionNode.methodCall != null) {
|
||||
dotSubtractionNode.methodCall.accept(this);
|
||||
} else if (dotSubtractionNode.calculationExpression != null) {
|
||||
dotSubtractionNode.calculationExpression.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NonCalculationNode nonCalculationNode) {
|
||||
Label labelFalse = new Label();
|
||||
Label labelTrue = new Label();
|
||||
switch (nonCalculationNode.operator) {
|
||||
case AND:
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IFEQ, labelFalse); // Value compared to 0 (false)
|
||||
nonCalculationNode.expression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IFEQ, labelFalse);
|
||||
break;
|
||||
case OR:
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IFNE, labelTrue);
|
||||
nonCalculationNode.expression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IFEQ, labelFalse);
|
||||
break;
|
||||
case GREATER:
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
nonCalculationNode.expression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IF_ICMPLE, labelFalse);
|
||||
break;
|
||||
case LESS:
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
nonCalculationNode.expression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IF_ICMPGE, labelFalse);
|
||||
break;
|
||||
case GREATER_EQUAL:
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
nonCalculationNode.expression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IF_ICMPLT, labelFalse);
|
||||
break;
|
||||
case LESS_EQUAL:
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
nonCalculationNode.expression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IF_ICMPGT, labelFalse);
|
||||
break;
|
||||
case EQUAL:
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
nonCalculationNode.expression.accept(this);
|
||||
if (nonCalculationNode.unaryExpression.getType() instanceof BaseType && nonCalculationNode.expression.getType() instanceof BaseType) {
|
||||
methodVisitor.visitJumpInsn(IF_ICMPNE, labelFalse);
|
||||
} else {
|
||||
methodVisitor.visitJumpInsn(IF_ACMPNE, labelFalse);
|
||||
}
|
||||
break;
|
||||
case NOT_EQUAL:
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
nonCalculationNode.expression.accept(this);
|
||||
if (nonCalculationNode.unaryExpression.getType() instanceof BaseType && nonCalculationNode.expression.getType() instanceof BaseType) {
|
||||
methodVisitor.visitJumpInsn(IF_ICMPEQ, labelFalse);
|
||||
} else {
|
||||
methodVisitor.visitJumpInsn(IF_ACMPEQ, labelFalse);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Label labelEndLogicalExpression = new Label();
|
||||
|
||||
methodVisitor.visitLabel(labelTrue);
|
||||
methodVisitor.visitInsn(ICONST_1); // true
|
||||
methodVisitor.visitJumpInsn(GOTO, labelEndLogicalExpression);
|
||||
|
||||
methodVisitor.visitLabel(labelFalse);
|
||||
methodVisitor.visitInsn(ICONST_0); // false
|
||||
|
||||
methodVisitor.visitLabel(labelEndLogicalExpression);
|
||||
}
|
||||
|
||||
|
||||
// Unary expressions
|
||||
|
||||
@Override
|
||||
public void visit(MemberAccessNode memberAccessNode) {
|
||||
// Only used to get, not to put
|
||||
int localVarIndex = localVariables.indexOf("memberAccessNode.identifier"); // TODO
|
||||
if (localVarIndex >= 0) { // local var object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else { // this field
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
}
|
||||
|
||||
if (memberAccessNode.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), mapper.getTypeChar((BaseType) memberAccessNode.getTypeNode()));
|
||||
} else if (memberAccessNode.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), "L" + ((ReferenceType) memberAccessNode.getTypeNode()).getIdentifier() + ";");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NotNode notNode) {
|
||||
Label labelFalse = new Label();
|
||||
Label labelTrue = new Label();
|
||||
|
||||
notNode.expression.accept(this);
|
||||
methodVisitor.visitJumpInsn(Opcodes.IFNE, labelFalse); // false if value is true (value != 0)
|
||||
|
||||
Label labelEndLogicalExpression = new Label();
|
||||
|
||||
methodVisitor.visitLabel(labelTrue);
|
||||
methodVisitor.visitInsn(ICONST_1); // true
|
||||
methodVisitor.visitJumpInsn(GOTO, labelEndLogicalExpression);
|
||||
|
||||
methodVisitor.visitLabel(labelFalse);
|
||||
methodVisitor.visitInsn(ICONST_0); // false
|
||||
|
||||
methodVisitor.visitLabel(labelEndLogicalExpression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(UnaryNode unaryNode) {
|
||||
if (unaryNode.thisExp != null) {
|
||||
methodVisitor.visitVarInsn(ALOAD, 0); // this
|
||||
} else if (unaryNode.memberAccess != null) {
|
||||
unaryNode.memberAccess.setTypeNode(unaryNode.getType());
|
||||
unaryNode.memberAccess.accept(this);
|
||||
} else if (unaryNode.value != null) {
|
||||
unaryNode.value.accept(this);
|
||||
} else if (unaryNode.notExpression != null) {
|
||||
unaryNode.notExpression.accept(this);
|
||||
} else if (unaryNode.statement != null) {
|
||||
unaryNode.statement.accept(this);
|
||||
} else if (unaryNode.expression != null) {
|
||||
unaryNode.expression.accept(this);
|
||||
} else if (unaryNode.identifier != null) {
|
||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Statements
|
||||
|
||||
@Override
|
||||
public void visit(BlockNode blockNode) {
|
||||
for (IStatementNode statementNode : blockNode.statements) {
|
||||
statementNode.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(IfElseNode ifElseNode) {
|
||||
Label elseLabel = new Label();
|
||||
Label endLabel = new Label();
|
||||
|
||||
Label[] elseIfLabels = new Label[ifElseNode.elseIfStatements.size()];
|
||||
for (int i = 0; i < ifElseNode.elseIfStatements.size(); i++) {
|
||||
elseIfLabels[i] = new Label();
|
||||
}
|
||||
|
||||
ifElseNode.ifStatement.expression.accept(this);
|
||||
if (ifElseNode.elseIfStatements.isEmpty()) {
|
||||
// No else if
|
||||
methodVisitor.visitJumpInsn(IFEQ, elseLabel);
|
||||
} else {
|
||||
// else if statements
|
||||
methodVisitor.visitJumpInsn(IFEQ, elseIfLabels[0]);
|
||||
}
|
||||
|
||||
ifElseNode.ifStatement.block.accept(this); // accept if block
|
||||
|
||||
methodVisitor.visitJumpInsn(GOTO, endLabel);
|
||||
|
||||
for (int i = 0; i < ifElseNode.elseIfStatements.size(); i++) {
|
||||
methodVisitor.visitLabel(elseIfLabels[i]);
|
||||
ifElseNode.elseIfStatements.get(i).expression.accept(this);
|
||||
if (i + 1 < elseIfLabels.length) {
|
||||
// at least one more else if
|
||||
methodVisitor.visitJumpInsn(IFEQ, elseIfLabels[i + 1]);
|
||||
} else {
|
||||
methodVisitor.visitJumpInsn(IFEQ, elseLabel);
|
||||
}
|
||||
ifElseNode.elseIfStatements.get(i).block.accept(this);
|
||||
methodVisitor.visitJumpInsn(GOTO, endLabel);
|
||||
}
|
||||
|
||||
if (ifElseNode.elseStatement != null) {
|
||||
methodVisitor.visitLabel(elseLabel);
|
||||
ifElseNode.elseStatement.block.accept(this);
|
||||
} else {
|
||||
methodVisitor.visitLabel(elseLabel);
|
||||
}
|
||||
|
||||
methodVisitor.visitLabel(endLabel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(LocalVariableDeclarationNode localVariableDeclarationNode) {
|
||||
if (localVariableDeclarationNode.expression != null) {
|
||||
// Process expression
|
||||
localVariableDeclarationNode.expression.accept(this);
|
||||
|
||||
if (localVariableDeclarationNode.expression instanceof UnaryNode) {
|
||||
UnaryNode unaryNode = (UnaryNode) localVariableDeclarationNode.expression;
|
||||
if (unaryNode.statement instanceof IncrementNode) {
|
||||
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
|
||||
incrementNode.assignableExpression.typeNode = unaryNode.type;
|
||||
loadIncrement(incrementNode);
|
||||
} else if (unaryNode.statement instanceof DecrementNode) {
|
||||
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
|
||||
decrementNode.assignableExpression.typeNode = unaryNode.type;
|
||||
loadDecrement(decrementNode);
|
||||
}
|
||||
}
|
||||
|
||||
// add local var to list if not in list
|
||||
if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
|
||||
localVariables.add(localVariableDeclarationNode.identifier);
|
||||
}
|
||||
if (localVariableDeclarationNode.type instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
|
||||
} else if (localVariableDeclarationNode.type instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
|
||||
}
|
||||
} else {
|
||||
// Local var declaration
|
||||
if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
|
||||
localVariables.add(localVariableDeclarationNode.identifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(AssignNode assignNode) {
|
||||
if (assignNode.assignable.memberAccess != null) { // this / object
|
||||
assignNode.assignable.memberAccess.setTypeNode(assignNode.assignable.typeNode);
|
||||
if (assignNode.expression instanceof UnaryNode) {
|
||||
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
|
||||
if (unaryNode.statement instanceof IncrementNode) {
|
||||
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
|
||||
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
|
||||
incrementNode.accept(this); // crement
|
||||
fieldOrObjectVarCrementAssign(assignNode); // assign
|
||||
} else { // i++
|
||||
fieldOrObjectVarCrementAssign(assignNode); // assign
|
||||
incrementNode.accept(this); // crement
|
||||
}
|
||||
} else if (unaryNode.statement instanceof DecrementNode) {
|
||||
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
|
||||
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
|
||||
decrementNode.accept(this); // crement
|
||||
fieldOrObjectVarCrementAssign(assignNode); // assign
|
||||
} else {
|
||||
fieldOrObjectVarCrementAssign(assignNode); // assign
|
||||
decrementNode.accept(this); // crement
|
||||
}
|
||||
}
|
||||
} else {
|
||||
assignFieldOrObjectVar(assignNode);
|
||||
}
|
||||
} else { // local var
|
||||
if (assignNode.expression instanceof UnaryNode) {
|
||||
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
|
||||
if (unaryNode.statement instanceof IncrementNode || unaryNode.statement instanceof DecrementNode) {
|
||||
localVarCrementAssign(assignNode);
|
||||
}
|
||||
} else {
|
||||
assignNode.expression.accept(this);
|
||||
assignLocalVar(assignNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadIncrement(IncrementNode incrementNode) {
|
||||
AssignableNode assignableNode = incrementNode.assignableExpression;
|
||||
if (assignableNode.memberAccess != null) {
|
||||
assignableNode.memberAccess.accept(this);
|
||||
} else {
|
||||
if (assignableNode.typeNode instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignableNode.identifier));
|
||||
} else if (assignableNode.typeNode instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignableNode.identifier));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadDecrement(DecrementNode decrementNode) {
|
||||
AssignableNode assignableNode = decrementNode.assignableExpression;
|
||||
if (assignableNode.memberAccess != null) {
|
||||
assignableNode.memberAccess.accept(this);
|
||||
} else {
|
||||
if (assignableNode.typeNode instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignableNode.identifier));
|
||||
} else if (assignableNode.typeNode instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignableNode.identifier));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void assignLocalVar(AssignNode assignNode) {
|
||||
if (!localVariables.contains(assignNode.assignable.identifier)) {
|
||||
localVariables.add(assignNode.assignable.identifier);
|
||||
}
|
||||
|
||||
if (assignNode.expression.getType() instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
} else if (assignNode.expression.getType() instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
}
|
||||
}
|
||||
|
||||
private void assignFieldOrObjectVar(AssignNode assignNode) {
|
||||
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
|
||||
if (localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else if (assignNode.assignable.memberAccess.thisExpr) { // this
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
} else {
|
||||
localVariables.add(assignNode.assignable.identifier);
|
||||
}
|
||||
|
||||
assignNode.expression.accept(this);
|
||||
|
||||
if (assignNode.expression.getType() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
|
||||
} else if (assignNode.expression.getType() instanceof ReferenceType) {
|
||||
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
|
||||
}
|
||||
}
|
||||
|
||||
private void localVarCrementAssign(AssignNode assignNode) {
|
||||
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
|
||||
if (unaryNode.statement instanceof IncrementNode) {
|
||||
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
|
||||
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
|
||||
incrementNode.accept(this);
|
||||
incrementNode.assignableExpression.typeNode = unaryNode.getType();
|
||||
loadIncrement(incrementNode);
|
||||
assignLocalVar(assignNode);
|
||||
} else { // i++
|
||||
incrementNode.assignableExpression.typeNode = unaryNode.getType();
|
||||
loadIncrement(incrementNode);
|
||||
assignLocalVar(assignNode);
|
||||
incrementNode.accept(this);
|
||||
}
|
||||
} else if (unaryNode.statement instanceof DecrementNode) {
|
||||
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
|
||||
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
|
||||
decrementNode.accept(this);
|
||||
decrementNode.assignableExpression.typeNode = unaryNode.getType();
|
||||
loadDecrement(decrementNode);
|
||||
assignLocalVar(assignNode);
|
||||
} else {
|
||||
decrementNode.assignableExpression.typeNode = unaryNode.getType();
|
||||
loadDecrement(decrementNode);
|
||||
assignLocalVar(assignNode);
|
||||
decrementNode.accept(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fieldOrObjectVarCrementAssign(AssignNode assignNode) {
|
||||
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
|
||||
if (localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else if (assignNode.assignable.memberAccess.thisExpr) { // field
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
} else {
|
||||
localVariables.add(assignNode.assignable.identifier);
|
||||
}
|
||||
|
||||
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
|
||||
if (unaryNode.statement instanceof IncrementNode) {
|
||||
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
|
||||
incrementNode.assignableExpression.typeNode = unaryNode.getType();
|
||||
loadIncrement((incrementNode));
|
||||
} else if (unaryNode.statement instanceof DecrementNode) {
|
||||
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
|
||||
decrementNode.assignableExpression.typeNode = unaryNode.getType();
|
||||
loadDecrement(decrementNode);
|
||||
}
|
||||
|
||||
if (assignNode.expression.getType() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
|
||||
} else if (assignNode.expression.getType() instanceof ReferenceType) {
|
||||
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(IncrementNode incrementNode) {
|
||||
if (incrementNode.assignableExpression.memberAccess != null) { // Object var / field
|
||||
int localVarIndex = localVariables.indexOf(incrementNode.assignableExpression.identifier);
|
||||
if (localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else { // this
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
}
|
||||
if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) incrementNode.assignableExpression.memberAccess.getTypeNode()));
|
||||
} else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
|
||||
}
|
||||
methodVisitor.visitInsn(ICONST_1);
|
||||
methodVisitor.visitInsn(IADD);
|
||||
if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) incrementNode.assignableExpression.memberAccess.getTypeNode()));
|
||||
} else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
|
||||
}
|
||||
} else { // local var
|
||||
methodVisitor.visitIincInsn(localVariables.indexOf(incrementNode.assignableExpression.identifier), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DecrementNode decrementNode) {
|
||||
if (decrementNode.assignableExpression.memberAccess != null) { // Object var / field
|
||||
int localVarIndex = localVariables.indexOf(decrementNode.assignableExpression.identifier);
|
||||
if (localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else { // this
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
}
|
||||
if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode()));
|
||||
} else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) decrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
|
||||
}
|
||||
methodVisitor.visitInsn(ICONST_1);
|
||||
methodVisitor.visitInsn(ISUB);
|
||||
if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode()));
|
||||
} else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) decrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
|
||||
}
|
||||
} else { // local var
|
||||
methodVisitor.visitIincInsn(localVariables.indexOf(decrementNode.assignableExpression.identifier), -1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NewDeclarationNode newDeclarationNode) {
|
||||
methodVisitor.visitTypeInsn(NEW, newDeclarationNode.identifier);
|
||||
methodVisitor.visitInsn(DUP);
|
||||
List<ParameterNode> parameterNodes = new ArrayList<>();
|
||||
for (IExpressionNode expressionNode : newDeclarationNode.expressions) {
|
||||
expressionNode.accept(this);
|
||||
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
|
||||
}
|
||||
methodVisitor.visitMethodInsn(INVOKESPECIAL, newDeclarationNode.identifier, "<init>", mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), parameterNodes), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ValueNode valueNode) {
|
||||
switch (valueNode.valueType) {
|
||||
case INT_VALUE:
|
||||
int intValue = Integer.parseInt(valueNode.value);
|
||||
if (intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE) { // load int as byte
|
||||
methodVisitor.visitIntInsn(BIPUSH, intValue);
|
||||
} else if (intValue >= Short.MIN_VALUE && intValue <= Short.MAX_VALUE) { // load int as short
|
||||
methodVisitor.visitIntInsn(SIPUSH, intValue);
|
||||
} else { // load int as const
|
||||
methodVisitor.visitLdcInsn(intValue);
|
||||
}
|
||||
break;
|
||||
case BOOLEAN_VALUE:
|
||||
if (valueNode.value.equals("true")) {
|
||||
methodVisitor.visitInsn(ICONST_1);
|
||||
} else {
|
||||
methodVisitor.visitInsn(ICONST_0);
|
||||
}
|
||||
break;
|
||||
case CHAR_VALUE:
|
||||
char charValue = valueNode.value.charAt(0);
|
||||
if (charValue <= Byte.MAX_VALUE) { // load char as byte
|
||||
methodVisitor.visitIntInsn(BIPUSH, charValue);
|
||||
} else if (charValue <= Short.MAX_VALUE) { // load char as short
|
||||
methodVisitor.visitIntInsn(SIPUSH, charValue);
|
||||
} else { // load char as const
|
||||
methodVisitor.visitLdcInsn(charValue);
|
||||
}
|
||||
break;
|
||||
case NULL_VALUE:
|
||||
methodVisitor.visitInsn(ACONST_NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ReturnNode returnNode) {
|
||||
if (returnNode.voidReturn) { // Return nothing
|
||||
methodVisitor.visitInsn(RETURN);
|
||||
} else { // Return something
|
||||
// Process expression
|
||||
returnNode.expression.accept(this);
|
||||
|
||||
if (returnNode.expression instanceof UnaryNode) {
|
||||
UnaryNode unaryNode = (UnaryNode) returnNode.expression;
|
||||
if (unaryNode.statement instanceof IncrementNode) {
|
||||
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
|
||||
incrementNode.assignableExpression.typeNode = unaryNode.getType();
|
||||
loadIncrement(incrementNode);
|
||||
}
|
||||
if (unaryNode.statement instanceof DecrementNode) {
|
||||
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
|
||||
decrementNode.assignableExpression.typeNode = unaryNode.getType();
|
||||
loadDecrement(decrementNode);
|
||||
}
|
||||
}
|
||||
|
||||
// Return result of expression
|
||||
if (returnNode.expression.getType() instanceof BaseType) {
|
||||
methodVisitor.visitInsn(IRETURN);
|
||||
} else if (returnNode.expression.getType() instanceof ReferenceType) {
|
||||
methodVisitor.visitInsn(ARETURN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(WhileNode whileNode) {
|
||||
Label loopLabel = new Label();
|
||||
Label endOfLoopLabel = new Label();
|
||||
|
||||
methodVisitor.visitLabel(loopLabel);
|
||||
// while loop
|
||||
whileNode.expression.accept(this);
|
||||
methodVisitor.visitJumpInsn(IFEQ, endOfLoopLabel); // if condition is false, jump out of loop
|
||||
|
||||
whileNode.block.accept(this);
|
||||
methodVisitor.visitJumpInsn(GOTO, loopLabel);
|
||||
|
||||
methodVisitor.visitLabel(endOfLoopLabel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MethodCallNode methodCallNode) {
|
||||
List<ParameterNode> parameterNodes = new ArrayList<>();
|
||||
int localVarIndex = -1;
|
||||
if (methodCallNode.target.memberAccess.identifiers.size() > 1) {
|
||||
localVarIndex = localVariables.indexOf(methodCallNode.target.memberAccess.identifiers.get(1));
|
||||
}
|
||||
if (localVarIndex >= 0) { // local var object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else { // this field
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
}
|
||||
for (IExpressionNode expressionNode : methodCallNode.parameters) {
|
||||
expressionNode.accept(this);
|
||||
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
|
||||
}
|
||||
if(methodCallNode.type == null) {
|
||||
methodCallNode.type = new BaseType(TypeEnum.INT);
|
||||
}
|
||||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, methodCallNode.target.memberAccess.identifiers.get(0), methodCallNode.identifier, mapper.generateMethodDescriptor(methodCallNode.type, parameterNodes), false);
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package bytecode.visitor;
|
||||
|
||||
import ast.ClassNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.members.FieldNode;
|
||||
|
||||
public interface ClassVisitor {
|
||||
void visit(ClassNode classNode);
|
||||
void visit(FieldNode fieldNode);
|
||||
void visit(ClassNode classNode);
|
||||
void visit(FieldNode fieldNode);
|
||||
}
|
||||
|
@@ -1,9 +1,68 @@
|
||||
package bytecode.visitor;
|
||||
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.expressions.binaryexpressions.*;
|
||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||
import ast.expressions.unaryexpressions.NotNode;
|
||||
import ast.expressions.unaryexpressions.UnaryNode;
|
||||
import ast.members.ConstructorNode;
|
||||
import ast.members.MainMethodNode;
|
||||
import ast.members.MethodNode;
|
||||
import ast.statementexpressions.AssignNode;
|
||||
import ast.statementexpressions.NewDeclarationNode;
|
||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||
import ast.statements.*;
|
||||
import ast.type.ValueNode;
|
||||
|
||||
public interface MethodVisitor {
|
||||
// members
|
||||
void visit(ConstructorNode constructorNode);
|
||||
|
||||
void visit(MethodNode methodNode);
|
||||
|
||||
void visit(MainMethodNode mainMethodNode);
|
||||
|
||||
// Binary expressions
|
||||
void visit(BinaryNode binaryNode);
|
||||
|
||||
void visit(CalculationNode calculationNode);
|
||||
|
||||
void visit(DotNode dotNode);
|
||||
|
||||
void visit(DotSubtractionNode dotSubtractionNode);
|
||||
|
||||
void visit(NonCalculationNode nonCalculationNode);
|
||||
|
||||
// Unary expressions
|
||||
void visit(MemberAccessNode memberAccessNode);
|
||||
|
||||
void visit(NotNode notExpressionNode);
|
||||
|
||||
void visit(UnaryNode unaryExpressionNode);
|
||||
|
||||
// statements
|
||||
void visit(BlockNode blockNode);
|
||||
void visit(IfElseNode ifElseNode);
|
||||
|
||||
void visit(IncrementNode incrementNode);
|
||||
void visit(DecrementNode decrementNode);
|
||||
|
||||
void visit(LocalVariableDeclarationNode localVariableDeclarationNode);
|
||||
|
||||
void visit(ReturnNode returnNode);
|
||||
|
||||
void visit(WhileNode whileNode);
|
||||
|
||||
// statement expression
|
||||
void visit(MethodCallNode methodCallNode);
|
||||
|
||||
void visit(AssignNode assignNode);
|
||||
|
||||
void visit(NewDeclarationNode newDeclarationNode);
|
||||
|
||||
// type
|
||||
void visit(ValueNode valueNode);
|
||||
|
||||
}
|
||||
|
@@ -3,5 +3,5 @@ package bytecode.visitor;
|
||||
import ast.ProgramNode;
|
||||
|
||||
public interface ProgramVisitor {
|
||||
void visit(ProgramNode programNode);
|
||||
void visit(ProgramNode programNode);
|
||||
}
|
||||
|
Binary file not shown.
116
src/main/java/main/Main.java
Normal file
116
src/main/java/main/Main.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package main;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.ProgramNode;
|
||||
import parser.astBuilder.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
import semantic.SemanticAnalyzer;
|
||||
import bytecode.ByteCodeGenerator;
|
||||
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Start miniCompiler using make:
|
||||
* <p> <code> cd .\src\test\ </code>
|
||||
* <p> <code> make clean compile-miniCompiler </code>
|
||||
* <p> Start miniCompiler using jar:
|
||||
* <p> <code> java.exe -DgenJar=true_OR_false -DgenClass=true_OR_false -jar path_to_jar\JavaCompiler-1.0-jar-with-dependencies.jar 'path_to_input_file.java' 'path_to_output_directory' </code>
|
||||
* <p> Example (jar needs to be in the target directory, compile with make or mvn package first):
|
||||
* <code> java.exe -DgenJar=true -DgenClass=true -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' </code>
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length == 2) {
|
||||
// args[0] is the input file path
|
||||
// args[1] is the output directory path
|
||||
String inputFilePath = args[0];
|
||||
String outputDirectoryPath = args[1];
|
||||
System.out.println("Compiling file: " + inputFilePath);
|
||||
try {
|
||||
CharStream inputCharStream = CharStreams.fromPath(Paths.get(inputFilePath));
|
||||
compileFile(inputCharStream, outputDirectoryPath);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/finalTest/Car.java"));
|
||||
compileFile(codeCharStream, "src/test/resources/input/finalTest");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to compile a file from a given CharStream and output the bytecode to a specified directory.
|
||||
* It goes through the following steps:
|
||||
* <p>1. Scanner: It uses the SimpleJavaLexer to tokenize the input CharStream.
|
||||
* <p>2. Parser: It uses the SimpleJavaParser to parse the tokens and generate a ParseTree.
|
||||
* <p>3. AST Builder: It uses the ASTBuilder to visit the ParseTree and generate an Abstract Syntax Tree (AST).
|
||||
* <p>4. Semantic Analyzer: It uses the SemanticAnalyzer to generate a typed AST.
|
||||
* <p>5. Bytecode Generator: It uses the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory.
|
||||
*
|
||||
* @param inputCharStream The CharStream representing the input file to be compiled.
|
||||
* @param outputDirectoryPath The path of the directory where the output bytecode should be written.
|
||||
*/
|
||||
static void compileFile(CharStream inputCharStream, String outputDirectoryPath) {
|
||||
// Initialize the logger
|
||||
new MiniCompilerLogger();
|
||||
|
||||
/* ------------------------- Scanner -> tokens ------------------------- */
|
||||
// Use the SimpleJavaLexer to tokenize the input CharStream
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
tokenStream.fill();
|
||||
// Log the tokens
|
||||
MiniCompilerLogger.logScanner(tokenStream);
|
||||
|
||||
/*------------------------- Parser -> Parsetree -------------------------*/
|
||||
// Use the SimpleJavaParser to parse the tokens and generate a ParseTree
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree parseTree = parser.program(); // parse the input
|
||||
// Log the ParseTree
|
||||
MiniCompilerLogger.logParser(parseTree, parser);
|
||||
|
||||
/*------------------------- AST builder -> AST -------------------------*/
|
||||
// Use the ASTBuilder to visit the ParseTree and generate an Abstract Syntax Tree (AST)
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
ASTNode abstractSyntaxTree = astBuilder.visit(parseTree);
|
||||
// Log the AST
|
||||
MiniCompilerLogger.logAST(abstractSyntaxTree);
|
||||
|
||||
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
|
||||
// Use the SemanticAnalyzer to generate a typed AST
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
// Log the typed AST
|
||||
MiniCompilerLogger.logSemanticAnalyzer(typedAst);
|
||||
|
||||
if(SemanticAnalyzer.errors.isEmpty()){
|
||||
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
|
||||
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
|
||||
|
||||
final boolean genJar = Optional.ofNullable(System.getProperty("genJar")).map(String::toLowerCase).map(Boolean::parseBoolean).orElse(true);
|
||||
final boolean genClass = Optional.ofNullable(System.getProperty("genClass")).map(String::toLowerCase).map(Boolean::parseBoolean).orElse(true);
|
||||
|
||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath, genJar, genClass);
|
||||
assert typedAst != null;
|
||||
byteCodeGenerator.visit((ProgramNode) typedAst);
|
||||
// Log the bytecode generation
|
||||
MiniCompilerLogger.logBytecodeGenerator();
|
||||
} else {
|
||||
for(Exception exception : SemanticAnalyzer.errors){
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user