maroon 0.6.5 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,50 +1,44 @@
1
1
  class ImmutableQueue
2
-
3
- def push(element)
2
+ def push(element)
4
3
  b = (back or ImmutableStack.empty)
5
- ImmutableQueue.new(front, b.push(element))
4
+ ImmutableQueue.new(front, b.push(element))
5
+ end
6
6
 
7
- end
8
-
9
- def pop()
7
+ def pop()
10
8
  f, b = front, back
11
- if (f == ImmutableStack.empty) then
12
- until (b == ImmutableStack.empty) do
13
- (e, b = b.pop
14
- f = f.push(e))
9
+ if (f == ImmutableStack.empty) then
10
+ until (b == ImmutableStack.empty) do
11
+ (e, b = b.pop
12
+ f = f.push(e))
13
+ end
14
+ end
15
+ head, f = f.pop
16
+ if (f == b) then
17
+ [head, ImmutableQueue.empty]
18
+ else
19
+ [head, ImmutableQueue.new(f, b)]
20
+ end
15
21
  end
16
- end
17
- head, f = f.pop
18
- if (f == b) then
19
- [head, ImmutableQueue.empty]
20
- else
21
- [head, ImmutableQueue.new(f, b)]
22
- end
23
22
 
24
- end
25
-
26
- def self.empty()
23
+ def self.empty()
27
24
  @@empty ||= ImmutableQueue.new(ImmutableStack.empty, ImmutableStack.empty)
28
- end
29
-
30
- def push_array(arr)
31
- q = self
32
- arr.each { |i| q = q.push(i) } if arr
33
- q
25
+ end
34
26
 
35
- end
27
+ def push_array(arr)
28
+ q = self
29
+ arr.each { |i| q = q.push(i) } if arr
30
+ q
31
+ end
36
32
 
37
- private
38
-
39
- def initialize(front,back)
33
+ private
34
+ def initialize(front, back)
40
35
  @front = (front or ImmutableStack.empty)
41
- @back = (back or ImmutableStack.empty)
42
- self.freeze
36
+ @back = (back or ImmutableStack.empty)
37
+ self.freeze
38
+ end
39
+
40
+ attr_reader :front
41
+ attr_reader :back
42
+
43
43
 
44
- end
45
- attr_reader :front
46
- attr_reader :back
47
-
48
-
49
-
50
- end
44
+ end
@@ -1,38 +1,34 @@
1
1
  class ImmutableStack
2
-
3
- def pop()
2
+ def pop()
4
3
  [@head, @tail]
5
- end
6
-
7
- def push(element)
4
+ end
5
+
6
+ def push(element)
8
7
  ImmutableStack.new(element, self)
9
- end
10
-
11
- def self.empty()
8
+ end
9
+
10
+ def self.empty()
12
11
  @@empty ||= self.new(nil, nil)
13
- end
14
-
15
- def each()
12
+ end
13
+
14
+ def each()
16
15
  yield(head)
17
- t = tail
18
- while t.!=(ImmutableStack.empty) do
19
- h, t = t.pop
20
- yield(h)
21
- end
16
+ t = tail
17
+ while t.!=(ImmutableStack.empty) do
18
+ h, t = t.pop
19
+ yield(h)
20
+ end
21
+ end
22
22
 
23
- end
24
-
25
- def initialize(h,t)
23
+ def initialize(h, t)
26
24
  @head = h
27
- @tail = t
28
- self.freeze
25
+ @tail = t
26
+ self.freeze
27
+ end
28
+
29
+ private
30
+ attr_reader :head
31
+ attr_reader :tail
29
32
 
30
- end
31
33
 
32
- private
33
- attr_reader :head
34
- attr_reader :tail
35
-
36
-
37
-
38
- end
34
+ end
@@ -1,78 +1,66 @@
1
1
  class MethodInfo
2
-
3
- def initialize(on_self,block_source,is_private)
4
- raise("Must be S-Expressions") unless block_source.instance_of?(Sexp)
5
- if on_self.instance_of?(Hash) then
6
- @block = on_self[:block]
7
- @on_self = on_self[:self]
8
- else
9
- @on_self = on_self
10
- end
11
- @block_source = block_source
12
- @private = is_private
13
- self.freeze
2
+ def initialize(ast, defining_role, is_private)
3
+ raise("Must be S-Expressions") unless ast.instance_of?(Sexp)
4
+ @defining_role = defining_role
5
+ @private = is_private
6
+ @definition = ast
7
+ self.freeze
8
+ end
14
9
 
15
- end
16
-
17
- def is_private()
10
+ def is_private()
18
11
  @private
19
- end
20
-
21
- def build_as_context_method(context_method_name,interpretation_context)
22
- MethodDefinition.new(self_block_source_body, interpretation_context).transform
23
- body = Ruby2Ruby.new.process(self_block_source_body)
24
- args = if self_block_source_arguments then
25
- (("(" + self_block_source_arguments) + ")")
26
- else
27
- ""
28
- end
29
- on = on_self ? ("self.") : ("")
30
- (((((("\ndef " + on.to_s) + context_method_name.to_s) + args) + "\n ") + body) + "\n end\n")
12
+ end
13
+
14
+ def name()
15
+ self_definition_name
16
+ end
31
17
 
32
- end
18
+ def build_as_context_method(interpretation_context)
19
+ AstRewritter.new(self_definition_body, interpretation_context).rewrite!
20
+ body = Ruby2Ruby.new.process(self_definition_body)
21
+ raise("Body is undefined") unless body
22
+ args = self_definition_arguments
23
+ if args and args.length then
24
+ args = (("(" + args.join(",")) + ")")
25
+ else
26
+ args = ""
27
+ end
28
+ real_name = (
29
+ if (@defining_role == nil) then
30
+ name.to_s
31
+ else
32
+ ((("self_" + @defining_role.to_s) + "_") + name.to_s)
33
+ end).to_s
34
+ header = (("def " + real_name) + args)
35
+ (((header + " ") + body) + " end\n")
36
+ end
37
+
38
+ private
39
+ attr_reader :definition
33
40
 
34
- private
35
- attr_reader :on_self
36
- attr_reader :block
37
- attr_reader :block_source
38
-
39
-
40
- def self_block_source_get_arguments()
41
- sexp = block_source[2]
42
- return nil unless sexp
43
- return sexp[1] if (sexp[0] == :lasgn)
44
- return [] if (sexp[1] == nil)
45
- sexp = sexp[(1..-1)]
46
- args = []
47
- sexp.each do |e|
48
- (args << (if e.instance_of?(Symbol) then
49
- e
50
- else
51
- (e[0] == :splat) ? (("*" + e[1][1].to_s)) : (e[1])
52
- end))
53
- end
54
- if block then
55
- b = ("&" + block.to_s)
56
- if args then
57
- args = [args] unless args.instance_of?(Array)
58
- (args << b)
59
- else
60
- args = [b]
41
+ def self_definition_body()
42
+ args = definition.detect { |d| (d[0] == :args) }
43
+ index = (definition.index(args) + 1)
44
+ if (definition.length > (index + 1)) then
45
+ body = definition[(index..-1)]
46
+ body.insert(0, :block)
47
+ body
48
+ else
49
+ definition[index]
50
+ end
61
51
  end
62
- end
63
- args
64
52
 
65
- end
66
-
67
- def self_block_source_arguments()
68
- args = self_block_source_get_arguments
69
- args and args.length ? (args.join(",")) : (nil)
53
+ def self_definition_arguments()
54
+ args = definition.detect { |d| (d[0] == :args) }
55
+ args and (args.length > 1) ? (args[(1..-1)]) : ([])
56
+ end
70
57
 
71
- end
72
-
73
- def self_block_source_body()
74
- block_source[3]
75
- end
58
+ def self_definition_name()
59
+ if definition[1].instance_of?(Symbol) then
60
+ definition[1]
61
+ else
62
+ ((definition[1].select { |e| e.instance_of?(Symbol) }.map { |e| e.to_s }.join(".") + ".") + definition[2].to_s).to_sym
63
+ end
64
+ end
76
65
 
77
-
78
- end
66
+ end
@@ -0,0 +1,135 @@
1
+ class Production
2
+ def initialize(ast, interpretation_context)
3
+ rebind(ImmutableQueue.empty.push(ast), interpretation_context)
4
+ end
5
+
6
+ def type()
7
+ case
8
+ when (nil == production) then
9
+ nil
10
+ when self_production_is_block_with_bind? then
11
+ Tokens.block_with_bind
12
+ when self_production_is_block? then
13
+ Tokens.block
14
+ when (production.instance_of?(Fixnum) or production.instance_of?(Symbol)) then
15
+ Tokens.terminal
16
+ when self_production_is_rolemethod_call? then
17
+ Tokens.rolemethod_call
18
+ when self_production_is_role? then
19
+ Tokens.role
20
+ when self_production_is_indexer? then
21
+ Tokens.indexer
22
+ when self_production_is_call? then
23
+ Tokens.call
24
+ else
25
+ Tokens.other
26
+ end
27
+ end
28
+
29
+ def [](i)
30
+ @production[i]
31
+ end
32
+
33
+ def []=(i, v)
34
+ @production[i] = v
35
+ end
36
+
37
+ def length()
38
+ @production.length
39
+ end
40
+
41
+ def last()
42
+ @production.last
43
+ end
44
+
45
+ def first()
46
+ @production.first
47
+ end
48
+
49
+ def data()
50
+ return @data if @data
51
+ @data = case
52
+ when self_production_is_call? then
53
+ @production[2]
54
+ else
55
+ @production
56
+ end
57
+ end
58
+
59
+ def each()
60
+ yield(self)
61
+ if production.instance_of?((Sexp or production.instance_of?(Array))) then
62
+ @queue = @queue.push_array(production)
63
+ end
64
+ while @queue.!=(ImmutableQueue.empty) do
65
+ rebind(@queue, @interpretation_context)
66
+ yield(self)
67
+ if production.instance_of?((Sexp or production.instance_of?(Array))) then
68
+ @queue = @queue.push_array(production)
69
+ end
70
+ end
71
+ end
72
+
73
+ private
74
+ def rebind(queue, ctx)
75
+ @data = nil
76
+ @production, @queue = queue.pop
77
+ @interpretation_context = ctx
78
+ end
79
+
80
+ attr_reader :interpretation_context
81
+ attr_reader :queue
82
+ attr_reader :production
83
+
84
+ def self_production_is_role?()
85
+ case
86
+ when (self_production_is_call? and interpretation_context.roles.has_key?(production[2])) then
87
+ @date = [production[2]]
88
+ return true
89
+ when (((production == :self) or ((self_production_is_indexer? and ((production[1] == nil) or (production[1] == :self))) or (production and ((production.instance_of?(Sexp) or production.instance_of?(Array)) and (production[0] == :self))))) and @interpretation_context.defining_role) then
90
+ @data = @interpretation_context.defining_role
91
+ return true
92
+ else
93
+ false
94
+ end
95
+ end
96
+
97
+ def self_production_is_indexer?()
98
+ self_production_is_call? and ((production[2] == :[]) or (production[2] == :[]=))
99
+ end
100
+
101
+ def self_production_is_call?()
102
+ production and ((production.instance_of?(Sexp) or production.instance_of?(Array)) and (production[0] == :call))
103
+ end
104
+
105
+ def self_production_is_block?()
106
+ production and ((production.instance_of?(Sexp) or production.instance_of?(Array)) and (production[0] == :iter))
107
+ end
108
+
109
+ def self_production_is_block_with_bind?()
110
+ if self_production_is_block? then
111
+ body = @production.last
112
+ if body and exp = body[0] then
113
+ bind = Production.new(exp, @interpretation_context)
114
+ true if (bind.type == Tokens.call) and (bind.data == :bind)
115
+ end
116
+ end
117
+ end
118
+
119
+ def self_production_is_rolemethod_call?()
120
+ can_be = self_production_is_call?
121
+ if can_be then
122
+ instance = Production.new(production[1], @interpretation_context)
123
+ can_be = (instance.type == Tokens.role)
124
+ if can_be then
125
+ instance_data = instance.data
126
+ role = @interpretation_context.roles[instance_data]
127
+ data = production[2]
128
+ can_be = role.has_key?(data)
129
+ @data = [data, instance_data]
130
+ end
131
+ end
132
+ can_be
133
+ end
134
+
135
+ end
@@ -0,0 +1,24 @@
1
+ class Tokens
2
+ def self.define_token(name)
3
+ class_eval("@@#{name} = Tokens.new :#{name};def Tokens.#{name};@@#{name};end")
4
+ end
5
+
6
+ def to_s
7
+ @type.to_s
8
+ end
9
+
10
+ private
11
+ def initialize(type)
12
+ @type = type
13
+ self.freeze
14
+ end
15
+
16
+ define_token :terminal
17
+ define_token :role
18
+ define_token :rolemethod_call
19
+ define_token :other
20
+ define_token :call
21
+ define_token :indexer
22
+ define_token :block
23
+ define_token :block_with_bind
24
+ end
@@ -1,14 +1,14 @@
1
1
  require 'sorcerer'
2
2
  require 'sourcify'
3
3
 
4
- require_relative 'self'
5
- require_relative 'bind'
6
- require_relative 'ImmutableQueue'
7
- require_relative 'ImmutableStack'
8
- require_relative 'interpretation_context'
9
- require_relative 'MethodDefinition'
10
- require_relative 'MethodInfo'
11
- require_relative 'MethodCall'
12
-
13
- require_relative 'Context'
4
+
5
+ require_relative './Tokens'
6
+ require_relative './ImmutableStack'
7
+ require_relative './ImmutableQueue'
8
+ require_relative './interpretation_context'
9
+ require_relative './Production'
10
+ require_relative './AstRewritter'
11
+ require_relative './MethodInfo'
12
+
13
+ require_relative './Context'
14
14