public BlockExpression Compile(string rawsource) { var lexer = new SilverLexer("source", rawsource); var stream = new CommonTokenStream(lexer.Queue); var parser = new SilverParser(stream); lexer.SetLines(parser); return AstExpression.Block(parser.program()); }
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()); }
private static dynamic InstanceEval(object self, string eval, SilverScope scope) { string xexpression = string.Format("{0};", eval); var lexer = new SilverLexer("source", xexpression); var stream = new CommonTokenStream(lexer.Queue); var parser = new SilverParser(stream); lexer.SetLines(parser); var instance = self as SilverInstance; if (instance == null) { return null; } Expression[] res = parser.program(); Expression block; if (res != null) { scope["self"] = scope["super"] = instance; scope["<ironsilver_context_invokemember>"] = true; string selfName; var selfScope = scope.SearchForObject(instance, out selfName); if (selfScope != null && selfName != null) { scope["<ironsilver_context_selfscope>"] = selfScope; scope["<ironsilver_context_selfname>"] = selfName; } block = AstExpression.Block(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; } dynamic val = CompilerServices.CreateLambdaForExpression(block)(); return val; }
private static dynamic ClassEval(object self, string eval, SilverScope scope) { SilverClass @class; var instance = self as SilverInstance; if (instance != null) { @class = instance.Class; } else { @class = self as SilverClass; } if (@class == null) return null; var xexpression = string.Format("{0};", eval); var lexer = new SilverLexer("source", xexpression); var stream = new CommonTokenStream(lexer.Queue); var parser = new SilverParser(stream); lexer.SetLines(parser); var res = parser.program(); return res != null ? RuntimeOperations.DefineCategory(@class, res.ToList(), scope) : null; }
private static dynamic Eval(string eval, SilverScope scope) { var xexpression = string.Format("{0};", eval); 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); // 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; } dynamic val = CompilerServices.CreateLambdaForExpression(block)(); return val; }