diff --git a/compiler.rb b/compiler.rb index 615651f..9a4c2f0 100644 --- a/compiler.rb +++ b/compiler.rb @@ -41,7 +41,7 @@ def push(obj) $outfile.puts "push #{segment} #{index}" elsif obj.class == String if obj.is_integer? - $outfile.puts "push constant #{obj.to_i}" + $outfile.puts "push constant #{obj}" elsif is_var(obj.to_sym) index,segment=get_index(obj.to_sym) $outfile.puts "push #{segment} #{index}" @@ -148,15 +148,15 @@ def phase_two $outfile.puts "call Main.#{parsed_line[1]} #{nargs}" else if line[1].include? "+" - operand=line[1].split("+") + operands=line[1].split("+") op="add" end if line[1].include? "-" - operand=line[1].split("-") + operands=line[1].split("-") op="sub" end - push(operand[0]) - push(operand[1]) + push(operands[0]) + push(operands[1]) $outfile.puts(op) end end @@ -175,7 +175,7 @@ def phase_two push(0) end $outfile.puts "return" - $outfile.puts(" ") + $outfile.puts("") end end @@ -185,16 +185,17 @@ def write_init sysfile.puts("call Main.main 0") sysfile.puts("label halt") sysfile.puts("goto halt") - sysfile.puts(" ") + sysfile.puts("") end if !File.exists? "vmprog" Dir.mkdir("vmprog") + Dir.chdir("vmprog") write_init() +else + Dir.chdir("vmprog") end -Dir.chdir("vmprog") - $outfile=File.new("Main.vm","w") prog=<<-END diff --git a/vmprog/Main.vm b/vmprog/Main.vm index 5cefd83..ce334c4 100644 --- a/vmprog/Main.vm +++ b/vmprog/Main.vm @@ -10,7 +10,7 @@ call Main.testfunc 1 pop local 2 push constant 0 return - + function Main.testfunc 1 push argument 0 push constant 7 @@ -18,4 +18,4 @@ sub pop local 0 push local 0 return - + diff --git a/vmprog/Sys.vm b/vmprog/Sys.vm index 9ebdb5f..474cf1b 100644 --- a/vmprog/Sys.vm +++ b/vmprog/Sys.vm @@ -2,4 +2,4 @@ function Sys.init 0 call Main.main 0 label halt goto halt - +