[06] Assembler done and tested
Also started a clone assembler in rust
This commit is contained in:
parent
abbb826dd0
commit
09bf3f1e67
|
@ -86,6 +86,6 @@ Counting number of instructions by `wc -l $file.hack`
|
||||||
|
|
||||||
### Symbolic Programs
|
### Symbolic Programs
|
||||||
|
|
||||||
- [ ] `Max.asm`
|
- [x] `Max.asm`
|
||||||
- [ ] `Rect.asm`
|
- [x] `Rect.asm`
|
||||||
- [ ] `Pong.asm`
|
- [x] `Pong.asm`
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
assembler
|
|
@ -33,15 +33,15 @@ class SymbolTable
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(symb, address)
|
def add(symb, address)
|
||||||
@st[symb] = address
|
@st[symb.to_sym] = address
|
||||||
end
|
end
|
||||||
|
|
||||||
def includes?
|
def has?(symb)
|
||||||
@st.has? symb
|
@st.has_key? symb.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(symb)
|
def get(symb)
|
||||||
@st[symb]
|
@st[symb.to_sym]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -96,6 +96,7 @@ class Code
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.comp(str)
|
def self.comp(str)
|
||||||
|
str.strip!
|
||||||
a = c1 = c2 = c3 = c4 = c5 = c6 = 0
|
a = c1 = c2 = c3 = c4 = c5 = c6 = 0
|
||||||
a = str.include? 'M'
|
a = str.include? 'M'
|
||||||
key = str.gsub(/[AM]/, 'Y')
|
key = str.gsub(/[AM]/, 'Y')
|
||||||
|
@ -187,10 +188,16 @@ class Parser
|
||||||
@ic = 0
|
@ic = 0
|
||||||
@lines = []
|
@lines = []
|
||||||
File.readlines(fn).each do |line|
|
File.readlines(fn).each do |line|
|
||||||
line.chomp!
|
line.strip!
|
||||||
next if line.empty?
|
next if line.empty?
|
||||||
next if line[0...2] == '//'
|
next if line[0...2] == '//'
|
||||||
|
|
||||||
|
if line.include? '//'
|
||||||
|
comment_start = line.index('//')
|
||||||
|
line = line[0...comment_start]
|
||||||
|
line.strip!
|
||||||
|
end
|
||||||
|
|
||||||
@lines << line
|
@lines << line
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -213,8 +220,9 @@ class Assembler
|
||||||
if command[0] == :L_COMMAND
|
if command[0] == :L_COMMAND
|
||||||
symbol = command[1][0]
|
symbol = command[1][0]
|
||||||
@st.add(symbol, ic)
|
@st.add(symbol, ic)
|
||||||
|
else
|
||||||
|
ic+=1
|
||||||
end
|
end
|
||||||
ic+=1
|
|
||||||
end
|
end
|
||||||
# we rewind our parser
|
# we rewind our parser
|
||||||
parser.reset
|
parser.reset
|
||||||
|
@ -229,7 +237,7 @@ class Assembler
|
||||||
val =@st.get(symbol)
|
val =@st.get(symbol)
|
||||||
else
|
else
|
||||||
val = memory_pointer
|
val = memory_pointer
|
||||||
@st.add_var(symbol, memory_pointer)
|
@st.add(symbol, memory_pointer)
|
||||||
memory_pointer+=1
|
memory_pointer+=1
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|
|
@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||||
|
where P: AsRef<Path>, {
|
||||||
|
let file = File::open(filename)?;
|
||||||
|
Ok(io::BufReader::new(file).lines())
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,15 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Symbol Less assembly programs
|
||||||
diff ../projects/06/add/Add.hack <(ruby assembler.rb ../projects/06/add/Add.asm)
|
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/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/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/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)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue