Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Bit of fun II

Reply
Thread Tools

Bit of fun II

 
 
Lyndon Samson
Guest
Posts: n/a
 
      10-24-2005
More idle coding A simple Compiler/Assembler/VM for a ( very basic
) RPN (dc) style language. I suspect its quite amenable to golfing,
but at a cost to readability.

def compile(pgm)
outs =3D []

lines =3D pgm.split("\n")
lines.each { |line|
tokens =3D line.strip.chomp.split(" ")
pos =3D 0
while true
case tokens[pos]
when '+'
outs << "pop 1"
outs << "pop 2"
outs << "addr 1,2"
outs << "push 1"
when 'p'
outs << "pop 1"
outs << "print 1"
when '',nil
else
outs << "loadr 1,#{tokens[pos]}"
outs << "push 1"
end

pos =3D pos + 1
break if pos > tokens.length
end
}

return outs.join("\n")
end

def assemble(pgm)
mem =3D []
pc =3D 0

lines =3D pgm.split("\n")
lines.each { |line|
tokens =3D line.strip.split(/ |,/)
case tokens[0]
when 'loadr'
mem[pc]=3D1
mem[pc+1]=3Dtokens[1].to_i
mem[pc+2]=3Dtokens[2].to_i
pc =3D pc+3
when 'addr'
mem[pc]=3D2
mem[pc+1]=3Dtokens[1].to_i
mem[pc+2]=3Dtokens[2].to_i
pc =3D pc+3
when 'print'
mem[pc]=3D3
mem[pc+1]=3Dtokens[1].to_i
pc =3D pc+2
when 'push'
mem[pc]=3D4
mem[pc+1]=3Dtokens[1].to_i
pc =3D pc+2
when 'pop'
mem[pc]=3D5
mem[pc+1]=3Dtokens[1].to_i
pc =3D pc+2
end

}

mem.push(0)

return mem
end

def execute(mem)
pc =3D 0
regs=3D[]
stack=3D[]

while true
#puts "> PC:#{pc} CODE:#{mem[pc]} STACK:#{stack.join(',')}"

case mem[pc]
when 0
break
when 1
regs[mem[pc+1]] =3D mem[pc+2]
pc=3Dpc+3
when 2
regs[mem[pc+1]] =3D regs[mem[pc+1]] + regs[mem[pc+2]]
pc=3Dpc+3
when 3
puts "#{regs[mem[pc+1]]}"
pc=3Dpc+2
when 4 #push
stack.push(regs[mem[pc+1]])
pc=3Dpc+2
when 5 #pop
regs[mem[pc+1]] =3D stack.pop()
pc=3Dpc+2
end
end

end

aapgm =3D compile("12 10 + 14 + p")
code =3D assemble(aapgm)

execute(code)


--
Into RFID? www.rfidnewsupdate.com Simple, fast, news.


 
Reply With Quote
 
 
 
 
Xeno Campanoli
Guest
Posts: n/a
 
      10-24-2005


Lyndon Samson wrote:

>More idle coding A simple Compiler/Assembler/VM for a ( very basic
>) RPN (dc) style language. I suspect its quite amenable to golfing,
>but at a cost to readability.
>
>def compile(pgm)
> outs = []
>
> lines = pgm.split("\n")
>
>

Why not save a step and just do:

pgm.each_line { |line|...?

> lines.each { |line|
> tokens = line.strip.chomp.split(" ")
> pos = 0
> while true
> case tokens[pos]
> when '+'
> outs << "pop 1"
> outs << "pop 2"
> outs << "addr 1,2"
> outs << "push 1"
> when 'p'
> outs << "pop 1"
> outs << "print 1"
> when '',nil
> else
> outs << "loadr 1,#{tokens[pos]}"
> outs << "push 1"
> end
>
> pos = pos + 1
> break if pos > tokens.length
> end
> }
>
> return outs.join("\n")
>end
>
>def assemble(pgm)
> mem = []
> pc = 0
>
> lines = pgm.split("\n")
># Same comment as above...?
> lines.each { |line|
> tokens = line.strip.split(/ |,/)
> case tokens[0]
> when 'loadr'
> mem[pc]=1
> mem[pc+1]=tokens[1].to_i
> mem[pc+2]=tokens[2].to_i
> pc = pc+3
> when 'addr'
> mem[pc]=2
> mem[pc+1]=tokens[1].to_i
> mem[pc+2]=tokens[2].to_i
> pc = pc+3
> when 'print'
> mem[pc]=3
> mem[pc+1]=tokens[1].to_i
> pc = pc+2
> when 'push'
> mem[pc]=4
> mem[pc+1]=tokens[1].to_i
> pc = pc+2
> when 'pop'
> mem[pc]=5
> mem[pc+1]=tokens[1].to_i
> pc = pc+2
> end
>
> }
>
> mem.push(0)
>
> return mem
>end
>
>def execute(mem)
> pc = 0
> regs=[]
> stack=[]
>
> while true
> #puts "> PC:#{pc} CODE:#{mem[pc]} STACK:#{stack.join(',')}"
>
> case mem[pc]
> when 0
> break
> when 1
> regs[mem[pc+1]] = mem[pc+2]
> pc=pc+3
> when 2
> regs[mem[pc+1]] = regs[mem[pc+1]] + regs[mem[pc+2]]
> pc=pc+3
> when 3
> puts "#{regs[mem[pc+1]]}"
> pc=pc+2
> when 4 #push
> stack.push(regs[mem[pc+1]])
> pc=pc+2
> when 5 #pop
> regs[mem[pc+1]] = stack.pop()
> pc=pc+2
> end
> end
>
>end
>
>aapgm = compile("12 10 + 14 + p")
>code = assemble(aapgm)
>
>execute(code)
>
>
>--
>Into RFID? www.rfidnewsupdate.com Simple, fast, news.
>
>
>
>
>
>


--
Xeno Campanoli: http://www.eskimo.com/~xeno
The real disaster is ANY TIME WE GET A BUSH FOR PRESIDENT!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
3 PIX VPN questions - FUN FUN FUN frishack@gmail.com Cisco 3 03-16-2006 02:25 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM
Fun fun fun Luke Computer Support 3 10-07-2003 03:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57