-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lox.cs
210 lines (176 loc) · 7.57 KB
/
Lox.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cslox
{
public static class Lox
{
static Interpreter interpreter = new Interpreter();
static bool ErrorFlag = false;
static bool RuntimeErrorFlag = false;
static int Main(string[] args)
{
Expr expression = new Expr.Binary(
new Expr.Unary(
new Token(TokenType.MINUS, "-", null, 1, 0),
new Expr.Literal(123)),
new Token(TokenType.STAR, "*", null, 1, 0),
new Expr.Grouping(
new Expr.Literal(45.67)));
Console.WriteLine(new AstPrinter().Print(expression));
int exitCode = 0;
if (args.Length > 1)
{
Console.WriteLine("Usage cslox: [script]");
exitCode = (int)ErrorCode.EX_USAGE;
}
else if (args.Length == 1)
{
exitCode = RunFile(args[0]);
}
else
{
exitCode = RunPrompt();
}
return exitCode;
}
static int RunFile(string script)
{
string text = File.ReadAllText(script);
Run(text);
if (ErrorFlag)
{
// Syntax error
return (int)ErrorCode.EX_DATAERR;
}
else if (RuntimeErrorFlag)
{
// Runtime error
return (int)ErrorCode.EX_SOFTWARE;
}
return (int)ErrorCode.SUCCESS;
}
static int RunPrompt()
{
while (true)
{
Console.Write("> ");
string text = Console.ReadLine();
if (String.Equals(text, "quit") || String.Equals(text, "exit"))
{
break;
}
ErrorFlag = false;
RuntimeErrorFlag = false;
Run(text);
}
return 0;
}
static void Run(string source)
{
Scanner scanner = new Scanner(source);
List<Token> tokens = scanner.ScanTokens();
#if false
// DEBUG: Print tokens
Console.WriteLine("\nTokens:");
Console.WriteLine("-------");
foreach (Token token in tokens)
{
Console.WriteLine(token);
}
#endif
Parser parser = new Parser(tokens);
Expr expr = parser.Parse();
// Syntax error
if (ErrorFlag)
{
return;
}
#if false
// DEBUG: Print AST
Console.WriteLine("\nAST:");
Console.WriteLine("-------");
AstPrinter printer = new AstPrinter();
Console.WriteLine(printer.Print(expr));
Console.WriteLine();
#endif
interpreter.Interpret(expr);
}
public static void Error(int line, int column, string message)
{
ReportError(line, column, "", message);
}
public static void Error(Token token, string message)
{
if (token.type == TokenType.EOF)
{
ReportError(token.line, token.column, " at end", message);
}
else
{
ReportError(token.line, token.column, $" at '{token.lexeme}'", message);
}
}
public static void RuntimeError(RuntimeError error)
{
Console.WriteLine($"{error.Message}\n[line {error.token.line}]");
RuntimeErrorFlag = true;
}
private static void ReportError(int line, int column, string where, string message)
{
Console.WriteLine($"[line {line}:column {column}] Error {where}: {message}");
ErrorFlag = true;
}
enum ErrorCode
{
SUCCESS = 0,
EX_USAGE = 64, // The command was used incorrectly, e.g., with the
// wrong number of arguments, a bad flag, a bad syntax
// in a parameter, or whatever.
EX_DATAERR = 65, // The input data was incorrect in some way. This
// should only be used for user's data and not system
// files.
EX_NOINPUT = 66, // An input file (not a system file) did not exist or
// was not readable. This could also include errors
// like ``No message'' to a mailer (if it cared to
// catch it).
EX_NOUSER = 67, // The user specified did not exist. This might be
// used for mail addresses or remote logins.
EX_NOHOST = 68, // The host specified did not exist. This is used in
// mail addresses or network requests.
EX_UNAVAILABLE = 69, // A service is unavailable. This can occur if a sup
// port program or file does not exist. This can also
// be used as a catchall message when something you
// wanted to do doesn't work, but you don't know why.
EX_SOFTWARE = 70, // An internal software error has been detected. This
// should be limited to non-operating system related
// errors as possible.
EX_OSERR = 71, // An operating system error has been detected. This
// is intended to be used for such things as ``cannot
// fork'', ``cannot create pipe'', or the like. It
// includes things like getuid returning a user that
// does not exist in the passwd file.
EX_OSFILE = 72, // Some system file (e.g., /etc/passwd, /var/run/utmp,
// etc.) does not exist, cannot be opened, or has some
// sort of error (e.g., syntax error).
EX_CANTCREAT = 73, // A (user specified) output file cannot be created.
EX_IOERR = 74, // An error occurred while doing I/O on some file.
EX_TEMPFAIL = 75, // Temporary failure, indicating something that is not
// really an error. In sendmail, this means that a
// mailer (e.g.) could not create a connection, and
// the request should be reattempted later.
EX_PROTOCOL = 76, // The remote system returned something that was ``not
// possible'' during a protocol exchange.
EX_NOPERM = 77, // You did not have sufficient permission to perform
// the operation. This is not intended for file sys
// tem problems, which should use EX_NOINPUT or
// EX_CANTCREAT, but rather for higher level permis
// sions.
EX_CONFIG = 78, // Something was found in an unconfigured or miscon
// figured state.
}
}
}