This commit is contained in:
pjht 2018-03-04 14:18:45 -06:00
parent ae23e9fbe6
commit c5eb2a33d7
3 changed files with 13 additions and 12 deletions

View File

@ -41,7 +41,7 @@ def push(obj)
$outfile.puts "push #{segment} #{index}" $outfile.puts "push #{segment} #{index}"
elsif obj.class == String elsif obj.class == String
if obj.is_integer? if obj.is_integer?
$outfile.puts "push constant #{obj.to_i}" $outfile.puts "push constant #{obj}"
elsif is_var(obj.to_sym) elsif is_var(obj.to_sym)
index,segment=get_index(obj.to_sym) index,segment=get_index(obj.to_sym)
$outfile.puts "push #{segment} #{index}" $outfile.puts "push #{segment} #{index}"
@ -148,15 +148,15 @@ def phase_two
$outfile.puts "call Main.#{parsed_line[1]} #{nargs}" $outfile.puts "call Main.#{parsed_line[1]} #{nargs}"
else else
if line[1].include? "+" if line[1].include? "+"
operand=line[1].split("+") operands=line[1].split("+")
op="add" op="add"
end end
if line[1].include? "-" if line[1].include? "-"
operand=line[1].split("-") operands=line[1].split("-")
op="sub" op="sub"
end end
push(operand[0]) push(operands[0])
push(operand[1]) push(operands[1])
$outfile.puts(op) $outfile.puts(op)
end end
end end
@ -175,7 +175,7 @@ def phase_two
push(0) push(0)
end end
$outfile.puts "return" $outfile.puts "return"
$outfile.puts(" ") $outfile.puts("")
end end
end end
@ -185,16 +185,17 @@ def write_init
sysfile.puts("call Main.main 0") sysfile.puts("call Main.main 0")
sysfile.puts("label halt") sysfile.puts("label halt")
sysfile.puts("goto halt") sysfile.puts("goto halt")
sysfile.puts(" ") sysfile.puts("")
end end
if !File.exists? "vmprog" if !File.exists? "vmprog"
Dir.mkdir("vmprog") Dir.mkdir("vmprog")
Dir.chdir("vmprog")
write_init() write_init()
else
Dir.chdir("vmprog")
end end
Dir.chdir("vmprog")
$outfile=File.new("Main.vm","w") $outfile=File.new("Main.vm","w")
prog=<<-END prog=<<-END

View File

@ -10,7 +10,7 @@ call Main.testfunc 1
pop local 2 pop local 2
push constant 0 push constant 0
return return
function Main.testfunc 1 function Main.testfunc 1
push argument 0 push argument 0
push constant 7 push constant 7
@ -18,4 +18,4 @@ sub
pop local 0 pop local 0
push local 0 push local 0
return return

View File

@ -2,4 +2,4 @@ function Sys.init 0
call Main.main 0 call Main.main 0
label halt label halt
goto halt goto halt