public override Func <T> CreateGetFunction <T>(CompilerData cdata)
        {
            Func <object> accessorFunc = CompilerUtility.ForceGetFunction(_accessor, cdata);
            Func <object> targetFunc   = (CompilerUtility.IsFunc(_targetObject)) ? (Func <object>)_targetObject : null;

            if (targetFunc == null && accessorFunc == null)
            {
                return(() =>
                {
                    return (T)((IList)_targetObject)[(int)_accessor];
                });
            }
            else if (targetFunc == null && accessorFunc != null)
            {
                return(() =>
                {
                    return (T)((IList)_targetObject)[(int)accessorFunc()];
                });
            }
            else if (targetFunc != null && accessorFunc == null)
            {
                return(() =>
                {
                    return (T)((IList)targetFunc())[(int)_accessor];
                });
            }
            else
            {
                return(() =>
                {
                    return (T)((IList)targetFunc())[(int)accessorFunc()];
                });
            }
        }
Exemplo n.º 2
0
 public override Func <T> CreateGetFunction <T>(CompilerData cdata)
 {
     if (CompilerUtility.IsFunc(_targetObject))
     {
         Func <object> targetFunc = _targetObject as Func <object>;
         return((Func <T>)(() =>
         {
             ResolveArguments(_argNodeBuffer, ref _argBuffer, cdata);
             object targ = targetFunc();
             if (targ.GetType() == typeof(System.Type))
             {
                 throw new Exception("Methods in the type System.Type cannot be accessed");
             }
             return (T)_cdata._delegateCache.CallMethod(targetFunc(), _identifer, _argBuffer);
         }));
     }
     else
     {
         return((Func <T>)(() =>
         {
             ResolveArguments(_argNodeBuffer, ref _argBuffer, cdata);
             return (T)_cdata._delegateCache.CallMethod(_targetObject, _identifer, _argBuffer);
         }));
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Main compilation entry
        /// </summary>
        /// <returns></returns>
        ///
        protected virtual CompiledScript _InternalCompile()
        {
            _cdata = new CompilerData(_assembly);
            List <Func <object> > functions = new List <Func <object> >();

            while (!isAtEnd)
            {
                Func <object> function = null;
                object        compiled = CompileDeclaration();
                if (compiled == null)
                {
                    continue;
                }
                Type returnType = CompilerUtility.GetReturnType(compiled);

                TypeDef typeDef = _assembly.GetTypeDef(returnType);

                if (CompilerUtility.IsFunc(compiled))
                {
                    if (compiled.GetType() != typeof(Func <object>))
                    {
                        function = typeDef.ToGenericFunction(compiled, _cdata);
                    }
                    else
                    {
                        function = (Func <object>)compiled;
                    }
                }
                else if (CompilerUtility.IsReference(compiled))
                {
                    function = ((Reference)compiled).CreateGetFunction(_cdata);
                }
                else
                {
                    function = () => { return(compiled); };
                }
                functions.Add(function);
            }
            Func <object>[] funcArray = functions.ToArray();

            CompiledScript compiledScript = new CompiledScript(() =>
            {
                object output = null;
                for (int mn = 0; mn < funcArray.Length; mn += 1)
                {
                    output = funcArray[mn]();
                    if ((_cdata._runtimeFlags & (int)CompilerData.ControlInstruction.Exit) != 0)
                    {
                        break;
                    }
                }
                _cdata._runtimeFlags = 0;
                return(_cdata._returnValue);
            },
                                                               _cdata);

            return(compiledScript);
        }
Exemplo n.º 4
0
 public virtual Func <object> ToGenericFunction(object arg, CompilerData _internal)
 {
     if (CompilerUtility.IsFunc(arg))
     {
         if (type.IsSubclassOf(typeof(object)))
         {
             return((Func <object>)arg);
         }
         throw new InvalidCastException("Cannot cast " + type.Name + " to Func<object>");
     }
     else
     {
         return(() => { return arg; });
     }
 }
        public override Func <T> CreateSetFunction <T>(object value, CompilerData cdata)
        {
            if (CompilerUtility.IsFunc(value))
            {
                Func <T> valueFunc = (Func <T>)value;
                return(() =>
                {
                    T ob = valueFunc();
                    //Type checking
                    Type refType = cdata._environment[_environmentIndex].type;
                    if (refType != typeof(Object) && refType != ob.GetType())
                    {
                        throw new RuntimeException("data type does not match: " + refType.Name + " is not " + ob.GetType().Name);
                    }

                    //Assign data
                    cdata._environment[_environmentIndex].data = ob;
                    return (T)(cdata._environment[_environmentIndex].data);
                });
            }
            else
            {
                T val = (T)value;
                return(() =>
                {
                    //Type checking
                    Type refType = cdata._environment[_environmentIndex].type;
                    if (refType != typeof(Object) && refType != val.GetType())
                    {
                        throw new RuntimeException("data type does not match");
                    }

                    //Assign data
                    cdata._environment[_environmentIndex].data = val;
                    return val;
                });
            }
        }
Exemplo n.º 6
0
        protected virtual object CompileHelpStatement()
        {
            if (isAtEnd || Peek().type == Token.Type.EOS)
            {
                return((Func <object>)(() =>
                {
                    string meta = ReflectionUtility.PrintMembers(_cdata._contextInterface.context);
                    _cdata._exit(meta);
                    return meta;
                }));
            }

            Func <object> func      = null;
            object        statement = CompileExpressionStatement();

            if (statement == null)
            {
                return((Func <object>)(() =>
                {
                    return null;
                }));
            }

            Type returnType = CompilerUtility.GetReturnType(statement);

            TypeDef typedef = _assembly.GetTypeDef(returnType);

            if (CompilerUtility.IsFunc(statement))
            {
                if (returnType != typeof(object))
                {
                    func = typedef.ToGenericFunction(statement, _cdata);
                }
                else
                {
                    func = (Func <object>)statement;
                }

                return((Func <object>)(() =>
                {
                    object val = ReflectionUtility.PrintMembers(func());
                    _cdata._exit(val);
                    return val;
                }));
            }
            else if (CompilerUtility.IsReference(statement))
            {
                func = ((Reference)statement).CreateGetFunction(_cdata);

                return((Func <object>)(() =>
                {
                    object val = ReflectionUtility.PrintMembers(func());
                    _cdata._exit(val);
                    return val;
                }));
            }
            else
            {
                return((Func <object>)(() =>
                {
                    _cdata._exit(ReflectionUtility.PrintMembers(statement));
                    return statement;
                }));
            }
        }
        public override Func <T> CreateGetFunction <T>(CompilerData cdata)
        {
            Func <object> accessorFunc = CompilerUtility.ForceGetFunction(_accessor, cdata);
            Func <object> targetFunc   = (CompilerUtility.IsFunc(_targetObject)) ? (Func <object>)_targetObject : null;

            if (targetFunc == null && accessorFunc == null)
            {
                IList list          = _accessor as IList;
                int   accessorCount = list.Count;
                return(() =>
                {
                    Type et = (_elemType != typeof(object)) ? _elemType : _targetObject.GetType().GetElementType();
                    IList arr = System.Array.CreateInstance(et, accessorCount);
                    for (int i = 0; i < accessorCount; i += 1)
                    {
                        arr[i] = ((IList)_targetObject)[(int)list[i]];
                    }
                    return (T)arr;
                });
            }
            else if (targetFunc == null && accessorFunc != null)
            {
                return(() =>
                {
                    IList list = accessorFunc() as IList;
                    int accessorCount = list.Count;
                    Type et = (_elemType != typeof(object)) ? _elemType : _targetObject.GetType().GetElementType();
                    IList arr = System.Array.CreateInstance(et, accessorCount);
                    for (int i = 0; i < accessorCount; i += 1)
                    {
                        arr[i] = ((IList)_targetObject)[(int)list[i]];
                    }
                    return (T)arr;
                });
            }
            else if (targetFunc != null && accessorFunc == null)
            {
                IList list          = _accessor as IList;
                int   accessorCount = list.Count;
                return(() =>
                {
                    object targetObject = targetFunc();
                    Type et = (_elemType != typeof(object)) ? _elemType : targetObject.GetType().GetElementType();
                    IList arr = System.Array.CreateInstance(et, accessorCount);
                    for (int i = 0; i < accessorCount; i += 1)
                    {
                        arr[i] = ((IList)targetObject)[(int)list[i]];
                    }
                    return (T)arr;
                });
            }
            else
            {
                return(() =>
                {
                    IList list = accessorFunc() as IList;
                    int accessorCount = list.Count;
                    object targetObject = targetFunc();
                    Type et = (_elemType != typeof(object)) ? _elemType : targetObject.GetType().GetElementType();
                    IList arr = System.Array.CreateInstance(et, accessorCount);
                    for (int i = 0; i < accessorCount; i += 1)
                    {
                        arr[i] = ((IList)targetObject)[(int)list[i]];
                    }
                    return (T)arr;
                });
            }
        }