Exemple #1
0
 protected override LayeObject Infix__notEqualTo(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     var arg = args[0];
     if (!(arg is LayeString))
         return TRUE;
     return (LayeBool)((ths as LayeString).value != (arg as LayeString).value);
 }
Exemple #2
0
 public bool Fire(LayeState state, params LayeObject[] args)
 {
     for (int i = handles.Count - 1; i >= 0; i--)
         if (handles[i].handler.Invoke(state, args).ToBool(state))
             return true;
     return false;
 }
Exemple #3
0
 protected override LayeObject Infix__notEqualTo(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     if (ReferenceEquals(ths, args[0]))
         return FALSE;
     var arg = args[0] as LayeFloat;
     if (arg == null)
         return TRUE;
     return (LayeBool)((ths as LayeFloat).value != arg.value);
 }
Exemple #4
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);
 }
Exemple #5
0
 protected override LayeObject Infix__notEqualTo(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     var arg = args[0];
     if (ReferenceEquals(this, arg))
         return FALSE;
     if (!(arg is LayeSymbol))
         return TRUE;
     return (LayeBool)((ths as LayeSymbol).value != (arg as LayeSymbol).value);
 }
Exemple #6
0
 public override LayeObject this[LayeState state, params LayeObject[] args]
 {
     get
     {
         if (indexGetDelegate != null)
             return indexGetDelegate.Invoke(state, args);
         return base[state, args];
     }
     set
     {
         base[state, args] = value;
     }
 }
Exemple #7
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);
 }
Exemple #8
0
 public override LayeObject this[LayeState state, string key, bool raiseExceptions = true]
 {
     get
     {
         if (instanceFieldInfos.ContainsKey(key))
             return (LayeObject)instanceFieldInfos[key].GetValue(instance);
         return base[state, key, raiseExceptions];
     }
     set
     {
         if (instanceFieldInfos.ContainsKey(key))
             instanceFieldInfos[key].SetValue(instance, value);
         else base[state, key, raiseExceptions] = value;
     }
 }
Exemple #9
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;
     }
 }
Exemple #10
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);
 }
Exemple #11
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());
     }
 }
Exemple #12
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;
     }
 }
Exemple #13
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);
     }
 }
Exemple #14
0
        internal void PrintStack(LayeState state)
        {
            var offset = stackPointer + 1;
            var builder = new StringBuilder();

            builder.AppendFormat("({0}/{1}) [", offset, stack.Length);
            for (var i = 0; i < offset; i++)
            {
                if (i > 0)
                    builder.Append(", ");
                builder.Append(stack[i].ToString(state));
            }
            builder.Append("]");

            Console.WriteLine(builder.ToString());
        }
Exemple #15
0
        internal void PrintLocals(LayeState state)
        {
            var builder = new StringBuilder();

            builder.Append("[");
            for (var i = 0; i < locals.Length; i++)
            {
                if (i > 0)
                    builder.Append(", ");
                builder.Append(locals[i] == null ? "null" : locals[i].ToString(state));
            }
            builder.Append("]");

            Console.WriteLine(builder.ToString());
        }
Exemple #16
0
 internal void Run(LayeState state)
 {
     // Sealed means we've run this kit.
     if (Sealed)
         return;
     // add the std kit:
     Use(state, state.std, "std");
     UseAllFrom(state, state.std);
     // Run!
     body.Invoke(state);
     // We're done here.
     Seal();
 }
Exemple #17
0
 public override LayeObject Invoke(LayeState state, params LayeObject[] args)
 {
     return InvokeAsMethod(state, NULL, args);
 }
Exemple #18
0
 protected override LayeObject As__Bool(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     return (LayeBool)((ths as LayeFloat).value != 0);
 }
Exemple #19
0
 internal void SetGlobal(LayeState state, string key, LayeObject value)
 {
     globals[key] = value;
 }
Exemple #20
0
 protected override LayeObject Infix__notEqualTo(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     return ReferenceEquals(ths, args[0]) ? FALSE : TRUE;
 }
Exemple #21
0
 protected override LayeObject IMethod__toString(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     return new LayeString("null");
 }
Exemple #22
0
 protected override LayeObject As__Bool(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     return FALSE;
 }
Exemple #23
0
 protected override LayeObject IPropertyGet__hashCode(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     return LayeInt.ValueOf(0);
 }
Exemple #24
0
 internal void Set(LayeState state, LayeObject value)
 {
     LayeObject temp;
     if ((temp = values[Index]) is LayeReference)
         (temp as LayeReference).Store(state, value);
     else values[Index] = value;
 }
Exemple #25
0
 internal void UseFrom(LayeState state, LayeKit kit, params string[] fields)
 {
     foreach (var field in fields)
     {
         if (!kit.IsDefined(field))
             ; // TODO error
         else SetGlobal(state, field, kit[state, field]);
     }
 }
Exemple #26
0
 private LayeObject As__Int(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     return LayeInt.ValueOf((lint)(ths as LayeFloat).value);
 }
Exemple #27
0
 protected override LayeObject IMethod__toString(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     return new LayeString((ths as LayeFloat).value.ToString());
 }
Exemple #28
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];
 }
Exemple #29
0
 protected override LayeObject IPropertyGet__hashCode(LayeState state, LayeObject ths, params LayeObject[] args)
 {
     return LayeInt.ValueOf((ths as LayeFloat).value.GetHashCode());
 }
Exemple #30
0
 /// <summary>
 /// Does not actually invoke a method.
 /// It finds a field with the given name and invokes it normally.
 /// </summary>
 /// <param name="state"></param>
 /// <param name="methodName"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public override LayeObject MethodInvoke(LayeState state, string methodName, params LayeObject[] args)
 {
     return this[state, methodName].Invoke(state, args);
 }