Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
knsmr committed Feb 16, 2009
0 parents commit d0a6732
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bin/croud
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/ruby
$LOAD_PATH.unshift File.expand_path("../lib", File.dirname(__FILE__))
require 'croud.rb'

Croud.run(ARGF.read)
14 changes: 14 additions & 0 deletions example/fib.crd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fib.crd

(_) (_) () () () () ( ) () () ( _)
(__) (__) ( ) ( ) (_) (_) ( _) (_) (_) ( )

() () (_) () (__) () ()
( ) ( ) (__) (_) (__) ( ) ( )

() () () () () () (_) ()
(_) (_) ( ) (_) (_) (_) (__) (_)

() () () () ( ) () () (_) (_) (__)
( ) ( ) (_) (_) ( _) (_) ( ) (__) (__) (__)

46 changes: 46 additions & 0 deletions example/hello.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
push,8
push,9
mul
puts
push,3
push,a
mul
push,1
sub
add
puts
push,7
add
puts
puts
push,3
add
puts
swit
push,8
push,4
mul
puts
swit
push,8
push,3
mul
sub
puts
push,8
push,3
mul
add
puts
push,3
add
puts
push,6
sub
puts
push,8
sub
puts



20 changes: 20 additions & 0 deletions example/hello.crd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
hello.crd

() () (_ ) () () (_ )
(_) (_) ( ) (_) (_) ( _)

() () (_) () () () ( _) () () (__)
(_) (_) (__) (_) (_) (_) ( _) (_) (_) ( )

() () () () ( ) () () () () (_) ()
(_) (_) (_) (_) ( _) (_) ( ) (_) (_) (__) (_)

() () ( _) () () (_) () (_) ()
(_) (_) (__) (_) (_) (__) (_) (__) (_)

() () ( _) () () (_) () () ()
(_) (_) ( _) (_) (_) (__) (_) ( ) ( )

() () (_ ) () () ( ) () () (_) ()
(_) (_) ( ) (_) (_) (_ ) (_) (_) (__) (_)

4 changes: 4 additions & 0 deletions example/sample.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
() () ( _) () () (__) () () (_) ()
(_) (_) (__) (_) (_) ( _) (_) (_) (__) (_)


13 changes: 13 additions & 0 deletions lib/croud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'croud/parse'
require 'croud/compiler'
require 'croud/vm'

module Croud

def self.run(src)
asm = Croud::Parse.parse(src)
inst = Croud::Compiler.compile(asm)
Croud::VM.run(inst)
end

end
62 changes: 62 additions & 0 deletions lib/croud/compiler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module Croud

class Compiler

def self.compile(asm)
new(asm).compile
end

def initialize(asm)
@asm = asm
@inst = []
end

def compile
@asm.split(/\n/).each do |line|
op,num = line.split(/,/)
case op
when "push" then @inst << :push << num.to_i(16)
when "drop" then @inst << :drop
when "dup" then @inst << :dup
when "swap" then @inst << :swap

when "roll" then @inst << :roll
when "swit" then @inst << :swit
when "rot" then @inst << :rot
when "slide" then @inst << :slide
when "exp" then @inst << :exp

when "add" then @inst << :add
when "sub" then @inst << :sub
when "mul" then @inst << :mul
when "div" then @inst << :div

when "setl" then @inst << :setl << num.to_i(16)
when "jmpl" then @inst << :jmpl << num.to_i(16)
when "zjmpl" then @inst << :zjmpl << num.to_i(16)
when "nzjmpl" then @inst << :nzjmpl << num.to_i(16)

when "puts" then @inst << :puts
when "putnum" then @inst << :putnum
when "gets" then @inst << :gets
when "getnum" then @inst << :getnum

end
end
@inst
end
end

end

if $0 == __FILE__
asm = <<EOS
push,a
puts
setl,1
rot
exp
swit
EOS
p Croud::Compiler.compile(asm)
end
128 changes: 128 additions & 0 deletions lib/croud/parse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require "strscan"

SYMS = {" " => 0, "(" => 1, ")" => 2, "_" => 3}

CLOUD_SHAPE = {"5b2" => "1", "582" => "2", "17a" => "3",
"5fb2" => "4", "17fa" => "5"}

OPNUM = {11 => :push, 12 => :drop, 13 => :dup, 14 => :swap,
21 => :roll, 22 => :swit, 23 => :rot, 24 => :slide, 25 => :exp,
31 => :add, 32 => :sub, 33 => :mul, 34 => :div,
41 => :setl, 42 => :jmpl, 43 => :zjmpl, 44 => :nzjmpl,
51 => :puts, 52 => :gets, 53 => :putnum, 54 => :getnum}

module Croud
class Parse

def self.parse(src)
new(src).parse
end

def initialize(src)
@src = src.split(/\n/)
@asm = ""
end

def parse
nums = inst = ""

while line = @src.shift
next if line !~ /[\(\) _]/
nums += lpair2num(line, @src.shift)
end
s = StringScanner.new(nums)
while cld = s.scan(/0*([15][^2a]+[2a])/) # each cloud
if cld =~ /5(..)a/ # numeric literal
inst += cld2num($1).to_s(16)
else
cld.delete!("0")
inst += CLOUD_SHAPE[cld].to_s
end
end

s = StringScanner.new(inst)
while op = s.scan(/\d\d/)
@asm += OPNUM[op.to_i].to_s
if (op == "11") || (op == "41") || (op == "42") || (op == "43") || (op == "44")
n = s.scan(/[0-9a-f]/)
@asm += ","
@asm += n
end
@asm += "\n"
end
@asm
end

def lpair2num(*lines)
ul,dl = *lines
nums = ""

ul.split(//).zip(dl.split(//)).each do |pair|
nums += pair2num(*pair)
end
nums
end

def pair2num(*pair)
u,d = *pair

if (u =~ /[ \(\)_]/) && (u =~ /[ \(\)_]/)
num = SYMS[u] * 4 + SYMS[d]
else
raise "parse error"
end
num.to_s(16)
end

def cld2num(s)
s.gsub("0","00").gsub("c","10").gsub("3","01").gsub("f","11").to_i(2)
end
end
end

if $0 == __FILE__
# puts Croud::Parse.parse(ARGF.read)
src = <<EOS
() () ( _) () () (__) () () (_) ()
(_) (_) (__) (_) (_) ( ) (_) (_) (__) (_)
EOS
puts Croud::Parse.parse(src)
end

=begin
Some rules:
(
( = (( = 1 *4 + 1 = 5
)
_ = )_ = 2 * 4 + 3 = 11 = 0xb
Therefore,
()
(_) 1 :5b2
()
( ) 2 :582
()
(_) 3 :17a
(_)
(__) 4 :5fb2
(_)
(__) 5 :17fa
Numerics
( _)
(_ ) :53ca -> 3c -> 01,10 -> 0x6
( _)
( ) :50ca -> 0c > 0,10 -> 0x3
=end

36 changes: 36 additions & 0 deletions lib/croud/stack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Croud

class Stack

def initialize(n=2)
@stc = []
n.times do
@stc << []
end
@cur = 0
end

def stc
@stc[@cur]
end

def switch
@stc[@cur], @stc[@cur + 1] = @stc[@cur + 1], @stc[@cur]
end

def expand
@stc << []
end

def slide
@stc[@cur + 1] << @stc[@cur].pop
end

def rotate
@stc << @stc.shift
end

end

end

Loading

0 comments on commit d0a6732

Please sign in to comment.