-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
94 lines (77 loc) · 3.14 KB
/
Copy pathProgram.cs
File metadata and controls
94 lines (77 loc) · 3.14 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
using System;
using System.IO;
using System.Linq;
using TAScript.Runnable;
using TAScript.Player;
namespace TAScript
{
class Program
{
// DATA //
// Constants
public static ConsoleColor ERROR_COLOUR = ConsoleColor.Red;
public static ConsoleColor PROMPT_COLOUR = ConsoleColor.Cyan;
public static ConsoleColor INFO_COLOUR = ConsoleColor.White;
public static ConsoleColor DEBUG_COLOUR = ConsoleColor.Yellow;
// FUNCTIONS //
// Main
static void Main(string[] args)
{
// Sends the program debugger to the TAScript debugger
DebugLogger.debugDisplayer = DebugLog;
// Prints prompt
ColourConsole.WriteLine("Enter a text file to compile into a game. " +
"\nThis program will output all compiler data as it runs.", INFO_COLOUR);
ColourConsole.WriteLine("Enter text:", PROMPT_COLOUR);
// Gets the user input
string inputFilePath = Path.Combine(Console.ReadLine());
DebugLogger.DebugLog(inputFilePath, false);
// Creates the compiler w/ default options
Compiler.Compiler compilerToUse = new Compiler.Compiler();
// Tries compiling
Game compiledGame = CompileGameFromFile(inputFilePath, compilerToUse);
// Tries playing (this won't actually do anything right now)
if(compiledGame != null)
{
new GameController().PlayGame(compiledGame);
}
else
{
ColourConsole.WriteLine("[Main] Your input file could not be compiled!", ERROR_COLOUR);
}
}
// Debugging
public static void DebugLog(string text, bool isError)
{
ColourConsole.WriteLine(text, (isError ? ERROR_COLOUR : DEBUG_COLOUR));
}
// Compilation
private static Game CompileGameFromFile(string absoluteFilePath, Compiler.Compiler compiler)
{
// Caches a return value
Game returnValue = null;
// Starts reading the file if it exists, logs an error if not
StreamReader fileReader = null;
try
{
fileReader = new StreamReader(absoluteFilePath);
}
catch(IOException e)
{
ColourConsole.WriteLine(string.Format("[CompileGameFromFile] Could not load data!:\n {0}", e.Message), ERROR_COLOUR);
}
// If the file does exist, reads the entire file and compiles it into a game.
if(fileReader != null)
{
// Extracts all text and trims lead/trail whitespace
string fileText = fileReader.ReadToEnd().Trim();
// Closes the file reader
fileReader.Close();
// Compiles the text as best as it can. All compilation errors will be logged by the compiler and this doesn't care about that.
returnValue = compiler.CompileGame(fileText);
}
// Returns the cached return value
return returnValue;
}
}
}