summaryrefslogtreecommitdiff
path: root/Assembler/Program.cs
blob: b30df1ebf084817138dd28dedd2593093f5fd02b (plain)
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
namespace Q1.Assembler;

using CommandLine;

public class Program
{
    static void Main(string[] args)
    {
        Parser.Default.ParseArguments<Options>(args).WithParsed(Program.Main);
    }

    static void Main(Options options)
    {
        Environment.CurrentDirectory = Path.GetDirectoryName(options.InputFile) ?? Environment.CurrentDirectory;
        options.InputFile = Path.GetFileName(options.InputFile);
        
        string[] input = File.ReadAllLines(options.InputFile);

        try
        {
            Assembler assembler = new(input);
            byte[] bin = assembler.Assemble().ToArray();
            if (bin.Length > 8 * 1024)
                throw new AssemblerException("Assembled binary exceeds maximum size of 8KB.");

            File.WriteAllBytes(options.OutputFile, bin);

            Console.WriteLine($"Assembled {options.InputFile} to {options.OutputFile} ({bin.Length} bytes)");
        }
        catch (AssemblerException ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Error.WriteLine($"Error: {ex.Message}");
            Console.ResetColor();
            Environment.Exit(1);
        }
    }
}