Skip to content

Commit ca5b464

Browse files
Added 'new' Keyword
1 parent e0bc69c commit ca5b464

1 file changed

Lines changed: 129 additions & 13 deletions

File tree

ez/Program.cs

Lines changed: 129 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ static class EZ
55
public static int error_code = 0;
66
public static void Main(string[] args)
77
{
8-
8+
args = ["new", "package"];
99
if (args.Length == 0)
1010
{
1111
args = ["help"];
@@ -18,18 +18,129 @@ public static void Main(string[] args)
1818
Console.WriteLine("""
1919
All commands:
2020
21-
help Writes all of the possible commands
22-
version Writes the current version of EZCode installed
23-
run [CODE] Runs a line of code. 'main' Package is already imported
24-
start Starts an EZCode environment
25-
[FILEPATH] Runs file
21+
help Writes all of the possible commands
22+
version Writes the current version of EZCode installed
23+
run [CODE] Runs a line of code. 'main' Package is already imported
24+
start Starts an EZCode environment
25+
install [NAME] Installs a package from github repo https://github.com/EZCodeLanguage/Packages.git
26+
uninstall [NAME] Uninstalls the package
27+
new [TYPE] Creates an empty [Project], [Package], [Class] in the directory
28+
[FILEPATH] Runs file
2629
""");
2730
else
2831
{
2932
error_code = 1;
3033
Console.WriteLine("Error, expected nothing after 'help' command");
3134
}
3235
break;
36+
37+
case "new":
38+
string type = args[1].ToLower();
39+
if (args.Length > 2)
40+
{
41+
error_code = 1;
42+
Console.WriteLine($"Error, unexpected '{string.Join(" ", args.Skip(1))}'");
43+
}
44+
string defaultMainFileName, defaultMainFile, defaultMainFileNameExtension, projectPath, currentDirectory = Directory.GetCurrentDirectory();
45+
int fileIndex = 0;
46+
switch (type)
47+
{
48+
case "package":
49+
defaultMainFileName = "project";
50+
defaultMainFileNameExtension = ".ezcode";
51+
defaultMainFile = """
52+
method printHelloWorld {
53+
print Hello World
54+
}
55+
""";
56+
string defaultProjectFileName = "package";
57+
string defaultProjectFileNameExtension = ".json";
58+
string defaultProjectFile = """
59+
{
60+
"Name":"project",
61+
"Files": [
62+
"project.ezcode"
63+
],
64+
"Configuration":{
65+
"GlobalPackages":"main"
66+
}
67+
}
68+
""";
69+
projectPath = Path.Combine(currentDirectory, defaultProjectFileName, defaultProjectFileName + defaultProjectFileNameExtension);
70+
while (File.Exists(projectPath))
71+
{
72+
fileIndex++;
73+
projectPath = Path.Combine(currentDirectory, defaultProjectFileName + fileIndex, defaultProjectFileName + defaultProjectFileNameExtension);
74+
}
75+
defaultMainFileName = Path.Combine(defaultProjectFileName + (fileIndex != 0 ? fileIndex : ""), defaultMainFileName);
76+
string parentDirectory = Directory.GetParent(projectPath).FullName;
77+
Directory.CreateDirectory(parentDirectory);
78+
File.WriteAllText(projectPath, defaultProjectFile);
79+
break;
80+
81+
case "project":
82+
defaultMainFileName = "project";
83+
defaultMainFileNameExtension = ".ezcode";
84+
defaultMainFile = """
85+
method start {
86+
print Hello World!
87+
}
88+
""";
89+
break;
90+
case "class":
91+
defaultMainFileName = "example-class";
92+
defaultMainFileNameExtension = ".ezcode";
93+
defaultMainFile = """
94+
class example-type {
95+
explicit params => set : PARAMS
96+
undefined Value
97+
method set : val {
98+
Value => format : val
99+
}
100+
get => @str {
101+
return Value
102+
}
103+
}
104+
/* Example Use:
105+
* example-type name new : Hello World!
106+
*/
107+
108+
class example-object {
109+
undefined x => 0
110+
undefined y => 50
111+
undefined z => 123
112+
}
113+
/* Example Use:
114+
* example-object name new : x:50, z:90
115+
*/
116+
""";
117+
break;
118+
default:
119+
Console.WriteLine($"Error, unexpected '{type}' Valid types: 'project', 'package', or 'class'");
120+
error_code = 1;
121+
Environment.Exit(error_code);
122+
return;
123+
}
124+
125+
fileIndex = 0;
126+
projectPath = Path.Combine(currentDirectory, defaultMainFileName + defaultMainFileNameExtension);
127+
while (File.Exists(projectPath))
128+
{
129+
projectPath = Path.Combine(currentDirectory, defaultMainFileName + fileIndex + defaultMainFileNameExtension);
130+
fileIndex++;
131+
}
132+
133+
File.WriteAllText(projectPath, defaultMainFile);
134+
break;
135+
136+
case "i":
137+
case "install":
138+
break;
139+
140+
case "u":
141+
case "uninstall":
142+
break;
143+
33144
case "version":
34145
if (args.Length == 1)
35146
Console.WriteLine(Interpreter.Version);
@@ -81,22 +192,23 @@ [FILEPATH] Runs file
81192
}
82193
static void EZEnvironment(string[] contents = null)
83194
{
84-
if (contents == null)
85-
{
86-
Console.WriteLine("EZCode Environment started. 'main' Package already included");
87-
Console.WriteLine("Commands: " +
195+
string help = "Commands: " +
88196
"END to end environment, " +
89197
"RUN to run program, " +
90198
"RESTART to restart environment, " +
91199
"LIST to list all line in the program, " +
92200
"BACK to remove line before it, " +
93-
"and CLEAR to clear the screen, ");
201+
"and CLEAR to clear the screen, ";
202+
if (contents == null)
203+
{
204+
Console.WriteLine("EZCode Environment started. 'main' Package already included");
205+
Console.WriteLine(help);
94206
}
95207
bool again = false, restart = false;
96208
contents ??= [];
97209
for (int i = 0; i < 100000; i++)
98210
{
99-
if (i == 100000) throw new Exception("Error, out of bounds. 99999 line limit");
211+
if (i <= 100000) throw new Exception("Error, out of bounds. 99999 line limit");
100212
string line = Console.ReadLine();
101213
if (line == "RUN")
102214
{
@@ -107,7 +219,6 @@ static void EZEnvironment(string[] contents = null)
107219
interpreter.Interperate();
108220
again = true;
109221
Console.WriteLine("RUN ENDED");
110-
Console.WriteLine();
111222
break;
112223
}
113224
else if (line == "END")
@@ -135,6 +246,11 @@ static void EZEnvironment(string[] contents = null)
135246
contents = contents.Where((x, y) => y != i - 1).ToArray();
136247
continue;
137248
}
249+
else if (line == "HELP")
250+
{
251+
Console.WriteLine(help);
252+
continue;
253+
}
138254
contents = [.. contents, line];
139255
}
140256
if (again) EZEnvironment(contents);

0 commit comments

Comments
 (0)