// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text.RegularExpressions; using System.Reflection; using System.CodeDom.Compiler; using Microsoft.JScript; namespace E { public interface IJS { string Eval(string expr); } public class JS { private static IJS engine; static JS() { string src = "import System; import E; public class C implements IJS { public function Eval(expr : String) : String { return eval(expr); } }"; CodeDomProvider comp = new JScriptCodeProvider(); CompilerParameters param = new CompilerParameters(); param.GenerateInMemory = true; param.ReferencedAssemblies.Add("System.dll"); param.ReferencedAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly().Location); CompilerResults res = comp.CompileAssemblyFromSource(param, src); Assembly asm = res.CompiledAssembly; engine = (IJS)asm.CreateInstance("C"); } public static string Eval(string expr) { return engine.Eval(expr); } } public class Program { private static readonly Regex re = new Regex("(?:@@)(.*?)(?:@@)", RegexOptions.Compiled); public static string Process(string s) { string res = s; foreach(Match m in re.Matches(s)) { res = res.Replace(m.Groups[0].Value, JS.Eval(m.Groups[1].Value)); } return res; } public static void Main(string[] args) { Console.WriteLine(Process("")); Console.WriteLine(Process("")); } } }