-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
2,885 additions
and
527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ javadoc/ | |
out/ | ||
*.iml | ||
target/ | ||
dependency-reduced-pom.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,132 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>se.jsannemo</groupId> | ||
<artifactId>spooky-vm</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>13</maven.compiler.source> | ||
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>1.0</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.5.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.auto.value</groupId> | ||
<artifactId>auto-value-annotations</artifactId> | ||
<version>1.6.6</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>28.1-jre</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<compilerArgs> | ||
<arg>-XDcompilePolicy=simple</arg> | ||
<arg>-Xplugin:ErrorProne</arg> | ||
</compilerArgs> | ||
<annotationProcessors> | ||
<annotationProcessor>com.google.auto.value.processor.AutoValueProcessor</annotationProcessor> | ||
<annotationProcessor>com.google.auto.value.processor.AutoOneOfProcessor</annotationProcessor> | ||
</annotationProcessors> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>com.google.errorprone</groupId> | ||
<artifactId>error_prone_core</artifactId> | ||
<version>2.3.3</version> | ||
</path> | ||
<path> | ||
<groupId>com.google.auto.value</groupId> | ||
<artifactId>auto-value</artifactId> | ||
<version>1.6.6</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.github.spotbugs</groupId> | ||
<artifactId>spotbugs-maven-plugin</artifactId> | ||
<version>3.1.12.2</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.spotbugs</groupId> | ||
<artifactId>spotbugs</artifactId> | ||
<version>4.0.0-beta4</version> | ||
</dependency> | ||
</dependencies> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<version>2.22.2</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<artifactId>spooky-vm</artifactId> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.2.4</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<mainClass>se.jsannemo.spooky.Main</mainClass> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<artifactId>javacc-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>javacc</goal> | ||
</goals> | ||
<id>javacc</id> | ||
</execution> | ||
</executions> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<version>2.6</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<annotationProcessorPaths> | ||
<path> | ||
<artifactId>error_prone_core</artifactId> | ||
<groupId>com.google.errorprone</groupId> | ||
<version>2.3.3</version> | ||
</path> | ||
<path> | ||
<artifactId>auto-value</artifactId> | ||
<groupId>com.google.auto.value</groupId> | ||
<version>1.6.6</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
<annotationProcessors> | ||
<annotationProcessor>com.google.auto.value.processor.AutoValueProcessor | ||
</annotationProcessor> | ||
<annotationProcessor>com.google.auto.value.processor.AutoOneOfProcessor | ||
</annotationProcessor> | ||
</annotationProcessors> | ||
<compilerArgs> | ||
<compilerArg>--enable-preview</compilerArg> | ||
<compilerArg>-XDcompilePolicy=simple</compilerArg> | ||
<compilerArg>-Xplugin:ErrorProne</compilerArg> | ||
</compilerArgs> | ||
<release>14</release> | ||
<source>14</source> | ||
<target>14</target> | ||
</configuration> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<version>3.8.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>spotbugs-maven-plugin</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<artifactId>spotbugs</artifactId> | ||
<groupId>com.github.spotbugs</groupId> | ||
<version>4.0.0-beta4</version> | ||
</dependency> | ||
</dependencies> | ||
<groupId>com.github.spotbugs</groupId> | ||
<version>3.1.12.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<argLine>--enable-preview</argLine> | ||
</configuration> | ||
<version>2.22.2</version> | ||
</plugin> | ||
</plugins> | ||
<resources> | ||
<resource> | ||
<directory>src/test/resources</directory> | ||
</resource> | ||
</resources> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<artifactId>truth</artifactId> | ||
<groupId>com.google.truth</groupId> | ||
<scope>compile</scope> | ||
<version>1.0</version> | ||
</dependency> | ||
<dependency> | ||
<artifactId>junit-jupiter</artifactId> | ||
<groupId>org.junit.jupiter</groupId> | ||
<scope>test</scope> | ||
<version>5.5.2</version> | ||
</dependency> | ||
<dependency> | ||
<artifactId>auto-value-annotations</artifactId> | ||
<groupId>com.google.auto.value</groupId> | ||
<scope>compile</scope> | ||
<version>1.6.6</version> | ||
</dependency> | ||
<dependency> | ||
<artifactId>guava</artifactId> | ||
<groupId>com.google.guava</groupId> | ||
<scope>compile</scope> | ||
<version>28.1-jre</version> | ||
</dependency> | ||
</dependencies> | ||
<groupId>se.jsannemo</groupId> | ||
<modelVersion>4.0.0</modelVersion> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<version>1.0-SNAPSHOT</version> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package se.jsannemo.spooky; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import se.jsannemo.spooky.compiler.Compiler; | ||
import se.jsannemo.spooky.compiler.ParseException; | ||
import se.jsannemo.spooky.compiler.ValidationException; | ||
import se.jsannemo.spooky.vm.SpookyVm; | ||
import se.jsannemo.spooky.vm.VmException; | ||
import se.jsannemo.spooky.vm.code.Executable; | ||
import se.jsannemo.spooky.vm.code.ExecutableParser; | ||
import se.jsannemo.spooky.vm.code.InstructionException; | ||
|
||
public final class Main { | ||
|
||
public static void main(String[] args) | ||
throws ParseException, IOException, ValidationException, InstructionException, VmException { | ||
if (args.length == 0) { | ||
System.err.println("No command provided"); | ||
return; | ||
} | ||
switch (args[0]) { | ||
case "compile": | ||
compile(args); | ||
break; | ||
case "run": | ||
run(args); | ||
break; | ||
default: | ||
System.err.println("Unknown command: " + args[0]); | ||
} | ||
} | ||
|
||
private static void run(String[] args) throws IOException, InstructionException, VmException { | ||
if (args.length < 2) { | ||
System.err.println("Usage: run <BINARY>"); | ||
} | ||
Executable exec = ExecutableParser.fromFile(args[1]); | ||
SpookyVm vm = SpookyVm.newBuilder(exec).addStdLib().setMemorySize(1000).build(); | ||
while (vm.executeInstruction()) | ||
; | ||
} | ||
|
||
private static void compile(String[] args) | ||
throws IOException, ParseException, ValidationException { | ||
if (args.length < 3) { | ||
System.err.println("Usage: compile <INFILE> <OUTFILE>"); | ||
return; | ||
} | ||
byte[] code = Compiler.compile(new FileInputStream(args[1])); | ||
Files.write(Path.of(args[2]), code, StandardOpenOption.CREATE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package se.jsannemo.spooky.compiler; | ||
|
||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import se.jsannemo.spooky.compiler.ast.Program; | ||
import se.jsannemo.spooky.compiler.codegen.CodeGen; | ||
import se.jsannemo.spooky.compiler.ir.IrProgram; | ||
import se.jsannemo.spooky.compiler.ir.ToIr; | ||
import se.jsannemo.spooky.vm.code.Instructions; | ||
|
||
public final class Compiler { | ||
|
||
private Compiler() {} | ||
|
||
public static byte[] compile(InputStream is) throws ParseException, ValidationException { | ||
Parser parser = new Parser(is, StandardCharsets.UTF_8.name()); | ||
Program program = parser.Start(); | ||
IrProgram ir = ToIr.generate(program); | ||
List<Instructions.Instruction> instructions = CodeGen.codegen("program", ir); | ||
return Assembler.assemble(instructions); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/se/jsannemo/spooky/compiler/ValidationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package se.jsannemo.spooky.compiler; | ||
|
||
public final class ValidationException extends Exception { | ||
|
||
public ValidationException(String msg) { | ||
super(msg); | ||
} | ||
|
||
public ValidationException(String msg, Token loc) { | ||
this(loc.beginLine + ":" + loc.beginColumn + ": " + msg); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/se/jsannemo/spooky/compiler/ast/BinaryExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package se.jsannemo.spooky.compiler.ast; | ||
|
||
import com.google.auto.value.AutoValue; | ||
|
||
@AutoValue | ||
public abstract class BinaryExpression { | ||
BinaryExpression() {} | ||
|
||
public abstract Expression left(); | ||
|
||
public abstract Expression right(); | ||
|
||
public abstract BinaryOperator operator(); | ||
|
||
public static BinaryExpression of(Expression left, Expression right, BinaryOperator operator) { | ||
return new AutoValue_BinaryExpression(left, right, operator); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/se/jsannemo/spooky/compiler/ast/BinaryOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package se.jsannemo.spooky.compiler.ast; | ||
|
||
public enum BinaryOperator { | ||
/** Assign value of the right expression to the left reference. */ | ||
ASSIGN, | ||
/** Check if left value is smaller than right value. */ | ||
LESS_THAN, | ||
/** Check if left value is greater than right value. */ | ||
GREATER_THAN, | ||
/** Check if left value is smaller than or equal to the right value. */ | ||
LESS_EQUALS, | ||
/** Check if left value is greater than or equal to the right value. */ | ||
GREATER_EQUALS, | ||
/** Check if left value is equal to right value. */ | ||
EQUALS, | ||
/** Access array at given index. */ | ||
ARRAY_ACCESS, | ||
/** Add the left and right values.. */ | ||
ADD, | ||
/** Subtract the right from the left value. */ | ||
SUBTRACT, | ||
/** Multiply the left and right values */ | ||
MULTIPLY, | ||
/** Divide the left value by the right value. */ | ||
DIVIDE, | ||
/** Compute the left value modulo the right value.. */ | ||
MODULO, | ||
} |
Oops, something went wrong.