Skip to content
This repository was archived by the owner on Oct 18, 2022. It is now read-only.

Commit 8ab86ae

Browse files
committed
Let's do this: here it is, as it is
0 parents  commit 8ab86ae

File tree

141 files changed

+8025
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+8025
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.iml
3+
target/

pom.xml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>litil</groupId>
6+
<artifactId>litil</artifactId>
7+
<version>1.0</version>
8+
<packaging>jar</packaging>
9+
10+
<name>litil</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.8.2</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.ow2.asm</groupId>
26+
<artifactId>asm-all</artifactId>
27+
<version>4.0</version>
28+
</dependency>
29+
30+
</dependencies>
31+
</project>

src/main/java/litil/TypeScope.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package litil;
2+
3+
import litil.ast.Type;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class TypeScope {
9+
10+
private final Map<String, Type> scope;
11+
private final TypeScope parent;
12+
13+
private TypeScope(TypeScope parent, Map<String, Type> scope) {
14+
this.parent = parent;
15+
this.scope = scope;
16+
}
17+
18+
public TypeScope(TypeScope parent) {
19+
this(parent, new HashMap<String, Type>());
20+
}
21+
22+
public TypeScope() {
23+
this(null, new HashMap<String, Type>());
24+
}
25+
26+
public void define(String name, Type type) {
27+
scope.put(name, type);
28+
}
29+
30+
public Type get(String name) {
31+
Type res = scope.get(name);
32+
if (res == null && parent != null) {
33+
res = parent.get(name);
34+
}
35+
return res;
36+
}
37+
38+
public TypeScope child() {
39+
return new TypeScope(this);
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return scope + (parent == null ? "-|" : "->" + parent);
45+
}
46+
}

src/main/java/litil/Utils.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package litil;
2+
3+
import java.io.PrintStream;
4+
5+
public class Utils {
6+
public static String tab(int depth) {
7+
StringBuilder res = new StringBuilder("");
8+
for (int i = 0; i < depth; i++) {
9+
res.append("\t");
10+
}
11+
12+
return res.toString();
13+
}
14+
15+
public static PrintStream indenting(String pkg) {
16+
return new PrintStream(System.out) {
17+
@Override
18+
public void println(String s) {
19+
super.println(s);
20+
}
21+
};
22+
}
23+
}

src/main/java/litil/ast/AstNode.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package litil.ast;
2+
3+
import litil.TypeScope;
4+
5+
public abstract class AstNode {
6+
public TypeScope scope;
7+
public abstract String repr(int indent);
8+
9+
@Override
10+
public String toString() {
11+
return repr(0);
12+
}
13+
}

src/main/java/litil/ast/DataDecl.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package litil.ast;
2+
3+
import litil.Utils;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class DataDecl extends Instruction {
9+
public static final class TypeConstructor {
10+
public final String name;
11+
public final List<Type> types;
12+
13+
public TypeConstructor(String name, List<Type> types) {
14+
this.name = name;
15+
this.types = types;
16+
}
17+
18+
public TypeConstructor(String name) {
19+
this.name = name;
20+
this.types = Collections.emptyList();
21+
}
22+
}
23+
24+
public final String name;
25+
public final Type type;
26+
public final List<Type.Variable> typesVariables;
27+
public final List<TypeConstructor> typeConstructors;
28+
29+
public DataDecl(String name, Type type, List<Type.Variable> typesVariables, List<TypeConstructor> typeConstructors) {
30+
this.name = name;
31+
this.type = type;
32+
this.typesVariables = typesVariables;
33+
this.typeConstructors = typeConstructors;
34+
}
35+
36+
@Override
37+
public String repr(int indent) {
38+
StringBuilder res = new StringBuilder(Utils.tab(indent));
39+
res.append("data ").append(name);
40+
for (Type.Variable typesVariable : typesVariables) {
41+
res.append(typesVariable).append(" ");
42+
}
43+
res.append(" = ");
44+
boolean first = true;
45+
for (TypeConstructor typeConstructor : typeConstructors) {
46+
if (first) {
47+
first = false;
48+
} else {
49+
res.append(" | ");
50+
}
51+
res.append(typeConstructor.name);
52+
for (Type type : typeConstructor.types) {
53+
res.append(" ").append(type);
54+
}
55+
}
56+
return res.toString();
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package litil.ast;
2+
3+
import litil.Utils;
4+
5+
import java.util.List;
6+
7+
public class DestructuringLetBinding extends Instruction {
8+
public final Pattern main;
9+
public final List<Pattern> args;
10+
public final List<Instruction> instructions;
11+
12+
public DestructuringLetBinding(Pattern main, List<Pattern> args, List<Instruction> instructions) {
13+
this.main = main;
14+
this.args = args;
15+
this.instructions = instructions;
16+
}
17+
18+
@Override
19+
public String repr(int indent) {
20+
StringBuilder res = new StringBuilder(Utils.tab(indent));
21+
res.append("let ");
22+
res.append(main);
23+
24+
for (Pattern arg : args) {
25+
res.append(" ").append(arg);
26+
}
27+
res.append(" = \n");
28+
for (Instruction instruction : instructions) {
29+
res.append(instruction.repr(indent + 1)).append("\n");
30+
}
31+
return res.toString();
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return repr(0);
37+
}
38+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package litil.ast;
2+
3+
import litil.Utils;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class ExceptionDecl extends Instruction {
9+
public final String name;
10+
public final List<Type> types;
11+
12+
public ExceptionDecl(String name, List<Type> types) {
13+
this.name = name;
14+
this.types = types;
15+
}
16+
17+
@Override
18+
public String repr(int indent) {
19+
StringBuilder res = new StringBuilder(Utils.tab(indent));
20+
res.append("exception ").append(name);
21+
for (Type t : types) {
22+
res.append(" ").append(t);
23+
}
24+
return res.toString();
25+
}
26+
}

0 commit comments

Comments
 (0)