Ejemplo n.º 1
0
        public override EvalResult Evaluate(MakeState s)
        {
            var mangled_name = Mangle(s, out var args_to_pass);

            if (s.funcs.ContainsKey(mangled_name))
            {
                // add these here because error messages for a function
                //  call should relate to the location the function is
                //  called rather than defined
                s.fcall = this;
                return(s.funcs[mangled_name].Run(s, args_to_pass));
            }

            /*List<string> mangle_all = MangleAll(s);
             * foreach (var mangled_name in mangle_all)
             * {
             *  if (s.funcs.ContainsKey(mangled_name))
             *  {
             *      List<EvalResult> args_to_pass = new List<EvalResult>();
             *      foreach (Expression arg in args)
             *          args_to_pass.Add(arg.Evaluate(s));
             *      return s.funcs[mangled_name].Run(s, args_to_pass);
             *  }
             * }*/

            throw new Statement.SyntaxException("unable to find function " + Mangle(s, out var resultTypes), this);
        }
Ejemplo n.º 2
0
        public override EvalResult Evaluate(MakeState s)
        {
            FunctionStatement fs = new FunctionStatement();

            fs.name = name;
            fs.args = new List <FunctionStatement.FunctionArg>();
            foreach (var arg in args)
            {
                fs.args.Add(new FunctionStatement.FunctionArg {
                    argtype = arg
                });
            }
            var mangled = fs.Mangle();

            if (s.funcs.ContainsKey(mangled))
            {
                fs.args = s.funcs[mangled].args;
                fs.code = s.funcs[mangled].code;
                return(new EvalResult(fs));
            }
            else
            {
                return(new EvalResult());
            }
        }
Ejemplo n.º 3
0
 public override EvalResult Evaluate(MakeState s)
 {
     if (s.IsDefined(val))
     {
         return(s.GetDefine(val));
     }
     else if (s.funcs.ContainsKey(val))
     {
         return new EvalResult
                {
                    Type    = EvalResult.ResultType.Function,
                    funcval = new FunctionStatement
                    {
                        name = val
                    }
                }
     }
     ;
     else
     {
         return new EvalResult()
                {
                    Type = EvalResult.ResultType.Undefined, strval = val, orig_expr = this
                }
     };
 }
Ejemplo n.º 4
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            // run initializer
            Expression.EvalResult ret = init.Execute(s);
            if (ret.AsInt != 0)
            {
                return(ret);
            }

            while (true)
            {
                // check condition
                if (test.Evaluate(s).AsInt == 0)
                {
                    break;
                }

                // exec code
                ret = code.Execute(s);
                if (ret.AsInt != 0)
                {
                    return(ret);
                }
                if (s.returns != null)
                {
                    return(new Expression.EvalResult(0));
                }

                // run incrementer
                incr.Execute(s);
            }
            return(new Expression.EvalResult(0));
        }
Ejemplo n.º 5
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            if (name != null)
            {
                var arg_types = new List <Expression.EvalResult.ResultType>();
                foreach (var arg in args)
                {
                    arg_types.Add(arg.argtype);
                }
                var mangled_names = FuncCall.MangleAll(name, arg_types, s);

                foreach (var mangledname in mangled_names)
                {
                    s.funcs[mangledname] = this;

                    if (export)
                    {
                        MakeState cur_s = s.parent;
                        while (cur_s != null)
                        {
                            cur_s.funcs[mangledname] = this;
                            cur_s = cur_s.parent;
                        }
                    }
                }
            }

            return(new Expression.EvalResult(0));
        }
Ejemplo n.º 6
0
        public virtual MakeState Clone()
        {
            MakeState other = new MakeState();

            CloneTo(other);

            return(other);
        }
Ejemplo n.º 7
0
        public override EvalResult Evaluate(MakeState s)
        {
            FunctionStatement fs = new FunctionStatement();

            fs.name = null;
            fs.args = args;
            fs.code = code;
            return(new EvalResult(fs));
        }
Ejemplo n.º 8
0
 public override Expression.EvalResult Execute(MakeState s)
 {
     Expression.EvalResult e = new Expression.EvalResult(val);
     s.SetDefine(tok_name, e, assignop);
     if (export)
     {
         ExportDef(tok_name, s);
     }
     return(new Expression.EvalResult(0));
 }
Ejemplo n.º 9
0
        public override EvalResult Evaluate(MakeState s)
        {
            Dictionary <string, EvalResult> ret = new Dictionary <string, EvalResult>();

            foreach (ObjDef o in val)
            {
                ret[o.name] = o.val.Evaluate(s);
            }
            return(new EvalResult(ret));
        }
Ejemplo n.º 10
0
        public override EvalResult Evaluate(MakeState s)
        {
            List <EvalResult> ret = new List <EvalResult>();

            foreach (Expression e in val)
            {
                ret.Add(e.Evaluate(s));
            }
            return(new EvalResult(ret));
        }
Ejemplo n.º 11
0
        internal static void ExportDef(string tag, MakeState s)
        {
            Expression.EvalResult e     = s.GetDefine(tag);
            MakeState             cur_s = s.parent;

            while (cur_s != null)
            {
                cur_s.SetDefine(tag, e);
                cur_s = cur_s.parent;
            }
        }
Ejemplo n.º 12
0
        public string Mangle(MakeState s, out List <Expression.EvalResult> ers)
        {
            ers = new List <EvalResult>();
            var ert = new List <EvalResult.ResultType>();

            foreach (var arg in args)
            {
                var er = arg.Evaluate(s);
                ers.Add(er);
                ert.Add(er.Type);
            }
            return(Mangle(target, ert));
        }
Ejemplo n.º 13
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            var f = include_file.Evaluate(s).strval;

            var saved_this = s.GetDefine("THIS");

            MakeState new_s = s.Clone();
            var       ret   = TymakeLib.ExecuteFile(f, new_s);

            s.SetDefine("THIS", saved_this, true);

            return(ret);
        }
Ejemplo n.º 14
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            LabelExpression le = new LabelExpression {
                val = val
            };

            Expression.EvalResult e = le.Evaluate(s);
            s.SetDefine(tok_name, e, assignop);
            if (export)
            {
                ExportDef(tok_name, s);
            }
            return(new Expression.EvalResult(0));
        }
Ejemplo n.º 15
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            Expression.EvalResult e = enumeration.Evaluate(s);

            switch (e.Type)
            {
            case Expression.EvalResult.ResultType.Array:
                foreach (Expression.EvalResult i in e.arrval)
                {
                    //MakeState cur_s = s.Clone();
                    var cur_s = s;
                    cur_s.SetLocalDefine(val, i);
                    Expression.EvalResult ret = code.Execute(cur_s);
                    if (ret.AsInt != 0)
                    {
                        return(ret);
                    }
                    if (cur_s.returns != null)
                    {
                        s.returns = cur_s.returns;
                        return(new Expression.EvalResult(0));
                    }
                }
                return(new Expression.EvalResult(0));

            case Expression.EvalResult.ResultType.Object:
                // take a snapsnot now so we can still do updating from script code
                List <string> objssk = new List <string>(e.objval.Keys);
                foreach (var k in objssk)
                {
                    var cur_s = s;
                    cur_s.SetLocalDefine(val, new Expression.EvalResult(k));
                    Expression.EvalResult ret = code.Execute(cur_s);
                    if (ret.AsInt != 0)
                    {
                        return(ret);
                    }
                    if (cur_s.returns != null)
                    {
                        s.returns = cur_s.returns;
                        return(new Expression.EvalResult(0));
                    }
                }
                return(new Expression.EvalResult(0));

            default:
                throw new ParseException("foreach: does not evaluate to array or object", sline, scol);
            }
        }
Ejemplo n.º 16
0
        public override EvalResult Evaluate(MakeState s)
        {
            /* First, parse variables in the shell command */
            StringBuilder sb = new StringBuilder();
            int           i  = 0;

            while (i < val.Length)
            {
                if (val[i] == '\\' && i < (val.Length - 1))
                {
                    sb.Append(val[i++]);
                    sb.Append(val[i++]);
                }
                else if (val[i] == '$')
                {
                    /* Now try and match the largest possible variable we can */
                    int j = i + 1;

                    string match = null;

                    while (j < (val.Length) && val[j] != '$')
                    {
                        string test = val.Substring(i + 1, j - i);

                        if (s.IsDefined(test))
                        {
                            match = test;
                        }
                        j++;
                    }

                    if (match != null)
                    {
                        sb.Append(s.GetDefine(match).strval);
                        i++;
                        i += match.Length;
                    }
                    else
                    {
                        sb.Append(val[i++]);
                    }
                }
                else
                {
                    sb.Append(val[i++]);
                }
            }
            return(new EvalResult(sb.ToString()));
        }
Ejemplo n.º 17
0
 public void Merge(MakeState other)
 {
     foreach (KeyValuePair <string, Expression.EvalResult> kvp in other.defs)
     {
         this.defs[kvp.Key] = kvp.Value;
     }
     foreach (KeyValuePair <string, Expression.EvalResult> kvp in other.local_defs)
     {
         this.local_defs[kvp.Key] = kvp.Value;
     }
     foreach (KeyValuePair <string, FunctionStatement> kvp in other.funcs)
     {
         this.funcs[kvp.Key] = kvp.Value;
     }
 }
Ejemplo n.º 18
0
 public void SetDefine(string tag, Expression.EvalResult e, bool export)
 {
     if (export == false)
     {
         SetLocalDefine(tag, e);
     }
     else
     {
         MakeState s = this;
         while (s != null)
         {
             s.SetDefine(tag, e);
             s = s.parent;
         }
     }
 }
Ejemplo n.º 19
0
 public override Expression.EvalResult Execute(MakeState s)
 {
     if (test.Evaluate(s).AsInt == 0)
     {
         if (else_block != null)
         {
             return(else_block.Execute(s));
         }
     }
     else
     {
         if (if_block != null)
         {
             return(if_block.Execute(s));
         }
     }
     return(new Expression.EvalResult(0));
 }
Ejemplo n.º 20
0
        public virtual Expression.EvalResult Run(MakeState s, List <Expression.EvalResult> passed_args)
        {
            MakeState new_s = s.Clone();

            new_s.PromoteLocalDefines();
            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;


                    var arg_types = new List <Expression.EvalResult.ResultType>();
                    foreach (var arg in fs.args)
                    {
                        arg_types.Add(arg.argtype);
                    }
                    var mangledname = FuncCall.Mangle(fs.name, arg_types);

                    /* var mangled_names = FuncCall.MangleAll(fs.name, arg_types, s);
                     *
                     * foreach(var mangledname in mangled_names) */
                    new_s.funcs[mangledname] = 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.º 21
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            if (s.IsDefined(v) == false)
            {
                throw new Exception("export: variable " + v + " is not defined in this scope");
            }

            Expression.EvalResult e = s.GetDefine(v);
            s.SetDefine(v, e);
            MakeState cur_s = s.parent;

            while (cur_s != null)
            {
                cur_s.SetDefine(v, e);
                cur_s = cur_s.parent;
            }

            return(new Expression.EvalResult(0));
        }
Ejemplo n.º 22
0
 public override Expression.EvalResult Execute(MakeState s)
 {
     if (list != null)
     {
         foreach (Statement st in list)
         {
             Expression.EvalResult er = st.Execute(s);
             if (!(st is ExpressionStatement) && er.AsInt != 0)
             {
                 return(er);
             }
             if (s.returns != null)
             {
                 return(s.returns);
             }
         }
     }
     return(new Expression.EvalResult(0));
 }
Ejemplo n.º 23
0
        private Expression.EvalResult follow_chain(Expression.EvalResult o,
                                                   List <Expression> chain, int cur_idx, int max_idx,
                                                   MakeState s)
        {
            var next_member = chain[cur_idx];

            Expression.EvalResult next_result = null;

            if (next_member is LabelIndexedExpression)
            {
                var lie = next_member as LabelIndexedExpression;
                if (o.Type != Expression.EvalResult.ResultType.Array)
                {
                    throw new Exception();
                }
                next_result = o.arrval[(int)lie.index.Evaluate(s).AsInt];
            }
            else if (next_member is LabelMemberExpression)
            {
                var lme = next_member as LabelMemberExpression;
                if (o.Type != Expression.EvalResult.ResultType.Object)
                {
                    throw new Exception();
                }
                next_result = o.objval[((LabelExpression)lme.member).val];
            }
            else
            {
                throw new NotImplementedException();
            }

            cur_idx++;
            if (cur_idx == max_idx)
            {
                return(next_result);
            }

            return(follow_chain(next_result, chain, cur_idx, max_idx, s));
        }
Ejemplo n.º 24
0
        public override EvalResult Evaluate(MakeState s)
        {
            EvalResult elabel = label.Evaluate(s);
            EvalResult eindex = index.Evaluate(s);

            switch (elabel.Type)
            {
            case EvalResult.ResultType.String:
                return(new EvalResult(new string(elabel.strval[(int)eindex.AsInt], 1)));

            case EvalResult.ResultType.Array:
            {
                var idx = eindex.AsInt;
                if (idx < 0 || idx >= elabel.arrval.Count)
                {
                    return new EvalResult {
                               Type = EvalResult.ResultType.Null
                    }
                }
                ;
                else
                {
                    return(elabel.arrval[(int)idx]);
                }
            }

            case EvalResult.ResultType.Object:
                return(elabel.objval[eindex.strval]);

            case EvalResult.ResultType.Undefined:
                throw new Statement.SyntaxException("attempt to access array index of undefined object " + elabel.strval, this);

            default:
                throw new Statement.SyntaxException("indexing cannot be applied to object of type: " + elabel.Type.ToString(), this);
            }
        }
Ejemplo n.º 25
0
        protected void CloneTo(MakeState other)
        {
            foreach (KeyValuePair <string, Expression.EvalResult> kvp in defs)
            {
                other.defs[kvp.Key] = kvp.Value;
            }

            foreach (KeyValuePair <string, Expression.EvalResult> kvp in local_defs)
            {
                other.local_defs[kvp.Key] = kvp.Value;
            }

            foreach (KeyValuePair <string, FunctionStatement> kvp in funcs)
            {
                other.funcs[kvp.Key] = kvp.Value;
            }

            other.parent       = this;
            other.search_paths = new List <string>(search_paths);

            other.stdin  = stdin;
            other.stdout = stdout;
            other.stderr = stderr;
        }
Ejemplo n.º 26
0
 public abstract Expression.EvalResult Execute(MakeState s);
Ejemplo n.º 27
0
 public override Expression.EvalResult Execute(MakeState s)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 28
0
 public override Expression.EvalResult Execute(MakeState s)
 {
     return(expr.Evaluate(s));
 }
Ejemplo n.º 29
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            // if this is a conditional assign, we don't need to evaluate the RHS
            //  if the LHS is already assigned
            if (assignop == Tokens.ASSIGNIF & tok_name is LabelExpression)
            {
                var tn = ((LabelExpression)tok_name).val;
                if (s.IsDefined(tn))
                {
                    return(new Expression.EvalResult(0));
                }
            }

            Expression.EvalResult e = val.Evaluate(s);

            if (tok_name is LabelExpression)
            {
                var tn = ((LabelExpression)tok_name).val;
                s.SetDefine(tn, e, assignop);
                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));
        }
Ejemplo n.º 30
0
 public override Expression.EvalResult Execute(MakeState s)
 {
     s.returns = v.Evaluate(s);
     return(new Expression.EvalResult(0));
 }