-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build the first stage of the Hack Assembler which only assembles the …
…C-Instruction.
- Loading branch information
codecyang
committed
Jul 8, 2020
1 parent
c64bff6
commit 1dc12c5
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public class Assembler{ | ||
private static final String OUTPUT_EXTENSION = ".hack"; | ||
|
||
public static void main(String[] args) throws IOException{ | ||
Parser parser; | ||
for(String arg: args){ | ||
File currentFile = new File(arg); | ||
String fileName = currentFile.getName(); | ||
int index = fileName.indexOf("."); | ||
if(index > 0) | ||
fileName = fileName.substring(0, index); | ||
fileName += OUTPUT_EXTENSION; | ||
String filePath = Paths.get(currentFile.getParent()).resolve(fileName).toString(); | ||
File outFile = new File(filePath); | ||
|
||
PrintWriter printW | ||
= new PrintWriter(new BufferedWriter(new FileWriter(outFile))); | ||
|
||
parser = new Parser(arg); | ||
while(parser.hasMoreCommands()){ | ||
String line = ""; | ||
parser.advance(); | ||
if(parser.commandType() == Parser.CommandType.A_COMMAND | ||
|| parser.commandType() == Parser.CommandType.L_COMMAND) | ||
line = parser.symbol(); | ||
else if(parser.commandType() == Parser.CommandType.C_COMMAND){ | ||
String c = parser.comp(); | ||
String d = parser.dest(); | ||
String j = parser.jump(); | ||
String cc = Code.comp(c); | ||
String dd = Code.dest(d); | ||
String jj = Code.jump(j); | ||
|
||
line = "111" + cc + dd + jj; | ||
} | ||
printW.println(line); | ||
} | ||
printW.flush(); | ||
printW.close(); | ||
} | ||
} | ||
} |