Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed some mis-merges so we actually compile

  • Loading branch information...
commit c47bdef542fb6aa340aae27e67f4b35717f26bd2 1 parent e228f08
Mark Watts authored
41 crono/src/crono/CronoFunction.java
View
@@ -60,7 +60,7 @@ public String toString()
LIST(new Function(new TypeId[]{CronoType.TYPEID}, Cons.TYPEID, 1, true)
{
public CronoType run(Visitor v, List<CronoType> args) {
- return c.fromList();
+ return Cons.fromList(args);
}
public String toString() {
return "list";
@@ -70,7 +70,7 @@ public String toString() {
CronoType.TYPEID, 2)
{
public CronoType run(Visitor v, List<CronoType> args) {
- CronoArray a = args.remove(0);
+ CronoArray a = (CronoArray) args.remove(0);
int index = (int)((CronoInteger)args.remove(0)).value;
return a.get(index);
}
@@ -106,43 +106,6 @@ public String toString() {
return "append";
}
}),
- 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, List<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 "\\";
- }
- }),
EQ(new Function(new TypeId[]{CronoType.TYPEID, CronoType.TYPEID},
Cons.TYPEID, 2)
{
127 crono/src/crono/CronoSpecial.java
View
@@ -1,5 +1,7 @@
package crono;
import java.util.List;
+import java.util.LinkedList;
+import java.util.ListIterator;
import crono.type.Function.EvalType;
import crono.type.*;
/* These are like functions but have evaluation
@@ -11,16 +13,41 @@
LAMBDA(new Special(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, List<CronoType> args) {
+ if(!(args.get(0) instanceof Cons)) {
+ throw new InterpreterException(_bad_type,
+ args.get(0).typeId(),
+ args.get(1).typeId());
+ }
List<CronoType> list = ((Cons)args.remove(0)).toList();
+ for(CronoType item : list) {
+ if(!(item instanceof Symbol)) {
+ throw new InterpreterException(_bad_arg,item.typeId());
+ }
+ }
+
Symbol[] arglist = new Symbol[list.size()];
- return new LambdaFunction(list.toArray(arglist), args.remove(0),
- v.getEnv());
+
+ CronoType[] body;
+ List<CronoType> blist = new LinkedList<CronoType>();
+ ListIterator<CronoType> it = args.listIterator();
+ while (it.hasNext())
+ {
+ blist.add(it.next());
+ }
+ body = new CronoType[blist.size()];
+ body = blist.toArray(body);
+ return new LambdaFunction(list.toArray(arglist), body,
+ v.getEnv());
+ }
+ public String toString() {
+ return "\\";
}
- public String toString() {
- return "\\";
- }
});
//DEFINE(new Function(new TypeId[]{Symbol.TYPEID, CronoType.TYPEID},
//CronoType.TYPEID, 2, EvalType.NONE)
@@ -73,62 +100,64 @@ public String toString() {
//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_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());
- //}
+ //public CronoType run(Visitor v, List<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());
- //}
+ //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());
- //}
+ //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);
- //}
+ //cdr = cdr.accept(v);
+ //symlist.add((Symbol)car);
+ //arglist.add(cdr);
+ //}
- //Symbol[] lsyms = new Symbol[symlist.size()];
- //lsyms = symlist.toArray(lsyms);
+ //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 = Cons.fromList(bodylist);
- //CronoType[] largs = new CronoType[arglist.size()];
- //largs = arglist.toArray(largs);
+ //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);
- //LambdaFunction lambda = new LambdaFunction(lsyms,body,v.getEnv());
- //return lambda.run(v, largs); [>< -. - <]
- //}
- //public String toString() {
- //return "let";
- //}
+ //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)
//{
- //public CronoType run(Visitor v, CronoType[] args) {
+ //public CronoType run(Visitor v, List<CronoType> args) {
//CronoType check = args[0].accept(v);
//if(check != Nil.NIL) {
//return args[1].accept(v);
241 crono/src/crono/Interpreter.java
View
@@ -1,241 +0,0 @@
-package crono;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Stack;
-
-import crono.type.Atom;
-import crono.type.Cons;
-import crono.type.CronoType;
-import crono.type.Function;
-import crono.type.LambdaFunction;
-import crono.type.Nil;
-import crono.type.Quote;
-import crono.type.Symbol;
-import crono.type.CronoTypeId;
-import crono.type.TypeId;
-
-public class Interpreter extends Visitor {
- private static final String _scope_err = "No object %s in scope";
- private static final String _too_many_args =
- "Too many arguments to %s: %d/%d recieved";
- private static final String _type_scope_err = "No type %s in scope";
- private static final String _type_mismatch =
- "Function '%s' expected arguments %s; got %s";
-
- public boolean show_env;
- public boolean show_closure;
- public boolean dprint_enable;
- public boolean dprint_ident;
- public boolean dynamic;
-
- protected int indent_level;
- protected Function.EvalType eval;
-
- public Interpreter() {
- show_env = false;
- show_closure = false;
- dprint_enable = false;
- dprint_ident = true;
- dynamic = false;
-
- indent_level = 0;
- eval = Function.EvalType.FULL;
-
- env_stack = new Stack<Environment>();
- reset(); /*< Set up initial environment and types */
- }
- public void reset() {
- env_stack.clear();
- pushEnv(new Environment());
- }
-
- public CronoType visit(Cons c) {
- if(eval == Function.EvalType.NONE) {
- return c;
- }
-
- Iterator<CronoType> iter = c.iterator();
- if(!(iter.hasNext())) {
- return c; /*< C is an empty list (may be Nil or T) */
- }
-
- CronoType value = iter.next().accept(this);
- if(value instanceof Function) {
- Function fun = ((Function)value);
-
- Function.EvalType reserve = eval;
- /* Set the eval type to the current function's type; this keeps
- * type errors in builtins from happening, ex:
- * (+ arg1 arg2) under partial evaluation would fail since +
- * expects two numbers.
- */
- eval = fun.eval;
- if(eval.level > reserve.level) {
- eval = reserve;
- }
- List<CronoType> args = new ArrayList<CronoType>();
- while(iter.hasNext()) {
- args.add(iter.next().accept(this));
- }
- eval = reserve;
-
- int arglen = args.size();
- int nargs = fun.arity;
- if(arglen < nargs) {
- if(arglen == 0) {
- /* Special case -- we don't have to do anything to the
- * function to return it properly. */
- return fun;
- }
-
- /* Curry it */
- if(fun instanceof LambdaFunction) {
- LambdaFunction lfun = ((LambdaFunction)fun);
- Environment env = getEnv();
- if(!dynamic) {
- /* Use the lambda's stored environment */
- env = lfun.environment;
- }
- /* We want to preserve the current environment */
- env = new Environment(env);
-
- /* Put known values into the new environment */
- for(int i = 0; i < arglen; ++i) {
- env.put(lfun.arglist[i], args.get(i));
- }
- /* Create new argument list and remove remaining args from
- * the new environment */
- List<Symbol> largs = new ArrayList<Symbol>();
- for(int i = arglen; i < lfun.arglist.length; ++i) {
- largs.add(lfun.arglist[i]);
- env.remove(lfun.arglist[i]);
- }
-
- /* Evaluate the body as much as possible */
- reserve = eval;
- eval = Function.EvalType.PARTIAL;
- pushEnv(env);
- CronoType[] lbody = new CronoType[lfun.body.length];
- for(int i = 0; i < lfun.body.length; ++i) {
- lbody[i] = lfun.body[i].accept(this);
- }
- popEnv();
- eval = reserve;
-
- /* Return the new, partially evaluated lambda */
- Symbol[] arglist = new Symbol[largs.size()];
- return new LambdaFunction(largs.toArray(arglist),
- lbody, lfun.environment);
- }
- /* Builtin partial evaluation */
-
- List<CronoType> body = new LinkedList<CronoType>();
- body.add(fun);
-
- body.addAll(args); /*< Dump args in order into the new cons */
- CronoType[] barr = new CronoType[body.size()];
- barr = body.toArray(barr);
-
- /* Add symbols for missing args */
- List<Symbol> arglist = new ArrayList<Symbol>();
- Symbol sym;
- for(int i = arglen, n = 0; i < nargs; ++i, ++n) {
- sym = new Symbol(String.format("_i?%d!_", n));
- body.add(sym);
- arglist.add(sym);
- }
-
- /* Create a new lambda */
- Symbol[] narglist = new Symbol[arglist.size()];
- return new LambdaFunction(arglist.toArray(narglist), barr,
- getEnv());
- }
- if(arglen > nargs && !fun.variadic) {
- throw new RuntimeException(String.format(_too_many_args, fun,
- arglen, nargs));
- }
-
- /* Full evaluation */
- if(fun instanceof LambdaFunction && dynamic) {
- /* We have to trick the lambda function if we want dynamic
- * scoping. I hate making so many objects left and right, but
- * this is the easiest way to do what I want here. */
- LambdaFunction lfun = ((LambdaFunction)fun);
- lfun = new LambdaFunction(lfun.arglist, lfun.body, getEnv());
-
- CronoType[] argarray = new CronoType[args.size()];
- return lfun.run(this, args.toArray(argarray));
- }
- if(eval == Function.EvalType.FULL) {
- CronoType[] argarray = new CronoType[args.size()];
- argarray = args.toArray(argarray);
- TypeId[] types = new TypeId[args.size()];
- for(int i = 0; i < types.length; ++i) {
- types[i] = argarray[i].typeId();
- }
- for(int i = 0; i < fun.args.length; ++i) {
- if(!(fun.args[i].isType(argarray[i]))) {
- String argstr = Arrays.toString(types);
- String expected = Arrays.toString(fun.args);
- throw new InterpreterException(_type_mismatch, fun,
- expected, argstr);
- }
- }
- return ((Function)value).run(this, args.toArray(argarray));
- }else {
- args.add(0, value);
- return Cons.fromList(args);
- }
- }
-
- /* The initial value is not a function */
- throw new InterpreterException("Invalid Function Application: %s is not a function in %s", value, c);
- /*
- List<CronoType> list = new LinkedList<CronoType>();
- list.add(value);
- while(iter.hasNext()) {
- list.add(iter.next().accept(this));
- }
- return Cons.fromList(list);
- */
- }
-
- public CronoType visit(Atom a) {
- if(eval == Function.EvalType.NONE) {
- return a;
- }
-
- CronoType t = a;
- if(t instanceof Symbol) {
- t = getEnv().get((Symbol)a);
- if(t == null) {
- if(eval == Function.EvalType.FULL) {
- throw new RuntimeException(String.format(_scope_err,
- a.toString()));
- }
- t = a;
- }
- }
- /* Not else-if, so that we perform a double-resolution on a symbol that
- * represents a TypeId */
- if(t instanceof CronoTypeId) {
- CronoType res = t; /*< Save symbol resolution in new CronoType */
- t = getEnv().getType((CronoTypeId)t);
- if(t == null) {
- if(eval == Function.EvalType.FULL) {
- throw new InterpreterException(_type_scope_err, a);
- }
- t = res; /*< Revert to symbol resolution */
- }
- }
- return t;
- }
-
- public CronoType visit(Quote q) {
- return q.node;
- }
-}
390 crono/src/crono/Interpreter.java.noncomp
View
@@ -21,215 +21,221 @@ import crono.type.TypeId;
public class Interpreter extends Visitor {
private static final String _scope_err = "No object %s in scope";
private static final String _too_many_args =
- "Too many arguments to %s: %d/%d recieved";
+ "Too many arguments to %s: %d/%d recieved";
private static final String _type_scope_err = "No type %s in scope";
private static final String _type_mismatch =
- "Function '%s' expected arguments %s; got %s";
-
+ "Function '%s' expected arguments %s; got %s";
+
public boolean show_env;
public boolean show_closure;
public boolean dprint_enable;
public boolean dprint_ident;
public boolean dynamic;
-
+
protected int indent_level;
protected Function.EvalType eval;
-
+
public Interpreter() {
- show_env = false;
- show_closure = false;
- dprint_enable = false;
- dprint_ident = true;
- dynamic = false;
-
- indent_level = 0;
- eval = Function.EvalType.FULL;
-
- env_stack = new Stack<Environment>();
- reset(); /*< Set up initial environment and types */
+ show_env = false;
+ show_closure = false;
+ dprint_enable = false;
+ dprint_ident = true;
+ dynamic = false;
+
+ indent_level = 0;
+ eval = Function.EvalType.FULL;
+
+ env_stack = new Stack<Environment>();
+ reset(); /*< Set up initial environment and types */
}
-
public void reset() {
- env_stack.clear();
- pushEnv(new Environment());
+ env_stack.clear();
+ pushEnv(new Environment());
}
-
+
public CronoType visit(Cons c) {
- if(eval == Function.EvalType.NONE) {
- return c;
- }
-
- Iterator<CronoType> iter = c.iterator();
- if(!(iter.hasNext())) {
- return c; /*< C is an empty list (may be Nil or T) */
- }
-
- CronoType value = iter.next().accept(this);
- if(value instanceof Function) {
- Function fun = ((Function)value);
-
- Function.EvalType reserve = eval;
- /* Set the eval type to the current function's type; this keeps
- * type errors in builtins from happening, ex:
- * (+ arg1 arg2) under partial evaluation would fail since +
- * expects two numbers.
- */
- eval = fun.eval;
- if(eval.level > reserve.level) {
- eval = reserve;
- }
- List<CronoType> args = new ArrayList<CronoType>();
- while(iter.hasNext()) {
- args.add(iter.next().accept(this));
- }
- eval = reserve;
-
- int arglen = args.size();
- int nargs = fun.arity;
- if(arglen < nargs) {
- if(arglen == 0) {
- /* Special case -- we don't have to do anything to the
- * function to return it properly. */
- return fun;
- }
-
- /* Curry it */
- if(fun instanceof LambdaFunction) {
- LambdaFunction lfun = ((LambdaFunction)fun);
- Environment env = getEnv();
- if(!dynamic) {
- /* Use the lambda's stored environment */
- env = lfun.environment;
- }
- /* We want to preserve the current environment */
- env = new Environment(env);
-
- /* Put known values into the new environment */
- for(int i = 0; i < arglen; ++i) {
- env.put(lfun.arglist[i], args.get(i));
- }
- /* Create new argument list and remove remaining args from
- * the new environment */
- List<Symbol> largs = new ArrayList<Symbol>();
- for(int i = arglen; i < lfun.arglist.length; ++i) {
- largs.add(lfun.arglist[i]);
- env.remove(lfun.arglist[i]);
- }
-
- /* Evaluate the body as much as possible */
- reserve = eval;
- eval = Function.EvalType.PARTIAL;
- pushEnv(env);
- CronoType lbody = lfun.body.accept(this);
- popEnv();
- eval = reserve;
-
- /* Return the new, partially evaluated lambda */
- Symbol[] arglist = new Symbol[largs.size()];
- return new LambdaFunction(largs.toArray(arglist),
- lbody, lfun.environment);
- }
- /* Builtin partial evaluation */
-
- List<CronoType> body = new LinkedList<CronoType>();
- body.add(fun);
-
- body.addAll(args); /*< Dump args in order into the new cons */
-
- /* Add symbols for missing args */
- List<Symbol> arglist = new ArrayList<Symbol>();
- Symbol sym;
- for(int i = arglen, n = 0; i < nargs; ++i, ++n) {
- sym = new Symbol(String.format("_i?%d!_", n));
- body.add(sym);
- arglist.add(sym);
- }
-
- /* Create a new lambda */
- Symbol[] narglist = new Symbol[arglist.size()];
- return new LambdaFunction(arglist.toArray(narglist),
- Cons.fromList(body), getEnv());
- }
- if(arglen > nargs && !fun.variadic) {
- throw new RuntimeException(String.format(_too_many_args, fun,
- arglen, nargs));
- }
-
- /* Full evaluation */
- if(fun instanceof LambdaFunction && dynamic) {
- /* We have to trick the lambda function if we want dynamic
- * scoping. I hate making so many objects left and right, but
- * this is the easiest way to do what I want here. */
- LambdaFunction lfun = ((LambdaFunction)fun);
- lfun = new LambdaFunction(lfun.arglist, lfun.body, getEnv());
-
- CronoType[] argarray = new CronoType[args.size()];
- return lfun.run(this, args.toArray(argarray));
- }
- if(eval == Function.EvalType.FULL) {
- CronoType[] argarray = new CronoType[args.size()];
- argarray = args.toArray(argarray);
- TypeId[] types = new TypeId[args.size()];
- for(int i = 0; i < types.length; ++i) {
- types[i] = argarray[i].typeId();
- }
- for(int i = 0; i < argarray.length; ++i) {
- if(!(fun.args[i].isType(argarray[i]))) {
- String argstr = Arrays.toString(argarray);
- String expected = Arrays.toString(fun.args);
- throw new InterpreterException(_type_mismatch, fun,
- expected, argstr);
- }
- }
- return ((Function)value).run(this, args.toArray(argarray));
- }else {
- args.add(0, value);
- return Cons.fromList(args);
- }
- }
-
- /* The initial value is not a function */
- List<CronoType> list = new LinkedList<CronoType>();
- list.add(value);
- while(iter.hasNext()) {
- list.add(iter.next().accept(this));
- }
- return Cons.fromList(list);
+ if(eval == Function.EvalType.NONE) {
+ return c;
+ }
+
+ Iterator<CronoType> iter = c.iterator();
+ if(!(iter.hasNext())) {
+ return c; /*< C is an empty list (may be Nil or T) */
+ }
+
+ CronoType value = iter.next().accept(this);
+ if(value instanceof Function) {
+ Function fun = ((Function)value);
+
+ Function.EvalType reserve = eval;
+ /* Set the eval type to the current function's type; this keeps
+ * type errors in builtins from happening, ex:
+ * (+ arg1 arg2) under partial evaluation would fail since +
+ * expects two numbers.
+ */
+ eval = fun.eval;
+ if(eval.level > reserve.level) {
+ eval = reserve;
+ }
+ List<CronoType> args = new ArrayList<CronoType>();
+ while(iter.hasNext()) {
+ args.add(iter.next().accept(this));
+ }
+ eval = reserve;
+
+ int arglen = args.size();
+ int nargs = fun.arity;
+ if(arglen < nargs) {
+ if(arglen == 0) {
+ /* Special case -- we don't have to do anything to the
+ * function to return it properly. */
+ return fun;
+ }
+
+ /* Curry it */
+ if(fun instanceof LambdaFunction) {
+ LambdaFunction lfun = ((LambdaFunction)fun);
+ Environment env = getEnv();
+ if(!dynamic) {
+ /* Use the lambda's stored environment */
+ env = lfun.environment;
+ }
+ /* We want to preserve the current environment */
+ env = new Environment(env);
+
+ /* Put known values into the new environment */
+ for(int i = 0; i < arglen; ++i) {
+ env.put(lfun.arglist[i], args.get(i));
+ }
+ /* Create new argument list and remove remaining args from
+ * the new environment */
+ List<Symbol> largs = new ArrayList<Symbol>();
+ for(int i = arglen; i < lfun.arglist.length; ++i) {
+ largs.add(lfun.arglist[i]);
+ env.remove(lfun.arglist[i]);
+ }
+
+ /* Evaluate the body as much as possible */
+ reserve = eval;
+ eval = Function.EvalType.PARTIAL;
+ pushEnv(env);
+ CronoType[] lbody = new CronoType[lfun.body.length];
+ for(int i = 0; i < lfun.body.length; ++i) {
+ lbody[i] = lfun.body[i].accept(this);
+ }
+ popEnv();
+ eval = reserve;
+
+ /* Return the new, partially evaluated lambda */
+ Symbol[] arglist = new Symbol[largs.size()];
+ return new LambdaFunction(largs.toArray(arglist),
+ lbody, lfun.environment);
+ }
+ /* Builtin partial evaluation */
+
+ List<CronoType> body = new LinkedList<CronoType>();
+ body.add(fun);
+
+ body.addAll(args); /*< Dump args in order into the new cons */
+ CronoType[] barr = new CronoType[body.size()];
+ barr = body.toArray(barr);
+
+ /* Add symbols for missing args */
+ List<Symbol> arglist = new ArrayList<Symbol>();
+ Symbol sym;
+ for(int i = arglen, n = 0; i < nargs; ++i, ++n) {
+ sym = new Symbol(String.format("_i?%d!_", n));
+ body.add(sym);
+ arglist.add(sym);
+ }
+
+ /* Create a new lambda */
+ Symbol[] narglist = new Symbol[arglist.size()];
+ return new LambdaFunction(arglist.toArray(narglist), barr,
+ getEnv());
+ }
+ if(arglen > nargs && !fun.variadic) {
+ throw new RuntimeException(String.format(_too_many_args, fun,
+ arglen, nargs));
+ }
+
+ /* Full evaluation */
+ if(fun instanceof LambdaFunction && dynamic) {
+ /* We have to trick the lambda function if we want dynamic
+ * scoping. I hate making so many objects left and right, but
+ * this is the easiest way to do what I want here. */
+ LambdaFunction lfun = ((LambdaFunction)fun);
+ lfun = new LambdaFunction(lfun.arglist, lfun.body, getEnv());
+
+ CronoType[] argarray = new CronoType[args.size()];
+ return lfun.run(this, args.toArray(argarray));
+ }
+ if(eval == Function.EvalType.FULL) {
+ CronoType[] argarray = new CronoType[args.size()];
+ argarray = args.toArray(argarray);
+ TypeId[] types = new TypeId[args.size()];
+ for(int i = 0; i < types.length; ++i) {
+ types[i] = argarray[i].typeId();
+ }
+ for(int i = 0; i < fun.args.length; ++i) {
+ if(!(fun.args[i].isType(argarray[i]))) {
+ String argstr = Arrays.toString(types);
+ String expected = Arrays.toString(fun.args);
+ throw new InterpreterException(_type_mismatch, fun,
+ expected, argstr);
+ }
+ }
+ return ((Function)value).run(this, args.toArray(argarray));
+ }else {
+ args.add(0, value);
+ return Cons.fromList(args);
+ }
+ }
+
+ /* The initial value is not a function */
+ throw new InterpreterException("Invalid Function Application: %s is not a function in %s", value, c);
+ /*
+ List<CronoType> list = new LinkedList<CronoType>();
+ list.add(value);
+ while(iter.hasNext()) {
+ list.add(iter.next().accept(this));
+ }
+ return Cons.fromList(list);
+ */
}
-
+
public CronoType visit(Atom a) {
- if(eval == Function.EvalType.NONE) {
- return a;
- }
-
- CronoType t = a;
- if(t instanceof Symbol) {
- t = getEnv().get((Symbol)a);
- if(t == null) {
- if(eval == Function.EvalType.FULL) {
- throw new RuntimeException(String.format(_scope_err,
- a.toString()));
- }
- t = a;
- }
- }
- /* Not else-if, so that we perform a double-resolution on a symbol that
- * represents a TypeId */
- if(t instanceof CronoTypeId) {
- CronoType res = t; /*< Save symbol resolution in new CronoType */
- t = getEnv().getType((CronoTypeId)t);
- if(t == null) {
- if(eval == Function.EvalType.FULL) {
- throw new InterpreterException(_type_scope_err, a);
- }
- t = res; /*< Revert to symbol resolution */
- }
- }
- return t;
+ if(eval == Function.EvalType.NONE) {
+ return a;
+ }
+
+ CronoType t = a;
+ if(t instanceof Symbol) {
+ t = getEnv().get((Symbol)a);
+ if(t == null) {
+ if(eval == Function.EvalType.FULL) {
+ throw new RuntimeException(String.format(_scope_err,
+ a.toString()));
+ }
+ t = a;
+ }
+ }
+ /* Not else-if, so that we perform a double-resolution on a symbol that
+ * represents a TypeId */
+ if(t instanceof CronoTypeId) {
+ CronoType res = t; /*< Save symbol resolution in new CronoType */
+ t = getEnv().getType((CronoTypeId)t);
+ if(t == null) {
+ if(eval == Function.EvalType.FULL) {
+ throw new InterpreterException(_type_scope_err, a);
+ }
+ t = res; /*< Revert to symbol resolution */
+ }
+ }
+ return t;
}
-
+
public CronoType visit(Quote q) {
- return q.node;
+ return q.node;
}
- public CronoType defaultVisit(CronoType object) {return object;}
}
2  crono/src/crono/type/CronoStruct.java
View
@@ -141,7 +141,7 @@ protected CronoStruct(CronoStruct cs) {
this.type = cs.type;
}
- public CronoType run(Visitor v, CronoType[] args) {
+ public CronoType run(Visitor v, List<CronoType> args) {
/* CronoStruct doesn't contain an AST, so we can ignore the visitor */
Field field;
switch(args.size()) {
112 crono/src/crono/type/CronoVector.java
View
@@ -6,87 +6,87 @@
public class CronoVector extends CronoArray {
public static final TypeId TYPEID = new TypeId(":vector",
- CronoVector.class,
- CronoArray.TYPEID);
+ CronoVector.class,
+ CronoArray.TYPEID);
private static final String _rank_dim_mismatch =
- "Wrong number of dimensions to Array of rank %d: %d";
+ "Wrong number of dimensions to Array of rank %d: %d";
private static final String _data_size_mismatch =
- "Data size (%d) does not match Array size: %d";
+ "Data size (%d) does not match Array size: %d";
private static final String _inv_rank =
- "Array Rank must be a positive, non-zero integer";
-
+ "Array Rank must be a positive, non-zero integer";
+
protected ArrayList<CronoType> data;
protected int size;
protected TypeId type;
-
+
public CronoVector(int size) {
- this(size, CronoType.TYPEID);
+ this(size, CronoType.TYPEID);
}
-
+
public CronoVector(int size, TypeId accept) {
- super(accept);
-
- this.type = new NestedTypeId(":vector", CronoVector.class,
- new TypeId[]{accept}, TYPEID);
-
- this.size = size;
- this.data = new ArrayList<CronoType>(size);
- for(int i = 0; i < size; ++i) {
- this.data.add(i, Nil.NIL);
- }
+ super(accept);
+
+ this.type = new NestedTypeId(":vector", CronoVector.class,
+ new TypeId[]{accept}, TYPEID);
+
+ this.size = size;
+ this.data = new ArrayList<CronoType>(size);
+ for(int i = 0; i < size; ++i) {
+ this.data.add(i, Nil.NIL);
+ }
}
public CronoVector(CronoType[] data) {
- this(data, CronoType.TYPEID);
+ this(data, CronoType.TYPEID);
}
public CronoVector(CronoType[] data, TypeId accept) {
- super(accept);
- this.type = new NestedTypeId(":vector", CronoVector.class,
- new TypeId[]{accept}, TYPEID);
- this.data = new ArrayList<CronoType>();
- for(int i = 0; i < data.length; ++i) {
- this.data.add(data[i]);
- }
- this.size = data.length;
+ super(accept);
+ this.type = new NestedTypeId(":vector", CronoVector.class,
+ new TypeId[]{accept}, TYPEID);
+ this.data = new ArrayList<CronoType>();
+ for(int i = 0; i < data.length; ++i) {
+ this.data.add(data[i]);
+ }
+ this.size = data.length;
}
-
+
public int size() {
- return size;
+ return size;
}
-
+
public CronoType get(int n) {
- if(n >= size || n < 0) {
- throw new InterpreterException("Array index out of bounds");
- }
- return data.get(n);
+ if(n >= size || n < 0) {
+ throw new InterpreterException("Array index out of bounds");
+ }
+ return data.get(n);
}
public CronoType put(int n, CronoType item) {
- if(n >= size || n < 0) {
- throw new InterpreterException("Array index out of bounds");
- }
- data.set(n, item);
- return item;
+ if(n >= size || n < 0) {
+ throw new InterpreterException("Array index out of bounds");
+ }
+ data.set(n, item);
+ return item;
}
public CronoType append(CronoType item) {
- data.add(item);
- size++;
- return item;
+ data.add(item);
+ size++;
+ return item;
}
public CronoType concat(CronoArray array) {
- return Nil.NIL;
+ return Nil.NIL;
}
-
+
public TypeId typeId() {
- return type;
+ return type;
}
public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append("[");
- for(int i = 0; i < size - 1; ++i) {
- builder.append(data.get(i).repr());
- builder.append(", ");
- }
- builder.append(data.get(size - 1).repr());
- builder.append("]");
- return builder.toString();
+ StringBuilder builder = new StringBuilder();
+ builder.append("[");
+ for(int i = 0; i < size - 1; ++i) {
+ builder.append(data.get(i).repr());
+ builder.append(", ");
+ }
+ builder.append(data.get(size - 1).repr());
+ builder.append("]");
+ return builder.toString();
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.