Beispiel #1
0
        private static dynamic InstanceEval(object self, string eval, KumaScope scope)
        {
            if (!(self is KumaInstance instance))
            {
                return(null);
            }

            var        xexpression = string.Format("{0};", eval);
            var        res         = KumaParser.Parse(xexpression);
            Expression block;

            if (res != null)
            {
                scope["self"] = scope["super"] = instance;
                scope["<kuma_context_invokemember>"] = true;
                string selfName;
                var    selfScope = scope.SearchForObject(instance, out selfName);
                if (selfScope != null && selfName != null)
                {
                    scope["<kuma_context_selfscope>"] = selfScope;
                    scope["<kuma_context_selfname>"]  = selfName;
                }
                block = KumaExpression.KumaBlock(res);
                // We want eval'd expressions to execute in the current scope, not its own child scopes.  This ensures assignment evals work properly.
                ((BlockExpression)block).Scope = scope;
                ((BlockExpression)block).SetChildrenScopes(scope);
            }
            else
            {
                return(null);
            }
            var val = CompilerServices.CreateLambdaForExpression(block)();

            return(val);
        }
Beispiel #2
0
 public KumaTokenQueue(KumaParser parser, KumaLexer lexer, string sourceName)
 {
     Parser     = parser;
     _lexer     = lexer;
     SourceName = sourceName;
     _factory   = new KumaTokenFactory {
         Parser = parser
     };
     _tokens = new Queue <CommonToken>();
 }
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            var res = KumaParser.Parse(sourceUnit.GetCode(), sourceUnit);

            if (res != null)
            {
                Expression mainBlock = KumaExpression.KumaBlock(res);
                return(new KumaScriptCode(mainBlock, sourceUnit));
            }
            else
            {
                throw new SyntaxErrorException("Syntax error", sourceUnit, SourceSpan.None, 0, Severity.Error);
            }
        }
        public override Expression Reduce()
        {
            var ci = 0;

            Arguments.ForEach(arg => arg.Index = ci++);
            var realBody = new List <Expression>(((BlockExpression)Body).Body);

            if (Name == "new")
            {
                realBody.Add(Return(new List <FunctionArgument> {
                    new FunctionArgument(null, Variable(Constant("self")))
                }));
            }
            realBody.Add(Label(KumaParser.ReturnTarget, Constant(null, typeof(object))));

            return(Operation.SingletonDefine(typeof(KumaFunction), Constant(Singleton), Constant(Name),
                                             Constant(Arguments),
                                             Constant(KumaParser.CreateBlock(realBody)),
                                             Constant(Scope)));
        }
Beispiel #5
0
        private static dynamic Eval(string eval, KumaScope scope)
        {
            var xexpression = string.Format("{0};", eval);

            var        res = KumaParser.Parse(xexpression);
            Expression block;

            if (res != null)
            {
                block = KumaExpression.KumaBlock(res);
                // We want eval'd expressions to execute in the current scope, not its own child scopes.  This ensures assignment evals work properly.
                ((BlockExpression)block).Scope = scope;
                ((BlockExpression)block).SetChildrenScopes(scope);
            }
            else
            {
                return(null);
            }
            var val = CompilerServices.CreateLambdaForExpression(block)();

            return(val);
        }
Beispiel #6
0
        private static dynamic ClassEval(object self, string eval, KumaScope scope)
        {
            KumaClass @class;
            var       instance = self as KumaInstance;

            if (instance != null)
            {
                @class = instance.Class;
            }
            else
            {
                @class = self as KumaClass;
            }
            if (@class == null)
            {
                return(null);
            }

            var xexpression = string.Format("{0};", eval);

            var res = KumaParser.Parse(xexpression);

            return(res != null?RuntimeOperations.DefineCategory(@class, res.ToList(), scope) : null);
        }
Beispiel #7
0
        internal static dynamic String(object rawEval, object rawScope)
        {
            StringBuilder @new;
            var           eval = rawEval as String;

            var components = eval.Split(new[] { "#{" }, StringSplitOptions.None);

            if (components.Count() == 1)
            {
                return(new KumaString(eval));
            }
            @new = new StringBuilder(components[0]);
            for (var i = 1; i < components.Count(); i++)
            {
                var parts        = components[i].Split(new[] { "}" }, StringSplitOptions.None);
                var expression   = parts[0];
                var escapeString = false;
                if (expression != null && expression[0] == ':')
                {
                    escapeString = true;
                    expression   = expression.Substring(1);
                }
                if (expression != null)
                {
                    var scope       = (KumaScope)rawScope;
                    var xexpression = string.Format("{0};", expression);

                    var        res = KumaParser.Parse(xexpression);
                    Expression block;
                    if (res != null)
                    {
                        block = KumaExpression.KumaBlock(res);
                    }
                    else
                    {
                        return(null);
                    }
                    var myScope = new KumaScope();
                    var visitor = new VariableNameVisitor();
                    visitor.Visit(block);
                    visitor.VariableNames.ForEach(name => myScope[name] = scope[name]);
                    var val = CompilerServices.CompileExpression(block, myScope);
                    if (val != null)
                    {
                        string stringVal = val.ToString();
                        if (escapeString && val is string)
                        {
                            stringVal = string.Format("\"{0}\"", stringVal);
                        }
                        @new.Append(stringVal ?? "");
                    }
                    else
                    {
                        @new.Append(expression);
                    }
                }
                if (parts.Count() > 1)
                {
                    @new.Append(parts[1]);
                    var j = 2;
                    while (j < parts.Count())
                    {
                        @new.Append("}");
                        @new.Append(parts[j++]);
                    }
                }
            }

            return(new KumaString(@new.ToString()));
        }
 public void SetLines(KumaParser parser)
 {
     parser.Lines = _lines;
 }