Skip to content

Commit

Permalink
Build the first stage of the Hack Assembler which only assembles the …
Browse files Browse the repository at this point in the history
…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.
50 changes: 50 additions & 0 deletions assembler/src/java/Assembler.java
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();
}
}
}

0 comments on commit 1dc12c5

Please sign in to comment.