Exemplo n.º 1
0
 public override string ToString()
 {
     if (operation == BlockOperation.Comment)
     {
         return("    // " + (string)parameter);
     }
     else if (operation == BlockOperation.MarkLabel)
     {
         return("  " + ((BlockLabel)parameter).Name + ":");
     }
     else if (operation == BlockOperation.DeclareLocal)
     {
         return("    local " + TypeNames.GetName(((BlockLocal)parameter).Type));
     }
     else if (operation == BlockOperation.BeginScope)
     {
         return("    {");
     }
     else if (operation == BlockOperation.EndScope)
     {
         return("    }");
     }
     else
     {
         throw new Exception();
     }
 }
Exemplo n.º 2
0
 public static object Convert(object o, Type type)
 {
     if ((o != null) && type.IsAssignableFrom(o.GetType()))
     {
         return(o);
     }
     else if (type == typeof(bool))
     {
         return(ToBool(o));
     }
     else if (type == typeof(double))
     {
         return(ToDouble(o));
     }
     else if (type == typeof(int))
     {
         return(ToInt(o));
     }
     else if (type == typeof(string))
     {
         return(ToString(o));
     }
     else if (type.IsArray)
     {
         return(ToArray(o, type));
     }
     else
     {
         throw new NotImplementedException(
                   "No conversion to " + TypeNames.GetName(type) + " from "
                   + TypeNames.GetName(o.GetType()));
     }
 }
Exemplo n.º 3
0
        public static object GetMember(object obj, string member,
                                       object[] parametersHint, bool throwIfUndefined)
        {
            if (obj is IScope)
            {
                // FIXME - shouldn't be using exceptions for this

                try
                {
                    return(((IScope)obj).GetName(member));
                }
                catch
                {
                }
            }

            MemberInfo memberInfo = FindMember(obj, member,
                                               parametersHint);

            if (memberInfo == null)
            {
                if (throwIfUndefined)
                {
                    throw new Exception("can't find getable member "
                                        + TextEscape.Quote(member) + " in "
                                        + TypeNames.GetName(obj.GetType()));
                }
                else
                {
                    return(null);
                }
            }

            if (memberInfo is MethodInfo)
            {
                return(new ClrObjectMethodBinding(obj, (MethodInfo)memberInfo));
            }
            else if (memberInfo is PropertyInfo)
            {
                return(((PropertyInfo)memberInfo).GetValue(obj, null));
            }
            else if (memberInfo is FieldInfo)
            {
                object fieldValue = ((FieldInfo)memberInfo).GetValue(obj);

                if (fieldValue is Method)
                {
                    fieldValue = new ObjectMethodBinding(obj,
                                                         (Method)fieldValue);
                }

                return(fieldValue);
            }
            else
            {
                throw new Exception("can't get "
                                    + TypeNames.GetName(memberInfo.GetType()));
            }
        }
Exemplo n.º 4
0
        public ObjectViewHeader GetHeader(object obj)
        {
            IList list = (IList)obj;

            return(new ObjectViewHeader(TypeNames.GetName(obj.GetType()),
                                        list.Count > 0,
                                        true));
        }
Exemplo n.º 5
0
        public ObjectViewHeader GetHeader(object obj)
        {
            IDictionary dictionary = (IDictionary)obj;

            return(new ObjectViewHeader(TypeNames.GetName(obj.GetType()),
                                        dictionary.Count > 0,
                                        true));
        }
Exemplo n.º 6
0
        public ObjectViewHeader GetHeader(object obj)
        {
            Array array = (Array)obj;

            return(new ObjectViewHeader(TypeNames.GetName(obj.GetType()),
                                        array.Length > 0,
                                        false));
        }
Exemplo n.º 7
0
        public override ParseTree Parse(Lexer lexer, ParserState state)
        {
            int start = lexer.Position;

            if (type == null)
            {
                throw new Exception();
            }

            if (state.RuntimeState.Runtime.TraceParser)
            {
                state.RuntimeState.Runtime.ParseTrace.Enter(this,
                                                            lexer.CurrentSource(),
                                                            "User defined node " + TypeNames.GetName(type));
            }

            object instance    = NewNode.New(state.RuntimeState, type, null);
            object parseMethod = MemberNode.GetMember(instance, "Parse", true);
            object result      = CallNode.Call(state.RuntimeState, parseMethod,
                                               new object[] { lexer });

            if (result == null)
            {
                result = ParseTree.Yes;
            }
            else if (result is bool)
            {
                if ((bool)result)
                {
                    result = ParseTree.Yes;
                }
                else
                {
                    result = ParseTree.No;
                }
            }

            if (state.RuntimeState.Runtime.TraceParser)
            {
                if (result == ParseTree.No)
                {
                    state.RuntimeState.Runtime.ParseTrace.No(this,
                                                             lexer.SourceFrom(start));
                }
                else
                {
                    state.RuntimeState.Runtime.ParseTrace.Yes(this,
                                                              lexer.SourceFrom(start));
                }
            }

            return((ParseTree)result);
        }
Exemplo n.º 8
0
 public static object Not(object a)
 {
     if (a is bool)
     {
         return(!((bool)a));
     }
     else
     {
         throw new NotImplementedException(
                   "No not operator for "
                   + TypeNames.GetName(a.GetType()));
     }
 }
Exemplo n.º 9
0
        public ObjectViewHeader GetHeader(object obj)
        {
            if (obj == null)
            {
                return(new ObjectViewHeader("null", false, false));
            }

            Type type = obj.GetType();

            string description = string.Format("({0}) 0x{1:x}", TypeNames.GetName(type), obj.GetHashCode());
            bool   hasContents = GetFields(type).Length > 0;

            return(new ObjectViewHeader(description, hasContents, false));
        }
Exemplo n.º 10
0
 public static object Multiply(object a, object b)
 {
     if (a is int)
     {
         return((int)a * ConvertNode.ToInt(b));
     }
     else
     {
         throw new NotImplementedException(
                   "No multiply operator for "
                   + TypeNames.GetName(a.GetType()) + " and "
                   + (b == null ? "null" : TypeNames.GetName(b.GetType())));
     }
 }
Exemplo n.º 11
0
        public static bool SetMember(object obj, string member, object v,
                                     bool throwIfUndefined)
        {
            if (obj is IScope)
            {
                // FIXME - shouldn't be using exceptions for this

                try
                {
                    ((IScope)obj).SetName(member, v);
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }

            MemberInfo memberInfo = FindMember(obj, member, null);

            if (memberInfo == null)
            {
                if (throwIfUndefined)
                {
                    throw new Exception("can't find setable " + member
                                        + " in " + obj.GetType());
                }
                else
                {
                    return(false);
                }
            }

            if (memberInfo is PropertyInfo)
            {
                ((PropertyInfo)memberInfo).SetValue(obj, v, null);
            }
            else if (memberInfo is FieldInfo)
            {
                ((FieldInfo)memberInfo).SetValue(obj, v);
            }
            else
            {
                throw new Exception("can't set "
                                    + TypeNames.GetName(memberInfo.GetType()));
            }

            return(true);
        }
Exemplo n.º 12
0
 public static bool ToBool(object o)
 {
     if (o == null)
     {
         return(false);
     }
     else if (o is bool)
     {
         return((bool)o);
     }
     else
     {
         throw new NotImplementedException(
                   "No conversion to bool from " + TypeNames.GetName(o.GetType()));
     }
 }
Exemplo n.º 13
0
 public static object Add(object a, object b)
 {
     if (a is int)
     {
         return((int)a + ConvertNode.ToInt(b));
     }
     else if (a is string)
     {
         return((string)a + ConvertNode.ToString(b));
     }
     else
     {
         throw new NotImplementedException(
                   "No add operator for "
                   + TypeNames.GetName(a.GetType()) + " and "
                   + (b == null ? "null" : TypeNames.GetName(b.GetType())));
     }
 }
Exemplo n.º 14
0
        public static bool CompareLessOrEqual(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) <= ConvertNode.ToDouble(b));
            }

            if (a is int)
            {
                return((int)a <= ConvertNode.ToInt(b));
            }
            else
            {
                throw new NotImplementedException(
                          "No less or equal comparision for "
                          + TypeNames.GetName(a.GetType()) + " and "
                          + (b == null ? "null" : TypeNames.GetName(b.GetType())));
            }
        }
Exemplo n.º 15
0
        private TreeIter Add(TreeIter parent, string prefix, RuntimeObject node)
        {
            Type type = node.GetType();

            parent = store.AppendValues(parent, prefix + TypeNames.GetName(type), node);

            // FIXME
            try
            {
                foreach (string field in Pattern.PatternForType(type).Fields)
                {
                    Add(parent, field + ": ", type.GetField(field).GetValue(node));
                }
            }
            catch (Exception)
            {
            }

            return(parent);
        }
Exemplo n.º 16
0
        private void OnGrammarPatternDefined(Pattern pattern)
        {
            Application.Invoke(delegate
            {
                string name = TypeNames.GetName(pattern.Type);

                if (pattern is AbstractPattern)
                {
                    name += " (abstract)";
                }

                TreeIter iter = store.AppendValues(name, pattern);
                AddNode(iter, pattern.ParseGraph);

                patternPaths[pattern] = store.GetPath(iter);

                signal.Send();
            });

            signal.WaitFor();
        }
Exemplo n.º 17
0
        public static bool CompareInequality(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) != ConvertNode.ToDouble(b));
            }

            if (a == null)
            {
                return(b != null);
            }
            else if (b == null)
            {
                return(true);
            }
            else if (a is int)
            {
                return((int)a != ConvertNode.ToInt(b));
            }
            else if (a is double)
            {
                return((double)a != ConvertNode.ToDouble(b));
            }
            else if (a is string)
            {
                return((string)a != ConvertNode.ToString(b));
            }
            else if (a is char)
            {
                return(a.ToString() != ConvertNode.ToString(b));
            }
            else
            {
                throw new NotImplementedException(
                          "No inequality comparision for "
                          + TypeNames.GetName(a.GetType()) + " and "
                          + (b == null ? "null" : TypeNames.GetName(b.GetType())));
            }
        }
Exemplo n.º 18
0
 public override string ToString()
 {
     if (parameter == null)
     {
         return("    " + op.ToString());
     }
     else if (parameter is string)
     {
         return("    " + op + " " + TextEscape.Quote((string)parameter));
     }
     else if (parameter is Type)
     {
         return("    " + op + " " + TypeNames.GetName((Type)parameter));
     }
     else if (parameter is FieldInfo)
     {
         FieldInfo field = (FieldInfo)parameter;
         return("    " + op + " " + TypeNames.GetName(field.DeclaringType) + "::" + field.Name);
     }
     else if (parameter is MethodInfo)
     {
         MethodInfo method = (MethodInfo)parameter;
         return("    " + op + " " + TypeNames.GetName(method.DeclaringType) + "::" + method.Name);
     }
     else if (parameter is ConstructorInfo)
     {
         ConstructorInfo constructor = (ConstructorInfo)parameter;
         return("    " + op + " " + TypeNames.GetName(constructor.DeclaringType));
     }
     else if (parameter is BlockLocal)
     {
         return("    " + op + " " + TypeNames.GetName(((BlockLocal)parameter).Type));
     }
     else
     {
         return("    " + op + " " + parameter);
     }
 }
Exemplo n.º 19
0
        public static bool CompareGreater(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) > ConvertNode.ToDouble(b));
            }

            if (a is int)
            {
                return((int)a > ConvertNode.ToInt(b));
            }
            else if (a is double)
            {
                return((double)a > ConvertNode.ToDouble(b));
            }
            else
            {
                throw new NotImplementedException(
                          "No greater comparision for "
                          + TypeNames.GetName(a.GetType()) + " and "
                          + (b == null ? "null" : TypeNames.GetName(b.GetType())));
            }
        }
Exemplo n.º 20
0
        public static object Divide(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) / ConvertNode.ToDouble(b));
            }

            if (a is int)
            {
                return((int)a / ConvertNode.ToInt(b));
            }
            else if (a is double)
            {
                return((double)a / ConvertNode.ToDouble(b));
            }
            else
            {
                throw new NotImplementedException(
                          "No divide operator for "
                          + TypeNames.GetName(a.GetType()) + " and "
                          + (b == null ? "null" : TypeNames.GetName(b.GetType())));
            }
        }
Exemplo n.º 21
0
 public static int ToInt(object o)
 {
     if (o == null)
     {
         return(0);
     }
     else if (o is int)
     {
         return((int)o);
     }
     else if (o is double)
     {
         return((int)(double)o);
     }
     else if (o is string)
     {
         return(System.Convert.ToInt32((string)o));
     }
     else
     {
         throw new NotImplementedException(
                   "No conversion to int from " + TypeNames.GetName(o.GetType()));
     }
 }
Exemplo n.º 22
0
 private void AppendReturnType(StringBuilder str)
 {
     str.Append(TypeNames.GetName(Method.ReturnType));
 }
Exemplo n.º 23
0
        public static object Call(RuntimeState state, object callable,
                                  object[] parameters, bool wantRefParams, out bool[] refParams)
        {
            if (parameters == null)
            {
                parameters = new object[] {}
            }
            ;

            if (callable == null)
            {
                throw new NotImplementedException(
                          "No call operation for null");
            }
            else if (callable is ICallable)
            {
                return(((ICallable)callable).Call(state, parameters,
                                                  wantRefParams, out refParams));
            }
            else if (callable is MethodInfo)
            {
                ClrObjectMethodBinding binding
                    = new ClrObjectMethodBinding(null, (MethodInfo)callable);

                return(binding.Call(state, parameters, wantRefParams,
                                    out refParams));
            }
            else
            {
                object callMethod = MemberNode.GetMember(callable, "Call",
                                                         false);

                if (callMethod == null)
                {
                    throw new NotImplementedException(
                              "No call operation for "
                              + TypeNames.GetName(callable.GetType()));
                }

                if (wantRefParams)
                {
                    object getRefParamsMethod = MemberNode.GetMember(callable,
                                                                     "GetRefParams", false);

                    if (getRefParamsMethod != null)
                    {
                        object refParamsObject = CallNode.Call(state,
                                                               getRefParamsMethod, null);

                        refParams = (bool[])ConvertNode.Convert(
                            refParamsObject, typeof(bool[]));
                    }
                    else
                    {
                        refParams = null;
                    }
                }
                else
                {
                    refParams = null;
                }

                return(Call(state, callMethod, new object[] { parameters }));
            }
        }
    }
Exemplo n.º 24
0
        public ObjectViewHeader GetHeader(object obj)
        {
            string description = string.Format("({0}) {1}", TypeNames.GetName(obj.GetType()), obj);

            return(new ObjectViewHeader(description, false, false));
        }
Exemplo n.º 25
0
        public void Build(IMethodBuilder builder)
        {
            //Dump();

            ILGenerator il = builder.GetILGenerator();

            Dictionary <BlockLabel, Label> labels
                = new Dictionary <BlockLabel, Label>();

            Dictionary <BlockLocal, LocalBuilder> locals
                = new Dictionary <BlockLocal, LocalBuilder>();

            foreach (IBlockLine line in (IEnumerable <IBlockLine>)lines)
            {
                BlockInstruction instruction = line as BlockInstruction;

                if ((instruction != null) && (instruction.Operation == BlockOperation.MarkLabel))
                {
                    labels[(BlockLabel)instruction.Parameter] = il.DefineLabel();
                }
            }

            foreach (IBlockLine line in (IEnumerable <IBlockLine>)lines)
            {
                if (line is Instruction)
                {
                    Instruction instruction = line as Instruction;

                    if (instruction.Parameter == null)
                    {
                        il.Emit(instruction.Op);
                    }
                    else if (instruction.Parameter is int)
                    {
                        il.Emit(instruction.Op, (int)instruction.Parameter);
                    }
                    else if (instruction.Parameter is string)
                    {
                        il.Emit(instruction.Op, (string)instruction.Parameter);
                    }
                    else if (instruction.Parameter is Type)
                    {
                        il.Emit(instruction.Op, (Type)instruction.Parameter);
                    }
                    else if (instruction.Parameter is FieldInfo)
                    {
                        il.Emit(instruction.Op, (FieldInfo)instruction.Parameter);
                    }
                    else if (instruction.Parameter is MethodInfo)
                    {
                        il.Emit(instruction.Op, (MethodInfo)instruction.Parameter);
                    }
                    else if (instruction.Parameter is ConstructorInfo)
                    {
                        il.Emit(instruction.Op, (ConstructorInfo)instruction.Parameter);
                    }
                    else if (instruction.Parameter is BlockLabel)
                    {
                        BlockLabel blockLabel = (BlockLabel)instruction.Parameter;

                        Label label;

                        if (!labels.TryGetValue(blockLabel, out label))
                        {
                            throw new Exception("Label " + blockLabel.Name + " not marked");
                        }

                        il.Emit(instruction.Op, label);
                    }
                    else if (instruction.Parameter is BlockLocal)
                    {
                        il.Emit(instruction.Op, locals[(BlockLocal)instruction.Parameter]);
                    }
                    else
                    {
                        throw new NotImplementedException(instruction.Op.Value + " " + TypeNames.GetName(instruction.Parameter.GetType()));
                    }
                }
                else if (line is BlockInstruction)
                {
                    BlockInstruction instruction = line as BlockInstruction;

                    if (instruction.Operation == BlockOperation.Comment)
                    {
                    }
                    else if (instruction.Operation == BlockOperation.MarkLabel)
                    {
                        il.MarkLabel(labels[(BlockLabel)instruction.Parameter]);
                    }
                    else if (instruction.Operation == BlockOperation.DeclareLocal)
                    {
                        BlockLocal local = (BlockLocal)instruction.Parameter;
                        locals[local] = il.DeclareLocal(local.Type);
                    }
                    else if (instruction.Operation == BlockOperation.BeginScope)
                    {
                        il.BeginScope();
                    }
                    else if (instruction.Operation == BlockOperation.EndScope)
                    {
                        il.EndScope();
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                else
                {
                    throw new Exception();
                }
            }
        }
Exemplo n.º 26
0
 public virtual void Set(RuntimeState state, object v)
 {
     throw new NotImplementedException(TypeNames.GetName(GetType()));
 }
Exemplo n.º 27
0
 private void AppendGenericDefinition(StringBuilder str)
 {
     if (Method.IsGenericMethod)
     {
         str.AppendFormat("<{0}>", string.Join(", ",
                                               Method.GetGenericArguments().Select(x => TypeNames.GetName(x)).ToArray()));
     }
 }
Exemplo n.º 28
0
 public virtual void EmitSet(Block generator)
 {
     throw new NotImplementedException(TypeNames.GetName(GetType()));
 }
Exemplo n.º 29
0
 public virtual object Get(RuntimeState state, object[] parametersHint)
 {
     throw new NotImplementedException(TypeNames.GetName(GetType()));
 }
Exemplo n.º 30
0
        private void AppendParameters(StringBuilder str, int skip)
        {
            var parameters = Method.GetParameters().Skip(skip);

            str.AppendFormat("({0})",
                             string.Join(", ",
                                         parameters.Select(x => string.Format("{0}{1} {2}", GetParameterModifiers(x, true), TypeNames.GetName(x.ParameterType), x.Name)).ToArray()));
        }