Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:mwatts15/Crono

  • Loading branch information...
commit 172a3b28fd4602ff276023aee1b06c1ef7ad4594 2 parents b51b0c2 + 6f9071f
Mark Watts authored
View
1,453 crono/src/crono/CronoFunction.java
@@ -8,8 +8,28 @@
import java.util.List;
import java.util.Map;
-import crono.type.*;
+import crono.type.Cons;
+import crono.type.CronoArray;
+import crono.type.CronoCharacter;
+import crono.type.CronoFloat;
+import crono.type.CronoInteger;
+import crono.type.CronoNumber;
+import crono.type.CronoPrimitive;
+import crono.type.CronoString;
+import crono.type.CronoStruct;
+import crono.type.CronoType;
+import crono.type.Function;
import crono.type.Function.EvalType;
+import crono.type.Nil;
+import crono.type.LambdaFunction;
+import crono.type.Symbol;
+import crono.type.TruthValue;
+import crono.type.TypeId;
+
+import crono.inf.InfDatabase;
+import crono.inf.InfTriple;
+import crono.inf.InfClass;
+import crono.inf.InfStruct;
/**
* Define all builtins here. If the function is not variadic, don't define the
@@ -21,896 +41,749 @@
*/
public enum CronoFunction {
CONS(new Function(new TypeId[]{CronoType.TYPEID, CronoType.TYPEID},
- Cons.TYPEID, 2)
+ Cons.TYPEID, 2)
{
- public CronoType run(Visitor v, CronoType[] args) {
- return new Cons(args[0], args[1]);
- }
- public String toString() {
- return "cons";
- }
+ public CronoType run(Visitor v, CronoType[] args) {
+ return new Cons(args[0], args[1]);
+ }
+ public String toString() {
+ return "cons";
+ }
}),
CAR(new Function(new TypeId[]{Cons.TYPEID}, CronoType.TYPEID, 1)
{
- private static final String _not_cons = "%s is not a cons cell";
-
+ private static final String _not_cons = "%s is not a cons cell";
+
public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof Cons)) {
- throw new InterpreterException(_not_cons, args[0]);
- }
- return ((Cons)args[0]).car();
- }
- public String toString() {
- return "car";
- }
+ if(!(args[0] instanceof Cons)) {
+ throw new InterpreterException(_not_cons, args[0]);
+ }
+ return ((Cons)args[0]).car();
+ }
+ public String toString() {
+ return "car";
+ }
}),
CDR(new Function(new TypeId[]{Cons.TYPEID}, CronoType.TYPEID, 1)
{
- private static final String _not_cons = "%s is not a cons cell";
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof Cons)) {
- throw new InterpreterException(_not_cons, args[0]);
- }
- return ((Cons)args[0]).cdr();
- }
- public String toString() {
- return "cdr";
- }
+ private static final String _not_cons = "%s is not a cons cell";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(!(args[0] instanceof Cons)) {
+ throw new InterpreterException(_not_cons, args[0]);
+ }
+ return ((Cons)args[0]).cdr();
+ }
+ public String toString() {
+ return "cdr";
+ }
}),
LIST(new Function(new TypeId[]{CronoType.TYPEID}, Cons.TYPEID, 1, true)
{
- public CronoType run(Visitor v, CronoType[] args) {
- Cons c = Nil.NIL;
- for(int i = 0; i < args.length; ++i) {
- c = new Cons(args[i], c);
- }
- return c;
- }
- public String toString() {
- return "list";
- }
+ public CronoType run(Visitor v, CronoType[] args) {
+ Cons c = Nil.NIL;
+ for(int i = 0; i < args.length; ++i) {
+ c = new Cons(args[i], c);
+ }
+ return c;
+ }
+ public String toString() {
+ return "list";
+ }
}),
GET(new Function(new TypeId[]{CronoArray.TYPEID, CronoInteger.TYPEID},
- CronoType.TYPEID, 2)
+ CronoType.TYPEID, 2)
{
- public CronoType run(Visitor v, CronoType[] args) {
- int index = (int)((CronoInteger)args[1]).value;
- ((CronoArray)args[0]).get(index);
- return args[0];
- }
- public String toString() {
- return "get";
- }
+ public CronoType run(Visitor v, CronoType[] args) {
+ int index = (int)((CronoInteger)args[1]).value;
+ ((CronoArray)args[0]).get(index);
+ return args[0];
+ }
+ public String toString() {
+ return "get";
+ }
}),
PUT(new Function(new TypeId[]{CronoArray.TYPEID, CronoInteger.TYPEID,
- CronoType.TYPEID},
- CronoType.TYPEID, 3)
- {
- public CronoType run(Visitor v, CronoType[] args) {
- int index = (int)((CronoInteger)args[1]).value;
- ((CronoArray)args[0]).put(index, args[2]);
- return args[0];
- }
- public String toString() {
- return "put";
- }
+ CronoType.TYPEID},
+ CronoType.TYPEID, 3)
+ {
+ public CronoType run(Visitor v, CronoType[] args) {
+ int index = (int)((CronoInteger)args[1]).value;
+ ((CronoArray)args[0]).put(index, args[2]);
+ return args[0];
+ }
+ public String toString() {
+ return "put";
+ }
}),
APPEND(new Function(new TypeId[]{CronoArray.TYPEID, CronoType.TYPEID},
- CronoType.TYPEID, 2)
- {
- public CronoType run(Visitor v, CronoType[] args) {
- ((CronoArray)args[0]).append(args[1]);
- return args[0];
- }
- public String toString() {
- return "append";
- }
- }),
- INSERT(new Function(new TypeId[]{CronoArray.TYPEID, CronoInteger.TYPEID,
- CronoArray.TYPEID}, CronoArray.TYPEID, 3)
+ CronoType.TYPEID, 2)
{
- public CronoType run(Visitor v, CronoType[] args) {
- CronoArray dest = (CronoArray)args[0], src = (CronoArray)args[2];
- return dest.insert(src, (int)((CronoInteger)args[1]).value);
- }
- public String toString() {
- return "insert";
- }
- }),
- CONCAT(new Function(new TypeId[]{CronoArray.TYPEID, CronoArray.TYPEID},
- CronoArray.TYPEID, 2)
- {
- public CronoType run(Visitor v, CronoType[] args) {
- CronoArray dest = (CronoArray)args[0], src = (CronoArray)args[1];
- return dest.concat(src);
- }
- public String toString() {
- return "concat";
- }
+ public CronoType run(Visitor v, CronoType[] args) {
+ ((CronoArray)args[0]).append(args[1]);
+ return args[0];
+ }
+ public String toString() {
+ return "append";
+ }
}),
DEFINE(new Function(new TypeId[]{Symbol.TYPEID, CronoType.TYPEID},
- CronoType.TYPEID, 2, EvalType.NONE)
- {
- private static final String _bad_type =
- "DEFINE: expected :symbol, got %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof Symbol)) {
- throw new InterpreterException(_bad_type, args[0].typeId());
- }
- CronoType value = args[1].accept(v);
- v.getEnv().put(((Symbol)args[0]), value);
- return value;
- }
- public String toString() {
- return "define";
- }
- }),
- DEFUN(new Function(new TypeId[]{Symbol.TYPEID, Cons.TYPEID,
- CronoType.TYPEID}, LambdaFunction.TYPEID,
- 3, true, EvalType.NONE)
- {
- private static final String _bad_arg =
- "defun: argument list must be symbols; got %s in argument list";
- public CronoType run(Visitor v, CronoType[] args) {
- List<CronoType> list = ((Cons)args[1]).toList();
- for(CronoType item : list) {
- if(!(item instanceof Symbol)) {
- throw new InterpreterException(_bad_arg,item.typeId());
- }
- }
-
- Symbol[] arglist = new Symbol[list.size()];
- CronoType[] body;
- List<CronoType> blist = new LinkedList<CronoType>();
- for(int i = 2; i < args.length; ++i) {
- blist.add(args[i]);
- }
- body = new CronoType[blist.size()];
- body = blist.toArray(body);
- LambdaFunction lfun = new LambdaFunction(list.toArray(arglist),
- body, v.getEnv());
- v.getEnv().put((Symbol)args[0], lfun);
- lfun.environment.put((Symbol)args[0], lfun);
- return lfun;
- }
- public String toString() {
- return "defun";
- }
+ CronoType.TYPEID, 2, EvalType.NONE)
+ {
+ private static final String _bad_type =
+ "DEFINE: expected :symbol, got %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(!(args[0] instanceof Symbol)) {
+ throw new InterpreterException(_bad_type, args[0].typeId());
+ }
+ CronoType value = args[1].accept(v);
+ v.getEnv().put(((Symbol)args[0]), value);
+ return value;
+ }
+ public String toString() {
+ return "define";
+ }
}),
UNDEFINE(new Function(new TypeId[]{Symbol.TYPEID}, Nil.TYPEID, 1,
- EvalType.NONE)
- {
- private static final String _bad_type =
- "UNDEF: expected :symbol, got %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- StringBuilder builder = new StringBuilder();
- boolean errors = false;
- for(CronoType ct : args) {
- if(!(ct instanceof Symbol)) {
- builder.append(String.format(_bad_type, ct.typeId()));
- builder.append('\n');
- errors = true;
- continue;
- }
- v.getEnv().remove((Symbol)ct);
- }
-
- if(errors) {
- /* Remove last newline */
- builder.deleteCharAt(builder.length() - 1);
- throw new InterpreterException(builder.toString());
- }
- return Nil.NIL;
- }
- public String toString() {
- return "undef";
- }
+ EvalType.NONE)
+ {
+ private static final String _bad_type =
+ "UNDEF: expected :symbol, got %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ StringBuilder builder = new StringBuilder();
+ boolean errors = false;
+ for(CronoType ct : args) {
+ if(!(ct instanceof Symbol)) {
+ builder.append(String.format(_bad_type, ct.typeId()));
+ builder.append('\n');
+ errors = true;
+ continue;
+ }
+ v.getEnv().remove((Symbol)ct);
+ }
+
+ if(errors) {
+ /* Remove last newline */
+ builder.deleteCharAt(builder.length() - 1);
+ throw new InterpreterException(builder.toString());
+ }
+ return Nil.NIL;
+ }
+ public String toString() {
+ return "undef";
+ }
}),
LAMBDA(new Function(new TypeId[]{Cons.TYPEID, CronoType.TYPEID},
- Function.TYPEID, 2, true, EvalType.NONE)
- {
- private static final String _bad_type =
- "\\: expected :cons :any, got %s, %s";
- private static final String _bad_arg =
- "\\: arguments must be :symbol, got %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof Cons)) {
- throw new InterpreterException(_bad_type,
- args[0].typeId(),
- args[1].typeId());
- }
-
- List<CronoType> list = ((Cons)args[0]).toList();
- for(CronoType item : list) {
- if(!(item instanceof Symbol)) {
- throw new InterpreterException(_bad_arg,item.typeId());
- }
- }
-
- Symbol[] arglist = new Symbol[list.size()];
- CronoType[] body;
- List<CronoType> blist = new LinkedList<CronoType>();
- for(int i = 1; i < args.length; ++i) {
- blist.add(args[i]);
- }
- body = new CronoType[blist.size()];
- body = blist.toArray(body);
- return new LambdaFunction(list.toArray(arglist), body,
- v.getEnv());
- }
- public String toString() {
- return "\\";
- }
+ Function.TYPEID, 2, true, EvalType.NONE)
+ {
+ private static final String _bad_type =
+ "\\: expected :cons :any, got %s, %s";
+ private static final String _bad_arg =
+ "\\: arguments must be :symbol, got %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(!(args[0] instanceof Cons)) {
+ throw new InterpreterException(_bad_type,
+ args[0].typeId(),
+ args[1].typeId());
+ }
+
+ List<CronoType> list = ((Cons)args[0]).toList();
+ for(CronoType item : list) {
+ if(!(item instanceof Symbol)) {
+ throw new InterpreterException(_bad_arg,item.typeId());
+ }
+ }
+
+ Symbol[] arglist = new Symbol[list.size()];
+ CronoType[] body;
+ List<CronoType> blist = new LinkedList<CronoType>();
+ for(int i = 1; i < args.length; ++i) {
+ blist.add(args[i]);
+ }
+ body = new CronoType[blist.size()];
+ body = blist.toArray(body);
+ return new LambdaFunction(list.toArray(arglist), body,
+ v.getEnv());
+ }
+ public String toString() {
+ return "\\";
+ }
}),
LET(new Function(new TypeId[]{Cons.TYPEID, CronoType.TYPEID},
- CronoType.TYPEID, 2, true, EvalType.NONE)
- {
- private static final String _subst_list_type =
- "let: substitution list must be :cons, got %s";
- private static final String _subst_not_cons =
- "let: expected :cons in substitution list, got %s";
- private static final String _subst_not_sym =
- "let: argument names numst be :symbol, got %s";
- private static final String _subst_not_pair =
- "let: arguments expect pairs; got %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof Cons)) {
- throw new InterpreterException(_subst_list_type,
- args[0].typeId());
- }
-
- List<Symbol> symlist = new LinkedList<Symbol>();
- List<CronoType> arglist = new LinkedList<CronoType>();
- for(CronoType ct : ((Cons)args[0])) {
- if(!(ct instanceof Cons)) {
- throw new InterpreterException(_subst_not_cons,
- ct.typeId());
- }
-
- CronoType car = ((Cons)ct).car();
- CronoType cdr = ((Cons)ct).cdr();
- if(!(car instanceof Symbol)) {
- throw new InterpreterException(_subst_not_sym,
- car.typeId());
- }
- if(cdr instanceof Cons) {
- if(((Cons)cdr).cdr() != Nil.NIL) {
- throw new InterpreterException(_subst_not_pair,
- cdr);
- }
- cdr = ((Cons)cdr).car();
- }
- cdr = cdr.accept(v);
- symlist.add((Symbol)car);
- arglist.add(cdr);
- }
-
- Symbol[] lsyms = new Symbol[symlist.size()];
- lsyms = symlist.toArray(lsyms);
-
- List<CronoType> bodylist = new LinkedList<CronoType>();
- for(int i = 1; i < args.length; ++i) {
- bodylist.add(args[i]);
- }
- CronoType[] body = new CronoType[bodylist.size()];
- body = bodylist.toArray(body);
-
- CronoType[] largs = new CronoType[arglist.size()];
- largs = arglist.toArray(largs);
-
- LambdaFunction lambda = new LambdaFunction(lsyms,body,v.getEnv());
- return lambda.run(v, largs); /*< -. - */
- }
- public String toString() {
- return "let";
- }
- }),
- LETREC(new Function(new TypeId[]{Cons.TYPEID, CronoType.TYPEID},
- CronoType.TYPEID, 2, true, EvalType.NONE)
- {
- private static final String _subst_list_type =
- "letrec: substitution list must be :cons, got %s";
- private static final String _subst_not_cons =
- "letrec: expected :cons in substitution list, got %s";
- private static final String _subst_not_sym =
- "letrec: argument names numst be :symbol, got %s";
- private static final String _subst_not_pair =
- "let: arguments expect pairs; got %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof Cons)) {
- throw new InterpreterException(_subst_list_type,
- args[0].typeId());
- }
-
- List<Symbol> symlist = new LinkedList<Symbol>();
- List<CronoType> arglist = new LinkedList<CronoType>();
- for(CronoType ct : ((Cons)args[0])) {
- if(!(ct instanceof Cons)) {
- throw new InterpreterException(_subst_not_cons,
- ct.typeId());
- }
-
- CronoType car = ((Cons)ct).car();
- CronoType cdr = ((Cons)ct).cdr();
- if(!(car instanceof Symbol)) {
- throw new InterpreterException(_subst_not_sym,
- car.typeId());
- }
-
- if(cdr instanceof Cons) {
- if(((Cons)cdr).cdr() != Nil.NIL) {
- throw new InterpreterException(_subst_not_pair,
- cdr);
- }
- cdr = ((Cons)cdr).car();
- }
- cdr = cdr.accept(v);
- symlist.add((Symbol)car);
- arglist.add(cdr);
-
- if(cdr instanceof LambdaFunction) {
- LambdaFunction lfun = ((LambdaFunction)cdr);
- lfun.environment.put((Symbol)car, cdr);
- }
- }
-
- Symbol[] lsyms = new Symbol[symlist.size()];
- lsyms = symlist.toArray(lsyms);
-
- List<CronoType> bodylist = new LinkedList<CronoType>();
- for(int i = 1; i < args.length; ++i) {
- bodylist.add(args[i]);
- }
- CronoType[] body = new CronoType[bodylist.size()];
- body = bodylist.toArray(body);
-
- CronoType[] largs = new CronoType[arglist.size()];
- largs = arglist.toArray(largs);
-
- LambdaFunction lambda = new LambdaFunction(lsyms,body,v.getEnv());
- return lambda.run(v, largs); /*< -. - */
- }
- public String toString() {
- return "letrec";
- }
- }),
- WHILE(new Function(new TypeId[]{CronoType.TYPEID, CronoType.TYPEID},
- CronoType.TYPEID, 2, EvalType.NONE)
- {
- public CronoType run(Visitor v, CronoType[] args) {
- CronoType result = Nil.NIL;
- while((args[0].accept(v)) != Nil.NIL) {
- result = args[1].accept(v);
- }
- return result;
- }
- public String toString() {
- return "while";
- }
+ CronoType.TYPEID, 2, true, EvalType.NONE)
+ {
+ private static final String _subst_list_type =
+ "LET: substitution list must be :cons, got %s";
+ private static final String _subst_not_cons =
+ "LET: expected :cons in substitution list, got %s";
+ private static final String _subst_not_sym =
+ "LET: argument names numst be :symbol, got %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(!(args[0] instanceof Cons)) {
+ throw new InterpreterException(_subst_list_type,
+ args[0].typeId());
+ }
+
+ List<Symbol> symlist = new LinkedList<Symbol>();
+ List<CronoType> arglist = new LinkedList<CronoType>();
+ for(CronoType ct : ((Cons)args[0])) {
+ if(!(ct instanceof Cons)) {
+ throw new InterpreterException(_subst_not_cons,
+ ct.typeId());
+ }
+
+ CronoType car = ((Cons)ct).car();
+ CronoType cdr = ((Cons)ct).cdr();
+ if(!(car instanceof Symbol)) {
+ throw new InterpreterException(_subst_not_sym,
+ car.typeId());
+ }
+
+ cdr = cdr.accept(v);
+ symlist.add((Symbol)car);
+ arglist.add(cdr);
+ }
+
+ Symbol[] lsyms = new Symbol[symlist.size()];
+ lsyms = symlist.toArray(lsyms);
+
+ List<CronoType> bodylist = new LinkedList<CronoType>();
+ for(int i = 1; i < args.length; ++i) {
+ bodylist.add(args[i]);
+ }
+ CronoType[] body = new CronoType[bodylist.size()];
+ body = bodylist.toArray(body);
+
+ CronoType[] largs = new CronoType[arglist.size()];
+ largs = arglist.toArray(largs);
+
+ LambdaFunction lambda = new LambdaFunction(lsyms,body,v.getEnv());
+ return lambda.run(v, largs); /*< -. - */
+ }
+ public String toString() {
+ return "let";
+ }
}),
IF(new Function(new TypeId[]{CronoType.TYPEID, CronoType.TYPEID,
- CronoType.TYPEID},
- CronoType.TYPEID, 3, EvalType.NONE)
+ CronoType.TYPEID},
+ CronoType.TYPEID, 3, EvalType.NONE)
{
- public CronoType run(Visitor v, CronoType[] args) {
- CronoType check = args[0].accept(v);
- if(check != Nil.NIL) {
- return args[1].accept(v);
- }
- return args[2].accept(v);
- }
+ public CronoType run(Visitor v, CronoType[] args) {
+ CronoType check = args[0].accept(v);
+ if(check != Nil.NIL) {
+ return args[1].accept(v);
+ }
+ return args[2].accept(v);
+ }
public String toString() {
- return "if";
- }
+ return "if";
+ }
}),
EQ(new Function(new TypeId[]{CronoType.TYPEID, CronoType.TYPEID},
- Cons.TYPEID, 2)
+ Cons.TYPEID, 2)
{
- public CronoType run(Visitor v, CronoType[] args) {
- return (args[0].equals(args[1])) ?
- TruthValue.T : Nil.NIL;
- }
- public String toString() {
- return "=";
- }
+ public CronoType run(Visitor v, CronoType[] args) {
+ return (args[0].equals(args[1])) ?
+ TruthValue.T : Nil.NIL;
+ }
+ public String toString() {
+ return "=";
+ }
}),
LT(new Function(new TypeId[]{CronoPrimitive.TYPEID, CronoPrimitive.TYPEID},
- Cons.TYPEID, 2)
- {
- private static final String _bad_type =
- "<: expected :primitive :primitive, got %s %s";
-
- private Number resolve(CronoType type) {
- if(type instanceof CronoInteger) {
- return ((Long)((CronoInteger)type).value);
- }else if(type instanceof CronoFloat) {
- return ((Double)((CronoFloat)type).value);
- }else if(type instanceof CronoCharacter) {
- return ((Long)((long)((CronoCharacter)type).ch));
- }
- return null;
- }
- public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof CronoPrimitive &&
- args[1] instanceof CronoPrimitive)) {
- throw new InterpreterException(_bad_type, args[0].typeId(),
- args[1].typeId());
- }
-
- Number lhs = resolve(args[0]);
- Number rhs = resolve(args[1]);
- if(lhs instanceof Double || rhs instanceof Double) {
- return (lhs.doubleValue() < rhs.doubleValue()) ?
- TruthValue.T : Nil.NIL;
- }
- return (lhs.longValue() < rhs.longValue()) ?
- TruthValue.T : Nil.NIL;
- }
- public String toString() {
- return "<";
- }
+ Cons.TYPEID, 2)
+ {
+ private static final String _bad_type =
+ "<: expected :primitive :primitive, got %s %s";
+
+ private Number resolve(CronoType type) {
+ if(type instanceof CronoInteger) {
+ return ((Long)((CronoInteger)type).value);
+ }else if(type instanceof CronoFloat) {
+ return ((Double)((CronoFloat)type).value);
+ }else if(type instanceof CronoCharacter) {
+ return ((Long)((long)((CronoCharacter)type).ch));
+ }
+ return null;
+ }
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(!(args[0] instanceof CronoPrimitive &&
+ args[1] instanceof CronoPrimitive)) {
+ throw new InterpreterException(_bad_type, args[0].typeId(),
+ args[1].typeId());
+ }
+
+ Number lhs = resolve(args[0]);
+ Number rhs = resolve(args[1]);
+ if(lhs instanceof Double || rhs instanceof Double) {
+ return (lhs.doubleValue() < rhs.doubleValue()) ?
+ TruthValue.T : Nil.NIL;
+ }
+ return (lhs.longValue() < rhs.longValue()) ?
+ TruthValue.T : Nil.NIL;
+ }
+ public String toString() {
+ return "<";
+ }
}),
GT(new Function(new TypeId[]{CronoPrimitive.TYPEID, CronoPrimitive.TYPEID},
- Cons.TYPEID, 2)
- {
- private static final String _bad_type =
- ">: expected :primitive :primitive, got %s %s";
-
- private Number resolve(CronoType type) {
- if(type instanceof CronoInteger) {
- return ((Long)((CronoInteger)type).value);
- }else if(type instanceof CronoFloat) {
- return ((Double)((CronoFloat)type).value);
- }else if(type instanceof CronoCharacter) {
- return ((Long)((long)((CronoCharacter)type).ch));
- }
- return null;
- }
- public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof CronoPrimitive &&
- args[1] instanceof CronoPrimitive)) {
- throw new InterpreterException(_bad_type, args[0].typeId(),
- args[1].typeId());
- }
-
- Number lhs = resolve(args[0]);
- Number rhs = resolve(args[1]);
- if(lhs instanceof Double || rhs instanceof Double) {
- return (lhs.doubleValue() > rhs.doubleValue()) ?
- TruthValue.T : Nil.NIL;
- }
- return (lhs.longValue() > rhs.longValue()) ?
- TruthValue.T : Nil.NIL;
- }
- public String toString() {
- return ">";
- }
+ Cons.TYPEID, 2)
+ {
+ private static final String _bad_type =
+ ">: expected :primitive :primitive, got %s %s";
+
+ private Number resolve(CronoType type) {
+ if(type instanceof CronoInteger) {
+ return ((Long)((CronoInteger)type).value);
+ }else if(type instanceof CronoFloat) {
+ return ((Double)((CronoFloat)type).value);
+ }else if(type instanceof CronoCharacter) {
+ return ((Long)((long)((CronoCharacter)type).ch));
+ }
+ return null;
+ }
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(!(args[0] instanceof CronoPrimitive &&
+ args[1] instanceof CronoPrimitive)) {
+ throw new InterpreterException(_bad_type, args[0].typeId(),
+ args[1].typeId());
+ }
+
+ Number lhs = resolve(args[0]);
+ Number rhs = resolve(args[1]);
+ if(lhs instanceof Double || rhs instanceof Double) {
+ return (lhs.doubleValue() > rhs.doubleValue()) ?
+ TruthValue.T : Nil.NIL;
+ }
+ return (lhs.longValue() > rhs.longValue()) ?
+ TruthValue.T : Nil.NIL;
+ }
+ public String toString() {
+ return ">";
+ }
}),
ADD(new Function(new TypeId[]{CronoNumber.TYPEID, CronoNumber.TYPEID},
- CronoNumber.TYPEID, 2)
- {
- private static final String _bad_type =
- "+: expected types :number :number, got %s %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- CronoNumber lhs = null, rhs = null;
- if(!(args[0] instanceof CronoNumber &&
- args[1] instanceof CronoNumber)) {
- throw new InterpreterException(_bad_type, args[0].typeId(),
- args[0].typeId());
- }
-
- lhs = (CronoNumber)(args[0]);
- rhs = (CronoNumber)(args[1]);
-
- if(lhs instanceof CronoFloat) {
- double val1, val2;
- val1 = ((CronoFloat)lhs).value;
- if(rhs instanceof CronoFloat) {
- val2 = ((CronoFloat)rhs).value;
- }else {
- val2 = (double)(((CronoInteger)rhs).value);
- }
- return new CronoFloat(val1 + val2);
- }else {
- if(rhs instanceof CronoFloat) {
- double val1, val2;
- val1 = (double)(((CronoInteger)lhs).value);
- val2 = ((CronoFloat)rhs).value;
- return new CronoFloat(val1 + val2);
- }else {
- long val1, val2;
- val1 = ((CronoInteger)lhs).value;
- val2 = ((CronoInteger)rhs).value;
- return new CronoInteger(val1 + val2);
- }
- }
- }
- public String toString() {
- return "+";
- }
+ CronoNumber.TYPEID, 2)
+ {
+ private static final String _bad_type =
+ "+: expected types :number :number, got %s %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ CronoNumber lhs = null, rhs = null;
+ if(!(args[0] instanceof CronoNumber &&
+ args[1] instanceof CronoNumber)) {
+ throw new InterpreterException(_bad_type, args[0].typeId(),
+ args[0].typeId());
+ }
+
+ lhs = (CronoNumber)(args[0]);
+ rhs = (CronoNumber)(args[1]);
+
+ if(lhs instanceof CronoFloat) {
+ double val1, val2;
+ val1 = ((CronoFloat)lhs).value;
+ if(rhs instanceof CronoFloat) {
+ val2 = ((CronoFloat)rhs).value;
+ }else {
+ val2 = (double)(((CronoInteger)rhs).value);
+ }
+ return new CronoFloat(val1 + val2);
+ }else {
+ if(rhs instanceof CronoFloat) {
+ double val1, val2;
+ val1 = (double)(((CronoInteger)lhs).value);
+ val2 = ((CronoFloat)rhs).value;
+ return new CronoFloat(val1 + val2);
+ }else {
+ long val1, val2;
+ val1 = ((CronoInteger)lhs).value;
+ val2 = ((CronoInteger)rhs).value;
+ return new CronoInteger(val1 + val2);
+ }
+ }
+ }
+ public String toString() {
+ return "+";
+ }
}),
SUB(new Function(new TypeId[]{CronoNumber.TYPEID, CronoNumber.TYPEID},
- CronoNumber.TYPEID, 2)
- {
- private static final String _bad_type =
- "-: expected types :number :number, got %s %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- CronoNumber lhs = null, rhs = null;
- if(!(args[0] instanceof CronoNumber &&
- args[1] instanceof CronoNumber)) {
- throw new InterpreterException(_bad_type, args[0].typeId(),
- args[1].typeId());
- }
-
- lhs = (CronoNumber)(args[0]);
- rhs = (CronoNumber)(args[1]);
-
- if(lhs instanceof CronoFloat) {
- double val1, val2;
- val1 = ((CronoFloat)lhs).value;
- if(rhs instanceof CronoFloat) {
- val2 = ((CronoFloat)rhs).value;
- }else {
- val2 = (double)(((CronoInteger)rhs).value);
- }
- return new CronoFloat(val1 - val2);
- }else {
- if(rhs instanceof CronoFloat) {
- double val1, val2;
- val1 = (double)(((CronoInteger)lhs).value);
- val2 = ((CronoFloat)rhs).value;
- return new CronoFloat(val1 - val2);
- }else {
- long val1, val2;
- val1 = ((CronoInteger)lhs).value;
- val2 = ((CronoInteger)rhs).value;
- return new CronoInteger(val1 - val2);
- }
- }
- }
- public String toString() {
- return "-";
- }
+ CronoNumber.TYPEID, 2)
+ {
+ private static final String _bad_type =
+ "-: expected types :number :number, got %s %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ CronoNumber lhs = null, rhs = null;
+ if(!(args[0] instanceof CronoNumber &&
+ args[1] instanceof CronoNumber)) {
+ throw new InterpreterException(_bad_type, args[0].typeId(),
+ args[1].typeId());
+ }
+
+ lhs = (CronoNumber)(args[0]);
+ rhs = (CronoNumber)(args[1]);
+
+ if(lhs instanceof CronoFloat) {
+ double val1, val2;
+ val1 = ((CronoFloat)lhs).value;
+ if(rhs instanceof CronoFloat) {
+ val2 = ((CronoFloat)rhs).value;
+ }else {
+ val2 = (double)(((CronoInteger)rhs).value);
+ }
+ return new CronoFloat(val1 - val2);
+ }else {
+ if(rhs instanceof CronoFloat) {
+ double val1, val2;
+ val1 = (double)(((CronoInteger)lhs).value);
+ val2 = ((CronoFloat)rhs).value;
+ return new CronoFloat(val1 - val2);
+ }else {
+ long val1, val2;
+ val1 = ((CronoInteger)lhs).value;
+ val2 = ((CronoInteger)rhs).value;
+ return new CronoInteger(val1 - val2);
+ }
+ }
+ }
+ public String toString() {
+ return "-";
+ }
}),
MUL(new Function(new TypeId[]{CronoNumber.TYPEID, CronoNumber.TYPEID},
- CronoNumber.TYPEID, 2)
- {
- private static final String _bad_type =
- "*: expected types :number :number, got %s %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- CronoNumber lhs = null, rhs = null;
- if(!(args[0] instanceof CronoNumber &&
- args[1] instanceof CronoNumber)) {
- throw new InterpreterException(_bad_type, args[0].typeId(),
- args[1].typeId());
- }
-
- lhs = (CronoNumber)(args[0]);
- rhs = (CronoNumber)(args[1]);
-
- if(lhs instanceof CronoFloat) {
- double val1, val2;
- val1 = ((CronoFloat)lhs).value;
- if(rhs instanceof CronoFloat) {
- val2 = ((CronoFloat)rhs).value;
- }else {
- val2 = (double)(((CronoInteger)rhs).value);
- }
- return new CronoFloat(val1 * val2);
- }else {
- if(rhs instanceof CronoFloat) {
- double val1, val2;
- val1 = (double)(((CronoInteger)lhs).value);
- val2 = ((CronoFloat)rhs).value;
- return new CronoFloat(val1 * val2);
- }else {
- long val1, val2;
- val1 = ((CronoInteger)lhs).value;
- val2 = ((CronoInteger)rhs).value;
- return new CronoInteger(val1 * val2);
- }
- }
- }
- public String toString() {
- return "*";
- }
+ CronoNumber.TYPEID, 2)
+ {
+ private static final String _bad_type =
+ "*: expected types :number :number, got %s %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ CronoNumber lhs = null, rhs = null;
+ if(!(args[0] instanceof CronoNumber &&
+ args[1] instanceof CronoNumber)) {
+ throw new InterpreterException(_bad_type, args[0].typeId(),
+ args[1].typeId());
+ }
+
+ lhs = (CronoNumber)(args[0]);
+ rhs = (CronoNumber)(args[1]);
+
+ if(lhs instanceof CronoFloat) {
+ double val1, val2;
+ val1 = ((CronoFloat)lhs).value;
+ if(rhs instanceof CronoFloat) {
+ val2 = ((CronoFloat)rhs).value;
+ }else {
+ val2 = (double)(((CronoInteger)rhs).value);
+ }
+ return new CronoFloat(val1 * val2);
+ }else {
+ if(rhs instanceof CronoFloat) {
+ double val1, val2;
+ val1 = (double)(((CronoInteger)lhs).value);
+ val2 = ((CronoFloat)rhs).value;
+ return new CronoFloat(val1 * val2);
+ }else {
+ long val1, val2;
+ val1 = ((CronoInteger)lhs).value;
+ val2 = ((CronoInteger)rhs).value;
+ return new CronoInteger(val1 * val2);
+ }
+ }
+ }
+ public String toString() {
+ return "*";
+ }
}),
DIV(new Function(new TypeId[]{CronoNumber.TYPEID, CronoNumber.TYPEID},
- CronoNumber.TYPEID, 2)
- {
- private static final String _bad_type =
- "/: expected types :number :number, got %s %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- CronoNumber lhs = null, rhs = null;
- if(!(args[0] instanceof CronoNumber &&
- args[1] instanceof CronoNumber)) {
- throw new InterpreterException(_bad_type, args[0].typeId(),
- args[1].typeId());
- }
-
- lhs = (CronoNumber)(args[0]);
- rhs = (CronoNumber)(args[1]);
-
- if(lhs instanceof CronoFloat) {
- double val1, val2;
- val1 = ((CronoFloat)lhs).value;
- if(rhs instanceof CronoFloat) {
- val2 = ((CronoFloat)rhs).value;
- }else {
- val2 = (double)(((CronoInteger)rhs).value);
- }
- return new CronoFloat(val1 / val2);
- }else {
- if(rhs instanceof CronoFloat) {
- double val1, val2;
- val1 = (double)(((CronoInteger)lhs).value);
- val2 = ((CronoFloat)rhs).value;
- return new CronoFloat(val1 / val2);
- }else {
- long val1, val2;
- val1 = ((CronoInteger)lhs).value;
- val2 = ((CronoInteger)rhs).value;
- return new CronoInteger(val1 / val2);
- }
- }
- }
-
- public String toString() {
- return "/";
- }
+ CronoNumber.TYPEID, 2)
+ {
+ private static final String _bad_type =
+ "/: expected types :number :number, got %s %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ CronoNumber lhs = null, rhs = null;
+ if(!(args[0] instanceof CronoNumber &&
+ args[1] instanceof CronoNumber)) {
+ throw new InterpreterException(_bad_type, args[0].typeId(),
+ args[1].typeId());
+ }
+
+ lhs = (CronoNumber)(args[0]);
+ rhs = (CronoNumber)(args[1]);
+
+ if(lhs instanceof CronoFloat) {
+ double val1, val2;
+ val1 = ((CronoFloat)lhs).value;
+ if(rhs instanceof CronoFloat) {
+ val2 = ((CronoFloat)rhs).value;
+ }else {
+ val2 = (double)(((CronoInteger)rhs).value);
+ }
+ return new CronoFloat(val1 / val2);
+ }else {
+ if(rhs instanceof CronoFloat) {
+ double val1, val2;
+ val1 = (double)(((CronoInteger)lhs).value);
+ val2 = ((CronoFloat)rhs).value;
+ return new CronoFloat(val1 / val2);
+ }else {
+ long val1, val2;
+ val1 = ((CronoInteger)lhs).value;
+ val2 = ((CronoInteger)rhs).value;
+ return new CronoInteger(val1 / val2);
+ }
+ }
+ }
+
+ public String toString() {
+ return "/";
+ }
}),
INT(new Function(new TypeId[]{CronoPrimitive.TYPEID},CronoInteger.TYPEID,1)
{
- public static final String _bad_type =
- "INT: Excepted one of :char, :float, or :int; got %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(args[0] instanceof CronoCharacter) {
- return new CronoInteger(((long)((CronoCharacter)args[0]).ch));
- }else if(args[0] instanceof CronoFloat) {
- return new CronoInteger(((long)((CronoFloat)args[0]).value));
- }else if(args[0] instanceof CronoInteger) {
- return args[0];
- }
- throw new InterpreterException(_bad_type, args[0].typeId());
- }
- public String toString() {
- return "int";
- }
+ public static final String _bad_type =
+ "INT: Excepted one of :char, :float, or :int; got %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(args[0] instanceof CronoCharacter) {
+ return new CronoInteger(((long)((CronoCharacter)args[0]).ch));
+ }else if(args[0] instanceof CronoFloat) {
+ return new CronoInteger(((long)((CronoFloat)args[0]).value));
+ }else if(args[0] instanceof CronoInteger) {
+ return args[0];
+ }
+ throw new InterpreterException(_bad_type, args[0].typeId());
+ }
+ public String toString() {
+ return "int";
+ }
}),
CHAR(new Function(new TypeId[]{CronoPrimitive.TYPEID},
- CronoCharacter.TYPEID, 1)
- {
- private static final String _bad_type =
- "CHAR: expected one of :int, or :char; got %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(args[0] instanceof CronoInteger) {
- return new CronoCharacter((char)((CronoInteger)args[0]).value);
- }else if(args[0] instanceof CronoFloat) {
- return new CronoCharacter((char)((CronoFloat)args[0]).value);
- }else if(args[0] instanceof CronoCharacter) {
- return args[0];
- }
- throw new InterpreterException(_bad_type, args[0].typeId());
- }
- public String toString() {
- return "char";
- }
+ CronoCharacter.TYPEID, 1)
+ {
+ private static final String _bad_type =
+ "CHAR: expected one of :int, or :char; got %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(args[0] instanceof CronoInteger) {
+ return new CronoCharacter((char)((CronoInteger)args[0]).value);
+ }else if(args[0] instanceof CronoFloat) {
+ return new CronoCharacter((char)((CronoFloat)args[0]).value);
+ }else if(args[0] instanceof CronoCharacter) {
+ return args[0];
+ }
+ throw new InterpreterException(_bad_type, args[0].typeId());
+ }
+ public String toString() {
+ return "char";
+ }
}),
FLOAT(new Function(new TypeId[]{CronoPrimitive.TYPEID},CronoFloat.TYPEID,1)
{
- private static final String _bad_type =
- "FLOAT: expected one of :int, or :float; got %s";
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(args[0] instanceof CronoInteger) {
- return new CronoFloat((double)((CronoInteger)args[0]).value);
- }else if(args[0] instanceof CronoCharacter) {
- return new CronoFloat((double)((CronoCharacter)args[0]).ch);
- }else if(args[0] instanceof CronoFloat) {
- return args[0];
- }
-
- throw new InterpreterException(_bad_type, args[0].typeId());
- }
- public String toString() {
- return "float";
- }
+ private static final String _bad_type =
+ "FLOAT: expected one of :int, or :float; got %s";
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(args[0] instanceof CronoInteger) {
+ return new CronoFloat((double)((CronoInteger)args[0]).value);
+ }else if(args[0] instanceof CronoCharacter) {
+ return new CronoFloat((double)((CronoCharacter)args[0]).ch);
+ }else if(args[0] instanceof CronoFloat) {
+ return args[0];
+ }
+
+ throw new InterpreterException(_bad_type, args[0].typeId());
+ }
+ public String toString() {
+ return "float";
+ }
}),
LOAD(new Function(new TypeId[]{CronoString.TYPEID}, CronoType.TYPEID, 1)
{
- private static final String _bad_type =
- "LOAD: expected :string, got %s";
- private static final String _file_not_found =
- "LOAD: could not open file %s";
- private static final String _bad_parse =
- "LOAD: error parsing file:\n%s";
-
- private CronoType loadLisp(Visitor v, String fname) {
- InputStream is = null;
- try {
- is = new FileInputStream(fname);
- }catch(FileNotFoundException fnfe) {
- throw new InterpreterException(_file_not_found,
- fnfe.getMessage());
- }
- Parser p = new Parser(is);
- CronoType program = null;
- try {
- program = p.program();
- }catch(ParseException pe) {
- throw new InterpreterException(_bad_parse, pe.getMessage());
- }
- if(!(program instanceof Cons)) {
- return TruthValue.T;
- }
- for(CronoType item : (Cons)program) {
- item.accept(v);
- }
- return TruthValue.T;
- }
- private CronoType loadPackage(Visitor v, String fname) {
- CronoPackage pack = CronoPackage.load(fname);
- Environment env = v.getEnv();
- Function[] funcs = pack.functions();
- if(funcs != null) {
- for(Function f : funcs) {
- env.put(new Symbol(f.toString()), f);
- }
- }
- TypeId[] types = pack.types();
- if(types != null) {
- for(TypeId t : types) {
- env.put(t);
- }
- }
-
- CronoPackage.SymbolPair[] syms = pack.symbols();
- if(syms != null) {
- for(CronoPackage.SymbolPair s : syms) {
- env.put(s.sym, s.type);
- }
- }
-
- return TruthValue.T;
- }
-
- public CronoType run(Visitor v, CronoType[] args) {
- if(!(args[0] instanceof CronoString)) {
- throw new InterpreterException(_bad_type, args[0].typeId());
- }
- String fname = ((CronoString)args[0]).toString();
- String lname = fname.toLowerCase();
- if(lname.matches("[^\\n\\r]*\\.[^\\n\\r]*")) {
- return loadLisp(v, fname);
- }
- return loadPackage(v, fname);
- }
- public String toString() {
- return "load";
+ private static final String _bad_type =
+ "LOAD: expected :string, got %s";
+ private static final String _file_not_found =
+ "LOAD: could not open file %s";
+ private static final String _bad_parse =
+ "LOAD: error parsing file:\n%s";
+
+ private CronoType loadLisp(Visitor v, String fname) {
+ InputStream is = null;
+ try {
+ is = new FileInputStream(fname);
+ }catch(FileNotFoundException fnfe) {
+ throw new InterpreterException(_file_not_found,
+ fnfe.getMessage());
+ }
+ Parser p = new Parser(is);
+ CronoType program = null;
+ try {
+ program = p.program();
+ }catch(ParseException pe) {
+ throw new InterpreterException(_bad_parse, pe.getMessage());
+ }
+ return program.accept(v);
+ }
+ private CronoType loadPackage(Visitor v, String fname) {
+ CronoPackage pack = CronoPackage.load(fname);
+ Environment env = v.getEnv();
+ Function[] funcs = pack.functions();
+ if(funcs != null) {
+ for(Function f : funcs) {
+ env.put(new Symbol(f.toString()), f);
+ }
+ }
+ TypeId[] types = pack.types();
+ if(types != null) {
+ for(TypeId t : types) {
+ env.put(t);
+ }
+ }
+
+ CronoPackage.SymbolPair[] syms = pack.symbols();
+ if(syms != null) {
+ for(CronoPackage.SymbolPair s : syms) {
+ env.put(s.sym, s.type);
+ }
+ }
+
+ return TruthValue.T;
+ }
+
+ public CronoType run(Visitor v, CronoType[] args) {
+ if(!(args[0] instanceof CronoString)) {
+ throw new InterpreterException(_bad_type, args[0].typeId());
+ }
+ String fname = ((CronoString)args[0]).toString();
+ String lname = fname.toLowerCase();
+ if(lname.endsWith(".lisp") || lname.endsWith(".crono")) {
+ return loadLisp(v, fname);
+ }
+ return loadPackage(v, fname);
+ }
+ public String toString() {
+ return "load";
}
}),
PRINT(new Function(new TypeId[]{CronoType.TYPEID}, Nil.TYPEID, 1, true)
{
public CronoType run(Visitor v, CronoType[] args) {
- for(int i = 0; i < args.length; ++i) {
- System.out.print(args[i].toString());
- }
- return Nil.NIL;
+ for(int i = 0; i < args.length; ++i) {
+ System.out.print(args[i].toString());
+ }
+ return Nil.NIL;
}
public String toString() {
- return "print";
+ return "print";
}
}),
PRINTLN(new Function(new TypeId[]{CronoType.TYPEID}, Nil.TYPEID, 1, true)
{
- public CronoType run(Visitor v, CronoType[] args) {
- PRINT.function.run(v, args);
- System.out.println();
- return Nil.NIL;
- }
- public String toString() {
- return "println";
- }
+ public CronoType run(Visitor v, CronoType[] args) {
+ PRINT.function.run(v, args);
+ System.out.println();
+ return Nil.NIL;
+ }
+ public String toString() {
+ return "println";
+ }
}),
STRUCT(new Function(new TypeId[]{Symbol.TYPEID}, CronoStruct.TYPEID, 1,
- false, EvalType.NONE)
+ false, EvalType.NONE)
{
- private static final String _name = "struct";
- private static final String _no_struct_in_scope =
- "struct: No structure definition for %s in scope";
+ private static final String _name = "struct";
+ private static final String _no_struct_in_scope =
+ "struct: No structure definition for %s in scope";
public CronoType run(Visitor v, CronoType[] args) {
- CronoStruct struct = v.getEnv().getStruct((Symbol)args[0]);
- if(struct == null) {
- throw new InterpreterException(_no_struct_in_scope, args[0]);
- }
-
- return struct.copy();
+ CronoStruct struct = v.getEnv().getStruct((Symbol)args[0]);
+ if(struct == null) {
+ throw new InterpreterException(_no_struct_in_scope, args[0]);
+ }
+
+ return struct.copy();
}
public String toString() {
- return _name;
+ return _name;
}
}),
SUBSTRUCT(new Function(new TypeId[]{Symbol.TYPEID, Symbol.TYPEID,
- Cons.TYPEID},
- Nil.TYPEID, 3, false, EvalType.NONE)
+ Cons.TYPEID},
+ Nil.TYPEID, 3, false, EvalType.NONE)
{
- private static final String _name = "substruct";
- private static final String _no_struct_in_scope =
- "substruct: no structure definition for %s in scope";
-
+ private static final String _name = "substruct";
+ private static final String _no_struct_in_scope =
+ "substruct: no structure definition for %s in scope";
+
public CronoType run(Visitor v, CronoType[] args) {
- CronoStruct par = v.getEnv().getStruct((Symbol)args[1]);
- if(par == null) {
- throw new InterpreterException(_no_struct_in_scope, args[1]);
- }
- Map<String, CronoStruct.Field> fields;
- fields = CronoStruct.BuildFieldMap(_name, (Cons)args[2]);
- v.getEnv().put(new CronoStruct(args[0].toString(), fields, par));
- return Nil.NIL;
+ CronoStruct par = v.getEnv().getStruct((Symbol)args[1]);
+ if(par == null) {
+ throw new InterpreterException(_no_struct_in_scope, args[1]);
+ }
+ Map<String, CronoStruct.Field> fields;
+ fields = CronoStruct.BuildFieldMap(_name, (Cons)args[2]);
+ v.getEnv().put(new CronoStruct(args[0].toString(), fields, par));
+ return Nil.NIL;
}
public String toString() {
- return _name;
+ return _name;
}
}),
DEFSTRUCT(new Function(new TypeId[]{Symbol.TYPEID, Cons.TYPEID},
- Nil.TYPEID, 2, false, EvalType.NONE)
+ Nil.TYPEID, 2, false, EvalType.NONE)
{
- private static final String _name = "defstruct";
+ private static final String _name = "defstruct";
public CronoType run(Visitor v, CronoType[] args) {
- Map<String, CronoStruct.Field> fields;
- fields = CronoStruct.BuildFieldMap(_name, (Cons)args[1]);
- v.getEnv().put(new CronoStruct(args[0].toString(), fields));
- return Nil.NIL;
+ Map<String, CronoStruct.Field> fields;
+ fields = CronoStruct.BuildFieldMap(_name, (Cons)args[1]);
+ v.getEnv().put(new CronoStruct(args[0].toString(), fields));
+
+ InfDatabase.insertDefstruct(args[0].toString(),fields);
+
+ return Nil.NIL;
}
public String toString() {
- return _name;
+ return _name;
}
}),
- EXEC(new Function(new TypeId[]{CronoType.TYPEID}, CronoType.TYPEID, 1,
- true)
+ EVAL(new Function(new TypeId[]{CronoString.TYPEID}, Cons.TYPEID, 1)
{
public CronoType run(Visitor v, CronoType[] args) {
- return args[args.length - 1];
+ StringReader reader;
+ reader = new StringReader(((CronoString)args[0]).toString());
+
+ Parser p = new Parser(reader);
+
+ try {
+ return p.program().accept(v);
+ }catch(ParseException pe) {
+ throw new InterpreterException("EVAL: parse error\n%s",
+ pe.getMessage());
+ }
}
public String toString() {
- return "exec";
+ return "eval";
}
}),
- EVAL(new Function(new TypeId[]{CronoString.TYPEID}, Cons.TYPEID, 1)
+ ENTAIL(new Function(new TypeId[]{}, TruthValue.TYPEID, 0)
{
- public CronoType run(Visitor v, CronoType[] args) {
- StringReader reader;
- reader = new StringReader(((CronoString)args[0]).toString());
-
- Parser p = new Parser(reader);
-
- try {
- return p.program().accept(v);
- }catch(ParseException pe) {
- throw new InterpreterException("EVAL: parse error\n%s",
- pe.getMessage());
- }
- }
- public String toString() {
- return "eval";
- }
+ public CronoType run(Visitor v, CronoType[] args) {
+ Environment env = v.getEnv();
+ InfDatabase.entail(env);
+ System.out.println(InfDatabase.printdb());
+ return TruthValue.T;
+ }
+ public String toString(){
+ return "entail";
+ }
}),
;
public final Function function;
private CronoFunction(Function fun) {
- this.function = fun;
+ this.function = fun;
}
}
View
3  crono/src/crono/Interpreter.java
@@ -407,7 +407,8 @@ public CronoType visit(Cons c) {
for(int i = 0; i < types.length; ++i) {
types[i] = argarray[i].typeId();
}
- for(int i = 0; i < fun.args.length; ++i) {
+ int check = Math.min(argarray.length,fun.args.length);
+ for(int i = 0; i < check; ++i) {
if(!(fun.args[i].isType(argarray[i]))) {
String argstr = Arrays.toString(types);
String expected = Arrays.toString(fun.args);
View
1  crono/src/crono/inf/InfClass.java
@@ -0,0 +1 @@
+package crono.inf;
View
107 crono/src/crono/inf/InfDatabase.java
@@ -0,0 +1,107 @@
+package crono.inf;
+import java.util.*;
+import crono.type.*;
+import crono.*;
+
+public class InfDatabase{
+
+ private static ArrayList<InfTriple> elements = new ArrayList<InfTriple>();
+
+ public static boolean insert(InfTriple triple){
+ if (elements.contains(triple)){
+ System.out.println("Duplicate of "+triple+" detected!");
+ return false;
+ }
+ return elements.add(triple);
+ }
+
+ public static void insertDefstruct(String name, Map<String, CronoStruct.Field> fields){
+ InfClass nameClass = new InfClass(name);
+ insert(new InfTriple(nameClass,new Rdf.Type(),new InfStruct.structDef()));
+ for(Map.Entry<String,CronoStruct.Field> entry: fields.entrySet()){
+ //CronoStruct.Field field = entry.getValue();
+ String fieldname = entry.getKey();
+ insert(new InfTriple(nameClass,new InfStruct.HasA(),new InfClass(fieldname)));
+ }
+ System.out.println("Inserted defstruct: "+printdb());
+ return;
+ }
+
+ public static void clearEntailments(){
+ //System.out.println("DESTROY");
+ for(Iterator i = elements.iterator();i.hasNext();){
+ InfTriple t = (InfTriple)i.next();
+ if(t.isEntailed())
+ i.remove();
+ }
+ return;
+ }
+
+ public static ArrayList<InfTriple> getElements(){
+ return elements;
+ }
+
+ public static ArrayList<InfTriple> seek(InfClass iclass,int what){
+ ArrayList<InfTriple> ret = new ArrayList<InfTriple>();
+ for(Iterator i = elements.iterator();i.hasNext();){
+ InfTriple t = (InfTriple)i.next();
+ switch(what){
+ default: case 0:
+ if(iclass.equals(t.getSubject())) ret.add(t);
+ break;
+ case 1:
+ if(iclass.equals(t.getPredicate())) ret.add(t);
+ break;
+ case 2:
+ if(iclass.equals(t.getPredicate())) ret.add(t);
+ break;}
+ }
+ return ret;
+ }
+
+ /*
+ public static ArrayList<InfTriple> getStructFields(Environment env){
+ ArrayList<InfTriple> ret = new ArrayList<InfTriple>();
+ for(Iterator i = elements.iterator();i.hasNext();){
+ InfTriple t = (InfTriple)i.next();
+ if(t.getPredicate() instanceof InfStruct.HasA)
+ insert(new InfTriple(t.getSubject(),t.getObject(),,true))
+ }
+ }
+ }*/
+
+
+ public static boolean entail(Environment env){
+ boolean changed;
+ ArrayList<InfTriple> copy;
+ do{
+ //System.out.println("Entailing...");
+ copy = new ArrayList<InfTriple>(elements);
+ changed = false;
+ for(Iterator i = elements.iterator();i.hasNext();){
+ InfTriple t = (InfTriple)i.next();
+ t.getPredicate().fn(t,copy);
+ }
+ boolean exists = false;
+ for(Iterator j = copy.iterator();j.hasNext();){
+ InfTriple t = (InfTriple)j.next();
+ exists = elements.contains(t);
+ if(!exists){
+ changed = true;
+ insert(t);
+ }
+ }
+ copy = null;
+ }while(changed);
+ return false;
+ }
+
+
+ public static String printdb(){
+ String ret = "Database:\n";
+ for(Iterator i = elements.iterator();i.hasNext();){
+ ret+=i.next()+"\n";
+ }
+ return ret;
+ }
+}
View
42 crono/src/crono/inf/InfStruct.java
@@ -0,0 +1,42 @@
+package crono.inf;
+
+public class InfStruct{
+ public static final IsA isa = new IsA();
+ public static final HasA hasA = new HasA();
+
+ /*
+ public Rdfs(){
+ subClassOf = new SubClassOf();
+ Class = new Class();
+ }*/
+
+ private static enum c{
+ isA(-22,"struct:isA"),
+ hasA(-23,"struct:hasA"),
+ structDef(-24,"struct:structDef"),;
+ public final int id;
+ public final String name;
+ private c(int id, String name){this.id=id;this.name=name;}
+ }
+ public static class structDef extends InfClass{
+ public structDef(){
+ this.id = c.structDef.id;
+ this.name = c.structDef.name;
+ }
+ }
+
+ public static class IsA extends InfClass{
+ public IsA(){
+ this.id = c.isA.id;
+ this.name = c.isA.name;
+ }
+ }
+
+ public static class HasA extends InfClass{
+ public HasA(){
+ this.id = c.hasA.id;
+ this.name = c.hasA.name;
+ }
+ }
+
+}
View
73 crono/src/crono/inf/InfTriple.java
@@ -0,0 +1,73 @@
+package crono.inf;
+import java.util.*;
+
+public class InfTriple
+{
+ private InfClass subject;
+ private InfClass predicate;
+ private InfClass object;
+ private boolean entailed;
+
+ //constructors
+ public InfTriple()
+ {
+ this.subject = new InfClass();
+ this.predicate = new InfClass();
+ this.object = new InfClass();
+ this.entailed = entailed;
+ }
+ public InfTriple(InfClass subject,InfClass predicate,InfClass object)
+ {
+ this.subject = subject;
+ this.predicate = predicate;
+ this.object = object;
+ this.entailed = entailed;
+ }
+ public InfTriple(InfClass subject,InfClass predicate,InfClass object,boolean entailed){
+ this.subject = subject;
+ this.predicate = predicate;
+ this.object = object;
+ this.entailed = entailed;
+ }
+ //accessors/mutators
+ public InfClass getSubject(){
+ return subject;
+ }
+ public InfClass getPredicate(){
+ return predicate;
+ }
+ public InfClass getObject(){
+ return object;
+ }
+ public boolean isEntailed(){
+ return entailed;
+ }
+
+ //overrides
+ @Override public String toString()
+ {
+ return "Triple:("+getSubject().getName()+"("+getSubject().getID()+"),"
+ +getPredicate().getName()+"("+getPredicate().getID()+"),"
+ +getObject().getName()+"("+getObject().getID()+"))"+(isEntailed()?" - Entailed":"");
+ }
+
+ @Override public boolean equals(Object triple2)
+ {
+ if(triple2 instanceof InfTriple)
+ {
+ InfTriple that = (InfTriple)triple2;
+ return this.getSubject().getID() == that.getSubject().getID() &&
+ this.getPredicate().getID() == that.getPredicate().getID() &&
+ this.getObject().getID() == that.getObject().getID();
+ //return this.hashCode() == ((InfClass)triple2).hashCode();
+
+ }
+ return false;
+ }
+
+ @Override public int hashCode()
+ {
+ return (41*(41+getSubject().getID())) + (23*(23+getPredicate().getID())) + getObject().getID();
+ }
+
+}
View
42 crono/src/crono/inf/Rdf.java
@@ -0,0 +1,42 @@
+package crono.inf;
+import java.util.*;
+
+public class Rdf{
+ //private Rdfs rdfs = new Rdfs();
+ //public final Property Property = new Property();
+ //public final Type type = new Type();
+
+ /*
+ public Rdf(){
+ Property = new Property();
+ type = new Type();
+ }*/
+ private static enum c{
+ type(-1,"rdf:type"),
+ Property(-2,"rdf:Property");
+ public final int id;
+ public final String name;
+ private c(int id, String name){this.id=id;this.name=name;}
+ }
+
+ public static class Property extends InfClass{
+ public Property(){
+ this.id = c.Property.id;
+ this.name = c.Property.name;
+ }
+ }
+
+ public static class Type extends InfClass{
+ public Type(){
+ this.id = c.type.id;
+ this.name = c.type.name;
+ }
+
+ protected boolean fn(InfTriple cont, ArrayList<InfTriple> db){
+ InfClass A = (InfClass)cont.getSubject();
+ InfClass B = (InfClass)cont.getObject();
+ return db.add(new InfTriple(B,new Rdf.Type(),new Rdfs.Class(),true));
+ }
+ }
+
+}
View
34 crono/src/crono/inf/Rdfs.java
@@ -0,0 +1,34 @@
+package crono.inf;
+//import java.util.*;
+
+public class Rdfs{
+ //public final SubClassOf subClassOf = new SubClassOf();
+ //public final Class Class = new Class();
+ /*
+ public Rdfs(){
+ subClassOf = new SubClassOf();
+ Class = new Class();
+ }*/
+ private static enum c{
+ subClassOf(-9,"rdfs:subClassOf"),
+ Class(-2,"rdfs:Class");
+ public final int id;
+ public final String name;
+ private c(int id, String name){this.id=id;this.name=name;}
+ }
+
+ public static class Class extends InfClass{
+ public Class(){
+ this.id = c.Class.id;
+ this.name = c.Class.name;
+ }
+ }
+
+ public static class SubClassOf extends InfClass{
+ public SubClassOf(){
+ this.id = c.subClassOf.id;
+ this.name = c.subClassOf.name;
+ }
+ }
+
+}
View
6 crono/src/crono/type/CronoStruct.java
@@ -8,6 +8,8 @@
import crono.InterpreterException;
import crono.Visitor;
+import crono.inf.InfDatabase;
+
/**
* A structure, similar to C structures.
* CronoStruct inherits from Function so that they can be accessed as if their
@@ -137,6 +139,9 @@ protected CronoStruct(CronoStruct cs) {
this.parent = cs.parent;
this.fields = new HashMap<String, Field>();
this.fields.putAll(cs.fields);
+ for(Map.Entry<String,Field> a: fields.entrySet()){
+ System.out.printf("field %s = %s\n",a.getKey(),a.getValue().get());
+ }
this.type = cs.type;
}
@@ -159,6 +164,7 @@ public CronoType run(Visitor v, CronoType[] args) {
throw new InterpreterException(_inv_field_name, name,
args[0].toString());
}
+ InfDatabase.clearEntailments();
return field.put(args[1].accept(v));
default:
Please sign in to comment.
Something went wrong with that request. Please try again.