root/trunk/ProjectFortress/src/com/sun/fortress/interpreter/evaluator/BuildLetEnvironments.java

Revision 4185, 5.1 KB (checked in by sukyoungryu, 2 months ago)

[ast] Replaced List<Expr> with Block for the body of LetExpr?.

Line 
1/*******************************************************************************
2 Copyright 2009 Sun Microsystems, Inc.,
3 4150 Network Circle, Santa Clara, California 95054, U.S.A.
4 All rights reserved.
5
6 U.S. Government Rights - Commercial software.
7 Government users are subject to the Sun Microsystems, Inc. standard
8 license agreement and applicable provisions of the FAR and its supplements.
9
10 Use is subject to license terms.
11
12 This distribution may include materials developed by third parties.
13
14 Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15 trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18package com.sun.fortress.interpreter.evaluator;
19
20import com.sun.fortress.exceptions.FortressException;
21import static com.sun.fortress.exceptions.ProgramError.error;
22import static com.sun.fortress.exceptions.ProgramError.errorMsg;
23import com.sun.fortress.interpreter.env.IndirectionCell;
24import com.sun.fortress.interpreter.evaluator.types.FType;
25import com.sun.fortress.interpreter.evaluator.types.FTypeTop;
26import com.sun.fortress.interpreter.evaluator.values.FTuple;
27import com.sun.fortress.interpreter.evaluator.values.FValue;
28import com.sun.fortress.interpreter.evaluator.values.FunctionClosure;
29import com.sun.fortress.interpreter.evaluator.values.Parameter;
30import com.sun.fortress.nodes.*;
31import com.sun.fortress.nodes_util.NodeUtil;
32import edu.rice.cs.plt.tuple.Option;
33
34import java.util.Iterator;
35import java.util.List;
36
37public 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    /* (non-Javadoc)
48     * @see com.sun.fortress.interpreter.nodes.NodeVisitor#forLetFn(com.sun.fortress.interpreter.nodes.LetFn)
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            // TODO Local functions cannot be Enclosing, can they?
71            // Local functions cannot be any operator including Enclosing.
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        //FValue res = Evaluator.evVoid;
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}
Note: See TracBrowser for help on using the browser.