예제 #1
0
        public override HeronValue Eval(VM vm)
        {
            HeronValue val = vm.Eval(rvalue);

            if (lvalue is Name)
            {
                string name = (lvalue as Name).name;
                if (vm.HasVar(name))
                {
                    vm.SetVar(name, val);
                    return(val);
                }
                else if (vm.HasField(name))
                {
                    vm.SetField(name, val);
                    return(val);
                }
                else
                {
                    throw new Exception(name + " is not a member field or local variable that can be assigned to");
                }
            }
            else if (lvalue is ChooseField)
            {
                ChooseField field = lvalue as ChooseField;
                HeronValue  self  = vm.Eval(field.self);
                self.SetField(field.name, val);
                return(val);
            }
            else if (lvalue is ReadAt)
            {
                ReadAt     ra    = lvalue as ReadAt;
                HeronValue self  = vm.Eval(ra.self);
                HeronValue index = vm.Eval(ra.index);
                ListValue  list  = self as ListValue;
                if (list != null)
                {
                    list.SetAtIndex(index, val);
                    return(val);
                }
                ArrayValue array = self as ArrayValue;
                if (array != null)
                {
                    array.SetAtIndex(index, val);
                    return(val);
                }

                throw new Exception("Unimplemented");
            }
            else
            {
                throw new Exception("Cannot assign to expression " + lvalue.ToString());
            }
        }
예제 #2
0
        public static Expression CreatePostfixExpr(ParseNode x, ref int i)
        {
            int        old = i;
            Expression r   = CreatePrimaryExpr(x, ref i);

            Assure(x, r != null, "unable to create primary expression");

            while (i < x.GetNumChildren())
            {
                old = i;
                ParseNode tmp = x.GetChild(i);

                if (tmp.Label == "bracketedexpr")
                {
                    i++;
                    Assure(x, tmp.GetNumChildren() == 1, "brackets must contain only a single sub-expression");
                    r = new ReadAt(r, CreateExpr(tmp.GetChild(0)));
                }
                else if (tmp.Label == "paranexpr")
                {
                    i++;
                    r = new FunCall(r, CreateCompoundExpr(tmp));
                }
                else if (tmp.ToString() == ".")
                {
                    i++;
                    Assure(x, x.GetNumChildren() > i, "a '.' operator must be followed by a valid name expression");
                    tmp = x.GetChild(i);
                    Assure(x, tmp.Label == "name", "a '.' operator must be followed by a valid name expression");
                    string sName = tmp.ToString();
                    Assure(x, sName.Length > 0, "Name expression must have non-zero length");
                    i++;
                    r = new ChooseField(r, sName);
                }
                else if (tmp.ToString() == "++")
                {
                    // You can't have anything to the left of ++
                    i++;
                    return(new PostIncExpr(r));
                }
                else
                {
                    return(r);
                }
                Assure(x, i > 0, "internal error, failed to advance sub-expression index");
            }

            return(r);
        }