aboutsummaryrefslogtreecommitdiff
path: root/parse.rb
blob: bb8cbec3e5b6e4919b916e0097b22fa07d79bf30 (plain)
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
def sub_vars(string)
  outvars = []
  blockvars = []
  notvars = %w(true false nil)
  vars = string.split(",").map(&:strip)
  vars.each do |var|
    if var =~ /[a-z_][a-zA-Z0-9_]*?/ && !notvars.include?(var) then
      outvars << "Patter::Var"
      blockvars << var
    else
      outvars << var
    end
  end
  return outvars.join(", "), blockvars.join(", ")
end

lines = $stdin.readlines.map(&:chomp)
funs = []

puts "require './patter'"
puts "include Patter"
puts

lines.each do |line|
  if line =~ /\s*([a-z_][a-zA-Z0-9_]*?)\((.*)\) is\s*$/ then
    unless funs.include?($1) then
      funs << $1
      puts "patterfun#{$1} = Patter::Fun.new(:#{$1})"
    end
    arg, blk = sub_vars($2)
    if blk.empty? then
      puts "patterfun#{$1}.when(#{arg}) do"
    else
      puts "patterfun#{$1}.when(#{arg}) do |#{blk}|"
    end
  else
    puts line
  end
end