Hatena::Grouprubyist

たばさの RSSフィード

11/26(月) 2007

道具めも

| 道具めも - たばさの を含むブックマーク はてなブックマーク - 道具めも - たばさの


# whitespace-rd-ana.rb

# analyze whitespace sources
# 
# 
# 
# 


$k_sp ="[Space]"
$k_tb ="[Tab]"
$k_lf ="[LF]"

$w=[[" ", $k_sp ],
  ["\t", $k_tb ],
  ["\n", $k_lf ]
]


$mode=1

def pro o
  #print o
end


def trans n, wo, mode=$mode
  from,to = 0,1
  from,to = to, from if mode != 1
  wo.gsub!($w[n][from],$w[n][to])
  wo
end

def delcomment wo
  from,to = 0,1
  from,to = to, from if $mode != 1
  (
    wo.sub!(/^([^\[\n]*)/,'') 
    wo.gsub!(/\]([^\[\n]*)/,']')
  ) if $mode == 1 
  wo
end

def addCr wo
  wo.gsub!($w[2][1],$w[2][1]+"\n") 
end

def wpush wo,w
  w+wo
end

def wpop wo
  /(\[[^\]]*\])(.*)/ =~ wo
  [$1,$2]
end

def wpop2 wo
  w,wo=wpop wo
  y,wo=wpop wo
  [w+y,wo]
end

def analytab so
    o,so = wpop so
    pro o
    case o
    when $k_lf
      print "I/O."
      o,so = wpop2 so
      case o
      when $k_sp+$k_sp
        print"print.Chr"
      when $k_sp+$k_tb
        print"print.Num"
      when $k_tb+$k_sp
        print"read.Chr"
      when $k_tb+$k_tb
        print"read.Num"
      else
        print"?"
      end
    when $k_tb
      print"HeapAc."
      o,so = wpop so
      if o==$k_sp
        print"store"
      elsif o==$k_tb
        print"retrieve"
      else
        print"?"
      end
    when $k_sp
      print "Arith."
      o,so = wpop2 so
      case o
      when $k_sp+$k_sp
        print"+"
      when $k_sp+$k_tb
        print"-"
      when $k_sp+$k_lf
        print"*"
      when $k_tb+$k_sp
        print"/"
      when $k_tb+$k_tb
        print"%"
      else
        print"?"
      end
    else
      print"?"
    end
    [o,so]
end

def get01 wo
  o =""
  num =""
  len =0
  while 1
    o,wo = wpop wo
    pro o
    break if o==$k_lf || o==nil
    num += (o==$k_sp ? "0" : "1")
    len+=1
  end
  [o,wo,num.to_i(2),num]
end

def getnum wo
  o,wo,num = get01 wo
  pro num
  if num>' '[0] && num<= '~'[0] # && /[0-9a-zA-Z]/ =~ num.chr
   print"(#{num};#{num.chr})"
  else
   print"(#{num})"
  end
  [o,wo]
end

def str8 w
 a=""
 l=w.length/8
 l.times{|i|
  a<<w[i*8,8].to_i(2)
 }
 a
end

def getlabel wo
  o,wo,num ,str = get01 wo
  pro num
  print" _#{num}..#{str8(str)}" #num.chr
  [o,wo]
end

def stackmanip so
    o,so = wpop so
    pro o
    case o
    when $k_lf
      o,so = wpop so
      pro o
      case o
      when $k_tb
        print "Swap."
      when $k_lf
        print "Discard."
      when $k_sp
        print"Dup."
      else
        print"?"
      end
    when $k_tb
      o,so = wpop so
      pro o
      case o
      when $k_sp
        print "CopyNth."
        o,so = getnum so
      when $k_lf
        print "SlideN."
        o,so = getnum so
      else
        print"?"
      end
    when $k_sp
      print "pushNum."
      o,so = getnum so
    else
      print"?"
    end
    [o,so]
end

def analy wo
  o,so = wpop wo
  pro o
  case o
  when $k_lf
    print "_" #"FlowCtrl."
    o,so = wpop2 so
    case o
    when $k_sp+$k_sp
      print "Label"
      o,so = getlabel so
    when $k_sp+$k_tb
      print "callSub"
      o,so = getlabel so
    when $k_sp+$k_lf
      print "UnconJp"
      o,so = getlabel so
    when $k_tb+$k_sp
      print "If0JpTo"
      o,so = getlabel so
    when $k_tb+$k_tb
      print "If-JpTo"
      o,so = getlabel so
    when $k_tb+$k_lf
      print "ret."
    when $k_lf+$k_lf
      puts "exit"
    else
    end
  when $k_tb
    o,so = analytab so
  when $k_sp
    print "stack." #"st_manip."
    o,so = stackmanip so
  else
    print"?"
  end
  wo = so
end

def dp w
p w
end

$d=""
#p ARGV[0]
if ARGV[0]!=nil ;open(ARGV[0]){|f|
 f.each_line{|l|
  $d<<l
 }
}
else 
 while gets
 $d<<$_
 end
end

wo = delcomment(trans(2,trans(1,trans(0,$d))))
  # wo = addCr wo
  # dp wo
while (wo=analy(wo)).length>0
   puts
end



# ws-comp.rb

# no-whitespace 2 ws
# [sp][tab][lf] etc. can be used.
#
# [120] int
# ['a] char 'a'
# [print "str"] str print
# [print 123] int print
# [putc a] one char print

$sp=" "
$tb="\t"
$lf="\x0a"
$w=[
       [$sp,"[space]"],
       [$tb,"[tab]"],
       [$lf,"[lf]"],
       [$sp,"[sp]"],
       [$tb,"[tb]"],
       [$sp,"[s]"],
       [$tb,"[t]"],
       [$lf,"[l]"]
]


def trans n, wo
  from,to = 1,0
  wo.gsub!($w[n][from],$w[n][to])
  wo
end

def delcomment wo
  from,to = 1,0
  (
    wo.gsub!("\n",'') 
    wo.sub!(/^([^\[\n]*)/,'') 
    wo.gsub!(/\]([^\[\n]*)/,']')
  )
  wo
end

def ws_print ch
 "[S][S][S]"+"[#{ch}]"+"[T][L][S][S]"
end

def printstrcalc d
 while 1
  /\[ *print *"([^"]+)" *\]/ =~ d
  #p $1
  return d if $1==nil
  a=""
  $1.each_byte{|i|
  a<< ws_print(i)
  }
  ahead=$` 
  alast=$'
  d = ahead +a +alast
 end
end

def printnumcalc d
 while 1
  /\[ *print *([0-9]+) *\]/ =~ d
  #p $1
  return d if $1==nil
  ahead=$` 
  alast=$'
  d = ahead +ws_print($1) +alast
 end
end

def putccalc d
 while 1
  /\[ *putc *(.) *\]/ =~ d
  #p $1
  return d if $1==nil
  ahead=$` 
  alast=$'
  d = ahead +ws_print($1[0]) +alast
 end
end

def numcalc d
 while 1
  /\[([0-9]+)\]/ =~ d
  # p $1
  return d if $1==nil
  a=$1.to_i.to_s(2)
  ahead=$` 
  alast=$'
  a.gsub!("1","[t]").gsub!("0","[s]")
  d = ahead +a +"[lf]" +alast
 end
end

def charcalc d
 while 1
  /\['(.)\]/ =~ d
  p $1
  return d if $1==nil
  a=$1[0].to_s(2)
  ahead=$` 
  alast=$'
  a.gsub!("1","[t]").gsub!("0","[s]")
  d = ahead +a +"[lf]" +alast
 end
end

exit if ARGV.length < 2 
# if ARGV==[]
# source =$stdin 
# else
 source =File.new(ARGV[0], "r")
# end

d = source.readlines.join
d = printstrcalc d
d = printnumcalc d
d = putccalc d
d = numcalc d
d = charcalc d
d=delcomment(d)
d.downcase!
$w.length.times{|t|
  d=trans(t,d)
}
open(ARGV[1],"w+b"){|f|
 f.write(d)
}


# ws-scomp.rb

# no-whitespace simple 2 ws
# line head must be in "stl".
# if not, ignored as comments.
# read "stl" only.

$sp=" "
$tb="\t"
$lf="\x0a"
$w=[
       [$sp,"s"], #Space
       [$tb,"t"], #Tab
       [$lf,"l"]  #Lf
]


def trans n, wo
  from,to = 1,0
  wo.gsub!($w[n][from],$w[n][to])
  wo
end

def delcomment wo
  wo.gsub!(/[^stl]/,'') 
end

(puts "usage : #$0 infile outfile";exit) if ARGV.length < 2 
source =File.new(ARGV[0], "r")

lines = source.readlines
d = lines.select{|i| /^[stl]/ =~ i }.join
d.downcase!
delcomment(d)
p d
$w.length.times{|t|
  d=trans(t,d)
}
open(ARGV[1],"w+b"){|f|
 f.write(d)
}

トラックバック - http://rubyist.g.hatena.ne.jp/hatecha/20071126
カレンダー
<< 2007/11 >>
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
archive Error : RSSが取得できませんでした。