public override object Run(Scope scope) { var body = (Body as BlockExpression); body.Scope.MergeWithScope(Silver.Globals); body.Scope.MergeWithScope(scope); var visitor = new VariableNameVisitor(); visitor.Visit(body); body.SetChildrenScopes(body.Scope); dynamic block = CompilerServices.CreateLambdaForExpression(body); dynamic 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 SilverInstance) { var so = (SilverInstance) res; if (so is SilverBoxedInstance) { res = ((SilverBoxedInstance) so).BoxedObject; } } else if (res is SilverNumber) { res = SilverNumber.Convert(res); } else if (res is SilverString) { res = (string) res; } else if (res is SilverArray) { res = ConvertElements((SilverArray)res); } else if (res is SilverDictionary) { res = ConvertElements((SilverDictionary)res); } body.Scope.MergeIntoScope(scope); return res; }
internal static dynamic String(object rawEval, object rawScope) { StringBuilder @new; var eval = rawEval as String; string[] components = eval.Split(new[] {"#{"}, StringSplitOptions.None); if (components.Count() == 1) { return new SilverString(eval); } @new = new StringBuilder(components[0]); for (int i = 1; i < components.Count(); i++) { string[] parts = components[i].Split(new[] {"}"}, StringSplitOptions.None); string expression = parts[0]; bool escapeString = false; if (expression != null && expression[0] == ':') { escapeString = true; expression = expression.Substring(1); } if (expression != null) { var scope = (SilverScope) rawScope; string xexpression = string.Format("{0};", expression); var lexer = new SilverLexer("source", xexpression); var stream = new CommonTokenStream(lexer.Queue); var parser = new SilverParser(stream); lexer.SetLines(parser); Expression[] res = parser.program(); Expression block; if (res != null) { block = AstExpression.Block(res); } else { return null; } var myScope = new SilverScope(); var visitor = new VariableNameVisitor(); visitor.Visit(block); visitor.VariableNames.ForEach(name => myScope[name] = scope[name]); dynamic 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]); int j = 2; while (j < parts.Count()) { @new.Append("}"); @new.Append(parts[j++]); } } } return new SilverString(@new.ToString()); }