maroon 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41c4d573b28bd312c8a720f51ae4b283cd9311bd
4
- data.tar.gz: a2a0f21a0278e3af009c884204e18bfa5c633d55
3
+ metadata.gz: 8869041ee066afcbfc382e5679bdacbfef33401d
4
+ data.tar.gz: 30910b3f6b2a216c99bc8b204d52fe6b735a7c01
5
5
  SHA512:
6
- metadata.gz: b2a48a828cc60e7e8e33bb4df82bdfacc6ff6c128f142a08f0fdca0858dfa5c25ffa2647855d20a745a2c1338a9ea08b0b71dc1beed452ac38955e1512317085
7
- data.tar.gz: 00111ba32f2d574dc7ccfd1ba1435b241fd17e97736bc5ca573afab56c6463f26a50f67b40839381a2c3b0383aff3073fc535eac7efb05e253e78c215ef69e3c
6
+ metadata.gz: 91a7a468e03ddb94d1549f138d52df08cf819bd85d4942064d867e0bcf6540911fc2882d33b3a33f92188479af5404deed83d730785db4466e52ac87cba895f4
7
+ data.tar.gz: ad2cc204002f1f19c07cfb8f596648bb78dd2541d4de4c27565e48e355a1aa476d0a3c16fcffe26647921a31356a546552d6967e308a723a39bc6984017c109a
@@ -1,14 +1,17 @@
1
1
  require 'test/unit'
2
2
  require_relative 'test_helper'
3
+
3
4
  class MaroonInternal
4
5
 
5
6
  end
7
+ #Context::generate_files_in('.')
6
8
  class ContextTest < Test::Unit::TestCase
7
9
 
8
10
  def test_role_method_call
9
11
  name = :MyContextRoleMethodCall
10
12
  role_name = :rol
11
- Context::define name do
13
+
14
+ c=Context::define name do
12
15
  role role_name do
13
16
  def rolem(x, y)
14
17
  x+y
@@ -23,7 +26,7 @@ class ContextTest < Test::Unit::TestCase
23
26
  assert_equal(7, MyContextRoleMethodCall.new.add(3, 4))
24
27
  end
25
28
 
26
- def test_simple
29
+ def xtest_simple
27
30
  name = :MyContextSimple
28
31
  role_name = :r
29
32
  Context::define name do
@@ -31,13 +34,34 @@ class ContextTest < Test::Unit::TestCase
31
34
  end
32
35
  end
33
36
  assert(Kernel::const_defined? name)
37
+ end
34
38
 
39
+ def xtest_bind
40
+ name = :MyContextBind
41
+
42
+ c= Context::define name do
43
+ role :role_name do
44
+ def sum
45
+ @sum += role_name
46
+ end
47
+ end
48
+ def inter
49
+ @sum = 0
50
+ [1,2].each {|p|
51
+ bind :p=>:role_name
52
+ role_name.sum()
53
+ }
54
+ @sum
55
+ end
56
+ end
57
+ assert(Kernel::const_defined? name)
58
+ assert_equal(3, MyContextBind.new.inter)
35
59
  end
36
60
 
37
- def test_role_method
61
+ def xtest_role_method
38
62
  name = :MyContext
39
63
  role_name = :rol
40
- c = Context::define name do
64
+ Context::define name do
41
65
  role role_name do
42
66
  def rolem
43
67
  0+1
@@ -47,7 +71,7 @@ class ContextTest < Test::Unit::TestCase
47
71
  assert_equal(1, MyContext.new.send(:self_rol_rolem))
48
72
  end
49
73
 
50
- def test_role_method_args
74
+ def xtest_role_method_args
51
75
  name = :MyContextArgs
52
76
  role_name = :rol
53
77
  Context::define name do
@@ -60,7 +84,7 @@ class ContextTest < Test::Unit::TestCase
60
84
  assert_equal(7, MyContextArgs.new.send(:self_rol_rolem, 3, 4))
61
85
  end
62
86
 
63
- def test_role_method_splat
87
+ def xtest_role_method_splat
64
88
  name = :MyContextSplat
65
89
  role_name = :rol
66
90
  Context::define name do
@@ -73,24 +97,31 @@ class ContextTest < Test::Unit::TestCase
73
97
  assert_equal(7, MyContextSplat.new.send(:self_rol_rolem, 3, 4))
74
98
  end
75
99
 
76
- def test_role_method_block
100
+ def xtest_role_method_block
77
101
  name = :MyContextBlock
78
102
  role_name = :rol
79
- Context::define name do
103
+ c= Context::define name do
104
+ role :num do
105
+ def next
106
+ num + 3
107
+ end
108
+ end
109
+
80
110
  role role_name do
81
111
  def rolem(*args, &b)
82
112
  res = 0
83
113
  args.each { |x|
84
- res = b.call res, x
114
+ bind :x=>:num
115
+ res = b.call res, num.next
85
116
  }
86
117
  res
87
118
  end
88
119
  end
89
120
  end
90
- assert_equal(7, MyContextBlock.new.send(:self_rol_rolem, 3, 4) { |x, res| res + x })
121
+ assert_equal(9, MyContextBlock.new.send(:self_rol_rolem, 3, 4) { |x, res| res + x })
91
122
  end
92
123
 
93
- def test_class_method_block
124
+ def xtest_class_method_block
94
125
  name = :MyContextClass
95
126
  role_name = :rol
96
127
  Context::define name do
@@ -1,13 +1,18 @@
1
1
  require 'test/unit'
2
2
  require_relative '../generated/Tokens'
3
- require_relative '../generated/Production'
3
+ require_relative '../generated/AbstractSyntaxTree'
4
4
  require_relative 'test_helper'
5
- class ProductionTest < Test::Unit::TestCase
5
+
6
+ class AbstractSyntaxTreeTest < Test::Unit::TestCase
6
7
  def get_method_call &b
7
8
  exp = get_sexp &b
8
9
  exp[3]
9
10
  end
10
11
 
12
+ def get_context(roles={},contracts={},role_aliases={},defining=nil,private_interactions= {})
13
+ InterpretationContext.new(roles, contracts, role_aliases,defining,private_interactions)
14
+ end
15
+
11
16
  def test_rolemethod
12
17
  method_call = get_method_call { foo.bar }
13
18
 
@@ -19,7 +24,7 @@ class ProductionTest < Test::Unit::TestCase
19
24
  def get_production(method_call)
20
25
  contracts ={}
21
26
  roles = {:foo => {:bar => []}}
22
- Production.new(method_call, InterpretationContext.new(roles, contracts, nil, nil))
27
+ AbstractSyntaxTree.new(method_call, get_context(roles, contracts))
23
28
  end
24
29
 
25
30
  def test_call
@@ -1,4 +1,4 @@
1
- context :Production do
1
+ context :AbstractSyntaxTree do
2
2
  role :interpretation_context do
3
3
  end
4
4
  role :queue do
@@ -36,9 +36,19 @@ context :Production do
36
36
  def is_block_with_bind?
37
37
  if production.is_block?
38
38
  body = @production.last()
39
- if body && (exp = body[0])
40
- bind = Production.new exp, @interpretation_context
39
+ if body && (exp = body[1])
40
+ bind = AbstractSyntaxTree.new exp, @interpretation_context
41
41
  if bind.type == Tokens::call && bind.data == :bind
42
+ aliases = {}
43
+ list = exp.last[1..-1]
44
+ (list.length/2).times{|i|
45
+ local = list[i*2].last
46
+ role_name = list[i*2+1].last
47
+ raise 'Local in bind should be a symbol' unless local.instance_of? Symbol
48
+ raise 'Role name in bind should be a symbol' unless role_name.instance_of? Symbol
49
+ aliases[local] = role_name
50
+ }
51
+ @data = aliases
42
52
  true
43
53
  end
44
54
  end
@@ -48,7 +58,7 @@ context :Production do
48
58
  def is_rolemethod_call?
49
59
  can_be = production.is_call?
50
60
  if can_be
51
- instance = Production.new(production[1], @interpretation_context)
61
+ instance = AbstractSyntaxTree.new(production[1], @interpretation_context)
52
62
  can_be = instance.type == Tokens::role
53
63
  if can_be
54
64
  instance_data = instance.data
@@ -120,7 +130,7 @@ context :Production do
120
130
  end
121
131
  end
122
132
 
123
- def each
133
+ def each_production
124
134
  yield self
125
135
  if production.instance_of? Sexp || production.instance_of?(Array)
126
136
  @queue = @queue.push_array production
@@ -3,11 +3,12 @@ context :AstRewritter do
3
3
  end
4
4
 
5
5
  def initialize (ast, interpretation_context)
6
- @ast = Production.new ast, interpretation_context
6
+ @ast = AbstractSyntaxTree.new ast, interpretation_context
7
+ @roles = interpretation_context.roles
7
8
  end
8
9
 
9
10
  def rewrite!
10
- ast.each { |production|
11
+ ast.each_production { |production|
11
12
 
12
13
  case production.type
13
14
  when Tokens::rolemethod_call
@@ -16,44 +17,54 @@ context :AstRewritter do
16
17
  production[1] = nil
17
18
  when Tokens::block_with_bind
18
19
  block = production.last
19
- must_b_sym = 'aliased_role must be a Symbol'.to_sym
20
- local_must_b_sym = 'local must be a Symbol'.to_sym
21
- raise must_b_sym unless aliased_role.instance_of? Symbol
22
- raise local_must_b_sym unless local.instance_of? Symbol
23
- # assigning role player to role field
24
- #notice that this will be executed after the next block
25
- aliased_field = ('@' + aliased_role.to_s).to_sym
26
- temp_symbol = ('temp____' + aliased_role.to_s).to_sym
20
+ block.delete_at 1
21
+ production.data.each do |local,aliased_role|
22
+ must_b_sym = 'aliased_role must be a Symbol'.to_sym
23
+ local_must_b_sym = 'local must be a Symbol'.to_sym
24
+ raise must_b_sym unless aliased_role.instance_of? Symbol
25
+ raise local_must_b_sym unless local.instance_of? Symbol
26
+ unless @roles.has_key? aliased_role
27
+ role_names = []
28
+ @interpretation_context.each do |k,v|
29
+ role_names << k.to_s
30
+ end
31
+ raise aliased_role.to_s + ' is not a role. Available roles are ' + role_names.join(',')
32
+ end
33
+ # assigning role player to role field
34
+ #notice that this will be executed after the next block
35
+ aliased_field = ('@' + aliased_role.to_s).to_sym
36
+ temp_symbol = ('temp____' + aliased_role.to_s).to_sym
27
37
 
28
- assignment = Sexp.new
29
- assignment[0] = :iasgn
30
- assignment[1] = aliased_field
31
- load_arg = Sexp.new
32
- load_arg[0] = :lvar
33
- load_arg[1] = local
34
- assignment[2] = load_arg
35
- block.insert 1, assignment
38
+ assignment = Sexp.new
39
+ assignment[0] = :iasgn
40
+ assignment[1] = aliased_field
41
+ load_arg = Sexp.new
42
+ load_arg[0] = :lvar
43
+ load_arg[1] = local
44
+ assignment[2] = load_arg
45
+ block.insert 1, assignment
36
46
 
37
- # assign role player to temp
38
- # notice this is prepended Ie. inserted in front of the role player to role field
39
- assignment = Sexp.new
40
- assignment[0] = :lasgn
41
- assignment[1] = temp_symbol
42
- load_field = Sexp.new
43
- load_field[0] = :ivar
44
- load_field[1] = aliased_field
45
- assignment[2] = load_field
46
- block.insert 1, assignment
47
+ # assign role player to temp
48
+ # notice this is prepended Ie. inserted in front of the role player to role field
49
+ assignment = Sexp.new
50
+ assignment[0] = :lasgn
51
+ assignment[1] = temp_symbol
52
+ load_field = Sexp.new
53
+ load_field[0] = :ivar
54
+ load_field[1] = aliased_field
55
+ assignment[2] = load_field
56
+ block.insert 1, assignment
47
57
 
48
- # reassign original player
49
- assignment = Sexp.new
50
- assignment[0] = :iasgn
51
- assignment[1] = aliased_field
52
- load_temp = Sexp.new
53
- load_temp[0] = :lvar
54
- load_temp[1] = temp_symbol
55
- assignment[2] = load_temp
56
- block[block.length] = assignment
58
+ # reassign original player
59
+ assignment = Sexp.new
60
+ assignment[0] = :iasgn
61
+ assignment[1] = aliased_field
62
+ load_temp = Sexp.new
63
+ load_temp[0] = :lvar
64
+ load_temp[1] = temp_symbol
65
+ assignment[2] = load_temp
66
+ block[block.length] = assignment
67
+ end
57
68
  else
58
69
  #do nothing
59
70
  end
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
1
  ##
4
2
  # The Context class is used to define a DCI context with roles and their role methods
5
3
  # to define a context call define with the name of the context (this name will become the name of the class that defines the context)
@@ -37,27 +35,40 @@
37
35
  ##
38
36
  c = context :Context do
39
37
 
38
+
39
+
40
40
  def self.define(*args, &block)
41
+ name, base_class, default_interaction = *args
42
+ if default_interaction and (not base_class.instance_of?(Class)) then
43
+ base_class = eval(base_class.to_s)
44
+ end
45
+ if base_class and ((not default_interaction) and (not base_class.instance_of?(Class))) then
46
+ base_class, default_interaction = default_interaction, base_class
47
+ end
41
48
  @@with_contracts ||= nil
42
49
  @@generate_file_path ||= nil
43
- (alias :method_missing :role_or_interaction_method)
44
-
45
- base_class, ctx, default_interaction, name = self.send(:create_context_factory, args, block)
46
- if (args.last.instance_of?(FalseClass) or args.last.instance_of?(TrueClass)) then
47
- ctx.generate_files_in(args.last)
48
- end
49
- return ctx.send(:finalize, name, base_class, default_interaction, @@generate_file_path, @@with_contracts)
50
-
50
+ ctx = self.send(:create_context_factory, name, base_class, default_interaction, block)
51
+ transformer = Transformer.new name, ctx.roles,ctx.interactions,ctx.private_interactions,base_class, default_interaction
52
+ return transformer.transform @@generate_file_path, @@with_contracts
51
53
  end
52
54
 
53
55
  def self.generate_files_in(*args, &b)
54
- if block_given? then
55
- return role_or_interaction_method(:generate_files_in, *args, &b)
56
- end
56
+
57
57
  @@generate_file_path = args[0]
58
58
 
59
59
  end
60
60
 
61
+ def roles
62
+ @roles
63
+ end
64
+ def interactions
65
+ @interactions
66
+ end
67
+ def private_interactions
68
+ @private_interactions
69
+ end
70
+
71
+
61
72
  private
62
73
 
63
74
  def get_definitions(b)
@@ -77,15 +88,9 @@ c = context :Context do
77
88
  end
78
89
  end
79
90
 
80
- def self.create_context_factory(args, block)
81
- name, base_class, default_interaction = *args
82
- if default_interaction and (not base_class.instance_of?(Class)) then
83
- base_class = eval(base_class.to_s)
84
- end
85
- if base_class and ((not default_interaction) and (not base_class.instance_of?(Class))) then
86
- base_class, default_interaction = default_interaction, base_class
87
- end
88
- ctx = Context.new
91
+ def self.create_context_factory(name, base_class, default_interaction, block)
92
+
93
+ ctx = Context.new name, base_class, default_interaction
89
94
  ctx.instance_eval {
90
95
  sexp = block.to_sexp
91
96
  temp_block = sexp[3]
@@ -107,8 +112,7 @@ c = context :Context do
107
112
  ctx.instance_eval &block
108
113
  }
109
114
 
110
- return [base_class, ctx, default_interaction, name]
111
-
115
+ ctx
112
116
  end
113
117
 
114
118
  def self.with_contracts(*args)
@@ -121,183 +125,66 @@ c = context :Context do
121
125
 
122
126
  end
123
127
 
124
- def createInfo(definition)
125
- MethodInfo.new(definition, @defining_role, @private)
126
- end
127
-
128
128
  def is_definition?(exp)
129
129
  exp && (exp[0] == :defn || exp[0] == :defs)
130
130
  end
131
131
 
132
132
  def role(*args, &b)
133
133
  role_name = args[0]
134
- if (args.length.!=(1) or (not role_name.instance_of?(Symbol))) then
135
- return role_or_interaction_method(:role, *args, &b)
136
- end
134
+
137
135
  @defining_role = role_name
138
136
  @roles = {} unless @roles
139
137
  @roles[role_name] = Hash.new
140
-
141
- definitions = get_definitions(b)
142
-
143
- definitions.each do |exp|
144
- add_method(exp)
145
- end
146
-
147
- end
148
-
149
- def current_interpretation_context(*args, &b)
150
- if block_given? then
151
- return role_or_interaction_method(:current_interpretation_context, *args, &b)
138
+ if block_given?
139
+ definitions = get_definitions(b)
140
+ definitions.each do |exp|
141
+ add_method(exp)
142
+ end
152
143
  end
153
- InterpretationContext.new(@roles, @contracts, @role_alias, nil)
154
144
 
155
145
  end
156
146
 
157
147
  def get_methods(*args, &b)
158
- return role_or_interaction_method(:get_methods, *args, &b) if block_given?
159
148
  name = args[0]
160
149
  sources = (@defining_role ? (@roles[@defining_role]) : (@interactions))[name]
161
150
  if @defining_role and (not sources) then
162
151
  @roles[@defining_role][name] = []
163
152
  else
153
+ @private_interactions[name] = true if @private
164
154
  @interactions[name] = []
165
155
  end
166
156
 
167
157
  end
168
158
 
169
- def add_method(*args, &b)
170
- return role_or_interaction_method(:add_method, *args, &b) if block_given?
171
- exp = args[0]
172
- info = createInfo exp
173
- sources = get_methods(info.name)
174
- (sources << info)
175
- end
176
-
177
- def finalize(*args, &b)
178
- return role_or_interaction_method(:finalize, *args, &b) if block_given?
179
- name, base_class, default, file_path, with_contracts = *args
180
- code = generate_context_code(default, name)
181
- if file_path then
182
- name = name.to_s
183
- complete = ((((('class ' + name) + (base_class ? (('<< ' + base_class.name)) : (''))) + '
184
- ') + code.to_s) + '
185
- end')
186
- File.open((((('./' + file_path.to_s) + '/') + name) + '.rb'), 'w') do |f|
187
- f.write(complete)
188
- end
189
- complete
190
- else
191
- c = base_class ? (Class.new(base_class)) : (Class.new)
192
- if with_contracts then
193
- c.class_eval(
194
- 'def self.assert_that(obj)
195
- ContextAsserter.new(self.contracts,obj)
196
- end
197
- def self.refute_that(obj)
198
- ContextAsserter.new(self.contracts,obj,false)
199
- end
200
- def self.contracts
201
- @@contracts
202
- end
203
- def self.contracts=(value)
204
- @@contracts = value
205
- end')
206
- c.contracts = contracts
207
- end
208
- Kernel.const_set(name, c)
209
- begin
210
- temp = c.class_eval(code)
211
- rescue SyntaxError
212
- p 'error: ' + code
213
- end
214
-
215
- (temp or c)
216
- end
217
-
218
- end
219
-
220
- def generate_context_code(*args, &b)
221
- if block_given? then
222
- return role_or_interaction_method(:generate_context_code, *args, &b)
223
- end
224
- default, name = args
225
- getters = ''
226
- impl = ''
227
- interactions = ''
228
- @interactions.each do |method_name, methods|
229
- methods.each do |method|
230
- @defining_role = nil
231
- code = (' ' + method.build_as_context_method(current_interpretation_context))
232
- method.is_private ? ((getters << code)) : ((interactions << code))
233
- end
234
- end
235
- if default then
236
- (interactions << (((((((('
237
- def self.call(*args)
238
- arity = ' + name.to_s) + '.method(:new).arity
239
- newArgs = args[0..arity-1]
240
- obj = ') + name.to_s) + '.new *newArgs
241
- if arity < args.length
242
- methodArgs = args[arity..-1]
243
- obj.') + default.to_s) + ' *methodArgs
244
- else
245
- obj.') + default.to_s) + '
246
- end
247
- end
248
- '))
249
- (interactions << (('
250
- def call(*args);' + default.to_s) + ' *args; end
251
- '))
252
- end
253
- @roles.each do |role, methods|
254
- (getters << (('attr_reader :' + role.to_s) + '
255
- '))
256
- methods.each do |method_name, method_sources|
257
- unless (method_sources.length < 2) then
258
- raise(('Duplicate definition of ' + method_name.to_s))
259
- end
260
- unless (method_sources.length > 0) then
261
- raise(('No source for ' + method_name.to_s))
262
- end
263
- method_source = method_sources[0]
264
- @defining_role = role
265
-
266
- definition = method_source.build_as_context_method(current_interpretation_context)
267
- (impl << (' ' + definition.to_s)) if definition
268
- end
269
- end
270
- private_string = (getters + impl).strip! != '' ? '
271
- private
272
- ' : ''
273
- impl = impl.strip! != '' ? '
274
- ' + impl + '
275
- ' : '
276
- '
277
- interactions + private_string + getters + impl
278
-
279
- end
280
-
281
- def role_or_interaction_method(*arguments, &b)
282
- method_name, on_self = *arguments
283
- unless method_name.instance_of?(Symbol) then
284
- on_self = method_name
285
- method_name = :role_or_interaction_method
286
- end
287
- raise(('Method with out block ' + method_name.to_s)) unless block_given?
159
+ def add_method(definition)
160
+ name = if definition[1].instance_of? Symbol
161
+ definition[1]
162
+ else
163
+ (definition[1].select { |e| e.instance_of? Symbol }.map { |e| e.to_s }.join('.') + '.' + definition[2].to_s).to_sym
164
+ end
288
165
 
166
+ sources = get_methods(name)
167
+ (sources << definition)
289
168
  end
290
169
 
291
170
  def private
292
171
  @private = true
293
172
  end
294
173
 
295
- def initialize
174
+ def initialize(name,base_class,default_interaction)
296
175
  @roles = {}
297
176
  @interactions = {}
177
+ @private_interactions = {}
298
178
  @role_alias = {}
179
+ @name = name
180
+ @base_class = base_class
181
+ @default_interaction = default_interaction
299
182
  end
300
183
 
184
+ role :name do end
185
+ role :base_class do end
186
+ role :default_interaction do end
187
+
301
188
  end
302
189
 
303
190
  if c.instance_of? String