private object GetFirstObjectFromMember(ExprNode memberExp, List <Node> suffixes)
        {
            if (memberExp is SelfExpression)
            {
                return(Context.CurrentFrame.Resolve("this"));
            }
            if (memberExp is Identifier)
            {
                var id = ((Identifier)memberExp).Value;
                if (Context.CurrentFrame.Exist(id))
                {
                    return(Context.CurrentFrame.Resolve(id));
                }

                // TODO handle nested type
                // propably CLR type
                if (!(suffixes[0] is TypeDescriptorSuffix) && Context.IsClrType(id))
                {
                    return(Context.LoadTypeFromName(id));
                }

                // start with namespace?  attempt further
                for (int i = 0; i < suffixes.Count; i++)
                {
                    var suffix = suffixes[i];
                    if (suffix is IndexSuffix)
                    {
                        break;
                    }

                    if (suffix is TypeDescriptorSuffix)
                    {
                        var typeParams = (Type[])suffix.Accept(this);
                        suffixes.RemoveRange(0, i + 1);
                        return(Context.LoadTypeFromName(id, typeParams));
                    }

                    id += "." + suffix.SuffixValue(this);
                    if (i < suffixes.Count - 1 && (suffixes[i + 1] is TypeDescriptorSuffix))
                    {
                        var typeParams = (Type[])suffixes[i + 1].Accept(this);
                        suffixes.RemoveRange(0, i + 2);
                        return(Context.LoadTypeFromName(id, typeParams));
                    }

                    if (Context.IsClrType(id))
                    {
                        suffixes.RemoveRange(0, i + 1);
                        return(Context.LoadTypeFromName(id));
                    }
                }

                if (id.IndexOf('.') >= 0)
                {
                    id = id.Substring(0, id.IndexOf('.'));
                }
                throw ErrorFactory.CreateNotDefinedError(id);
            }
            return(memberExp.Accept(this));
        }
Exemple #2
0
 public object Resolve(string name)
 {
     if (name.StartsWith("$") && !IsGlobal)
     {
         return(global.Resolve(name));
     }
     if (members.ContainsKey(name))
     {
         return(this[name]);
     }
     if (parent != null)
     {
         return(parent.Resolve(name));
     }
     throw ErrorFactory.CreateNotDefinedError(name);
 }
 public int HashCode(object obj)
 {
     if (obj == null)
     {
         throw ErrorFactory.CreateTypeError("Invalid operand type: 'null'");
     }
     if (obj is BikeObject)
     {
         var lbo = (BikeObject)obj;
         if (lbo.Exist("hash_code") || lbo.Exist(InterpreterHelper.MemberMissing))
         {
             var hc = lbo.Resolve("hash_code");
             if (hc is BikeFunction)
             {
                 return((int)((BikeNumber)CallBikeFunction(
                                  (BikeFunction)hc, lbo, new object[0])).Value);
             }
             throw ErrorFactory.CreateTypeError("hash_code is not a function");
         }
         throw ErrorFactory.CreateNotDefinedError("hash_code");
     }
     return(obj.GetHashCode());
 }
Exemple #4
0
 public void Assign(string name, object value)
 {
     if (name.StartsWith("$") && !IsGlobal)
     {
         global.Assign(name, value);
     }
     else if (members.ContainsKey(name))
     {
         this[name] = value;
     }
     else if (parent != null)
     {
         parent.Assign(name, value);
     }
     else if (name.StartsWith("$"))
     {
         members[name] = value;
     }
     else
     {
         throw ErrorFactory.CreateNotDefinedError(name);
     }
 }
 public string Stringify(object obj, bool printError = false)
 {
     if (obj == null)
     {
         throw ErrorFactory.CreateTypeError("Invalid operand type: 'null'");
     }
     if (obj is BikeObject)
     {
         var sb = new StringBuilder();
         var bo = (BikeObject)obj;
         if (bo.Exist("to_string") || bo.Exist(InterpreterHelper.MemberMissing))
         {
             var ts = bo.Resolve("to_string");
             if (ts is BikeFunction)
             {
                 var value = CallBikeFunction((BikeFunction)ts, bo, new object[0]);
                 sb.Append(((BikeString)value).Value);
             }
             else
             {
                 throw ErrorFactory.CreateTypeError("to_string is not a function");
             }
         }
         else
         {
             throw ErrorFactory.CreateNotDefinedError("to_string");
         }
         if (printError && !bo.Exist(InterpreterHelper.SpecialSuffix + "is_error") && bo.Exist("stack_trace"))
         {
             var st = ((BikeString)bo.Resolve("stack_trace")).Value;
             sb.AppendLine().Append("Stack Trace:").AppendLine().Append(st).AppendLine();
         }
         return(sb.ToString());
     }
     return(obj.ToString());
 }