public override object Run(Scope scope) { var body = (Body as BlockExpression); body.Scope.MergeWithScope(Kuma.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 KumaInstance) { var so = (KumaInstance)res; if (so is KumaBoxedInstance) { res = ((KumaBoxedInstance)so).BoxedObject; } } else if (res is KumaNumber) { res = KumaNumber.Convert(res); } else if (res is KumaString) { res = (string)res; } else if (res is KumaArray) { res = ConvertElements((KumaArray)res); } else if (res is KumaDictionary) { res = ConvertElements((KumaDictionary)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 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())); }