static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: *.exe .vm"); Environment.Exit(-1); } string fileName = args[0]; string output = fileName.Replace(".vm", ".asm"); Parse parse = new Parse(fileName); codeWriter writer = new codeWriter(output, parse); while (parse.hasMoreCommand()) { Parse.CodeType type = parse.commandType(); if (type == Parse.CodeType.C_ARITHMETIC) { writer.writeArithmetic(); } else if (type == Parse.CodeType.C_PUSH || type == Parse.CodeType.C_POP) { writer.writePushPop(type, parse.arg1(), Convert.ToInt32(parse.arg2())); } else { Console.WriteLine("No Finish"); Environment.Exit(-1); } parse.advance(); } writer.close(); }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Program.exe asmfile"); return; } string filename = args[0]; List <string> resultCode = new List <string>(); var parse = new Parse(filename); while (parse.hasMoreLine()) { Parse.CodeType type = parse.commandType(); if (type == Parse.CodeType.A_COMMAND) { int result; string symbol = parse.symbol(); if (Int32.TryParse(symbol, out result) == true) { resultCode.Add(padString(result, 16)); } else { if (parse._symbol_dict.ContainsKey(symbol)) { } else { parse._symbol_dict[symbol] = parse.baseAddress + parse.varCount; ++parse.varCount; } resultCode.Add(padString(parse._symbol_dict[symbol], 16)); } } else if (type == Parse.CodeType.C_COMMAND) { resultCode.Add("111" + Code.comp(parse.comp()) + Code.dest(parse.dest()) + Code.jump(parse.jump())); } else { Console.WriteLine("Something wrong"); Environment.Exit(-1); } parse.advance(); } string outfile = filename.Replace(".asm", ".hack"); File.WriteAllLines(outfile, resultCode); }
public void writePushPop(Parse.CodeType codeType, string segment, int segmentIndex) { if (codeType == Parse.CodeType.C_PUSH) { if (segment == "constant") { addCode("@" + segmentIndex.ToString()); addCode("D=A"); DtoStack(); } else if (segment == "local") { pushSegmentHelper("@LCL"); } else if (segment == "argument") { pushSegmentHelper("@ARG"); } else if (segment == "this") { pushSegmentHelper("@THIS"); } else if (segment == "that") { pushSegmentHelper("@THAT"); } else if (segment == "static") { pushSegmentHelper("@16"); } else if (segment == "temp") { pushSegmentTemp(); } else if (segment == "pointer") { pushSegmentPointer(); } else { Console.WriteLine("No finish:push"); Environment.Exit(-1); } } else if (codeType == Parse.CodeType.C_POP) { if (segment == "constant") { popToA(); } else if (segment == "local") { popSegmentHelper("@LCL"); } else if (segment == "argument") { popSegmentHelper("@ARG"); } else if (segment == "this") { popSegmentHelper("@THIS"); } else if (segment == "that") { popSegmentHelper("@THAT"); } else if (segment == "static") { popSegmentHelper("@16"); } else if (segment == "temp") { popSegmentTemp(); } else if (segment == "pointer") { popSegmentPointer(); } else { Console.WriteLine("No finish:pop"); Environment.Exit(-1); } } else { Console.WriteLine("Wrong Type : writePushPop"); Environment.Exit(-1); } }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: *.exe .vm"); Environment.Exit(-1); } List <string> fileList = new List <string>(); string fileName = args[0]; string output = ""; if (fileName.EndsWith(".vm")) { output = fileName.Replace(".vm", ".asm"); } else { var dirName = new DirectoryInfo(fileName).Name; output = fileName + @"\" + dirName + ".asm"; fileList.AddRange(Directory.GetFiles(fileName, "*.vm")); } codeWriter writer = new codeWriter(output); writer.writeBootLoader(); foreach (var name in fileList) { Parse parse = new Parse(name); writer.changeParse(parse); while (parse.hasMoreCommand()) { Parse.CodeType type = parse.commandType(); if (type == Parse.CodeType.C_ARITHMETIC) { writer.writeArithmetic(); } else if (type == Parse.CodeType.C_PUSH || type == Parse.CodeType.C_POP) { writer.writePushPop(type, parse.arg1(), Convert.ToInt32(parse.arg2())); } else if (type == Parse.CodeType.C_LABEL) { writer.writeLabel(); } else if (type == Parse.CodeType.C_FUNCTION) { writer.writeFunction(); } else if (type == Parse.CodeType.C_GOTO) { writer.writeGoto(); } else if (type == Parse.CodeType.C_IF) { writer.writeIf(); } else if (type == Parse.CodeType.C_RETURN) { writer.writeReturn(); } else if (type == Parse.CodeType.C_CALL) { writer.writeCall(); } else { Console.WriteLine("No Finish"); Environment.Exit(-1); } parse.advance(); } } writer.close(); }