-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProgram.cs
133 lines (112 loc) · 3.95 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using VSharp;
public class Program
{
public static string _Path = string.Empty;
private const string Version = "0.4.1 LTS";
public static void Main(string[] args)
{
if (args.Length == 0)
{
PrintLogo();
PrintUsage();
return;
}
switch (args[0])
{
case "--version":
case "-v":
Console.WriteLine($"VSharp version {Version}");
break;
case "run":
if (args.Length < 2)
{
Console.WriteLine("ERROR: No file specified to run.");
return;
}
RunFile(args[1]);
break;
case "--help":
case "-h":
PrintLogo();
PrintUsage();
break;
case "info":
PrintInfo();
break;
case "new":
if (args.Length < 2)
{
Console.WriteLine("ERROR: No project name specified.");
return;
}
CreateNewProject(args[1]);
break;
default:
Console.WriteLine($"ERROR: Unknown command '{args[0]}'\n");
PrintUsage();
break;
}
}
private static void PrintLogo()
{
Console.WriteLine("GitHub: https://github.com/funcieqDEV/VSharp");
Console.WriteLine("YouTube: www.youtube.com/@funcieq_");
Console.WriteLine();
}
private static void PrintUsage()
{
Console.WriteLine("Usage:");
Console.WriteLine(" --version, -v Display VSharp version");
Console.WriteLine(" run <file> Run the specified script file");
Console.WriteLine(" new <name> Create a new VSharp project");
Console.WriteLine(" --help, -h Show this help message");
Console.WriteLine(" info Display information about VSharp");
}
private static void PrintInfo()
{
Console.WriteLine("VSharp - A simple scripting language interpreter.");
Console.WriteLine("Version: " + Version);
}
private static void CreateNewProject(string projectName)
{
string projectPath = Path.Combine(Directory.GetCurrentDirectory(), projectName);
if (Directory.Exists(projectPath))
{
Console.WriteLine($"ERROR: Project '{projectName}' already exists.");
return;
}
Directory.CreateDirectory(projectPath);
File.WriteAllText(Path.Combine(projectPath, "main.vshrp"), "// Entry point for VSharp project");
Console.WriteLine($"New VSharp project '{projectName}' created successfully.");
}
private static void RunFile(string filePath)
{
string fullPath = Path.Combine(Directory.GetCurrentDirectory(), filePath);
if (!File.Exists(fullPath))
{
Console.WriteLine($"ERROR: File '{fullPath}' not found.");
return;
}
try
{
string initialDir = Environment.CurrentDirectory;
Environment.CurrentDirectory = new FileInfo(fullPath).Directory!.FullName;
string input = File.ReadAllText(fullPath);
_Path = fullPath;
Lexer lexer = new Lexer(input);
List<Token> tokens = lexer.Tokenize();
Parser parser = new Parser(tokens);
ProgramNode program = parser.Parse();
Interpreter interpreter = new Interpreter();
interpreter.Interpret(program);
Environment.CurrentDirectory = initialDir;
}
catch (Exception e)
{
Console.WriteLine($"ERROR: {e.Message}");
}
}
}