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
@@ -0,0 +1,11 @@
1
+ macro assert
2
+ if $Debug
3
+ :( p :hi )
4
+ end
5
+ end
6
+
7
+ begin
8
+ assert
9
+ end
10
+
11
+ if assert; 123 end
@@ -0,0 +1,5 @@
1
+ require 'macro'
2
+ $Debug=1
3
+ Macro.load 'example/assert0.rb'
4
+ $Debug=nil
5
+ Macro.load 'example/assert0.rb'
@@ -0,0 +1,19 @@
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
+ assert false
@@ -1,5 +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
+
1
19
  require 'macro'
2
20
  $Debug=1
3
21
  Macro.require 'example/assert'
4
22
 
5
23
  test_assert
24
+
25
+ $Debug=nil
26
+ Macro.require 'example/assert_does_nothing_when_disabled'
@@ -0,0 +1,88 @@
1
+ # rubymacros - a macro preprocessor for ruby
2
+ # Copyright (C) 2008, 2016 Caleb Clausen
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Lesser General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ example/loop_wrap.rb :
18
+ 0
19
+ 1
20
+ 2
21
+ 3
22
+ 4
23
+ 5
24
+ 6
25
+ 7
26
+ 8
27
+ 9
28
+ example/andand_wrap.rb :
29
+ Success!
30
+ example/__dir___wrap.rb :
31
+ "example"
32
+ example/formless_macro_wrap.rb :
33
+ 0 1 2 3 4
34
+ 0 1 2 3 4
35
+ 0 1 2
36
+ 0 1 2
37
+ example/linenum_wrap.rb :
38
+ 35
39
+ example/separated_scope_wrap.rb :
40
+ 11
41
+ 10
42
+ example/simple_wrap.rb :
43
+ 3
44
+ example/unproc_wrap.rb :
45
+ :foo
46
+ example/unroll_wrap.rb :
47
+ 1
48
+ 2
49
+ 3
50
+ 1
51
+ 2
52
+ 3
53
+ 1
54
+ 2
55
+ 3
56
+ 1
57
+ 2
58
+ 3
59
+ 1
60
+ 2
61
+ 3
62
+ 1
63
+ 2
64
+ 3
65
+ 1
66
+ 2
67
+ 3
68
+ 4
69
+ 5
70
+ 6
71
+ 7
72
+ 8
73
+ 1
74
+ 2
75
+ 3
76
+ 4
77
+ 5
78
+ 6
79
+ 7
80
+ 8
81
+ 9
82
+ example/with_wrap.rb :
83
+ [999, 3]
84
+ example/assert_wrap.rb :
85
+ all assertions passed
86
+ example/assert0_wrap.rb :
87
+ :hi
88
+ :hi
@@ -0,0 +1,123 @@
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
+ =begin surely doesn't work
20
+ macro formless_macro(name,args,body)
21
+ :(
22
+ macro ^name (^CommaOpNode[*args])
23
+ :(^^body)
24
+ end
25
+ )
26
+ end
27
+ =end
28
+
29
+ macro formless_macro(name,args,body)
30
+ argnames=args.map{|arg|
31
+ case arg
32
+ when RedParse::VarNode;
33
+ fail if /[A-Z$@]/===arg.name
34
+ arg
35
+ when RedParse::CallNode;
36
+ fail if /[A-Z$@]/===arg.name
37
+ fail if arg.receiver or arg.params or arg.block
38
+ RedParse::VarNode[arg.name]
39
+ else fail
40
+ end
41
+ } if args
42
+ Macro::MacroNode[nil,name.name,argnames,
43
+ Macro::FormNode[body],
44
+ [],nil,nil
45
+ ]
46
+ end
47
+
48
+ #form escape not enclosed in a form... that's deeply weird
49
+ #i'm frankly surprised that this works...
50
+
51
+ formless_macro(loop,[body],
52
+ while true
53
+ ^body
54
+ end
55
+ )
56
+
57
+ i=0
58
+ loop(
59
+ print i, ' '
60
+ break if (i+=1)>=5
61
+ )
62
+ print "\n"
63
+
64
+ body=nil
65
+ formless_macro(loop2,[body],
66
+ while true
67
+ ^body
68
+ end
69
+ )
70
+
71
+ i=0
72
+ loop2(
73
+ print i, ' '
74
+ break if (i+=1)>=5
75
+ )
76
+ print "\n"
77
+
78
+ macro formless_macro2(name,args=nil)
79
+ argnames=args.map{|arg|
80
+ case arg
81
+ when RedParse::VarNode;
82
+ fail if /[A-Z$@]/===arg.name
83
+ arg
84
+ when RedParse::CallNode;
85
+ fail if /[A-Z$@]/===arg.name
86
+ fail if arg.receiver or arg.params or arg.block
87
+ RedParse::VarNode[arg.name]
88
+ else fail
89
+ end
90
+ } if args
91
+ Macro::MacroNode[nil,name.name,argnames,
92
+ Macro::FormNode[yield],
93
+ [],nil,nil
94
+ ]
95
+ end
96
+
97
+ formless_macro2(loop3){
98
+ while true
99
+ ^yield
100
+ end
101
+ }
102
+
103
+ i=0
104
+ loop3{
105
+ print i, " "
106
+ break if (i+=1)>=3
107
+ }
108
+ print "\n"
109
+
110
+ body=nil
111
+ formless_macro2(loop4){
112
+ while true
113
+ ^yield
114
+ end
115
+ }
116
+
117
+ i=0
118
+ loop4{
119
+ print i, " "
120
+ break if (i+=1)>=3
121
+ }
122
+ print "\n"
123
+
@@ -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/formless_macro'
@@ -0,0 +1,97 @@
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 inline(method)
20
+ #block/&param must be re-varified if used more than once
21
+ #lvar defns in block should be scoped to just the block
22
+ #maybe block should always remain a proc, rather than inlining it?
23
+
24
+ #some things aren't supported in inline methods (yet)
25
+ #return|redo|retry|yield keywords, & parameter, and optional args are disallowed
26
+ fail if method.rfind{|n| RedParse::KWCallNode===n and /^(?:return|redo|retry|yield)$/===n.ident }
27
+ if params=method.params
28
+ fail if RedParse::UnaryAmpNode===params.last
29
+ last_non_star=params.last
30
+ last_non_star=params[-2] if RedParse::UnaryStarNode===last_non_star
31
+ fail if RedParse::AssignNode===last_non_star
32
+ end
33
+
34
+ result=:(:(inline_self=^receiver; begin end))
35
+ pre=result.val
36
+ body=result.val[-1]
37
+
38
+ #copy method innards to begin node
39
+ body.body=method.body
40
+ body.rescues=method.rescues
41
+ body.ensure=method.ensure
42
+ body.else=method.else
43
+
44
+ #make a list of known params to inline method
45
+ params={}
46
+ #params should be re-varified to ensure theyre evaled exactly once
47
+ #without hygienic macros, param names (+inline_self) leak into the caller!
48
+ method.params.each{|param|
49
+ case param
50
+ when RedParse::VarNode
51
+ params[param.name]=1
52
+ # pre[0]+= :( :((^^param)=^param) )
53
+ when RedParse::UnaryStarNode,RedParse::UnAmpNode
54
+ param=param.val
55
+ params[param.name]=1
56
+ # pre[0]+= :( :((^^param)=^param) )
57
+ when RedParse::AssignNode
58
+ default=param.right
59
+ param=param.left
60
+ params[param.name]=1
61
+ # pre[0]+= :( :((^^param)=^param||^default) )
62
+ end
63
+ } if method.params
64
+
65
+ inline_self=RedParse::VarNode["inline_self"]
66
+
67
+ body.walk{|parent,i,subi,node|
68
+ newnode=nil
69
+ case node
70
+ when RedParse::VarNode
71
+ #search method for params and escape them
72
+ if params[node.name]
73
+ newnode=RedParse::ParenedNode[Macro::FormEscapeNode[node]]
74
+ end
75
+
76
+ #what to do with receiver? implicit and explicit refs must be replaced
77
+ when RedParse::VarLikeNode
78
+ newnode=inline_self if node.name=="self"
79
+ when RedParse::CallNode
80
+ node.receiver||=inline_self
81
+
82
+ end
83
+ if newnode
84
+ if subi
85
+ parent[i][subi]=newnode
86
+ else
87
+ parent[i]=newnode
88
+ end
89
+ end
90
+ true
91
+ }
92
+
93
+ result.rebuild_transform #shouldn't be needed
94
+
95
+ result= Macro::MacroNode[nil,method.name,method.params,result]
96
+ return result
97
+ end
@@ -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
  macro __DIR__
2
20
  :(File.dirname(__FILE__))
3
21
  end
@@ -14,5 +32,5 @@ def user_of_foo
14
32
  end
15
33
 
16
34
  def linenumuser
17
- p __LINE__ #should print 17
35
+ p __LINE__ #should print 35
18
36
  end
@@ -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
  SCRIPT_LINES__={}
2
20
  require 'example/linenum_wrap.rb'
3
21