Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        public List <byte> ToByteArray(Expression.EvalResult v)
        {
            List <byte> ret = new List <byte>();

            ToByteArray(v, ret);
            return(ret);
        }
Exemplo n.º 3
0
        public override Expression.EvalResult Execute(MakeState s)
        {
            Expression.EvalResult e = enumeration.Evaluate(s);
            if (e.Type != Expression.EvalResult.ResultType.Array)
            {
                throw new Exception("does not evaluate to 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));
        }
Exemplo n.º 4
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));
 }
Exemplo n.º 5
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;
            }
        }
Exemplo n.º 6
0
        void Print(Expression.EvalResult e, bool toplevel)
        {
            switch (e.Type)
            {
            case Expression.EvalResult.ResultType.Int:
                Program.sw.Write(e.intval);
                break;

            case Expression.EvalResult.ResultType.String:
                if (!toplevel)
                {
                    Program.sw.Write("\"");
                }
                Program.sw.Write(e.strval);
                if (!toplevel)
                {
                    Program.sw.Write("\"");
                }
                break;

            case Expression.EvalResult.ResultType.Array:
                Program.sw.Write("[ ");
                for (int i = 0; i < e.arrval.Count; i++)
                {
                    if (i != 0)
                    {
                        Program.sw.Write(", ");
                    }
                    Print(e.arrval[i], false);
                }
                Program.sw.Write(" ]");
                break;

            case Expression.EvalResult.ResultType.Object:
                Program.sw.Write("[ ");
                int j = 0;
                foreach (KeyValuePair <string, Expression.EvalResult> kvp in e.objval)
                {
                    if (j != 0)
                    {
                        Program.sw.Write(", ");
                    }
                    Program.sw.Write(kvp.Key);
                    Program.sw.Write(": ");
                    Print(kvp.Value, false);
                    j++;
                }
                Program.sw.Write(" ]");
                break;
            }
        }
Exemplo n.º 7
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));
        }
Exemplo n.º 8
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;
         }
     }
 }
Exemplo n.º 9
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(new Expression.EvalResult(0));
             }
         }
     }
     return(new Expression.EvalResult(0));
 }
Exemplo n.º 10
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));
        }
Exemplo n.º 11
0
        public void ToByteArray(Expression.EvalResult v, List <byte> ret)
        {
            switch (v.Type)
            {
            case Expression.EvalResult.ResultType.Array:
                foreach (var a in v.arrval)
                {
                    ToByteArray(a, ret);
                }
                break;

            case Expression.EvalResult.ResultType.Int:
                CompressInt((int)v.intval, ret);

                /*ret.Add((byte)(v.intval & 0xff));
                 * ret.Add((byte)((v.intval >> 8) & 0xff));
                 * ret.Add((byte)((v.intval >> 16) & 0xff));
                 * ret.Add((byte)((v.intval >> 24) & 0xff));*/
                break;

            case Expression.EvalResult.ResultType.Object:
                var vlist = new List <string>();
                foreach (var kvp in v.objval)
                {
                    vlist.Add(kvp.Key);
                }
                vlist.Sort();
                foreach (var k in vlist)
                {
                    ToByteArray(v.objval[k]);
                }
                break;

            case Expression.EvalResult.ResultType.String:
                ret.AddRange(Encoding.UTF8.GetBytes(v.strval));
                break;

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 12
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));
        }
Exemplo n.º 13
0
        public void SetDefine(string tag, Expression.EvalResult e, Tokens assignop)
        {
            switch (assignop)
            {
            case Tokens.ASSIGN:
                local_defs[tag] = e;
                break;

            case Tokens.ASSIGNIF:
                if (!defs.ContainsKey(tag) && !local_defs.ContainsKey(tag))
                {
                    local_defs[tag] = e;
                }
                break;

            case Tokens.APPEND:
                if (local_defs.ContainsKey(tag))
                {
                    Expression.EvalResult src    = local_defs[tag];
                    Expression            append = new Expression {
                        a = src, b = e, op = Tokens.PLUS
                    };
                    local_defs[tag] = append.Evaluate(this);
                }
                else if (defs.ContainsKey(tag))
                {
                    Expression.EvalResult src    = defs[tag];
                    Expression            append = new Expression {
                        a = src, b = e, op = Tokens.PLUS
                    };
                    local_defs[tag] = append.Evaluate(this);
                }
                else
                {
                    defs[tag] = e;
                }
                break;
            }
        }
Exemplo n.º 14
0
        private IList <byte> GetBytes(Expression.EvalResult e)
        {
            switch (e.Type)
            {
            case Expression.EvalResult.ResultType.Int:
                return(BitConverter.GetBytes(e.intval));

            case Expression.EvalResult.ResultType.String:
                return(Encoding.UTF8.GetBytes(e.strval));

            case Expression.EvalResult.ResultType.Array:
                var ret = new List <byte>();
                foreach (var aentry in e.arrval)
                {
                    ret.AddRange(GetBytes(aentry));
                }
                return(ret);

            default:
                return(null);
            }
        }
Exemplo n.º 15
0
 public void SetLocalDefine(string tag, Expression.EvalResult e)
 {
     local_defs[tag] = e;
 }
Exemplo n.º 16
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));
        }
Exemplo n.º 17
0
 public void SetDefine(string tag, Expression.EvalResult e)
 {
     defs[tag] = e;
 }