rubymacros 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -0
- data/COPYING.LGPL +503 -158
- data/History.txt +115 -5
- data/Makefile +68 -0
- data/README.txt +29 -6
- data/TODO +1 -0
- data/bin/macroruby +69 -0
- data/example/__dir__.rb +18 -0
- data/example/__dir___wrap.rb +18 -0
- data/example/andand.rb +18 -0
- data/example/andand_wrap.rb +18 -0
- data/example/assert.rb +29 -8
- data/example/assert0.rb +11 -0
- data/example/assert0_wrap.rb +5 -0
- data/example/assert_does_nothing_when_disabled.rb +19 -0
- data/example/assert_wrap.rb +21 -0
- data/example/expected_output.txt +88 -0
- data/example/formless_macro.rb +123 -0
- data/example/formless_macro_wrap.rb +20 -0
- data/example/inline.rb +97 -0
- data/example/linenum.rb +19 -1
- data/example/linenum_user.rb +18 -0
- data/example/linenum_wrap.rb +18 -0
- data/example/loop.rb +18 -0
- data/example/loop_wrap.rb +18 -0
- data/example/meta.rb +25 -0
- data/example/meta_wrap.rb +20 -0
- data/example/nilresult.rb +26 -0
- data/example/nilresult_wrap.rb +21 -0
- data/example/pipeline.rb +37 -0
- data/example/rescuing.rb +33 -0
- data/example/rescuing_wrap.rb +21 -0
- data/example/role.rb +103 -0
- data/example/role_with_eval.rb +92 -0
- data/example/self_in_class.rb +27 -0
- data/example/separated_scope.rb +42 -0
- data/example/separated_scope_wrap.rb +20 -0
- data/example/simple.rb +18 -0
- data/example/simple_wrap.rb +18 -0
- data/example/unproc.rb +31 -0
- data/example/unproc_wrap.rb +21 -0
- data/example/unroll.rb +34 -0
- data/example/unroll_macros.rb +119 -0
- data/example/unroll_wrap.rb +22 -0
- data/example/with.rb +50 -7
- data/example/with_wrap.rb +19 -0
- data/lib/macro.rb +307 -72
- data/lib/macro/ReduceWithsFor_RedParse_RedParse__MacroMixin_RedParse__WithMacros_1_8.rb +18880 -0
- data/lib/macro/ReduceWithsFor_RedParse_RedParse__MacroMixin_RedParse__WithMacros_1_9.rb +19101 -0
- data/lib/macro/form.rb +136 -27
- data/lib/macro/node.rb +64 -0
- data/lib/macro/version.rb +2 -5
- data/lib/rubymacros.rb +19 -0
- data/lib/rubymacros/version.rb +23 -0
- data/lib/weakkeyhash.rb +18 -0
- data/rubymacros.gemspec +60 -0
- data/test/test_all.rb +27 -2
- data/test/test_examples.rb +91 -0
- data/test/test_expand.rb +56 -1
- data/test/test_form.rb +108 -10
- data/test/test_unroll.rb +120 -0
- metadata +93 -65
- data/Rakefile +0 -37
data/example/assert0.rb
ADDED
@@ -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
|
data/example/assert_wrap.rb
CHANGED
@@ -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'
|
data/example/inline.rb
ADDED
@@ -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/¶m 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
|
data/example/linenum.rb
CHANGED
@@ -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
|
35
|
+
p __LINE__ #should print 35
|
18
36
|
end
|
data/example/linenum_user.rb
CHANGED
@@ -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
|
|