/// <summary> /// Evals the specified lisp code. /// An exception may occure if the lisp code is invalid. /// </summary> /// <param name="lispCode">The lisp code.</param> /// <param name="scope">The scope.</param> /// <param name="moduleName">The module name and path.</param> /// <param name="tracing">if set to <c>true</c> [tracing].</param> /// <param name="onlyMacroExpand">if set to <c>true</c> [macro expanding].</param> /// <param name="nativeItems">The dictionary with native items.</param> /// <returns>The result of the script evaluation</returns> public static LispVariant Eval(string lispCode, LispScope scope = null, string moduleName = null, bool tracing = false, bool onlyMacroExpand = false, Dictionary <string, object> nativeItems = null) { // first create global scope, needed for macro expanding var currentScope = scope ?? LispEnvironment.CreateDefaultScope(); currentScope.ModuleName = moduleName; currentScope.Tracing = tracing; RegisterNativeObjects(nativeItems, currentScope); int offset; string code = LispUtils.DecorateWithBlock(lispCode, out offset); var ast = LispParser.Parse(code, offset, currentScope); #if ENABLE_COMPILE_TIME_MACROS var expandedAst = LispInterpreter.ExpandMacros(ast, currentScope); #else var expandedAst = ast; #endif LispVariant result = null; if (onlyMacroExpand) { result = new LispVariant(expandedAst); } else { result = LispInterpreter.EvalAst(expandedAst, currentScope); } return(result); }
/// <summary> /// Compiles the specified lisp code into C# code. /// </summary> /// <param name="code">The lisp code.</param> /// <returns>The C# code packed into a LispVariant</returns> public /*static*/ LispVariant CompileToCsCode(string code) { var globalScope = LispEnvironment.CreateDefaultScope(); int offset; code = LispUtils.DecorateWithBlock(code, out offset); var ast = LispParser.Parse(code, offset, globalScope); //var expandedAst = ExpandMacros(ast, globalScope); var compileResult = Compile(ast, globalScope, " ", "__return__", "scope"); var csCode = "namespace CsLisp\n{\nusing System;\nclass CompiledLisp\n{\n // functions:\n" + ShiftLines(compileResult.Item2, " ") + "\n public static LispVariant LispMain(string[] args)\n {\n var scope = new LispScope();\n LispVariant __return__;\n\n" + ShiftLines(compileResult.Item1, " ") + " return __return__;\n }\n\n public static void Main(string[] args)\n {\n var startTickCount = Environment.TickCount;\n LispMain(args);\n Console.WriteLine(string.Format(\"Execution time = {0} s\", (Environment.TickCount - startTickCount)*0.001));\n }\n}\n}"; return(new LispVariant(csCode)); }