-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (88 loc) · 2.25 KB
/
Program.cs
File metadata and controls
105 lines (88 loc) · 2.25 KB
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
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using TypeScriptNative;
using TypeScriptNative.Scan;
using TypeScriptNative.Parse;
using TypeScriptNative.AST;
using TypeScriptNative.Interpret;
using TypeScriptNative.Passes;
namespace prog_lang
{
enum Operation
{
INTERPRETER,
COMPILER
}
class Program
{
private static readonly Interpreter interpreter = new();
private static void Run(String source, String path)
{
Scanner scanner = new(source);
List<Token> tokens = scanner.scanTokens();
//scanner.debug();
Parser parser = new(tokens, path);
List<Stmt> statements = parser.parse();
// Stop if there was a syntax error.
if (ErrorReport.hadError) return;
Resolver resolver = new(interpreter);
resolver.resolve(statements);
//// Stop if there was a resolution error.
if (ErrorReport.hadError) return;
interpreter.interpret(statements);
}
private static void RunFile(String path)
{
if (!File.Exists(path)) {
Console.WriteLine("Provided file was not found.");
}
else
{
byte[] bytes = File.ReadAllBytes(path);
var directory = Path.GetDirectoryName(path);
Console.WriteLine("Running from the following path: " + directory);
Run(Encoding.Default.GetString(bytes), directory);
}
}
private static void RunPrompt()
{
var path = AppContext.BaseDirectory;
Console.WriteLine("Running from the following path: " + path);
while (true) // Loop indefinitely
{
Console.Write("> "); // Prompt
string line = Console.ReadLine(); // Get string from user
if (line == null) // Check string
{
break;
}
Run(line, path);
ErrorReport.hadError = false;
}
}
static int Main(string[] args)
{
Console.WriteLine("============================================");
Console.WriteLine("|| ::Welcome:: ||");
Console.WriteLine("|| TypeScript Native PoC v0.1 ||");
Console.WriteLine("============================================");
if (args.Length > 1)
{
Console.WriteLine("Usage: jlox [script]");
return 64;
}
else if (args.Length == 1)
{
RunFile(args[0]);
}
else
{
RunPrompt();
}
Console.WriteLine("Exiting...\n");
return 0;
}
}
}