-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatMain.cs
138 lines (124 loc) · 4.6 KB
/
CatMain.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
134
135
136
137
138
/// Dedicated to the public domain by Christopher Diggins
/// http://creativecommons.org/licenses/publicdomain/
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Timers;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
using System.Drawing;
namespace Cat
{
public class MainClass
{
static List<string> gsInputFiles = new List<string>();
static Executor exec = new Executor();
// TODO: LOWPRI: reintroduce sessions
//static string gsSessionFile = gsDataFolder + "\\session.cat";
static void Main(string[] a)
{
exec.RegisterType(typeof(CatMetaCommands));
exec.RegisterType(typeof(Primitives));
if (Config.gbShowWelcome)
{
WriteLine("Welcome to the Cat programming language =^,,^=");
WriteLine("version " + Config.gsVersion);
WriteLine("by Christopher Diggins");
WriteLine("licensed under the MIT License 1.0");
WriteLine("http://www.cat-language.com");
WriteLine("");
WriteLine("for help, type in #help followed by the enter key");
WriteLine("to exit, type in #exit followed by the enter key");
WriteLine("");
}
if (!Directory.Exists(Config.gsDataFolder))
{
try
{
DirectoryInfo di = Directory.CreateDirectory(Config.gsDataFolder);
if (di == null)
throw new Exception("Failed to create directory");
}
catch (Exception e)
{
WriteLine("Failed to create application folder: " + e.Message);
WriteLine("I will be unable to save session data or compiled output");
}
}
try
{
exec.LoadModule("everything.cat");
foreach (string s in a)
exec.LoadModule(s);
// main execution loop
while (true)
{
Prompt();
string s = Console.ReadLine();
if (s.Trim().Equals("#exit") || s.Trim().Equals("#x"))
break;
Output.LogLine(s);
if (s.Length > 0)
{
DateTime begin = DateTime.Now;
try
{
exec.Execute(s + '\n');
TimeSpan elapsed = DateTime.Now - begin;
if (Config.gbOutputTimeElapsed)
WriteLine("Time elapsed in msec " + elapsed.TotalMilliseconds.ToString("F"));
// Politely ask graphics window to redraw if needed
}
catch (Exception e)
{
WriteLine("exception occurred: " + e.Message);
}
if (Config.gbOutputStack)
exec.OutputStack();
#if (!NOGRAPHICS)
WindowGDI.Invalidate();
#endif
}
}
}
catch (Exception e)
{
WriteLine("exception: " + e.Message);
}
SaveTranscript(Path.GetTempFileName());
WriteLine("Press any key to exit ...");
Console.ReadKey();
}
// These functions were added after the fact. If I am bored some day I should
// remove them and redirect all calls directly to "Output"
#region console/loggging output function
public static void Write(object o)
{
Output.Write(o);
}
public static void WriteLine(object o)
{
Output.WriteLine(o);
}
public static void Write(string s)
{
Output.Write(s);
}
public static void WriteLine(string s)
{
Output.WriteLine(s);
}
public static void Prompt()
{
Output.Write(">> ");
}
public static void SaveTranscript(string sTranscript)
{
Output.SaveTranscript(sTranscript);
}
#endregion
}
}