Ejemplo n.º 1
0
        public override object Run(Scope scope)
        {
            var body = (Body as BlockExpression);

            body.Scope.MergeWithScope(Nova.Globals);
            body.Scope.MergeWithScope(scope);

            var visitor = new VariableNameVisitor();

            visitor.Visit(body);

            body.SetChildrenScopes(body.Scope);

            var block = CompilerServices.CreateLambdaForExpression(body);
            var res   = block();

            if (res is Symbol)
            {
                var symval = new BlockExpression(new List <Expression> {
                    new VariableExpression(res)
                }, body.Scope);
                res = CompilerServices.CreateLambdaForExpression(symval)();
            }
            else if (res is NovaInstance)
            {
                var so = (NovaInstance)res;
                if (so is NovaBoxedInstance)
                {
                    res = ((NovaBoxedInstance)so).BoxedObject;
                }
            }
            else if (res is NovaNumber)
            {
                res = NovaNumber.Convert(res);
            }
            else if (res is NovaString)
            {
                res = (string)res;
            }
            else if (res is NovaArray)
            {
                res = ConvertElements((NovaArray)res);
            }
            else if (res is NovaDictionary)
            {
                res = ConvertElements((NovaDictionary)res);
            }

            body.Scope.MergeIntoScope(scope);

            return(res);
        }
        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 NovaString(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       = (NovaScope)rawScope;
                    var xexpression = string.Format("{0};", expression);

                    var        res = NovaParser.Parse(xexpression);
                    Expression block;
                    if (res != null)
                    {
                        block = NovaExpression.NovaBlock(res);
                    }
                    else
                    {
                        return(null);
                    }
                    var myScope = new NovaScope();
                    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 NovaString(@new.ToString()));
        }