Ejemplo n.º 1
0
        public virtual Expression.EvalResult Run(MakeState s, List <Expression.EvalResult> passed_args)
        {
            MakeState new_s = s.Clone();

            new_s.ClearLocalDefines();

            for (int i = 0; i < args.Count; i++)
            {
                if (args[i].argtype == Expression.EvalResult.ResultType.Function)
                {
                    FunctionStatement fs = new FunctionStatement();
                    var ofs = passed_args[i].funcval;
                    fs.name = args[i].name;
                    fs.args = ofs.args;
                    fs.code = ofs.code;

                    new_s.funcs[fs.Mangle()] = fs;
                }
                else
                {
                    new_s.SetLocalDefine(args[i].name, passed_args[i]);
                }
            }

            code.Execute(new_s);
            if (new_s.returns != null)
            {
                return(new_s.returns);
            }

            return(new Expression.EvalResult());
        }
Ejemplo n.º 2
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            Expression.EvalResult e = val.Evaluate(s);

            if (tok_name is LabelExpression)
            {
                var tn = ((LabelExpression)tok_name).val;
                s.SetLocalDefine(tn, e);
                if (export)
                {
                    ExportDef(tn, s);
                }
            }
            else if (tok_name is LabelMemberExpression)
            {
                // left-most member must be a define
                var lme = tok_name as LabelMemberExpression;
                if (!(lme.label is LabelExpression))
                {
                    throw new Exception("unable to assign to " + tok_name.ToString());
                }

                var o = s.GetDefine(((LabelExpression)lme.label).val);

                // Now iterate through the various members, looking for
                //  what to set
                Expression cur_member_lval = lme.member;
                while (true)
                {
                    if (cur_member_lval is LabelExpression)
                    {
                        // we've reached the end of the chain
                        break;
                    }
                    else if (cur_member_lval is LabelMemberExpression)
                    {
                        var new_lme = cur_member_lval as LabelMemberExpression;

                        if (!(new_lme.label is LabelExpression))
                        {
                            throw new Exception("unable to assign to " + tok_name.ToString());
                        }
                        lme             = new_lme;
                        cur_member_lval = lme.member;

                        if (o.Type != Expression.EvalResult.ResultType.Object)
                        {
                            throw new Exception();
                        }

                        o = o.objval[((LabelExpression)lme.label).val];
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }

                string member_name = ((LabelExpression)cur_member_lval).val;
                o.objval[member_name] = e;
            }
            else if (tok_name is LabelIndexedExpression)
            {
                var chain = get_chain(tok_name);

                // left-most member must be a define
                var lme = chain[0];
                if (!(lme is LabelExpression))
                {
                    throw new Exception("unable to assign to " + tok_name.ToString());
                }

                var o = s.GetDefine(((LabelExpression)lme).val);

                // Now iterate through the various members, looking for
                //  what to set
                o = follow_chain(o, chain, 1, chain.Count - 1, s);

                if (o.Type != Expression.EvalResult.ResultType.Array)
                {
                    throw new Exception("unable to assign to " + tok_name.ToString());
                }

                var idx = ((LabelIndexedExpression)tok_name).index.Evaluate(s).AsInt;

                // increase the array size as appropriate
                if (idx >= o.arrval.Count)
                {
                    o.arrval.Capacity = (int)idx * 3 / 2;
                    while (idx >= o.arrval.Count)
                    {
                        o.arrval.Add(new Expression.EvalResult {
                            Type = Expression.EvalResult.ResultType.Null
                        });
                    }
                }
                o.arrval[(int)idx] = e;
            }
            else
            {
                throw new NotImplementedException();
            }
            return(new Expression.EvalResult(0));
        }