Beispiel #1
0
 public LayeObject Instantiate(LayeState state, string ctorName, params LayeObject[] args)
 {
     if (!ctorInfos.ContainsKey(ctorName))
     {
         if (ctorName != null)
             state.RaiseException("{0} does not have a constructor named {1}.", name, ctorName);
         else state.RaiseException("{0} does not have a default constructor.", name);
         return NULL;
     }
     var ctor = ctorInfos[ctorName];
     // TODO check contructors
     var instance = Activator.CreateInstance(type);
     return Bind(instance);
 }
Beispiel #2
0
 public override LayeObject MethodInvoke(LayeState state, string methodName, params LayeObject[] args)
 {
     if (!instanceMethodDelegates.ContainsKey(methodName))
     {
         state.RaiseException("No such method {1} in {0}.", TypeName, methodName);
         return NULL;
     }
     return instanceMethodDelegates[methodName](state, args);
 }
Beispiel #3
0
 public override LayeObject InvokeAsMethod(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     try
     {
         return callback(state, ths, args);
     }
     catch (UnhandledLayeException e)
     {
         throw e;
     }
     catch (Exception e)
     {
         state.RaiseException(e.GetType().FullName + ": " + e.Message + Environment.NewLine + e.StackTrace);
         return NULL;
     }
 }
Beispiel #4
0
 private LayeObject Infix__repeat(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     var arg = args[0] as LayeInt;
     if (arg == null)
     {
         state.RaiseException("Can only multiply a string by an integer value, got a(n) {0}.", args[0].TypeName);
         return NULL;
     }
     var count = arg.value;
     switch (count)
     {
         case 0: return EMPTY_STRING;
         case 1: return ths;
         case 2: return new LayeString((ths as LayeString).value + (ths as LayeString).value);
         default:
             var value = (ths as LayeString).value;
             var builder = new System.Text.StringBuilder();
             for (var i = 0; i < count; i++)
                 builder.Append(value);
             return new LayeString(builder.ToString());
     }
 }
Beispiel #5
0
 /// <summary>
 /// Attempts to invoke this object as a method on another object.
 /// If this object cannot be invoked, a <code>LayeIllegalOperation</code> exception is thrown.
 /// Otherwise, the result of the invocation is returned.
 /// </summary>
 /// <param name="state"></param>
 /// <param name="ths"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public virtual LayeObject InvokeAsMethod(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     state.RaiseException("Attempt to call {0}.", TypeName);
     return NULL;
 }
Beispiel #6
0
 public override LayeObject Infix(LayeState state, string op, LayeObject that)
 {
     if (that.IsNumeric)
     {
         lfloat thatValue = that is LayeInt ? (that as LayeInt).value : (that as LayeFloat).value;
         switch (op)
         {
             case "+": return new LayeFloat(value + thatValue);
             case "-": return new LayeFloat(value - thatValue);
             case "*": return new LayeFloat(value * thatValue);
             case "/":
                 if (thatValue == 0)
                 {
                     state.RaiseException("Attempt to divide by zero.");
                     return NULL;
                 }
                 return new LayeFloat(value / thatValue);
             case "//":
                 if (thatValue == 0)
                 {
                     state.RaiseException("Attempt to divide by zero.");
                     return NULL;
                 }
                 return LayeInt.ValueOf((lint)(value / thatValue));
             case "%":
                 if (thatValue == 0)
                 {
                     state.RaiseException("Attempt to divide by zero.");
                     return NULL;
                 }
                 return new LayeFloat(value % thatValue);
             case "^": return new LayeFloat((lfloat)Math.Pow(value, thatValue));
             case "==": return value == thatValue ? TRUE : FALSE;
             case "!=": return value != thatValue ? TRUE : FALSE;
             case "<": return value < thatValue ? TRUE : FALSE;
             case "<=": return value <= thatValue ? TRUE : FALSE;
             case ">": return value > thatValue ? TRUE : FALSE;
             case ">=": return value >= thatValue ? TRUE : FALSE;
             default: break;
         }
     }
     return base.Infix(state, op, that);
 }
Beispiel #7
0
 /// <summary>
 /// Access the fields of this object.
 /// If a field does not exist, an exception is thrown.
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public override LayeObject this[LayeState state, string key, bool raiseExceptions = true]
 {
     get
     {
         if (fields.ContainsKey(key))
             return fields[key];
         if (raiseExceptions)
             state.RaiseException("No such field {0} in kit.", key);
         return NULL;
     }
     set
     {
         if (Sealed)
         {
             if (fields.ContainsKey(key))
                 fields[key] = value;
             else if (raiseExceptions)
                 state.RaiseException("No such field {0} in kit.", key);
         }
         else fields[key] = value;
     }
 }
Beispiel #8
0
 internal LayeObject GetGlobal(LayeState state, string key, bool raiseException = true)
 {
     if (!globals.ContainsKey(key))
     {
         if (raiseException)
             state.RaiseException("Undefined symbol {0}.", key);
         return NULL;
     }
     return globals[key];
 }
Beispiel #9
0
 /// <summary>
 /// Access the fields of this object.
 /// If a field does not exist, an exception is thrown.
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public virtual LayeObject this[LayeState state, string key, bool raiseExceptions = true]
 {
     get
     {
         LayeObject property;
         if (type.FindInstancePropertyGet(key, out property))
             return property.InvokeAsMethod(state, this);
         else if (type.HasInstanceField(key))
             return fields[key];
         if (raiseExceptions)
             state.RaiseException("No such field {1} in {0}.", TypeName, key);
         return NULL;
     }
     set
     {
         LayeObject property;
         if (type.FindInstancePropertySet(key, out property))
             property.InvokeAsMethod(state, this, value);
         else if (type.HasInstanceField(key))
             fields[key] = value;
         else if (raiseExceptions)
             state.RaiseException("No such field {1} in {0}.", TypeName, key);
     }
 }
Beispiel #10
0
 public virtual LayeObject Infix(LayeState state, string op, LayeObject that)
 {
     if (type != null)
     {
         LayeObject method;
         if (type.FindInfix(op, out method))
             return method.InvokeAsMethod(state, this, that);
     }
     state.RaiseException("Attempt to invoke infix {0} on {1} with {2}.", op, TypeName, that.TypeName);
     return NULL;
 }
Beispiel #11
0
 public virtual LayeObject Prefix(LayeState state, string op)
 {
     if (type != null)
     {
         LayeObject method;
         if (type.FindPrefix(op, out method))
             return method.InvokeAsMethod(state, this);
     }
     state.RaiseException("Attempt to invoke prefix {0} on {1}.", op, TypeName);
     return NULL;
 }
Beispiel #12
0
 /// <summary>
 /// Attempts to invoke this object.
 /// If this object cannot be invoked, a <code>LayeIllegalOperation</code> exception is thrown.
 /// Otherwise, the result of the invocation is returned.
 /// </summary>
 /// <param name="state"></param>
 /// <param name="ths"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public virtual LayeObject Invoke(LayeState state, params LayeObject[] args)
 {
     if (type != null)
     {
         LayeObject invoke;
         if (type.GetInvoke(out invoke))
             return invoke.InvokeAsMethod(state, this, args);
     }
     state.RaiseException("Attempt to call {0}.", TypeName);
     return NULL;
 }
Beispiel #13
0
 /// <summary>
 /// Access an index of this object.
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public virtual LayeObject this[LayeState state, params LayeObject[] args]
 {
     get { state.RaiseException("Attempt to index {0}.", TypeName); return NULL; }
     set { state.RaiseException("Attempt to index {0}.", TypeName); }
 }
Beispiel #14
0
 public LayeObject As(LayeState state, LayeObject typeDef)
 {
     if (typeDef == null)
         throw new ArgumentException("typeDef");
     if (!(typeDef is LayeTypeDef))
     {
         state.RaiseException("Type expected in as expression, got {0}.", typeDef.TypeName);
         return NULL;
     }
     var type = typeDef as LayeTypeDef;
     if (this.type.InheritsFromOrIs(type))
         return this;
     LayeObject asCast;
     if (this.type.FindAsCast(type, out asCast))
     {
         LayeObject result = asCast.InvokeAsMethod(state, this);
         if (!result.TypeOf(state, type))
         {
             state.RaiseException("Expected result of type {0} to be returned by cast method, but {1} was found.", type.name, result.TypeName);
             return NULL;
         }
         return result;
     }
     return NULL;
 }
Beispiel #15
0
 public LayeObject OperatorIndexSet(LayeState state, string op, LayeObject value)
 {
     if (type != null)
     {
         LayeObject property;
         if (type.FindInstanceOperatorIndexSet(op, out property))
             return property.InvokeAsMethod(state, this, value);
     }
     state.RaiseException("Attempt to index {0} with operator {1}.", TypeName);
     return NULL;
 }
Beispiel #16
0
 public bool NotEqualTo(LayeState state, LayeObject that)
 {
     if (that == null)
         throw new ArgumentNullException("that");
     if (type != null)
     {
         LayeObject infixEqualTo;
         if (type.FindInfix("!=", out infixEqualTo))
         {
             var result = infixEqualTo.InvokeAsMethod(state, this, that);
             var boolval = result.As(state, LayeBool.TYPE) as LayeBool;
             if (boolval == null)
             {
                 state.RaiseException("Attempt to convert {0} to Bool.", result.TypeName);
                 return true;
             }
             return boolval.value;
         }
     }
     return !ReferenceEquals(this, that);
 }
Beispiel #17
0
 public int GetHashCode(LayeState state)
 {
     if (type != null)
     {
         LayeObject propHashCode;
         if (type.FindInstancePropertyGet("hashCode", out propHashCode))
         {
             var result = propHashCode.InvokeAsMethod(state, this);
             var ival = result.As(state, LayeInt.TYPE) as LayeInt;
             if (ival == null)
             {
                 state.RaiseException("Attempt to convert {0} to Int.", result.TypeName);
                 return 0;
             }
             return ival.value.GetHashCode();
         }
     }
     return base.GetHashCode();
 }
Beispiel #18
0
 public string ToString(LayeState state)
 {
     if (type != null)
     {
         LayeObject methToString;
         if (type.FindInstanceMethod("toString", out methToString))
         {
             var result = methToString.InvokeAsMethod(state, this);
             var strval = result.As(state, LayeString.TYPE) as LayeString;
             if (strval == null)
             {
                 state.RaiseException("Attempt to convert {0} to String.", result.TypeName);
                 return null;
             }
             return strval.value;
         }
     }
     return base.ToString();
 }
Beispiel #19
0
 /// <summary>
 /// Attempts to invoke a method on this object.
 /// If a method cannot be found, a <code>LayeNoSuchMethod</code> exception is thrown.
 /// Otherwise, the result of the invocation is returned.
 /// </summary>
 /// <param name="state"></param>
 /// <param name="ths"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public virtual LayeObject MethodInvoke(LayeState state, string methodName, params LayeObject[] args)
 {
     if (type != null)
     {
         LayeObject method;
         if (type.FindInstanceMethod(methodName, out method))
             return method.InvokeAsMethod(state, this, args);
     }
     state.RaiseException("No such method {1} in {0}.", TypeName, methodName);
     return NULL;
 }
Beispiel #20
0
 public override LayeObject Infix(LayeState state, string op, LayeObject that)
 {
     if (that.IsNumeric)
     {
         switch (op)
         {
             case "&":
                 if (that is LayeInt)
                     return ValueOf(value & (that as LayeInt).value);
                 break;
             case "|":
                 if (that is LayeInt)
                     return ValueOf(value | (that as LayeInt).value);
                 break;
             case "~":
                 if (that is LayeInt)
                     return ValueOf(value ^ (that as LayeInt).value);
                 break;
             case "<<":
                 if (that is LayeInt)
                     return ValueOf(value << (int)(that as LayeInt).value);
                 break;
             case ">>":
                 if (that is LayeInt)
                     return ValueOf(value >> (int)(that as LayeInt).value);
                 break;
             case ">>>":
                 if (that is LayeInt)
                     return ValueOf((lint)((ulint)value >> (int)(that as LayeInt).value));
                 break;
             case "+":
                 if (that is LayeInt)
                     return ValueOf(value + (that as LayeInt).value);
                 else return new LayeFloat(value + (that as LayeFloat).value);
             case "-":
                 if (that is LayeInt)
                     return ValueOf(value - (that as LayeInt).value);
                 else return new LayeFloat(value - (that as LayeFloat).value);
             case "*":
                 if (that is LayeInt)
                     return ValueOf(value * (that as LayeInt).value);
                 else return new LayeFloat(value * (that as LayeFloat).value);
             case "/":
             case "//":
                 if (that is LayeInt)
                 {
                     var thatValue = (that as LayeInt).value;
                     if (thatValue == 0)
                     {
                         state.RaiseException("Attempt to divide by zero.");
                         return NULL;
                     }
                     return ValueOf(value / thatValue);
                 }
                 else
                 {
                     var thatValue = (that as LayeFloat).value;
                     if (thatValue == 0)
                     {
                         state.RaiseException("Attempt to divide by zero.");
                         return NULL;
                     }
                     return new LayeFloat(value / thatValue);
                 }
             case "%":
                 if (that is LayeInt)
                 {
                     var thatValue = (that as LayeInt).value;
                     if (thatValue == 0)
                     {
                         state.RaiseException("Attempt to divide by zero.");
                         return NULL;
                     }
                     return ValueOf(value % thatValue);
                 }
                 else
                 {
                     var thatValue = (that as LayeFloat).value;
                     if (thatValue == 0)
                     {
                         state.RaiseException("Attempt to divide by zero.");
                         return NULL;
                     }
                     return new LayeFloat(value % thatValue);
                 }
             case "^":
                 return new LayeFloat((lfloat)Math.Pow(value, (that as LayeFloat).value));
             case "==":
                 if (that is LayeInt)
                     return value == (that as LayeInt).value ? TRUE : FALSE;
                 else return value == (that as LayeFloat).value ? TRUE : FALSE;
             case "!=":
                 if (that is LayeInt)
                     return value != (that as LayeInt).value ? TRUE : FALSE;
                 else return value != (that as LayeFloat).value ? TRUE : FALSE;
             case "<":
                 if (that is LayeInt)
                     return value < (that as LayeInt).value ? TRUE : FALSE;
                 else return value < (that as LayeFloat).value ? TRUE : FALSE;
             case "<=":
                 if (that is LayeInt)
                     return value <= (that as LayeInt).value ? TRUE : FALSE;
                 else return value <= (that as LayeFloat).value ? TRUE : FALSE;
             case ">":
                 if (that is LayeInt)
                     return value > (that as LayeInt).value ? TRUE : FALSE;
                 else return value > (that as LayeFloat).value ? TRUE : FALSE;
             case ">=":
                 if (that is LayeInt)
                     return value >= (that as LayeInt).value ? TRUE : FALSE;
                 else return value >= (that as LayeFloat).value ? TRUE : FALSE;
             default: break;
         }
     }
     return base.Infix(state, op, that);
 }