-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lox.cs
182 lines (161 loc) · 4.64 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpLox
{
internal static class Lox
{
static bool HadError;
static bool HadRuntimeError;
static string[] Lines;
static int[] LineStartPositions;
static bool supressErrors;
private static bool replMode;
static readonly Interpreter Interpreter = new Interpreter();
static void Main(string[] args)
{
if (args.Length > 1)
{
Console.WriteLine("Usage: csharplox [script]");
System.Environment.Exit(64);
}
else if (args.Length == 1)
{
RunFile(args[0]);
}
else
{
RunPrompt();
}
}
private static void RunPrompt()
{
replMode = true;
LineStartPositions = new[] { 0 };
Lines = new string[1];
while (true)
{
Console.Write("> ");
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
{
break;
}
Lines[0] = line;
Run(line);
HadError = HadRuntimeError = false;
}
}
static void RunFile(string path)
{
path = Path.GetFullPath(path);
if (!File.Exists(path))
{
Console.WriteLine($"File not found at:\n{path}");
System.Environment.Exit(1);
}
Lines = File.ReadAllLines(path);
LineStartPositions = new int[Lines.Length];
int total = 0;
for (int i = 0; i < Lines.Length; i++)
{
string line = Lines[i];
LineStartPositions[i] = total;
total += line.Length + 1;
}
Run(string.Join("\n", Lines));
if (HadError)
{
System.Environment.ExitCode = 65;
}
if (HadRuntimeError)
{
System.Environment.ExitCode = 70;
}
}
static void Run(string source)
{
var scanner = new Scanner(source);
List<Token> tokens = scanner.ScanTokens();
var parser = new Parser(tokens);
if (replMode)
{
try
{
supressErrors = true;
if (parser.ParseAsExpr() is Expr expr && !HadError)
{
Interpreter.Interpret(expr);
return;
}
}
finally
{
supressErrors = false;
}
}
List<Stmnt> stmnts = parser.Parse();
if (HadError)
{
return;
}
new Resolver(Interpreter).Resolve(stmnts);
if (HadError)
{
return;
}
Interpreter.Interpret(stmnts);
}
public static void Error(int pos, string msg)
{
Report(pos, msg);
}
static void Report(int pos, string msg, bool runtime = false)
{
if (supressErrors)
{
return;
}
int line = 0;
for (int i = 0; i < LineStartPositions.Length; i++)
{
if (LineStartPositions[i] > pos)
{
line = i;
}
}
if (line == 0)
{
line = Lines.Length;
}
int col = pos - LineStartPositions[line - 1];
Console.WriteLine($"{(runtime ? "Runtime ": "")}Error: {msg}");
if (!(runtime && replMode))
{
Console.WriteLine();
string preLine = $" {line} | ";
Console.WriteLine($"{preLine}{Lines[line - 1]}");
Console.WriteLine($"{new string(' ', preLine.Length + col)}^-- Here");
}
if (runtime)
{
HadRuntimeError = true;
}
else
{
HadError = true;
}
}
public static void Error(Token token, string msg)
{
Report(token.Pos, msg);
}
public static void RuntimeError(RuntimeError error)
{
Report(error.Token.Pos, error.Message, true);
}
}
}