2018-03-01 16:21:16 -06:00
|
|
|
$types=[:int,:void]
|
|
|
|
$functable={}
|
|
|
|
$cfunc=nil
|
|
|
|
$outfile=nil
|
|
|
|
|
|
|
|
class CompilationError < StandardError; end
|
|
|
|
|
|
|
|
class String
|
|
|
|
def is_integer?
|
|
|
|
self.to_i.to_s == self
|
2017-10-01 09:13:24 -05:00
|
|
|
end
|
2018-03-01 16:21:16 -06:00
|
|
|
end
|
|
|
|
|
2018-03-03 08:52:57 -06:00
|
|
|
def is_var(var)
|
|
|
|
return false if var.class!=Symbol
|
|
|
|
vartable=$functable[$cfunc][:vars]
|
|
|
|
argtable=$functable[$cfunc][:args]
|
|
|
|
if vartable.include? var or argtable.include? var
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-01 16:21:16 -06:00
|
|
|
def get_index(var)
|
|
|
|
vartable=$functable[$cfunc][:vars]
|
2018-03-03 08:52:57 -06:00
|
|
|
argtable=$functable[$cfunc][:args]
|
2018-03-01 16:21:16 -06:00
|
|
|
if vartable.include? var
|
2018-03-03 08:52:57 -06:00
|
|
|
return vartable.find_index(var),"local"
|
2018-03-01 16:21:16 -06:00
|
|
|
else
|
2018-03-03 08:52:57 -06:00
|
|
|
if argtable.include? var
|
|
|
|
return argtable.find_index(var),"argument"
|
|
|
|
end
|
2018-03-01 16:21:16 -06:00
|
|
|
raise CompilationError,"No such variable #{var}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-03 08:52:57 -06:00
|
|
|
def push(obj)
|
|
|
|
if obj.class == Symbol
|
|
|
|
index,segment=get_index(obj)
|
|
|
|
$outfile.puts "push #{segment} #{index}"
|
|
|
|
elsif obj.class == String
|
|
|
|
if obj.is_integer?
|
2018-03-04 14:18:45 -06:00
|
|
|
$outfile.puts "push constant #{obj}"
|
2018-03-03 08:52:57 -06:00
|
|
|
elsif is_var(obj.to_sym)
|
|
|
|
index,segment=get_index(obj.to_sym)
|
|
|
|
$outfile.puts "push #{segment} #{index}"
|
|
|
|
else
|
|
|
|
raise CompilationError, "Cannot push #{obj}, is not a variable or a number."
|
|
|
|
end
|
|
|
|
elsif obj.is_a? Numeric
|
|
|
|
$outfile.puts "push constant #{obj}"
|
|
|
|
else
|
|
|
|
raise CompilationError, "Cannot push #{obj}, is not a variable or a number."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pop(var)
|
|
|
|
if var.class != Symbol and !is_var(var)
|
|
|
|
raise CompilationError, "Cannot push #{var}, is not a variable."
|
|
|
|
end
|
|
|
|
index,segment=get_index(var)
|
|
|
|
$outfile.puts "pop #{segment} #{index}"
|
|
|
|
end
|
|
|
|
|
2018-03-01 16:21:16 -06:00
|
|
|
def tokenize_line(line)
|
|
|
|
split_line=line.split(" ")
|
|
|
|
cmd=split_line.shift.to_sym
|
|
|
|
tokens=[]
|
|
|
|
if $types.include? cmd
|
|
|
|
if /(\w+)\((.*)\) \{/.match(split_line.join(" "))
|
|
|
|
tokens.push(:func)
|
2018-03-03 08:52:57 -06:00
|
|
|
tokens.push(cmd)
|
2018-03-01 16:21:16 -06:00
|
|
|
tokens.push($1.to_sym)
|
2018-03-03 08:52:57 -06:00
|
|
|
args=[]
|
|
|
|
$2.split(",").each do |arg|
|
|
|
|
temp=arg.split(" ")
|
|
|
|
args.push(temp[1].to_sym)
|
|
|
|
end
|
|
|
|
tokens.push(args)
|
2018-03-01 16:21:16 -06:00
|
|
|
else
|
|
|
|
tokens.push(:newvar)
|
|
|
|
tokens.push(cmd)
|
|
|
|
tokens.push(split_line[0].to_sym)
|
2017-10-01 09:13:24 -05:00
|
|
|
end
|
2018-03-01 16:21:16 -06:00
|
|
|
else
|
2018-03-03 08:52:57 -06:00
|
|
|
if /(\w+)\((.*)\)/.match(cmd)
|
|
|
|
tokens.push(:call)
|
|
|
|
tokens.push($1.to_sym)
|
|
|
|
args=[]
|
|
|
|
$2.split(",").each do |arg|
|
|
|
|
args.push(arg.to_sym)
|
|
|
|
end
|
|
|
|
tokens.push(args)
|
|
|
|
end
|
2018-03-01 16:21:16 -06:00
|
|
|
if split_line[0] == "="
|
|
|
|
tokens.push(:assignment)
|
2018-03-03 08:52:57 -06:00
|
|
|
tokens.push(cmd)
|
2018-03-01 16:21:16 -06:00
|
|
|
tokens.push(split_line[1])
|
|
|
|
end
|
|
|
|
if cmd == "}"
|
|
|
|
tokens.push(:endfunc)
|
|
|
|
end
|
2018-03-03 08:52:57 -06:00
|
|
|
if cmd == :return
|
|
|
|
tokens.push(:return)
|
|
|
|
tokens.push(split_line[0])
|
|
|
|
end
|
2018-03-01 16:21:16 -06:00
|
|
|
end
|
|
|
|
return tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
def phase_one(tokens)
|
|
|
|
if $cfunc==nil and tokens[0] != :func
|
2018-03-03 08:52:57 -06:00
|
|
|
raise CompilationError, "Code must be inside a fuction, for line #{tokens}"
|
2018-03-01 16:21:16 -06:00
|
|
|
end
|
|
|
|
case tokens[0]
|
|
|
|
when :func
|
2018-03-03 08:52:57 -06:00
|
|
|
$functable[tokens[2]]={:vars=>[],:code=>[], :type=>tokens[1], :args=>tokens[3]}
|
|
|
|
$cfunc=tokens[2]
|
2018-03-01 16:21:16 -06:00
|
|
|
when :endfunc
|
|
|
|
$cfunc=nil
|
|
|
|
when :newvar
|
|
|
|
$functable[$cfunc][:vars].push(tokens[2])
|
|
|
|
else
|
2018-03-03 08:52:57 -06:00
|
|
|
$functable[$cfunc][:code].push(tokens) if tokens != []
|
2018-03-01 16:21:16 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def phase_two
|
|
|
|
$functable.each do |func,info|
|
2018-03-03 08:52:57 -06:00
|
|
|
$cfunc=func
|
2018-03-01 16:21:16 -06:00
|
|
|
vars=info[:vars]
|
|
|
|
$outfile.puts "function Main.#{func.to_s} #{vars.length}"
|
|
|
|
info[:code].each do |line|
|
2018-03-03 08:52:57 -06:00
|
|
|
action=line.shift
|
|
|
|
case action
|
|
|
|
when :assignment
|
|
|
|
if line[1].is_integer? or is_var(line[1])
|
|
|
|
push(line[1])
|
|
|
|
else
|
|
|
|
parsed_line=tokenize_line(line[1])
|
|
|
|
parsed_line=[""] if parsed_line==nil
|
|
|
|
if parsed_line[0]==:call
|
|
|
|
parsed_line[2].each do |arg|
|
|
|
|
push(arg)
|
|
|
|
end
|
|
|
|
nargs=$functable[parsed_line[1]][:args].length
|
|
|
|
$outfile.puts "call Main.#{parsed_line[1]} #{nargs}"
|
2018-03-01 16:21:16 -06:00
|
|
|
else
|
2018-03-03 08:52:57 -06:00
|
|
|
if line[1].include? "+"
|
2018-03-04 14:18:45 -06:00
|
|
|
operands=line[1].split("+")
|
2018-03-03 08:52:57 -06:00
|
|
|
op="add"
|
|
|
|
end
|
|
|
|
if line[1].include? "-"
|
2018-03-04 14:18:45 -06:00
|
|
|
operands=line[1].split("-")
|
2018-03-03 08:52:57 -06:00
|
|
|
op="sub"
|
|
|
|
end
|
2018-03-04 14:18:45 -06:00
|
|
|
push(operands[0])
|
|
|
|
push(operands[1])
|
2018-03-03 08:52:57 -06:00
|
|
|
$outfile.puts(op)
|
2018-03-01 16:21:16 -06:00
|
|
|
end
|
|
|
|
end
|
2018-03-03 08:52:57 -06:00
|
|
|
pop(line[0])
|
|
|
|
when :call
|
|
|
|
line[1].each do |arg|
|
|
|
|
push(arg)
|
|
|
|
end
|
|
|
|
nargs=$functable[line[0]][:args].length
|
|
|
|
$outfile.puts "call Main.#{line[0]} #{nargs}"
|
|
|
|
when :return
|
|
|
|
push(line[0])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if info[:type]==:void
|
|
|
|
push(0)
|
2017-10-01 09:13:24 -05:00
|
|
|
end
|
2018-03-01 16:21:16 -06:00
|
|
|
$outfile.puts "return"
|
2018-03-04 14:18:45 -06:00
|
|
|
$outfile.puts("")
|
2017-10-01 09:13:24 -05:00
|
|
|
end
|
|
|
|
end
|
2018-03-01 16:21:16 -06:00
|
|
|
|
|
|
|
def write_init
|
|
|
|
sysfile=File.new("Sys.vm","w")
|
|
|
|
sysfile.puts("function Sys.init 0")
|
|
|
|
sysfile.puts("call Main.main 0")
|
|
|
|
sysfile.puts("label halt")
|
|
|
|
sysfile.puts("goto halt")
|
2018-03-04 14:18:45 -06:00
|
|
|
sysfile.puts("")
|
2018-03-01 16:21:16 -06:00
|
|
|
end
|
2018-03-03 08:52:57 -06:00
|
|
|
|
2018-03-01 16:21:16 -06:00
|
|
|
if !File.exists? "vmprog"
|
|
|
|
Dir.mkdir("vmprog")
|
2018-03-04 14:18:45 -06:00
|
|
|
Dir.chdir("vmprog")
|
2018-03-03 08:52:57 -06:00
|
|
|
write_init()
|
2018-03-04 14:18:45 -06:00
|
|
|
else
|
|
|
|
Dir.chdir("vmprog")
|
2018-03-01 16:21:16 -06:00
|
|
|
end
|
2018-03-03 08:52:57 -06:00
|
|
|
|
2018-03-01 16:21:16 -06:00
|
|
|
$outfile=File.new("Main.vm","w")
|
2018-03-03 08:52:57 -06:00
|
|
|
|
|
|
|
prog=<<-END
|
|
|
|
void main() {
|
|
|
|
int x
|
|
|
|
x = 10
|
|
|
|
int y
|
|
|
|
y = x+17
|
|
|
|
int z
|
|
|
|
z = testfunc(y)
|
|
|
|
}
|
|
|
|
int testfunc(int c) {
|
|
|
|
int x
|
|
|
|
x = c-7
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
END
|
|
|
|
|
|
|
|
prog.each_line do |line|
|
|
|
|
phase_one(tokenize_line(line))
|
|
|
|
end
|
|
|
|
|
2018-03-01 16:21:16 -06:00
|
|
|
puts $functable
|
2018-03-03 08:52:57 -06:00
|
|
|
|
|
|
|
phase_two()
|