Skip to content
This repository
Browse code

Fixed some mis-merges so we actually compile

  • Loading branch information...
commit c47bdef542fb6aa340aae27e67f4b35717f26bd2 1 parent e228f08
mwatts15 authored
41 crono/src/crono/CronoFunction.java
@@ -60,7 +60,7 @@ public String toString()
60 60 LIST(new Function(new TypeId[]{CronoType.TYPEID}, Cons.TYPEID, 1, true)
61 61 {
62 62 public CronoType run(Visitor v, List<CronoType> args) {
63   - return c.fromList();
  63 + return Cons.fromList(args);
64 64 }
65 65 public String toString() {
66 66 return "list";
@@ -70,7 +70,7 @@ public String toString() {
70 70 CronoType.TYPEID, 2)
71 71 {
72 72 public CronoType run(Visitor v, List<CronoType> args) {
73   - CronoArray a = args.remove(0);
  73 + CronoArray a = (CronoArray) args.remove(0);
74 74 int index = (int)((CronoInteger)args.remove(0)).value;
75 75 return a.get(index);
76 76 }
@@ -106,43 +106,6 @@ public String toString() {
106 106 return "append";
107 107 }
108 108 }),
109   - LAMBDA(new Function(new TypeId[]{Cons.TYPEID, CronoType.TYPEID},
110   - Function.TYPEID, 2, true, EvalType.NONE)
111   - {
112   - private static final String _bad_type =
113   - "\\: expected :cons :any, got %s, %s";
114   - private static final String _bad_arg =
115   - "\\: arguments must be :symbol, got %s";
116   -
117   - public CronoType run(Visitor v, List<CronoType> args) {
118   - if(!(args[0] instanceof Cons)) {
119   - throw new InterpreterException(_bad_type,
120   - args[0].typeId(),
121   - args[1].typeId());
122   - }
123   -
124   - List<CronoType> list = ((Cons)args[0]).toList();
125   - for(CronoType item : list) {
126   - if(!(item instanceof Symbol)) {
127   - throw new InterpreterException(_bad_arg,item.typeId());
128   - }
129   - }
130   -
131   - Symbol[] arglist = new Symbol[list.size()];
132   - CronoType[] body;
133   - List<CronoType> blist = new LinkedList<CronoType>();
134   - for(int i = 1; i < args.length; ++i) {
135   - blist.add(args[i]);
136   - }
137   - body = new CronoType[blist.size()];
138   - body = blist.toArray(body);
139   - return new LambdaFunction(list.toArray(arglist), body,
140   - v.getEnv());
141   - }
142   - public String toString() {
143   - return "\\";
144   - }
145   - }),
146 109 EQ(new Function(new TypeId[]{CronoType.TYPEID, CronoType.TYPEID},
147 110 Cons.TYPEID, 2)
148 111 {
127 crono/src/crono/CronoSpecial.java
... ... @@ -1,5 +1,7 @@
1 1 package crono;
2 2 import java.util.List;
  3 +import java.util.LinkedList;
  4 +import java.util.ListIterator;
3 5 import crono.type.Function.EvalType;
4 6 import crono.type.*;
5 7 /* These are like functions but have evaluation
@@ -11,16 +13,41 @@
11 13 LAMBDA(new Special(new TypeId[]{Cons.TYPEID, CronoType.TYPEID},
12 14 Function.TYPEID, 2, true, EvalType.NONE)
13 15 {
  16 + private static final String _bad_type =
  17 + "\\: expected :cons :any, got %s, %s";
  18 + private static final String _bad_arg =
  19 + "\\: arguments must be :symbol, got %s";
14 20 public CronoType run(Visitor v, List<CronoType> args) {
  21 + if(!(args.get(0) instanceof Cons)) {
  22 + throw new InterpreterException(_bad_type,
  23 + args.get(0).typeId(),
  24 + args.get(1).typeId());
  25 + }
15 26 List<CronoType> list = ((Cons)args.remove(0)).toList();
16 27
  28 + for(CronoType item : list) {
  29 + if(!(item instanceof Symbol)) {
  30 + throw new InterpreterException(_bad_arg,item.typeId());
  31 + }
  32 + }
  33 +
17 34 Symbol[] arglist = new Symbol[list.size()];
18   - return new LambdaFunction(list.toArray(arglist), args.remove(0),
19   - v.getEnv());
  35 +
  36 + CronoType[] body;
  37 + List<CronoType> blist = new LinkedList<CronoType>();
  38 + ListIterator<CronoType> it = args.listIterator();
  39 + while (it.hasNext())
  40 + {
  41 + blist.add(it.next());
  42 + }
  43 + body = new CronoType[blist.size()];
  44 + body = blist.toArray(body);
  45 + return new LambdaFunction(list.toArray(arglist), body,
  46 + v.getEnv());
  47 + }
  48 + public String toString() {
  49 + return "\\";
20 50 }
21   - public String toString() {
22   - return "\\";
23   - }
24 51 });
25 52 //DEFINE(new Function(new TypeId[]{Symbol.TYPEID, CronoType.TYPEID},
26 53 //CronoType.TYPEID, 2, EvalType.NONE)
@@ -73,62 +100,64 @@ public String toString() {
73 100 //LET(new Function(new TypeId[]{Cons.TYPEID, CronoType.TYPEID},
74 101 //CronoType.TYPEID, 2, true, EvalType.NONE)
75 102 //{
76   - //private static final String _subst_list_type =
77   - //"LET: substitution list must be :cons, got %s";
78   - //private static final String _subst_not_cons =
79   - //"LET: expected :cons in substitution list, got %s";
80   - //private static final String _subst_not_sym =
81   - //"LET: argument names numst be :symbol, got %s";
  103 + //private static final String _subst_list_type =
  104 + //"LET: substitution list must be :cons, got %s";
  105 + //private static final String _subst_not_cons =
  106 + //"LET: expected :cons in substitution list, got %s";
  107 + //private static final String _subst_not_sym =
  108 + //"LET: argument names numst be :symbol, got %s";
82 109
83   - //public CronoType run(Visitor v, CronoType[] args) {
84   - //if(!(args[0] instanceof Cons)) {
85   - //throw new InterpreterException(_subst_list_type,
86   - //args[0].typeId());
87   - //}
  110 + //public CronoType run(Visitor v, List<CronoType> args) {
  111 + //if(!(args[0] instanceof Cons)) {
  112 + //throw new InterpreterException(_subst_list_type,
  113 + //args[0].typeId());
  114 + //}
88 115
89   - //List<Symbol> symlist = new LinkedList<Symbol>();
90   - //List<CronoType> arglist = new LinkedList<CronoType>();
91   - //for(CronoType ct : ((Cons)args[0])) {
92   - //if(!(ct instanceof Cons)) {
93   - //throw new InterpreterException(_subst_not_cons,
94   - //ct.typeId());
95   - //}
  116 + //List<Symbol> symlist = new LinkedList<Symbol>();
  117 + //List<CronoType> arglist = new LinkedList<CronoType>();
  118 + //for(CronoType ct : ((Cons)args[0])) {
  119 + //if(!(ct instanceof Cons)) {
  120 + //throw new InterpreterException(_subst_not_cons,
  121 + //ct.typeId());
  122 + //}
96 123
97   - //CronoType car = ((Cons)ct).car();
98   - //CronoType cdr = ((Cons)ct).cdr();
99   - //if(!(car instanceof Symbol)) {
100   - //throw new InterpreterException(_subst_not_sym,
101   - //car.typeId());
102   - //}
  124 + //CronoType car = ((Cons)ct).car();
  125 + //CronoType cdr = ((Cons)ct).cdr();
  126 + //if(!(car instanceof Symbol)) {
  127 + //throw new InterpreterException(_subst_not_sym,
  128 + //car.typeId());
  129 + //}
103 130
104   - //cdr = cdr.accept(v);
105   - //symlist.add((Symbol)car);
106   - //arglist.add(cdr);
107   - //}
  131 + //cdr = cdr.accept(v);
  132 + //symlist.add((Symbol)car);
  133 + //arglist.add(cdr);
  134 + //}
108 135
109   - //Symbol[] lsyms = new Symbol[symlist.size()];
110   - //lsyms = symlist.toArray(lsyms);
  136 + //Symbol[] lsyms = new Symbol[symlist.size()];
  137 + //lsyms = symlist.toArray(lsyms);
111 138
112   - //List<CronoType> bodylist = new LinkedList<CronoType>();
113   - //for(int i = 1; i < args.length; ++i) {
114   - //bodylist.add(args[i]);
115   - //}
116   - //CronoType body = Cons.fromList(bodylist);
117   - //CronoType[] largs = new CronoType[arglist.size()];
118   - //largs = arglist.toArray(largs);
  139 + //List<CronoType> bodylist = new LinkedList<CronoType>();
  140 + //for(int i = 1; i < args.length; ++i) {
  141 + //bodylist.add(args[i]);
  142 + //}
  143 + //CronoType[] body = new CronoType[bodylist.size()];
  144 + //body = bodylist.toArray(body);
119 145
120   - //LambdaFunction lambda = new LambdaFunction(lsyms,body,v.getEnv());
121   - //return lambda.run(v, largs); [>< -. - <]
122   - //}
123   - //public String toString() {
124   - //return "let";
125   - //}
  146 + //CronoType[] largs = new CronoType[arglist.size()];
  147 + //largs = arglist.toArray(largs);
  148 +
  149 + //LambdaFunction lambda = new LambdaFunction(lsyms,body,v.getEnv());
  150 + //return lambda.run(v, largs); [>< -. - <]
  151 + //}
  152 + //public String toString() {
  153 + //return "let";
  154 + //}
126 155 //}),
127 156 //IF(new Function(new TypeId[]{CronoType.TYPEID, CronoType.TYPEID,
128 157 //CronoType.TYPEID},
129 158 //CronoType.TYPEID, 3, EvalType.NONE)
130 159 //{
131   - //public CronoType run(Visitor v, CronoType[] args) {
  160 + //public CronoType run(Visitor v, List<CronoType> args) {
132 161 //CronoType check = args[0].accept(v);
133 162 //if(check != Nil.NIL) {
134 163 //return args[1].accept(v);
241 crono/src/crono/Interpreter.java
... ... @@ -1,241 +0,0 @@
1   -package crono;
2   -
3   -import java.util.ArrayList;
4   -import java.util.Arrays;
5   -import java.util.Iterator;
6   -import java.util.LinkedList;
7   -import java.util.List;
8   -import java.util.Stack;
9   -
10   -import crono.type.Atom;
11   -import crono.type.Cons;
12   -import crono.type.CronoType;
13   -import crono.type.Function;
14   -import crono.type.LambdaFunction;
15   -import crono.type.Nil;
16   -import crono.type.Quote;
17   -import crono.type.Symbol;
18   -import crono.type.CronoTypeId;
19   -import crono.type.TypeId;
20   -
21   -public class Interpreter extends Visitor {
22   - private static final String _scope_err = "No object %s in scope";
23   - private static final String _too_many_args =
24   - "Too many arguments to %s: %d/%d recieved";
25   - private static final String _type_scope_err = "No type %s in scope";
26   - private static final String _type_mismatch =
27   - "Function '%s' expected arguments %s; got %s";
28   -
29   - public boolean show_env;
30   - public boolean show_closure;
31   - public boolean dprint_enable;
32   - public boolean dprint_ident;
33   - public boolean dynamic;
34   -
35   - protected int indent_level;
36   - protected Function.EvalType eval;
37   -
38   - public Interpreter() {
39   - show_env = false;
40   - show_closure = false;
41   - dprint_enable = false;
42   - dprint_ident = true;
43   - dynamic = false;
44   -
45   - indent_level = 0;
46   - eval = Function.EvalType.FULL;
47   -
48   - env_stack = new Stack<Environment>();
49   - reset(); /*< Set up initial environment and types */
50   - }
51   - public void reset() {
52   - env_stack.clear();
53   - pushEnv(new Environment());
54   - }
55   -
56   - public CronoType visit(Cons c) {
57   - if(eval == Function.EvalType.NONE) {
58   - return c;
59   - }
60   -
61   - Iterator<CronoType> iter = c.iterator();
62   - if(!(iter.hasNext())) {
63   - return c; /*< C is an empty list (may be Nil or T) */
64   - }
65   -
66   - CronoType value = iter.next().accept(this);
67   - if(value instanceof Function) {
68   - Function fun = ((Function)value);
69   -
70   - Function.EvalType reserve = eval;
71   - /* Set the eval type to the current function's type; this keeps
72   - * type errors in builtins from happening, ex:
73   - * (+ arg1 arg2) under partial evaluation would fail since +
74   - * expects two numbers.
75   - */
76   - eval = fun.eval;
77   - if(eval.level > reserve.level) {
78   - eval = reserve;
79   - }
80   - List<CronoType> args = new ArrayList<CronoType>();
81   - while(iter.hasNext()) {
82   - args.add(iter.next().accept(this));
83   - }
84   - eval = reserve;
85   -
86   - int arglen = args.size();
87   - int nargs = fun.arity;
88   - if(arglen < nargs) {
89   - if(arglen == 0) {
90   - /* Special case -- we don't have to do anything to the
91   - * function to return it properly. */
92   - return fun;
93   - }
94   -
95   - /* Curry it */
96   - if(fun instanceof LambdaFunction) {
97   - LambdaFunction lfun = ((LambdaFunction)fun);
98   - Environment env = getEnv();
99   - if(!dynamic) {
100   - /* Use the lambda's stored environment */
101   - env = lfun.environment;
102   - }
103   - /* We want to preserve the current environment */
104   - env = new Environment(env);
105   -
106   - /* Put known values into the new environment */
107   - for(int i = 0; i < arglen; ++i) {
108   - env.put(lfun.arglist[i], args.get(i));
109   - }
110   - /* Create new argument list and remove remaining args from
111   - * the new environment */
112   - List<Symbol> largs = new ArrayList<Symbol>();
113   - for(int i = arglen; i < lfun.arglist.length; ++i) {
114   - largs.add(lfun.arglist[i]);
115   - env.remove(lfun.arglist[i]);
116   - }
117   -
118   - /* Evaluate the body as much as possible */
119   - reserve = eval;
120   - eval = Function.EvalType.PARTIAL;
121   - pushEnv(env);
122   - CronoType[] lbody = new CronoType[lfun.body.length];
123   - for(int i = 0; i < lfun.body.length; ++i) {
124   - lbody[i] = lfun.body[i].accept(this);
125   - }
126   - popEnv();
127   - eval = reserve;
128   -
129   - /* Return the new, partially evaluated lambda */
130   - Symbol[] arglist = new Symbol[largs.size()];
131   - return new LambdaFunction(largs.toArray(arglist),
132   - lbody, lfun.environment);
133   - }
134   - /* Builtin partial evaluation */
135   -
136   - List<CronoType> body = new LinkedList<CronoType>();
137   - body.add(fun);
138   -
139   - body.addAll(args); /*< Dump args in order into the new cons */
140   - CronoType[] barr = new CronoType[body.size()];
141   - barr = body.toArray(barr);
142   -
143   - /* Add symbols for missing args */
144   - List<Symbol> arglist = new ArrayList<Symbol>();
145   - Symbol sym;
146   - for(int i = arglen, n = 0; i < nargs; ++i, ++n) {
147   - sym = new Symbol(String.format("_i?%d!_", n));
148   - body.add(sym);
149   - arglist.add(sym);
150   - }
151   -
152   - /* Create a new lambda */
153   - Symbol[] narglist = new Symbol[arglist.size()];
154   - return new LambdaFunction(arglist.toArray(narglist), barr,
155   - getEnv());
156   - }
157   - if(arglen > nargs && !fun.variadic) {
158   - throw new RuntimeException(String.format(_too_many_args, fun,
159   - arglen, nargs));
160   - }
161   -
162   - /* Full evaluation */
163   - if(fun instanceof LambdaFunction && dynamic) {
164   - /* We have to trick the lambda function if we want dynamic
165   - * scoping. I hate making so many objects left and right, but
166   - * this is the easiest way to do what I want here. */
167   - LambdaFunction lfun = ((LambdaFunction)fun);
168   - lfun = new LambdaFunction(lfun.arglist, lfun.body, getEnv());
169   -
170   - CronoType[] argarray = new CronoType[args.size()];
171   - return lfun.run(this, args.toArray(argarray));
172   - }
173   - if(eval == Function.EvalType.FULL) {
174   - CronoType[] argarray = new CronoType[args.size()];
175   - argarray = args.toArray(argarray);
176   - TypeId[] types = new TypeId[args.size()];
177   - for(int i = 0; i < types.length; ++i) {
178   - types[i] = argarray[i].typeId();
179   - }
180   - for(int i = 0; i < fun.args.length; ++i) {
181   - if(!(fun.args[i].isType(argarray[i]))) {
182   - String argstr = Arrays.toString(types);
183   - String expected = Arrays.toString(fun.args);
184   - throw new InterpreterException(_type_mismatch, fun,
185   - expected, argstr);
186   - }
187   - }
188   - return ((Function)value).run(this, args.toArray(argarray));
189   - }else {
190   - args.add(0, value);
191   - return Cons.fromList(args);
192   - }
193   - }
194   -
195   - /* The initial value is not a function */
196   - throw new InterpreterException("Invalid Function Application: %s is not a function in %s", value, c);
197   - /*
198   - List<CronoType> list = new LinkedList<CronoType>();
199   - list.add(value);
200   - while(iter.hasNext()) {
201   - list.add(iter.next().accept(this));
202   - }
203   - return Cons.fromList(list);
204   - */
205   - }
206   -
207   - public CronoType visit(Atom a) {
208   - if(eval == Function.EvalType.NONE) {
209   - return a;
210   - }
211   -
212   - CronoType t = a;
213   - if(t instanceof Symbol) {
214   - t = getEnv().get((Symbol)a);
215   - if(t == null) {
216   - if(eval == Function.EvalType.FULL) {
217   - throw new RuntimeException(String.format(_scope_err,
218   - a.toString()));
219   - }
220   - t = a;
221   - }
222   - }
223   - /* Not else-if, so that we perform a double-resolution on a symbol that
224   - * represents a TypeId */
225   - if(t instanceof CronoTypeId) {
226   - CronoType res = t; /*< Save symbol resolution in new CronoType */
227   - t = getEnv().getType((CronoTypeId)t);
228   - if(t == null) {
229   - if(eval == Function.EvalType.FULL) {
230   - throw new InterpreterException(_type_scope_err, a);
231   - }
232   - t = res; /*< Revert to symbol resolution */
233   - }
234   - }
235   - return t;
236   - }
237   -
238   - public CronoType visit(Quote q) {
239   - return q.node;
240   - }
241   -}
390 crono/src/crono/Interpreter.java.noncomp
@@ -21,215 +21,221 @@ import crono.type.TypeId;
21 21 public class Interpreter extends Visitor {
22 22 private static final String _scope_err = "No object %s in scope";
23 23 private static final String _too_many_args =
24   - "Too many arguments to %s: %d/%d recieved";
  24 + "Too many arguments to %s: %d/%d recieved";
25 25 private static final String _type_scope_err = "No type %s in scope";
26 26 private static final String _type_mismatch =
27   - "Function '%s' expected arguments %s; got %s";
28   -
  27 + "Function '%s' expected arguments %s; got %s";
  28 +
29 29 public boolean show_env;
30 30 public boolean show_closure;
31 31 public boolean dprint_enable;
32 32 public boolean dprint_ident;
33 33 public boolean dynamic;
34   -
  34 +
35 35 protected int indent_level;
36 36 protected Function.EvalType eval;
37   -
  37 +
38 38 public Interpreter() {
39   - show_env = false;
40   - show_closure = false;
41   - dprint_enable = false;
42   - dprint_ident = true;
43   - dynamic = false;
44   -
45   - indent_level = 0;
46   - eval = Function.EvalType.FULL;
47   -
48   - env_stack = new Stack<Environment>();
49   - reset(); /*< Set up initial environment and types */
  39 + show_env = false;
  40 + show_closure = false;
  41 + dprint_enable = false;
  42 + dprint_ident = true;
  43 + dynamic = false;
  44 +
  45 + indent_level = 0;
  46 + eval = Function.EvalType.FULL;
  47 +
  48 + env_stack = new Stack<Environment>();
  49 + reset(); /*< Set up initial environment and types */
50 50 }
51   -
52 51 public void reset() {
53   - env_stack.clear();
54   - pushEnv(new Environment());
  52 + env_stack.clear();
  53 + pushEnv(new Environment());
55 54 }
56   -
  55 +
57 56 public CronoType visit(Cons c) {
58   - if(eval == Function.EvalType.NONE) {
59   - return c;
60   - }
61   -
62   - Iterator<CronoType> iter = c.iterator();
63   - if(!(iter.hasNext())) {
64   - return c; /*< C is an empty list (may be Nil or T) */
65   - }
66   -
67   - CronoType value = iter.next().accept(this);
68   - if(value instanceof Function) {
69   - Function fun = ((Function)value);
70   -
71   - Function.EvalType reserve = eval;
72   - /* Set the eval type to the current function's type; this keeps
73   - * type errors in builtins from happening, ex:
74   - * (+ arg1 arg2) under partial evaluation would fail since +
75   - * expects two numbers.
76   - */
77   - eval = fun.eval;
78   - if(eval.level > reserve.level) {
79   - eval = reserve;
80   - }
81   - List<CronoType> args = new ArrayList<CronoType>();
82   - while(iter.hasNext()) {
83   - args.add(iter.next().accept(this));
84   - }
85   - eval = reserve;
86   -
87   - int arglen = args.size();
88   - int nargs = fun.arity;
89   - if(arglen < nargs) {
90   - if(arglen == 0) {
91   - /* Special case -- we don't have to do anything to the
92   - * function to return it properly. */
93   - return fun;
94   - }
95   -
96   - /* Curry it */
97   - if(fun instanceof LambdaFunction) {
98   - LambdaFunction lfun = ((LambdaFunction)fun);
99   - Environment env = getEnv();
100   - if(!dynamic) {
101   - /* Use the lambda's stored environment */
102   - env = lfun.environment;
103   - }
104   - /* We want to preserve the current environment */
105   - env = new Environment(env);
106   -
107   - /* Put known values into the new environment */
108   - for(int i = 0; i < arglen; ++i) {
109   - env.put(lfun.arglist[i], args.get(i));
110   - }
111   - /* Create new argument list and remove remaining args from
112   - * the new environment */
113   - List<Symbol> largs = new ArrayList<Symbol>();
114   - for(int i = arglen; i < lfun.arglist.length; ++i) {
115   - largs.add(lfun.arglist[i]);
116   - env.remove(lfun.arglist[i]);
117   - }
118   -
119   - /* Evaluate the body as much as possible */
120   - reserve = eval;
121   - eval = Function.EvalType.PARTIAL;
122   - pushEnv(env);
123   - CronoType lbody = lfun.body.accept(this);
124   - popEnv();
125   - eval = reserve;
126   -
127   - /* Return the new, partially evaluated lambda */
128   - Symbol[] arglist = new Symbol[largs.size()];
129   - return new LambdaFunction(largs.toArray(arglist),
130   - lbody, lfun.environment);
131   - }
132   - /* Builtin partial evaluation */
133   -
134   - List<CronoType> body = new LinkedList<CronoType>();
135   - body.add(fun);
136   -
137   - body.addAll(args); /*< Dump args in order into the new cons */
138   -
139   - /* Add symbols for missing args */
140   - List<Symbol> arglist = new ArrayList<Symbol>();
141   - Symbol sym;
142   - for(int i = arglen, n = 0; i < nargs; ++i, ++n) {
143   - sym = new Symbol(String.format("_i?%d!_", n));
144   - body.add(sym);
145   - arglist.add(sym);
146   - }
147   -
148   - /* Create a new lambda */
149   - Symbol[] narglist = new Symbol[arglist.size()];
150   - return new LambdaFunction(arglist.toArray(narglist),
151   - Cons.fromList(body), getEnv());
152   - }
153   - if(arglen > nargs && !fun.variadic) {
154   - throw new RuntimeException(String.format(_too_many_args, fun,
155   - arglen, nargs));
156   - }
157   -
158   - /* Full evaluation */
159   - if(fun instanceof LambdaFunction && dynamic) {
160   - /* We have to trick the lambda function if we want dynamic
161   - * scoping. I hate making so many objects left and right, but
162   - * this is the easiest way to do what I want here. */
163   - LambdaFunction lfun = ((LambdaFunction)fun);
164   - lfun = new LambdaFunction(lfun.arglist, lfun.body, getEnv());
165   -
166   - CronoType[] argarray = new CronoType[args.size()];
167   - return lfun.run(this, args.toArray(argarray));
168   - }
169   - if(eval == Function.EvalType.FULL) {
170   - CronoType[] argarray = new CronoType[args.size()];
171   - argarray = args.toArray(argarray);
172   - TypeId[] types = new TypeId[args.size()];
173   - for(int i = 0; i < types.length; ++i) {
174   - types[i] = argarray[i].typeId();
175   - }
176   - for(int i = 0; i < argarray.length; ++i) {
177   - if(!(fun.args[i].isType(argarray[i]))) {
178   - String argstr = Arrays.toString(argarray);
179   - String expected = Arrays.toString(fun.args);
180   - throw new InterpreterException(_type_mismatch, fun,
181   - expected, argstr);
182   - }
183   - }
184   - return ((Function)value).run(this, args.toArray(argarray));
185   - }else {
186   - args.add(0, value);
187   - return Cons.fromList(args);
188   - }
189   - }
190   -
191   - /* The initial value is not a function */
192   - List<CronoType> list = new LinkedList<CronoType>();
193   - list.add(value);
194   - while(iter.hasNext()) {
195   - list.add(iter.next().accept(this));
196   - }
197   - return Cons.fromList(list);
  57 + if(eval == Function.EvalType.NONE) {
  58 + return c;
  59 + }
  60 +
  61 + Iterator<CronoType> iter = c.iterator();
  62 + if(!(iter.hasNext())) {
  63 + return c; /*< C is an empty list (may be Nil or T) */
  64 + }
  65 +
  66 + CronoType value = iter.next().accept(this);
  67 + if(value instanceof Function) {
  68 + Function fun = ((Function)value);
  69 +
  70 + Function.EvalType reserve = eval;
  71 + /* Set the eval type to the current function's type; this keeps
  72 + * type errors in builtins from happening, ex:
  73 + * (+ arg1 arg2) under partial evaluation would fail since +
  74 + * expects two numbers.
  75 + */
  76 + eval = fun.eval;
  77 + if(eval.level > reserve.level) {
  78 + eval = reserve;
  79 + }
  80 + List<CronoType> args = new ArrayList<CronoType>();
  81 + while(iter.hasNext()) {
  82 + args.add(iter.next().accept(this));
  83 + }
  84 + eval = reserve;
  85 +
  86 + int arglen = args.size();
  87 + int nargs = fun.arity;
  88 + if(arglen < nargs) {
  89 + if(arglen == 0) {
  90 + /* Special case -- we don't have to do anything to the
  91 + * function to return it properly. */
  92 + return fun;
  93 + }
  94 +
  95 + /* Curry it */
  96 + if(fun instanceof LambdaFunction) {
  97 + LambdaFunction lfun = ((LambdaFunction)fun);
  98 + Environment env = getEnv();
  99 + if(!dynamic) {
  100 + /* Use the lambda's stored environment */
  101 + env = lfun.environment;
  102 + }
  103 + /* We want to preserve the current environment */
  104 + env = new Environment(env);
  105 +
  106 + /* Put known values into the new environment */
  107 + for(int i = 0; i < arglen; ++i) {
  108 + env.put(lfun.arglist[i], args.get(i));
  109 + }
  110 + /* Create new argument list and remove remaining args from
  111 + * the new environment */
  112 + List<Symbol> largs = new ArrayList<Symbol>();
  113 + for(int i = arglen; i < lfun.arglist.length; ++i) {
  114 + largs.add(lfun.arglist[i]);
  115 + env.remove(lfun.arglist[i]);
  116 + }
  117 +
  118 + /* Evaluate the body as much as possible */
  119 + reserve = eval;
  120 + eval = Function.EvalType.PARTIAL;
  121 + pushEnv(env);
  122 + CronoType[] lbody = new CronoType[lfun.body.length];
  123 + for(int i = 0; i < lfun.body.length; ++i) {
  124 + lbody[i] = lfun.body[i].accept(this);
  125 + }
  126 + popEnv();
  127 + eval = reserve;
  128 +
  129 + /* Return the new, partially evaluated lambda */
  130 + Symbol[] arglist = new Symbol[largs.size()];
  131 + return new LambdaFunction(largs.toArray(arglist),
  132 + lbody, lfun.environment);
  133 + }
  134 + /* Builtin partial evaluation */
  135 +
  136 + List<CronoType> body = new LinkedList<CronoType>();
  137 + body.add(fun);
  138 +
  139 + body.addAll(args); /*< Dump args in order into the new cons */
  140 + CronoType[] barr = new CronoType[body.size()];
  141 + barr = body.toArray(barr);
  142 +
  143 + /* Add symbols for missing args */
  144 + List<Symbol> arglist = new ArrayList<Symbol>();
  145 + Symbol sym;
  146 + for(int i = arglen, n = 0; i < nargs; ++i, ++n) {
  147 + sym = new Symbol(String.format("_i?%d!_", n));
  148 + body.add(sym);
  149 + arglist.add(sym);
  150 + }
  151 +
  152 + /* Create a new lambda */
  153 + Symbol[] narglist = new Symbol[arglist.size()];
  154 + return new LambdaFunction(arglist.toArray(narglist), barr,
  155 + getEnv());
  156 + }
  157 + if(arglen > nargs && !fun.variadic) {
  158 + throw new RuntimeException(String.format(_too_many_args, fun,
  159 + arglen, nargs));
  160 + }
  161 +
  162 + /* Full evaluation */
  163 + if(fun instanceof LambdaFunction && dynamic) {
  164 + /* We have to trick the lambda function if we want dynamic
  165 + * scoping. I hate making so many objects left and right, but
  166 + * this is the easiest way to do what I want here. */
  167 + LambdaFunction lfun = ((LambdaFunction)fun);
  168 + lfun = new LambdaFunction(lfun.arglist, lfun.body, getEnv());
  169 +
  170 + CronoType[] argarray = new CronoType[args.size()];
  171 + return lfun.run(this, args.toArray(argarray));
  172 + }
  173 + if(eval == Function.EvalType.FULL) {
  174 + CronoType[] argarray = new CronoType[args.size()];
  175 + argarray = args.toArray(argarray);
  176 + TypeId[] types = new TypeId[args.size()];
  177 + for(int i = 0; i < types.length; ++i) {
  178 + types[i] = argarray[i].typeId();
  179 + }
  180 + for(int i = 0; i < fun.args.length; ++i) {
  181 + if(!(fun.args[i].isType(argarray[i]))) {
  182 + String argstr = Arrays.toString(types);
  183 + String expected = Arrays.toString(fun.args);
  184 + throw new InterpreterException(_type_mismatch, fun,
  185 + expected, argstr);
  186 + }
  187 + }
  188 + return ((Function)value).run(this, args.toArray(argarray));
  189 + }else {
  190 + args.add(0, value);
  191 + return Cons.fromList(args);
  192 + }
  193 + }
  194 +
  195 + /* The initial value is not a function */
  196 + throw new InterpreterException("Invalid Function Application: %s is not a function in %s", value, c);
  197 + /*
  198 + List<CronoType> list = new LinkedList<CronoType>();
  199 + list.add(value);
  200 + while(iter.hasNext()) {
  201 + list.add(iter.next().accept(this));
  202 + }
  203 + return Cons.fromList(list);
  204 + */
198 205 }
199   -
  206 +
200 207 public CronoType visit(Atom a) {
201   - if(eval == Function.EvalType.NONE) {
202   - return a;
203   - }
204   -
205   - CronoType t = a;
206   - if(t instanceof Symbol) {
207   - t = getEnv().get((Symbol)a);
208   - if(t == null) {
209   - if(eval == Function.EvalType.FULL) {
210   - throw new RuntimeException(String.format(_scope_err,
211   - a.toString()));
212   - }
213   - t = a;
214   - }
215   - }
216   - /* Not else-if, so that we perform a double-resolution on a symbol that
217   - * represents a TypeId */
218   - if(t instanceof CronoTypeId) {
219   - CronoType res = t; /*< Save symbol resolution in new CronoType */
220   - t = getEnv().getType((CronoTypeId)t);
221   - if(t == null) {
222   - if(eval == Function.EvalType.FULL) {
223   - throw new InterpreterException(_type_scope_err, a);
224   - }
225   - t = res; /*< Revert to symbol resolution */
226   - }
227   - }
228   - return t;
  208 + if(eval == Function.EvalType.NONE) {
  209 + return a;
  210 + }
  211 +
  212 + CronoType t = a;
  213 + if(t instanceof Symbol) {
  214 + t = getEnv().get((Symbol)a);
  215 + if(t == null) {
  216 + if(eval == Function.EvalType.FULL) {
  217 + throw new RuntimeException(String.format(_scope_err,
  218 + a.toString()));
  219 + }
  220 + t = a;
  221 + }
  222 + }
  223 + /* Not else-if, so that we perform a double-resolution on a symbol that
  224 + * represents a TypeId */
  225 + if(t instanceof CronoTypeId) {
  226 + CronoType res = t; /*< Save symbol resolution in new CronoType */
  227 + t = getEnv().getType((CronoTypeId)t);
  228 + if(t == null) {
  229 + if(eval == Function.EvalType.FULL) {
  230 + throw new InterpreterException(_type_scope_err, a);
  231 + }
  232 + t = res; /*< Revert to symbol resolution */
  233 + }
  234 + }
  235 + return t;
229 236 }
230   -
  237 +
231 238 public CronoType visit(Quote q) {
232   - return q.node;
  239 + return q.node;
233 240 }
234   - public CronoType defaultVisit(CronoType object) {return object;}
235 241 }
2  crono/src/crono/type/CronoStruct.java
@@ -141,7 +141,7 @@ protected CronoStruct(CronoStruct cs) {
141 141 this.type = cs.type;
142 142 }
143 143
144   - public CronoType run(Visitor v, CronoType[] args) {
  144 + public CronoType run(Visitor v, List<CronoType> args) {
145 145 /* CronoStruct doesn't contain an AST, so we can ignore the visitor */
146 146 Field field;
147 147 switch(args.size()) {
112 crono/src/crono/type/CronoVector.java
@@ -6,87 +6,87 @@
6 6
7 7 public class CronoVector extends CronoArray {
8 8 public static final TypeId TYPEID = new TypeId(":vector",
9   - CronoVector.class,
10   - CronoArray.TYPEID);
  9 + CronoVector.class,
  10 + CronoArray.TYPEID);
11 11 private static final String _rank_dim_mismatch =
12   - "Wrong number of dimensions to Array of rank %d: %d";
  12 + "Wrong number of dimensions to Array of rank %d: %d";
13 13 private static final String _data_size_mismatch =
14   - "Data size (%d) does not match Array size: %d";
  14 + "Data size (%d) does not match Array size: %d";
15 15 private static final String _inv_rank =
16   - "Array Rank must be a positive, non-zero integer";
17   -
  16 + "Array Rank must be a positive, non-zero integer";
  17 +
18 18 protected ArrayList<CronoType> data;
19 19 protected int size;
20 20 protected TypeId type;
21   -
  21 +
22 22 public CronoVector(int size) {
23   - this(size, CronoType.TYPEID);
  23 + this(size, CronoType.TYPEID);
24 24 }
25   -
  25 +
26 26 public CronoVector(int size, TypeId accept) {
27   - super(accept);
28   -
29   - this.type = new NestedTypeId(":vector", CronoVector.class,
30   - new TypeId[]{accept}, TYPEID);
31   -
32   - this.size = size;
33   - this.data = new ArrayList<CronoType>(size);
34   - for(int i = 0; i < size; ++i) {
35   - this.data.add(i, Nil.NIL);
36   - }
  27 + super(accept);
  28 +
  29 + this.type = new NestedTypeId(":vector", CronoVector.class,
  30 + new TypeId[]{accept}, TYPEID);
  31 +
  32 + this.size = size;
  33 + this.data = new ArrayList<CronoType>(size);
  34 + for(int i = 0; i < size; ++i) {
  35 + this.data.add(i, Nil.NIL);
  36 + }
37 37 }
38 38 public CronoVector(CronoType[] data) {
39   - this(data, CronoType.TYPEID);
  39 + this(data, CronoType.TYPEID);
40 40 }
41 41 public CronoVector(CronoType[] data, TypeId accept) {
42   - super(accept);
43   - this.type = new NestedTypeId(":vector", CronoVector.class,
44   - new TypeId[]{accept}, TYPEID);
45   - this.data = new ArrayList<CronoType>();
46   - for(int i = 0; i < data.length; ++i) {
47   - this.data.add(data[i]);
48   - }
49   - this.size = data.length;
  42 + super(accept);
  43 + this.type = new NestedTypeId(":vector", CronoVector.class,
  44 + new TypeId[]{accept}, TYPEID);
  45 + this.data = new ArrayList<CronoType>();
  46 + for(int i = 0; i < data.length; ++i) {
  47 + this.data.add(data[i]);
  48 + }
  49 + this.size = data.length;
50 50 }
51   -
  51 +
52 52 public int size() {
53   - return size;
  53 + return size;
54 54 }
55   -
  55 +
56 56 public CronoType get(int n) {
57   - if(n >= size || n < 0) {
58   - throw new InterpreterException("Array index out of bounds");
59   - }
60   - return data.get(n);
  57 + if(n >= size || n < 0) {
  58 + throw new InterpreterException("Array index out of bounds");
  59 + }
  60 + return data.get(n);
61 61 }
62 62 public CronoType put(int n, CronoType item) {
63   - if(n >= size || n < 0) {
64   - throw new InterpreterException("Array index out of bounds");
65   - }
66   - data.set(n, item);
67   - return item;
  63 + if(n >= size || n < 0) {
  64 + throw new InterpreterException("Array index out of bounds");
  65 + }
  66 + data.set(n, item);
  67 + return item;
68 68 }
69 69 public CronoType append(CronoType item) {
70   - data.add(item);
71   - size++;
72   - return item;
  70 + data.add(item);
  71 + size++;
  72 + return item;
73 73 }
74 74 public CronoType concat(CronoArray array) {
75   - return Nil.NIL;
  75 + return Nil.NIL;
76 76 }
77   -
  77 +
78 78 public TypeId typeId() {
79   - return type;
  79 + return type;
80 80 }
81 81 public String toString() {
82   - StringBuilder builder = new StringBuilder();
83   - builder.append("[");
84   - for(int i = 0; i < size - 1; ++i) {
85   - builder.append(data.get(i).repr());
86   - builder.append(", ");
87   - }
88   - builder.append(data.get(size - 1).repr());
89   - builder.append("]");
90   - return builder.toString();
  82 + StringBuilder builder = new StringBuilder();
  83 + builder.append("[");
  84 + for(int i = 0; i < size - 1; ++i) {
  85 + builder.append(data.get(i).repr());
  86 + builder.append(", ");
  87 + }
  88 + builder.append(data.get(size - 1).repr());
  89 + builder.append("]");
  90 + return builder.toString();
91 91 }
92   -}
  92 +}

0 comments on commit c47bdef

Please sign in to comment.
Something went wrong with that request. Please try again.