diff --git a/README.md b/README.md index 1c6b28d..fe8c1dc 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,6 @@ Counting number of instructions by `wc -l $file.hack` ### Symbolic Programs -- [ ] `Max.asm` -- [ ] `Rect.asm` -- [ ] `Pong.asm` +- [x] `Max.asm` +- [x] `Rect.asm` +- [x] `Pong.asm` diff --git a/assembler/.gitignore b/assembler/.gitignore new file mode 100644 index 0000000..af20eaf --- /dev/null +++ b/assembler/.gitignore @@ -0,0 +1 @@ +assembler diff --git a/assembler/assembler.rb b/assembler/assembler.rb index 4c0a7f0..e359738 100644 --- a/assembler/assembler.rb +++ b/assembler/assembler.rb @@ -33,15 +33,15 @@ class SymbolTable end def add(symb, address) - @st[symb] = address + @st[symb.to_sym] = address end - def includes? - @st.has? symb + def has?(symb) + @st.has_key? symb.to_sym end def get(symb) - @st[symb] + @st[symb.to_sym] end end @@ -96,6 +96,7 @@ class Code end def self.comp(str) + str.strip! a = c1 = c2 = c3 = c4 = c5 = c6 = 0 a = str.include? 'M' key = str.gsub(/[AM]/, 'Y') @@ -187,10 +188,16 @@ class Parser @ic = 0 @lines = [] File.readlines(fn).each do |line| - line.chomp! + line.strip! next if line.empty? next if line[0...2] == '//' + if line.include? '//' + comment_start = line.index('//') + line = line[0...comment_start] + line.strip! + end + @lines << line end end @@ -213,8 +220,9 @@ class Assembler if command[0] == :L_COMMAND symbol = command[1][0] @st.add(symbol, ic) + else + ic+=1 end - ic+=1 end # we rewind our parser parser.reset @@ -229,7 +237,7 @@ class Assembler val =@st.get(symbol) else val = memory_pointer - @st.add_var(symbol, memory_pointer) + @st.add(symbol, memory_pointer) memory_pointer+=1 end else diff --git a/assembler/assembler.rs b/assembler/assembler.rs new file mode 100644 index 0000000..172441b --- /dev/null +++ b/assembler/assembler.rs @@ -0,0 +1,17 @@ +use std::fs::File; +use std::env; +use std::io::{self, BufRead}; +use std::path::Path; +fn main() -> std::io::Result<()> { + let fileName = env::args().nth(1); + let fileLines = read_lines(fileName); + Ok(()) +} + + +fn read_lines

(filename: P) -> io::Result>> +where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + diff --git a/assembler/test b/assembler/test index 96e37ea..7336116 100755 --- a/assembler/test +++ b/assembler/test @@ -1,5 +1,15 @@ #!/bin/bash + +# Symbol Less assembly programs diff ../projects/06/add/Add.hack <(ruby assembler.rb ../projects/06/add/Add.asm) diff ../projects/06/max/MaxL.hack <(ruby assembler.rb ../projects/06/max/MaxL.asm) diff ../projects/06/rect/RectL.hack <(ruby assembler.rb ../projects/06/rect/RectL.asm) diff ../projects/06/pong/PongL.hack <(ruby assembler.rb ../projects/06/pong/PongL.asm) + + + +diff ../projects/06/max/Max.hack <(ruby assembler.rb ../projects/06/max/Max.asm) +diff ../projects/06/rect/Rect.hack <(ruby assembler.rb ../projects/06/rect/Rect.asm) +diff ../projects/06/pong/Pong.hack <(ruby assembler.rb ../projects/06/pong/Pong.asm) + +