Esempio n. 1
0
 private void CheckIndexType(HeronValue index)
 {
     if (index.As(layout.GetIndexType()) == null)
     {
         throw new Exception(index.ToString() + " is not a valid index type, expected " + layout.GetIndexType().ToString());
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Converts a HeronValue into the expected type. Checks for legal null
        /// passing (i.e. assures only nullable types receive nulls), and does type-checking.
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public HeronValue Coerce(HeronValue x)
        {
            HeronValue r = x;

            if (r is NullValue)
            {
                if (!type.nullable)
                {
                    throw new Exception("Passing null to a non-nullable variable " + name);
                }
                else
                {
                    return(r);
                }
            }
            else if (type != null)
            {
                r = x.As(type.type);
                if (r == null)
                {
                    throw new Exception("Failed to convert variable " + x + " from a " + x.Type.name + " to " + type.name);
                }
            }
            return(r);
        }
Esempio n. 3
0
        public override HeronValue Eval(VM vm)
        {
            HeronValue a = operand1.Eval(vm);
            HeronValue b = operand2.Eval(vm);

            switch (opcode)
            {
            case OpCode.opEq:
                return(new BoolValue(a.Equals(b)));

            case OpCode.opNEq:
                return(new BoolValue(!a.Equals(b)));

            case OpCode.opIs:
            {
                TypeValue tv = b as TypeValue;
                if (tv == null)
                {
                    throw new Exception("The second argument of the 'is' operator must be a type");
                }
                return(new BoolValue(a.Is(tv.Type)));
            }

            case OpCode.opAs:
            {
                HeronType t = b as HeronType;
                if (t == null)
                {
                    throw new Exception("The 'as' operator expects a type as a right hand argument");
                }
                HeronValue r = a.As(t);
                if (r != null)
                {
                    return(r);
                }
                if (t is InterfaceDefn && a is ClassInstance)
                {
                    DuckValue dv = new DuckValue(a as ClassInstance, t as InterfaceDefn);
                    return(dv);
                }
                throw new Exception("Failed to convert " + a.Type.name + " to a " + t.name);
            };
            }

            return(a.InvokeBinaryOperator(vm, opcode, b));
        }
Esempio n. 4
0
 public bool IsCompatible(ListValue x)
 {
     if (x.InternalCount() != Count)
     {
         return(false);
     }
     for (int i = 0; i < Count; ++i)
     {
         HeronValue val  = x.InternalAt(i);
         HeronType  type = GetTypes()[i];
         HeronValue test = val.As(type);
         if (test == null)
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 5
0
 public bool IsCompatible(RecordValue x)
 {
     for (int i = 0; i < names.Count; ++i)
     {
         string    name = names[i];
         HeronType type = types[i];
         int       n    = x.GetFieldIndex(name);
         if (n < 0)
         {
             return(false);
         }
         HeronValue val  = x.GetValue(n);
         HeronValue test = val.As(type);
         if (test == null)
         {
             return(false);
         }
     }
     return(true);
 }