/
SimpleConsole.cs
133 lines (112 loc) · 3.94 KB
/
SimpleConsole.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/**
* Author: David Miller
**/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using clojure.lang;
using System.Diagnostics;
using System.IO;
#if CLR2
using Microsoft.Scripting.Ast;
#else
using System.Linq.Expressions;
#endif
//using Microsoft.Scripting.Generation;
namespace clojure.console
{
class SimpleConsole
{
static void Main(string[] args)
{
new SimpleConsole().Run();
}
private void Run()
{
Console.WriteLine(
@"This application is for testing purposes only.
It loads a very minimal test environment.
Please use Clojure.Main for everyday chores."
);
Initialize();
RunInteractiveLoop();
}
private void Initialize()
{
Stopwatch sw = new Stopwatch();
sw.Start();
Var.pushThreadBindings(
RT.map(RT.CurrentNSVar, RT.CurrentNSVar.deref()));
try
{
//LoadFromStream(new StringReader(clojure.lang.Properties.Resources.core),false);
//RT.load("/core");
//LoadFromStream(new StringReader(clojure.lang.Properties.Resources.core_print), false);
//LoadFromStream(new StringReader(clojure.lang.Properties.Resources.test), false);
}
finally
{
Var.popThreadBindings();
}
sw.Stop();
Console.WriteLine("Loading took {0} milliseconds.", sw.ElapsedMilliseconds);
}
public object LoadFromStream(PushbackTextReader rdr, bool addPrint)
{
object ret = null;
object eofVal = new object();
object form;
while ((form = LispReader.read(rdr, false, eofVal, false)) != eofVal)
{
try
{
//LambdaExpression ast = Compiler.GenerateLambda(form, addPrint);
//ret = ast.Compile().DynamicInvoke();
ret = Compiler.eval(form);
RT.print(ret, Console.Out);
}
catch (Exception ex)
{
if (addPrint)
{
Exception root = ex;
while (root.InnerException != null)
root = root.InnerException;
Console.WriteLine("Error evaluating {0}: {1}", form, root.Message);
Console.WriteLine(root.StackTrace);
}
}
}
return ret;
}
private void RunInteractiveLoop()
{
Var.pushThreadBindings(RT.map(
RT.CurrentNSVar, RT.CurrentNSVar.deref(),
RT.WarnOnReflectionVar, RT.WarnOnReflectionVar.deref(),
RT.PrintMetaVar, RT.PrintMetaVar.deref(),
//RT.PRINT_LENGTH, RT.PRINT_LENGTH.deref(),
//RT.PRINT_LEVEL, RT.PRINT_LEVEL.deref(),
Compiler.CompilePathVar, Environment.GetEnvironmentVariable("clojure.compile.path" ?? "classes")
));
try
{
LoadFromStream(new LineNumberingTextReader(Console.In), true);
}
finally
{
Var.popThreadBindings();
}
}
}
}