Skip to content

Commit

Permalink
Add some new language features.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsannemo committed Feb 15, 2021
1 parent c0307ed commit 726780f
Show file tree
Hide file tree
Showing 34 changed files with 805 additions and 404 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ These are declared as `extern` functions in your source and provided by the exec
extern printInt(i: Int)
extern print(ch: Int)

func isPrime(n: Int) -> Int {
func isPrime(n: Int) -> Boolean {
p: Int = 1;
for (i: Int = 2; i * i <= n; i = i + 1) {
if (n % i == 0) {
Expand Down
282 changes: 147 additions & 135 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,137 +1,149 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<arg>--enable-preview</arg>
<arg>-XDcompilePolicy=simple</arg>
<arg>-Xplugin:ErrorProne</arg>
</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>
<dependency>
<groupId>org.eclipse.lsp4j</groupId>
<artifactId>org.eclipse.lsp4j</artifactId>
<version>0.10.0</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>
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>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</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>
<arg>--enable-preview</arg>
<arg>-XDcompilePolicy=simple</arg>
<arg>-Xplugin:ErrorProne</arg>
</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>
<trimStackTrace>false</trimStackTrace>
</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>
<dependency>
<groupId>org.eclipse.lsp4j</groupId>
<artifactId>org.eclipse.lsp4j</artifactId>
<version>0.10.0</version>
</dependency>
</dependencies>
<groupId>se.jsannemo</groupId>
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<version>2.0.0</version>
</project>
14 changes: 8 additions & 6 deletions src/main/java/se/jsannemo/spooky/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
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;
Expand All @@ -15,6 +10,12 @@
import se.jsannemo.spooky.vm.code.ExecutableParser;
import se.jsannemo.spooky.vm.code.InstructionException;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public final class Main {

public static void main(String[] args) throws Exception {
Expand Down Expand Up @@ -64,6 +65,7 @@ private static void compile(String[] args)
return;
}
byte[] code = Compiler.compile(new FileInputStream(args[1]));
Files.write(Path.of(args[2]), code, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
Files.write(
Path.of(args[2]), code, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
}
5 changes: 3 additions & 2 deletions src/main/java/se/jsannemo/spooky/compiler/Assembler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package se.jsannemo.spooky.compiler;

import se.jsannemo.spooky.vm.code.ExecutableParser;
import se.jsannemo.spooky.vm.code.Instructions.Instruction;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import se.jsannemo.spooky.vm.code.ExecutableParser;
import se.jsannemo.spooky.vm.code.Instructions.Instruction;

/** Assembler for VM instructions into binary code. */
public final class Assembler {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/se/jsannemo/spooky/compiler/Compiler.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public final class Compiler {

private Compiler() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public enum BinaryOperator {
GREATER_EQUALS,
/** Check if left value is equal to right value. */
EQUALS,
/** Check if left value is not equal to right value. */
NOT_EQUALS,
/** Access array at given index. */
ARRAY_ACCESS,
/** Add the left and right values.. */
Expand All @@ -23,6 +25,10 @@ public enum BinaryOperator {
MULTIPLY,
/** Divide the left value by the right value. */
DIVIDE,
/** Compute the left value modulo the right value.. */
/** Compute the left value modulo the right value. */
MODULO,
/** Compute the logical AND of the two values. */
AND,
/** Compute the logical OR of the two values. */
OR,
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public abstract class Conditional {
/** Returns the IF token. */
public abstract Token token();

public static Conditional of(Expression condition, StatementList body, @Nullable StatementList elseBody, Token token) {
public static Conditional of(
Expression condition, StatementList body, @Nullable StatementList elseBody, Token token) {
return new AutoValue_Conditional(condition, body, Optional.ofNullable(elseBody), token);
}
}
Loading

0 comments on commit 726780f

Please sign in to comment.