Пример #1
0
        public string ResolveString(ITokenErrLog tok, object scope, ResolvedEnoughDelegate isItResolvedEnough = null)
        {
            object result = Resolve(tok, scope, isItResolvedEnough);

            if (result == null)
            {
                return(null);
            }
            return(result.ToString());
        }
Пример #2
0
        public object Resolve(ITokenErrLog tok, object scope, ResolvedEnoughDelegate isItResolvedEnough = null)
        {
            DelimOp op = sourceMeta as DelimOp;

            if (op != null)
            {
                return(op.resolve.Invoke(tok, this, scope, isItResolvedEnough));
            }
            List <object> finalTerms = ResolveTerms(tok, scope, tokens, isItResolvedEnough);
            object        result     = finalTerms;

            if (rules != null && rules.Simplify != null)
            {
                if (isItResolvedEnough != null && isItResolvedEnough.Invoke(result))
                {
                    return(result);
                }
                result = rules.Simplify.Invoke(finalTerms);
            }
            return(result);
        }
Пример #3
0
        public object Resolve(ITokenErrLog tok, object scope, ResolvedEnoughDelegate isItResolvedEnough = null)
        {
            if (isItResolvedEnough != null && isItResolvedEnough(this))
            {
                return(this);
            }
            if (index == -1 && length == -1)
            {
                return(meta);
            }
            if (meta == null)
            {
                throw new NullReferenceException("can't resolve NULL token");
            }
            switch (meta)
            {
            case string s: {
                string str = ToString(s);
                //Show.Log("@@@  "+str+" "+scope);
                if (scope != null && (isItResolvedEnough == null || isItResolvedEnough.Invoke(str)))
                {
                    if (CodeRules.op_SearchForMember(str, out object value, out Type type, scope))
                    {
                        //Show.Log(str+" "+foundIt+" "+value);
                        return(value);
                    }
                }
                return(str);
            }

            case TokenSubstitution ss: return(ss.value);

            case Delim d: return(d.text);

            case SyntaxTree pce: return(pce.Resolve(tok, scope, isItResolvedEnough));
            }
            throw new DecoderFallbackException();
        }
Пример #4
0
        public static void ResolveTerms(ITokenErrLog tok, object scope, List <Token> tokens, int start, int length, List <object> results, ResolvedEnoughDelegate isItResolvedEnough = null)
        {
            List <int> found = new List <int>();

            FindTerms(tokens, start, length, found);
            for (int i = 0; i < found.Count; ++i)
            {
                Token  t      = tokens[found[i]];
                object result = t.Resolve(tok, scope, isItResolvedEnough);
                results.Add(result);
                // if this token is probably a method call, or there are arguments immediately after this token (so maybe a method call)
                Invocation mc = result as Invocation;
                if (mc != null || (i < found.Count - 1 && found[i + 1] == found[i] + 1))
                {
                    object target     = mc != null ? mc.target : scope;
                    object methodName = mc != null ? mc.methodName : result;
                    Token  methodArgs = tokens[found[i + 1]];
                    if (Invocation.TryExecuteFunction(target, methodName, methodArgs, out result, tok, isItResolvedEnough))
                    {
                        ++i;
                        results[results.Count - 1] = result;
                    }
                }
            }
        }
Пример #5
0
        public static List <object> ResolveTerms(ITokenErrLog tok, object scope, List <Token> tokens, ResolvedEnoughDelegate isItResolvedEnough = null)
        {
            List <object> results = new List <object>();

            ResolveTerms(tok, scope, tokens, 0, tokens.Count, results, isItResolvedEnough);
            return(results);
        }
Пример #6
0
        private static List <object> ResolveFunctionArgumentList(Token argsToken, object scope, ITokenErrLog tok, ResolvedEnoughDelegate isItResolvedEnough)
        {
            object argsRaw = argsToken.Resolve(tok, scope, isItResolvedEnough);

            if (argsRaw == null)
            {
                argsRaw = new List <object>();
            }
            List <object> args = argsRaw as List <object>;

            if (args == null)
            {
                args = new List <object> {
                    argsRaw
                };
            }
            // remove commas if they are comma tokens before and after being parsed
            SyntaxTree beforeParse = argsToken.GetAsSyntaxNode();

            for (int i = args.Count - 1; i >= 0; --i)
            {
                if ((args[i] as string) == "," && beforeParse.tokens[i + 1].StringifySmall() == ",")
                {
                    args.RemoveAt(i);
                }
            }
            return(args);
        }
Пример #7
0
        private static bool TryExecuteFunction(object scope, string funcName, Token argsToken, out object result, ITokenErrLog tok, ResolvedEnoughDelegate isItResolvedEnough)
        {
            result = null;
            if (!DeterminePossibleMethods(scope, funcName, out List <MethodInfo> possibleMethods, tok, argsToken))
            {
                return(false);
            }
            List <object> args = ResolveFunctionArgumentList(argsToken, scope, tok, isItResolvedEnough);

            if (!DetermineValidMethods(funcName, argsToken, possibleMethods, out List <ParameterInfo[]> validParams, args, tok))
            {
                return(false);
            }
            if (!DetermineMethod(args, possibleMethods, validParams, out MethodInfo mi, out object[] finalArgs, tok, argsToken))
            {
                return(false);
            }
            return(ExecuteMethod(scope, mi, finalArgs, out result, tok, argsToken));
        }
Пример #8
0
        public static bool TryExecuteFunction(object scope, object resolvedFunctionIdentifier, Token arguments, out object funcResult, ITokenErrLog errLog, ResolvedEnoughDelegate isItResolvedEnough = null)
        {
            string funcName = GetMethodCall(resolvedFunctionIdentifier, arguments);

            if (funcName != null && TryExecuteFunction(scope, funcName, arguments, out funcResult, errLog, isItResolvedEnough))
            {
                return(true);
            }
            funcResult = null;
            return(false);
        }