|
Revision 7916, 1.5 kB
(checked in by gyuque, 5 years ago)
|
|
tinyaction: added a util. script
|
| Line | |
|---|
| 1 | module X2
|
|---|
| 2 | class TempBrewer
|
|---|
| 3 | def initialize(funcs)
|
|---|
| 4 | @funcs = funcs
|
|---|
| 5 | @indent = 0
|
|---|
| 6 | end
|
|---|
| 7 |
|
|---|
| 8 | attr_reader :output
|
|---|
| 9 |
|
|---|
| 10 | def out_func(f)
|
|---|
| 11 | rets = []
|
|---|
| 12 | idt = "\t" * @indent
|
|---|
| 13 |
|
|---|
| 14 | # make arglist
|
|---|
| 15 | args = (f[:args] != nil) ? f[:args].join(', ') : ""
|
|---|
| 16 |
|
|---|
| 17 | rets << "\n#{idt}static function #{f[:name]}(#{args})"
|
|---|
| 18 | rets << "#{f[:body].gsub(/^/, idt)}"
|
|---|
| 19 |
|
|---|
| 20 | return rets.join("\n")
|
|---|
| 21 | end
|
|---|
| 22 |
|
|---|
| 23 | def check_body(funcs)
|
|---|
| 24 | funcs.each{|f|
|
|---|
| 25 | bd = f[:body]
|
|---|
| 26 | bd.gsub!(/\A[^{]+/m, '')
|
|---|
| 27 | bd.gsub!(/[^}]+\Z/m, '')
|
|---|
| 28 |
|
|---|
| 29 | raise "function body for #{f[:name]} is illegal" if bd.match(/\A\s*\{.*\}\s*\Z/m) == nil
|
|---|
| 30 |
|
|---|
| 31 | f[:body] = bd
|
|---|
| 32 | }
|
|---|
| 33 | end
|
|---|
| 34 |
|
|---|
| 35 | def check_args(funcs)
|
|---|
| 36 | funcs.each{|f|
|
|---|
| 37 | f[:args].each{|a|
|
|---|
| 38 | raise "argument name #{a} for #{f[:name]} is illegal" if a.gsub(/^[_a-zA-Z$][_a-zA-Z$0-9]*$/ ,'').length > 0
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 | end
|
|---|
| 42 |
|
|---|
| 43 | def dump
|
|---|
| 44 | check_args(@funcs)
|
|---|
| 45 | check_body(@funcs)
|
|---|
| 46 | @output = ""
|
|---|
| 47 |
|
|---|
| 48 | out_ln class_header
|
|---|
| 49 |
|
|---|
| 50 | @indent = 1
|
|---|
| 51 | @funcs.each{|f|
|
|---|
| 52 | out_ln out_func(f)
|
|---|
| 53 | }
|
|---|
| 54 | @indent = 0
|
|---|
| 55 |
|
|---|
| 56 | out_ln class_footer
|
|---|
| 57 | end
|
|---|
| 58 |
|
|---|
| 59 | def out_ln(ln)
|
|---|
| 60 | @output << "#{ln}\n"
|
|---|
| 61 | end
|
|---|
| 62 |
|
|---|
| 63 | def class_header
|
|---|
| 64 | "class Temp {"
|
|---|
| 65 | end
|
|---|
| 66 |
|
|---|
| 67 | def class_footer
|
|---|
| 68 | "}"
|
|---|
| 69 | end
|
|---|
| 70 | end
|
|---|
| 71 | end
|
|---|
| 72 |
|
|---|
| 73 | funcs = [
|
|---|
| 74 | {
|
|---|
| 75 | :name => "testfunc",
|
|---|
| 76 | :args => ["pobj"],
|
|---|
| 77 | :body => %-
|
|---|
| 78 | {
|
|---|
| 79 | pobj.x += 1;
|
|---|
| 80 | } - },
|
|---|
| 81 | {
|
|---|
| 82 | :name => "testfunc2",
|
|---|
| 83 | :args => ["pobj2"],
|
|---|
| 84 | :body => %-
|
|---|
| 85 | {
|
|---|
| 86 | pobj2.y += 0.1;
|
|---|
| 87 | } -
|
|---|
| 88 | }
|
|---|
| 89 | ]
|
|---|
| 90 |
|
|---|
| 91 | br = X2::TempBrewer.new(funcs)
|
|---|
| 92 | br.dump
|
|---|
| 93 |
|
|---|
| 94 | puts br.output |
|---|