Remove unused variables, style fixes

This commit is contained in:
Nemo 2020-05-30 04:08:30 +05:30
parent 09bf3f1e67
commit 26f427a8ad
2 changed files with 49 additions and 54 deletions

View File

@ -5,31 +5,29 @@ fn = ARGV[0]
# Not implemented yet # Not implemented yet
class SymbolTable class SymbolTable
def initialize def initialize
@st = {:SP => 0, @st = { SP: 0,
:LCL => 1, LCL: 1,
:ARG => 2, ARG: 2,
:THIS => 3, THIS: 3,
:THAT => 4, THAT: 4,
:R0 => 0, R0: 0,
:R1 => 1, R1: 1,
:R2 => 2, R2: 2,
:R3 => 3, R3: 3,
:R4 => 4, R4: 4,
:R5 => 5, R5: 5,
:R6 => 6, R6: 6,
:R7 => 7, R7: 7,
:R8 => 8, R8: 8,
:R9 => 9, R9: 9,
:R10 => 10, R10: 10,
:R11 => 11, R11: 11,
:R12 => 12, R12: 12,
:R13 => 13, R13: 13,
:R14 => 14, R14: 14,
:R15 => 15, R15: 15,
:SCREEN => 16384, SCREEN: 16_384,
:KBD => 24576 KBD: 24_576 }
}
end end
def add(symb, address) def add(symb, address)
@ -37,7 +35,7 @@ class SymbolTable
end end
def has?(symb) def has?(symb)
@st.has_key? symb.to_sym @st.key? symb.to_sym
end end
def get(symb) def get(symb)
@ -47,25 +45,25 @@ end
class Code class Code
DEST_MAP = { DEST_MAP = {
nil => "000", nil => '000',
'M' => "001", 'M' => '001',
'D' => "010", 'D' => '010',
'MD' => "011", 'MD' => '011',
'A' => "100", 'A' => '100',
'AM' => "101", 'AM' => '101',
'AD' => "110", 'AD' => '110',
'AMD' => "111" 'AMD' => '111'
}.freeze }.freeze
JUMP_MAP = { JUMP_MAP = {
nil => "000", nil => '000',
'JGT' => "001", 'JGT' => '001',
'JEQ' => "010", 'JEQ' => '010',
'JGE' => "011", 'JGE' => '011',
'JLT' => "100", 'JLT' => '100',
'JNE' => "101", 'JNE' => '101',
'JLE' => "110", 'JLE' => '110',
'JMP' => "111" 'JMP' => '111'
}.freeze }.freeze
COMP_MAP = { COMP_MAP = {
'0' => '101010', '0' => '101010',
@ -97,10 +95,8 @@ class Code
def self.comp(str) def self.comp(str)
str.strip! str.strip!
a = c1 = c2 = c3 = c4 = c5 = c6 = 0
a = str.include? 'M'
key = str.gsub(/[AM]/, 'Y') key = str.gsub(/[AM]/, 'Y')
(a ? '1' : '0') + COMP_MAP[key] (str.include?('M') ? '1' : '0') + COMP_MAP[key]
end end
end end
@ -139,6 +135,7 @@ class Parser
def dest def dest
return nil unless line.include? '=' return nil unless line.include? '='
line.split('=').first line.split('=').first
end end
@ -156,6 +153,7 @@ class Parser
def jump def jump
return nil unless line.include? ';' return nil unless line.include? ';'
line.split(';').last line.split(';').last
end end
@ -221,7 +219,7 @@ class Assembler
symbol = command[1][0] symbol = command[1][0]
@st.add(symbol, ic) @st.add(symbol, ic)
else else
ic+=1 ic += 1
end end
end end
# we rewind our parser # we rewind our parser
@ -234,18 +232,18 @@ class Assembler
symbol_type, symbol = command[1] symbol_type, symbol = command[1]
if symbol_type === :variable if symbol_type === :variable
if @st.has? symbol if @st.has? symbol
val =@st.get(symbol) val = @st.get(symbol)
else else
val = memory_pointer val = memory_pointer
@st.add(symbol, memory_pointer) @st.add(symbol, memory_pointer)
memory_pointer+=1 memory_pointer += 1
end end
else else
val = symbol.to_i val = symbol.to_i
end end
@res << val.to_i.to_s(2).rjust(16, "0") @res << val.to_i.to_s(2).rjust(16, '0')
when :C_COMMAND when :C_COMMAND
dest,comp,jump = command[1] dest, comp, jump = command[1]
@res << "111#{Code.comp(comp)}#{Code.dest(dest)}#{Code.jump(jump)}" @res << "111#{Code.comp(comp)}#{Code.dest(dest)}#{Code.jump(jump)}"
end end
end end

View File

@ -6,10 +6,7 @@ diff ../projects/06/max/MaxL.hack <(ruby assembler.rb ../projects/06/max/MaxL.as
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)
# Programs with symbols
diff ../projects/06/max/Max.hack <(ruby assembler.rb ../projects/06/max/Max.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/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) diff ../projects/06/pong/Pong.hack <(ruby assembler.rb ../projects/06/pong/Pong.asm)