-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
140 lines (116 loc) · 4.66 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
134
135
136
137
138
139
140
#nullable enable
using System;
using System.IO;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hexagony
{
static class Program
{
private static async Task<int> Main(string[] args)
{
// Directly output characters up to and including 255 as bytes, without modifying them.
// This allows UTF-8 encoded text to be output one byte at a time.
Console.OutputEncoding = Encoding.GetEncoding("iso-8859-1");
var bytes = Enumerable.Range(0, 256).Select(x => (byte)x).ToArray();
var encodedBytes = Console.OutputEncoding
.GetBytes(bytes.Select(x => (char)x).ToArray());
if (!bytes.SequenceEqual(encodedBytes))
{
throw new InvalidOperationException("Encoding error.");
}
var generateOption = new Option<int>(
new[] { "-g", "--generate" },
"Generate a hexagon with the given size.");
var debugOption = new Option<bool>(
new[] { "-d", "--debug" },
"Output debug information to STDERR for instructions preceded by \"`\".");
var debugAllOption = new Option<bool>(
new[] { "-D", "--debug-all" },
"Output debug information to STDERR after every tick.");
var fileArgument = new Argument<FileInfo>
{
Name = "File",
Arity = ArgumentArity.ZeroOrOne,
Description = "Path to code file. Use \"-\" for STDIN.",
};
var argumentsArgument = new Argument<string[]>
{
Name = "Arguments",
Arity = ArgumentArity.ZeroOrMore,
Description = "Optional arguments for program that will be joined with null characters. Otherwise, if STDIN is not used for the code file, it will be used for input.",
};
var rootCommand = new RootCommand
{
generateOption,
debugOption,
debugAllOption,
fileArgument,
argumentsArgument,
};
rootCommand.Description = "Hexagony interpreter";
rootCommand.SetHandler(async (context) =>
await MainTask(new CommandLineOptions
{
Generate = context.ParseResult.GetValueForOption(generateOption),
Debug = context.ParseResult.GetValueForOption(debugOption),
DebugAll = context.ParseResult.GetValueForOption(debugAllOption),
File = context.ParseResult.GetValueForArgument(fileArgument),
Arguments = context.ParseResult.GetValueForArgument(argumentsArgument),
}));
return await rootCommand.InvokeAsync(args);
}
private class CommandLineOptions
{
public int Generate { get; set; }
public string[] Arguments { get; set; } = Array.Empty<string>();
public FileInfo? File { get; set; }
public bool Debug { get; set; }
public bool DebugAll { get; set; }
}
private static async Task<int> MainTask(CommandLineOptions options)
{
if (options.Generate != 0)
{
Console.WriteLine(new Grid(options.Generate));
return 0;
}
if (options.File == null)
{
Console.Error.WriteLine("No file specified.");
return 1;
}
string code;
var stdinCode = options.File.Name == "-";
if (stdinCode)
{
code = await Console.In.ReadToEndAsync();
}
else
{
using var stream = options.File.OpenText();
code = await stream.ReadToEndAsync();
}
await using var inputStream = options.Arguments.Length > 0 ?
new MemoryStream(Encoding.UTF8.GetBytes(string.Join('\0', options.Arguments) + "\0")) :
!stdinCode ?
Console.OpenStandardInput() :
new MemoryStream(new byte[0]);
var debugLevel = options.DebugAll ? 2 : options.Debug ? 1 : 0;
var environment = new HexagonyEnv(code, inputStream, debugLevel);
try
{
environment.Run();
}
catch (Exception e)
{
Console.Error.WriteLine(e);
return 1;
}
return 0;
}
}
}