| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.interpreter.evaluator; |
|---|
| 19 | |
|---|
| 20 | import com.sun.fortress.exceptions.FortressException; |
|---|
| 21 | import static com.sun.fortress.exceptions.ProgramError.error; |
|---|
| 22 | import static com.sun.fortress.exceptions.ProgramError.errorMsg; |
|---|
| 23 | import com.sun.fortress.interpreter.env.IndirectionCell; |
|---|
| 24 | import com.sun.fortress.interpreter.evaluator.types.FType; |
|---|
| 25 | import com.sun.fortress.interpreter.evaluator.types.FTypeTop; |
|---|
| 26 | import com.sun.fortress.interpreter.evaluator.values.FTuple; |
|---|
| 27 | import com.sun.fortress.interpreter.evaluator.values.FValue; |
|---|
| 28 | import com.sun.fortress.interpreter.evaluator.values.FunctionClosure; |
|---|
| 29 | import com.sun.fortress.interpreter.evaluator.values.Parameter; |
|---|
| 30 | import com.sun.fortress.nodes.*; |
|---|
| 31 | import com.sun.fortress.nodes_util.NodeUtil; |
|---|
| 32 | import edu.rice.cs.plt.tuple.Option; |
|---|
| 33 | |
|---|
| 34 | import java.util.Iterator; |
|---|
| 35 | import java.util.List; |
|---|
| 36 | |
|---|
| 37 | public class BuildLetEnvironments extends NodeAbstractVisitor<FValue> { |
|---|
| 38 | |
|---|
| 39 | boolean firstPass = true; |
|---|
| 40 | |
|---|
| 41 | Environment containing; |
|---|
| 42 | |
|---|
| 43 | public BuildLetEnvironments(Environment within) { |
|---|
| 44 | this.containing = within; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | @Override |
|---|
| 51 | public FValue forLetFn(LetFn x) { |
|---|
| 52 | List<FnDecl> fns = x.getFns(); |
|---|
| 53 | Block body = x.getBody(); |
|---|
| 54 | |
|---|
| 55 | for (int i = 0; i < fns.size(); i++) { |
|---|
| 56 | FnDecl fn = fns.get(i); |
|---|
| 57 | |
|---|
| 58 | IdOrOpOrAnonymousName name = NodeUtil.getName(fn); |
|---|
| 59 | String fname = NodeUtil.nameString(name); |
|---|
| 60 | List<Param> params = NodeUtil.getParams(fn); |
|---|
| 61 | Option<Type> retType = NodeUtil.getReturnType(fn); |
|---|
| 62 | |
|---|
| 63 | FunctionClosure cl = new FunctionClosure(containing, fn); |
|---|
| 64 | try { |
|---|
| 65 | containing.putValue(fname, cl); |
|---|
| 66 | } |
|---|
| 67 | catch (FortressException pe) { |
|---|
| 68 | throw pe.setContext(x, containing); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | FType ft = EvalType.getFTypeFromOption(retType, containing, FTypeTop.ONLY); |
|---|
| 73 | List<Parameter> fparams = EvalType.paramsToParameters(containing, params); |
|---|
| 74 | cl.setParamsAndReturnType(fparams, ft); |
|---|
| 75 | } |
|---|
| 76 | return (new Evaluator(containing)).eval(body); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | public FValue forLocalVarDecl(LocalVarDecl x) { |
|---|
| 80 | List<LValue> lhs = x.getLhs(); |
|---|
| 81 | Option<Expr> rhs = x.getRhs(); |
|---|
| 82 | Block body = x.getBody(); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | Evaluator new_eval = new Evaluator(containing); |
|---|
| 86 | if (rhs.isSome()) { |
|---|
| 87 | if (lhs.size() == 1) { |
|---|
| 88 | FValue val = rhs.unwrap().accept(new_eval); |
|---|
| 89 | LHSEvaluator lhs_eval = new LHSEvaluator(new_eval, val); |
|---|
| 90 | lhs.get(0).accept(lhs_eval); |
|---|
| 91 | } else { |
|---|
| 92 | FValue val = rhs.unwrap().accept(new_eval); |
|---|
| 93 | |
|---|
| 94 | if (!(val instanceof FTuple)) { |
|---|
| 95 | error(x, containing, errorMsg("RHS does not yield a tuple")); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | FTuple rTuple = (FTuple) val; |
|---|
| 99 | Iterator<LValue> i = lhs.iterator(); |
|---|
| 100 | Iterator<FValue> j = rTuple.getVals().iterator(); |
|---|
| 101 | while (i.hasNext() && j.hasNext()) { |
|---|
| 102 | LValue lval = i.next(); |
|---|
| 103 | FValue rval = j.next(); |
|---|
| 104 | |
|---|
| 105 | LHSEvaluator lhs_eval = new LHSEvaluator(new_eval, rval); |
|---|
| 106 | lval.accept(lhs_eval); |
|---|
| 107 | } |
|---|
| 108 | if (i.hasNext()) { |
|---|
| 109 | LValue lval = i.next(); |
|---|
| 110 | error(lval, containing, errorMsg("Extra lvalue")); |
|---|
| 111 | } else if (j.hasNext()) { |
|---|
| 112 | FValue rval = j.next(); |
|---|
| 113 | error(rhs.unwrap(), containing, errorMsg("Extra rvalue")); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | } else { |
|---|
| 117 | EvalType eval_type = new EvalType(containing); |
|---|
| 118 | for (LValue lvb : lhs) { |
|---|
| 119 | if (lvb.isMutable() && lvb.getIdType().isSome()) { |
|---|
| 120 | FValue fv = lvb.accept(new_eval); |
|---|
| 121 | FType fvt = lvb.getIdType().unwrap().accept(eval_type); |
|---|
| 122 | containing.putVariable(fv.getString(), fvt); |
|---|
| 123 | } else { |
|---|
| 124 | containing.putValue(lvb.accept(new_eval), new IndirectionCell()); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | } |
|---|
| 129 | return new_eval.eval(body); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public FValue doLets(LetExpr exp) { |
|---|
| 133 | return exp.accept(this); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | } |
|---|