-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
84 lines (75 loc) · 2.96 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
using System;
using System.IO;
///<summary>
///VM to Hack translator program.
///Built as part of the nand2tetris course part 2 by Shimon Schocken
///and Noam Nisan, further information at www.nand2tetris.org.
///</summary>
namespace VMtoHackTranslator
{
class Program
{
static Parser parser = new Parser();
static CodeWriter codeWriter = new CodeWriter();
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("No argument given. Path for input file must be specified.");
return;
}
FileAttributes attributes;
try
{
attributes = File.GetAttributes(args[0]);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}
codeWriter.WriteInit();
if(attributes.HasFlag(FileAttributes.Directory))
{
string[] files = Directory.GetFiles(args[0], "*.vm");
foreach(string filePath in files)
{
TranslateFile(filePath);
}
codeWriter.Close(args[0], true);
}
else
{
TranslateFile(args[0]);
codeWriter.Close(args[0]);
}
}
private static void TranslateFile(string path)
{
//read file
parser.ReadFile(path);
codeWriter.SetFileName(Path.GetFileNameWithoutExtension(path));
while (parser.HasMoreCommands())
{
Parser.CommandType commandType = parser.GetCommandType();
if (commandType == Parser.CommandType.C_POP || commandType == Parser.CommandType.C_PUSH)
codeWriter.WritePushPop(commandType, parser.GetArg1(commandType), parser.GetArg2());
else if (commandType == Parser.CommandType.C_ARITHMETIC)
codeWriter.WriteArithmetic(parser.GetArg1(commandType));
else if (commandType == Parser.CommandType.C_GOTO)
codeWriter.WriteGoto(parser.GetArg1(commandType));
else if (commandType == Parser.CommandType.C_IF)
codeWriter.WriteIf(parser.GetArg1(commandType));
else if (commandType == Parser.CommandType.C_LABEL)
codeWriter.WriteLabel(parser.GetArg1(commandType));
else if (commandType == Parser.CommandType.C_CALL)
codeWriter.WriteCall(parser.GetArg1(commandType), parser.GetArg2());
else if (commandType == Parser.CommandType.C_FUNCTION)
codeWriter.WriteFunction(parser.GetArg1(commandType), parser.GetArg2());
else if (commandType == Parser.CommandType.C_RETURN)
codeWriter.WriteReturn();
parser.Advance();
}
}
}
}