Skip to content

Commit

Permalink
add union type
Browse files Browse the repository at this point in the history
  • Loading branch information
yinwang0 committed Feb 5, 2014
1 parent b4398a1 commit 5803f99
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/yinwang/yin/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ public static Scope buildInitScope() {
}


public static Scope buildInitTypeScope() {
Scope init = new Scope();

// init.putValue("+", new Add());
// init.putValue("-", new Sub());
// init.putValue("*", new Mult());
// init.putValue("/", new Div());
//
// init.putValue("<", new Lt());
// init.putValue("<=", new LtE());
// init.putValue(">", new Gt());
// init.putValue(">=", new GtE());
// init.putValue("=", new Eq());
// init.putValue("and", new And());
// init.putValue("or", new Or());
// init.putValue("not", new Not());

init.putValue("true", new BoolType());
init.putValue("false", new BoolType());

// init.putValue("Int", new IntType());
// init.putValue("Bool", new BoolType());
// init.putValue("String", new StringType());

return init;
}


public void put(String name, String key, Object value) {
Map<String, Object> item = table.get(name);
if (item == null) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/yinwang/yin/TypeChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public TypeChecker(String file) {
}


public Value interp(String file) {
public Value typecheck(String file) {
Node program = Parser.parse(file);
return program.typecheck(Scope.buildInitScope());
return program.typecheck(Scope.buildInitTypeScope());
}


public static void main(String[] args) {
TypeChecker i = new TypeChecker(args[0]);
Value result = i.interp(args[0]);
Value result = i.typecheck(args[0]);
_.msg(result.toString());
}

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/yinwang/yin/ast/If.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.yinwang.yin.Constants;
import org.yinwang.yin.Scope;
import org.yinwang.yin._;
import org.yinwang.yin.value.BoolType;
import org.yinwang.yin.value.BoolValue;
import org.yinwang.yin.value.UnionType;
import org.yinwang.yin.value.Value;

public class If extends Node {
Expand Down Expand Up @@ -37,7 +39,14 @@ public Value interp(Scope s) {

@Override
public Value typecheck(Scope s) {
return null;
Value tv = typecheck(test, s);
if (!(tv instanceof BoolType)) {
_.abort(test, "test is not boolean: " + tv);
return null;
}
Value thenType = typecheck(then, s);
Value elseType = typecheck(orelse, s);
return UnionType.union(thenType, elseType);
}


Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/yinwang/yin/ast/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public Value interp(Scope s) {

@Override
public Value typecheck(Scope s) {
return null;
Value v = s.lookup(id);
if (v != null) {
return v;
} else {
_.abort(this, "unbound variable: " + id);
return Value.VOID;
}
}


Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/yinwang/yin/ast/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static Value interp(Node node, Scope s) {
public abstract Value typecheck(Scope s);


public static Value typecheck(Node node, Scope s) {
return node.typecheck(s);
}


public static List<Value> interpList(List<Node> nodes, Scope s) {
List<Value> values = new ArrayList<>();
for (Node n : nodes) {
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/org/yinwang/yin/value/UnionType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.yinwang.yin.value;


import org.yinwang.yin.Constants;

import java.util.HashSet;
import java.util.Set;

public class UnionType extends Value {

public Set<Value> values = new HashSet<>();


public UnionType() {

}


public static UnionType union(Value... values) {
UnionType u = new UnionType();
for (Value v : values) {
u.add(v);
}
return u;
}


public void add(Value value) {
if (value instanceof UnionType) {
values.addAll(((UnionType) value).values);
} else {
values.add(value);
}
}


public int size() {
return values.size();
}


public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Constants.TUPLE_BEGIN).append("U ");

boolean first = true;
for (Value v : values) {
if (!first) {
sb.append(" ");
}
sb.append(v);
first = false;
}

sb.append(Constants.TUPLE_END);
return sb.toString();
}

}
3 changes: 3 additions & 0 deletions tests/typecheck1.yin
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
10
"ok"
true

(if true 10 "ok")

0 comments on commit 5803f99

Please sign in to comment.