aboutsummaryrefslogtreecommitdiff
path: root/ClojureCLR/Clojure/Simple.Console/SimpleConsole.cs
blob: 357cd02df188757302a700c5854d85ad4a34537f (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using clojure.lang;
using System.Diagnostics;
using System.IO;
using Microsoft.Linq.Expressions;
using Microsoft.Scripting.Generation;

namespace clojure.console
{
    class SimpleConsole
    {
        static void Main(string[] args)
        {
            new SimpleConsole().Run();
        }



        private void Run()
        {
            Initialize();
            RunInteractiveLoop();
        }


        private void Initialize()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            Var.pushThreadBindings(
                RT.map(RT.CURRENT_NS, RT.CURRENT_NS.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();
                }
                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.CURRENT_NS, RT.CURRENT_NS.deref(),
                RT.WARN_ON_REFLECTION, RT.WARN_ON_REFLECTION.deref(),
                RT.PRINT_META, RT.PRINT_META.deref(),
                RT.PRINT_LENGTH, RT.PRINT_LENGTH.deref(),
                RT.PRINT_LEVEL, RT.PRINT_LEVEL.deref(),
                Compiler.COMPILE_PATH, Environment.GetEnvironmentVariable("clojure.compile.path" ?? "classes")
                ));

            try
            {
            LoadFromStream(new LineNumberingTextReader(Console.In), true);
            }
            finally
            {
                Var.popThreadBindings();
            }
        }


    }
}