rubymacros 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -0
  2. data/COPYING.LGPL +503 -158
  3. data/History.txt +115 -5
  4. data/Makefile +68 -0
  5. data/README.txt +29 -6
  6. data/TODO +1 -0
  7. data/bin/macroruby +69 -0
  8. data/example/__dir__.rb +18 -0
  9. data/example/__dir___wrap.rb +18 -0
  10. data/example/andand.rb +18 -0
  11. data/example/andand_wrap.rb +18 -0
  12. data/example/assert.rb +29 -8
  13. data/example/assert0.rb +11 -0
  14. data/example/assert0_wrap.rb +5 -0
  15. data/example/assert_does_nothing_when_disabled.rb +19 -0
  16. data/example/assert_wrap.rb +21 -0
  17. data/example/expected_output.txt +88 -0
  18. data/example/formless_macro.rb +123 -0
  19. data/example/formless_macro_wrap.rb +20 -0
  20. data/example/inline.rb +97 -0
  21. data/example/linenum.rb +19 -1
  22. data/example/linenum_user.rb +18 -0
  23. data/example/linenum_wrap.rb +18 -0
  24. data/example/loop.rb +18 -0
  25. data/example/loop_wrap.rb +18 -0
  26. data/example/meta.rb +25 -0
  27. data/example/meta_wrap.rb +20 -0
  28. data/example/nilresult.rb +26 -0
  29. data/example/nilresult_wrap.rb +21 -0
  30. data/example/pipeline.rb +37 -0
  31. data/example/rescuing.rb +33 -0
  32. data/example/rescuing_wrap.rb +21 -0
  33. data/example/role.rb +103 -0
  34. data/example/role_with_eval.rb +92 -0
  35. data/example/self_in_class.rb +27 -0
  36. data/example/separated_scope.rb +42 -0
  37. data/example/separated_scope_wrap.rb +20 -0
  38. data/example/simple.rb +18 -0
  39. data/example/simple_wrap.rb +18 -0
  40. data/example/unproc.rb +31 -0
  41. data/example/unproc_wrap.rb +21 -0
  42. data/example/unroll.rb +34 -0
  43. data/example/unroll_macros.rb +119 -0
  44. data/example/unroll_wrap.rb +22 -0
  45. data/example/with.rb +50 -7
  46. data/example/with_wrap.rb +19 -0
  47. data/lib/macro.rb +307 -72
  48. data/lib/macro/ReduceWithsFor_RedParse_RedParse__MacroMixin_RedParse__WithMacros_1_8.rb +18880 -0
  49. data/lib/macro/ReduceWithsFor_RedParse_RedParse__MacroMixin_RedParse__WithMacros_1_9.rb +19101 -0
  50. data/lib/macro/form.rb +136 -27
  51. data/lib/macro/node.rb +64 -0
  52. data/lib/macro/version.rb +2 -5
  53. data/lib/rubymacros.rb +19 -0
  54. data/lib/rubymacros/version.rb +23 -0
  55. data/lib/weakkeyhash.rb +18 -0
  56. data/rubymacros.gemspec +60 -0
  57. data/test/test_all.rb +27 -2
  58. data/test/test_examples.rb +91 -0
  59. data/test/test_expand.rb +56 -1
  60. data/test/test_form.rb +108 -10
  61. data/test/test_unroll.rb +120 -0
  62. metadata +93 -65
  63. data/Rakefile +0 -37
@@ -1,3 +1,21 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
1
19
  require 'macro'
2
20
  Macro.require 'example/linenum.rb'
3
21
 
@@ -1,3 +1,21 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
1
19
  =begin doesn't work yet
2
20
  macro loop(&body)
3
21
  :(while true
@@ -1,3 +1,21 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
1
19
  require 'macro'
2
20
  Macro.require 'example/loop.rb'
3
21
 
@@ -0,0 +1,25 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ module X
20
+ class<<self
21
+ end
22
+ end
23
+
24
+ class<<self
25
+ end
@@ -0,0 +1,20 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ require "macro"
20
+ Macro.require "example/meta.rb"
@@ -0,0 +1,26 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ macro nilresult
20
+ nil
21
+ end
22
+
23
+
24
+ nilresult
25
+
26
+ p Macro.expand( "a; nilresult; b" ).unparse
@@ -0,0 +1,21 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ require 'macro'
20
+ Macro.require 'example/nilresult'
21
+
@@ -0,0 +1,37 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ macro pipeline stages
20
+ result=:(begin end)
21
+
22
+ end
23
+
24
+
25
+ =begin
26
+ (0..9).map{|n| n+100 }.select{|n| n%2==0 }.inject(0){|sum,n| sum+n }
27
+
28
+ begin
29
+ sum=0
30
+ for x in (0..9)
31
+ x=x+100
32
+ next if x%2==0
33
+ sum=sum+x
34
+ end
35
+ sum
36
+ end
37
+ =end
@@ -0,0 +1,33 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ macro rescuing_vars(method)
20
+ method.body=:(
21
+ begin
22
+ ^method.body
23
+ rescue => e; puts "rescuing" ; #raise
24
+ end
25
+ )
26
+ return method
27
+ end
28
+
29
+ rescuing_vars def foo
30
+ bar
31
+ end
32
+
33
+ foo
@@ -0,0 +1,21 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ require 'macro'
20
+ Macro.require 'example/rescuing'
21
+
@@ -0,0 +1,103 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ # Role base class. Use SimpleDelegator instead?
20
+ class Role
21
+ def initialize(mappings)
22
+ @mappings = mappings
23
+ end
24
+ attr :mappings
25
+ end
26
+
27
+ # Context (UseCase) base class.
28
+ class Context
29
+ end
30
+
31
+ macro context_class name, roles, verbs
32
+ lc_roles=roles.map{|role|
33
+ ConstantNode===role or fail
34
+ role.unparse.downcase.gsub("::","__")
35
+ }
36
+ sym_roles=lc_roles.map{|role|
37
+ LiteralNode[role]
38
+ }
39
+ at_roles=lc_roles.map{|role|
40
+ VarNode["@"+role]
41
+ }
42
+ var_roles=lc_roles.map{|role|
43
+ VarNode[role]
44
+ }
45
+ :(
46
+ # We can think of a context as setting a scene.
47
+ class ^name < Context
48
+ attr_accessor ^*sym_roles
49
+ def initialize(^*var_roles)
50
+ ^*at_roles = ^*var_roles
51
+ end
52
+ ^verbs.map{|verb| context_verb(^*verb) }
53
+ end
54
+ )
55
+ end
56
+
57
+ macro context_verb(name, *args)
58
+ :(:(
59
+ def ^^name(^^*args,&block)
60
+ ^*roles.zip(at_roles).map{|role,var|
61
+ :((^var).(^eval(role.unparse).mappings[name])(^^*args,&block))
62
+ }
63
+ end
64
+ ))
65
+ end
66
+
67
+
68
+
69
+ # --- example ---
70
+
71
+ # Mixins are fixed roles.
72
+ class Balance
73
+ def initialize
74
+ @balance = 0
75
+ end
76
+ def availableBalance
77
+ @balance
78
+ end
79
+ def increaseBalance(amount)
80
+ @balance += amount
81
+ puts "Tranfered from account #{__id__} $#{amount}"
82
+ end
83
+ def decreaseBalance(amount)
84
+ @balance -= amount
85
+ puts "Tranfered to account #{__id__} $#{amount}"
86
+ end
87
+ end
88
+
89
+ Balance::TransferSource= Role.new :transfer => :decreaseBalance
90
+ Balance::TransferDestination= Role.new :transfer => :increaseBalance
91
+
92
+ context_class Balance::Transfer,
93
+ Balance::TransferSource, Balance::TransferDestination,
94
+ [transfer, amount]
95
+
96
+ class Account<Balance
97
+ # An account by definition has a balance.
98
+ end
99
+
100
+ acct1 = Account.new
101
+ acct2 = Account.new
102
+
103
+ Balance::Transfer.new(acct1, acct2).transfer(50)
@@ -0,0 +1,92 @@
1
+ =begin
2
+ rubymacros - a macro preprocessor for ruby
3
+ Copyright (C) 2008, 2016 Caleb Clausen
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ def Context roles, verbs
20
+ lc_roles=roles.map{|role|
21
+ role.downcase.gsub("::","__")
22
+ }
23
+ sym_roles=lc_roles.map{|role|
24
+ ":"+role
25
+ }.join(',')
26
+ at_roles=lc_roles.map{|role|
27
+ "@"+role
28
+ }
29
+ var_roles=lc_roles.join(',')
30
+ result=Class.new(Context)
31
+ result.class_eval "
32
+ # We can think of a context as setting a scene.
33
+ attr_accessor #{sym_roles}
34
+ def initialize(#{var_roles})
35
+ #{at_roles.join ','} = #{var_roles}
36
+ end
37
+ #{verbs.map{|verb| Context.verb(roles.zip(at_roles), verb) }.join("\n")}
38
+ "
39
+ return result
40
+ end
41
+
42
+ # Context (UseCase) base class.
43
+ class Context
44
+ def self.verb(roles_n_vars, name)
45
+ "
46
+ def #{name}(*args, &block)
47
+ #{roles_n_vars.map{|role,var|
48
+ "#{var}.#{eval(role)[name.to_sym]}(*args, &block)"
49
+ }.join("\n")}
50
+ end
51
+ "
52
+ end
53
+ end
54
+
55
+
56
+
57
+ # --- example ---
58
+
59
+ # Mixins are fixed roles.
60
+ class Balance
61
+ def initialize
62
+ @balance = 0
63
+ end
64
+ def availableBalance
65
+ @balance
66
+ end
67
+ def increaseBalance(amount)
68
+ @balance += amount
69
+ puts "Tranfered from account #{__id__} $#{amount}"
70
+ end
71
+ def decreaseBalance(amount)
72
+ @balance -= amount
73
+ puts "Tranfered to account #{__id__} $#{amount}"
74
+ end
75
+ end
76
+
77
+ Balance::TransferSource= { :transfer => :decreaseBalance }
78
+ Balance::TransferDestination= { :transfer => :increaseBalance }
79
+
80
+ Balance::Transfer=Context(
81
+ %w[Balance::TransferSource Balance::TransferDestination],
82
+ %w[transfer]
83
+ )
84
+
85
+ class Account<Balance
86
+ # An account by definition has a balance.
87
+ end
88
+
89
+ acct1 = Account.new
90
+ acct2 = Account.new
91
+
92
+ Balance::Transfer.new(acct1, acct2).transfer(50)