Module: Rubasteme::Parser::DerivedConverter

Includes:
Utils
Included in:
Phase2Parser
Defined in:
lib/rubasteme/parser/derived_converter.rb

Constant Summary collapse

DERIVED_IDENTIFIERS =
[
  "cond", "case", "and", "or", "when", "unless",
  "let", "let*", "letrec", "letrec*",
  "let-values", "let*-values",
  "begin", "do",
  "delay", "delay-force",
  "parameterize",
  "guard",
  "case-lambda",
]

Instance Method Summary collapse

Methods included from Utils

#ast?, #ast_type?, #not_implemented_yet

Instance Method Details

#is_else_clause?(list) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rubasteme/parser/derived_converter.rb', line 41

def is_else_clause?(list)
  ast_type?(list[0], :ast_identifier) && list[0].identifier == "else"
end

#is_recipient_clause?(list) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rubasteme/parser/derived_converter.rb', line 63

def is_recipient_clause?(list)
  ast_type?(list[1], :ast_identifier) && list[1].identifier == "=>"
end

#to_and(list) ⇒ Object



143
144
145
# File 'lib/rubasteme/parser/derived_converter.rb', line 143

def to_and(list)
  to_logical_test("and", list)
end

#to_begin(list) ⇒ Object



242
243
244
245
246
247
# File 'lib/rubasteme/parser/derived_converter.rb', line 242

def to_begin(list)
  # ( begin <sequence> )
  begin_node = AST.instantiate(:ast_begin)
  begin_node.sequence = to_sequence(list[1..-1])
  begin_node
end

#to_bind_spec(list) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/rubasteme/parser/derived_converter.rb', line 202

def to_bind_spec(list)
  # ( <identifier> <expression> )
  spec = AST.instantiate(:ast_bind_spec)
  spec.identifier = list[0]
  spec.expression = parse(list[1])
  spec
end

#to_bindings(list) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/rubasteme/parser/derived_converter.rb', line 193

def to_bindings(list)
  # ( <binding spec>* )
  bindings = AST.instantiate(:ast_bindings)
  list.each { |e|
    bindings.add_bind_spec(to_bind_spec(e))
  }
  bindings
end

#to_case(list) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rubasteme/parser/derived_converter.rb', line 84

def to_case(list)
  # ( case <expression> <case clause>+ )
  # ( case <expression> <case clause>* ( else <sequence> ) )
  # ( case <expression> <case clause>* ( else => <recipient> ) )
  case_node = AST.instantiate(:ast_case)
  case_node.expression = parse(list[1])
  list[2..-2].each { |e|
    case_node.add_clause(to_case_clause(e))
  }
  last = list[-1]
  method = is_else_clause?(last) ? :to_case_else_clause : :to_case_clause
  case_node.add_clause(self.send(method, last))
  case_node
end

#to_case_clause(list) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubasteme/parser/derived_converter.rb', line 99

def to_case_clause(list)
  # ( ( <datum>* ) <sequence> )
  # ( ( <datum>* ) => <recipient> )
  if is_recipient_clause?(list)
    to_case_recipient_clause(list)
  else
    clause = AST.instantiate(:ast_case_clause)
    clause.data = to_data(list[0])
    clause.sequence = to_sequence(list[1..-1])
    clause
  end
end

#to_case_else_clause(list) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/rubasteme/parser/derived_converter.rb', line 119

def to_case_else_clause(list)
  # ( else <sequence> )
  # ( else => <recipient> )
  if is_recipient_clause?(list)
    to_case_else_recipient_clause(list)
  else
    to_else_clause(list)
  end
end

#to_case_else_recipient_clause(list) ⇒ Object



129
130
131
132
133
134
# File 'lib/rubasteme/parser/derived_converter.rb', line 129

def to_case_else_recipient_clause(list)
  # ( else => <recipient> )
  clause = AST.instantiate(:ast_else_recipient_clause)
  clause.recipient = to_recipient(list[2])
  clause
end

#to_case_lambda(list) ⇒ Object



308
309
310
# File 'lib/rubasteme/parser/derived_converter.rb', line 308

def to_case_lambda(list)
  not_implemented_yet("CASE-LAMBDA")
end

#to_case_recipient_clause(list) ⇒ Object



112
113
114
115
116
117
# File 'lib/rubasteme/parser/derived_converter.rb', line 112

def to_case_recipient_clause(list)
  # ( ( <datum>* ) => <recipient> )
  clause = AST.instantiate(:ast_case_recipient_clause)
  clause.data = to_data(list[0])
  caluse.recipient = to_recipient(list[2])
end

#to_cond(list) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubasteme/parser/derived_converter.rb', line 29

def to_cond(list)
  # ( cond <cond clause> ... <else clause> )
  cond = AST.instantiate(:ast_cond)
  list[1..-2].each { |e|
    cond.add_clause(to_cond_clause(e))
  }
  last = list[-1]
  method = is_else_clause?(last) ? :to_else_clause : :to_cond_clause
  cond.add_clause(self.send(method, last))
  cond
end

#to_cond_clause(list) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubasteme/parser/derived_converter.rb', line 45

def to_cond_clause(list)
  # type 1: ( <test> )
  # type 2: ( <test> <sequence> )
  # type 3: ( <test> => <recipient> )
  clause = nil
  if is_recipient_clause?(list)
    # type 3
    clause = AST.instantiate(:ast_cond_recipient_clause)
    clause.recipient = to_recipient(list[2..-1])
  else
    # type 1 and 2
    clause = AST.instantiate(:ast_cond_clause)
    clause.sequence = to_sequence(list[1..-1])
  end
  clause.test = to_test(list[0])
  clause
end

#to_data(list) ⇒ Object



136
137
138
139
140
141
# File 'lib/rubasteme/parser/derived_converter.rb', line 136

def to_data(list)
  # ( <datum>* )
  data = AST.instantiate(:ast_data)
  list.each{|e| data << parse(e)}
  data
end

#to_delay(list) ⇒ Object



292
293
294
# File 'lib/rubasteme/parser/derived_converter.rb', line 292

def to_delay(list)
  not_implemented_yet("DELAY")
end

#to_delay_force(list) ⇒ Object



296
297
298
# File 'lib/rubasteme/parser/derived_converter.rb', line 296

def to_delay_force(list)
  not_implemented_yet("DELAY-FORCE")
end

#to_derived_expression(list) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/rubasteme/parser/derived_converter.rb', line 20

def to_derived_expression(list)
  name = compose_method_name("to_", list[0].identifier).intern
  if self.respond_to?(name, true)
    self.send(name, list)
  else
    not_implemented_yet(list[0])
  end
end

#to_do(list) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/rubasteme/parser/derived_converter.rb', line 249

def to_do(list)
  # ( do ( <iteration spec>* ) ( <test> <do result> ) <command>* )
  do_node = AST.instantiate(:ast_do)
  do_node.iteration_bindings = to_iteration_bindings(list[1])
  do_node.test_and_do_result = to_test_and_do_result(list[2])
  list[3..-1].each { |e|
    do_node.add_command(parse(e))
  }
  do_node
end

#to_else_clause(list) ⇒ Object



67
68
69
70
71
72
# File 'lib/rubasteme/parser/derived_converter.rb', line 67

def to_else_clause(list)
  # ( else <sequence> )
  else_clause = AST.instantiate(:ast_else_clause)
  else_clause.sequence = to_sequence(list[1..-1])
  else_clause
end

#to_guard(list) ⇒ Object



304
305
306
# File 'lib/rubasteme/parser/derived_converter.rb', line 304

def to_guard(list)
  not_implemented_yet("GUARD")
end

#to_iteration_bindings(list) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/rubasteme/parser/derived_converter.rb', line 260

def to_iteration_bindings(list)
  # ( <iteration spec>* )
  node = AST.instantiate(:ast_iteration_bindings)
  list.each { |e|
    node.add_iteration_spec(to_iteration_spec(e))
  }
  node
end

#to_iteration_spec(list) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rubasteme/parser/derived_converter.rb', line 269

def to_iteration_spec(list)
  # ( <identifier> <init> )
  # ( <identifier> <init> <step> )
  # <init> -> <expression>
  # <step> -> <expression>
  spec = AST.instantiate(:ast_iteration_spec)
  spec.identifier = list[0]
  spec.init = parse(list[1])
  if list.size > 2
    spec.step = parse(list[2])
  end
  spec
end

#to_let(list) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rubasteme/parser/derived_converter.rb', line 178

def to_let(list)
  # ( let ( <binding spec>* ) <body> )
  # ( let <identifier> ( <binding spec>* ) <body> )
  let = AST.instantiate(:ast_let)
  bindings_pos = 1
  if ast_type?(list[1], :ast_identifier)
    # named let
    let.identifier = list[1]
    bindings_pos += 1
  end
  let.bindings = to_bindings(list[bindings_pos])
  let.body = to_body(list[(bindings_pos + 1)..-1])
  let
end

#to_let_base(type, list) ⇒ Object



223
224
225
226
227
228
229
230
231
232
# File 'lib/rubasteme/parser/derived_converter.rb', line 223

def to_let_base(type, list)
  # ( let* ( <binding spec>* ) <body> )
  # ( letrec ( <binding spec>* ) <body> )
  # ( letrec* ( <binding spec>* ) <body> )
  ast_type = "ast_#{type}".intern
  node = AST.instantiate(ast_type)
  node.bindings = to_bindings(list[1])
  node.body = to_body(list[2..-1])
  node
end

#to_let_star(list) ⇒ Object



211
212
213
# File 'lib/rubasteme/parser/derived_converter.rb', line 211

def to_let_star(list)
  to_let_base("let_star", list)
end

#to_let_star_values(list) ⇒ Object



238
239
240
# File 'lib/rubasteme/parser/derived_converter.rb', line 238

def to_let_star_values(list)
  not_implemented_yet("LET*-VALUES")
end

#to_let_values(list) ⇒ Object



234
235
236
# File 'lib/rubasteme/parser/derived_converter.rb', line 234

def to_let_values(list)
  not_implemented_yet("LET-VALUES")
end

#to_letrec(list) ⇒ Object



215
216
217
# File 'lib/rubasteme/parser/derived_converter.rb', line 215

def to_letrec(list)
  to_let_base("letrec", list)
end

#to_letrec_star(list) ⇒ Object



219
220
221
# File 'lib/rubasteme/parser/derived_converter.rb', line 219

def to_letrec_star(list)
  to_let_base("letrec_star", list)
end

#to_logical_test(type, list) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/rubasteme/parser/derived_converter.rb', line 151

def to_logical_test(type, list)
  # ( and <test>* )
  # ( or <test>* )
  ast_type = "ast_#{type}".intern
  node = AST.instantiate(ast_type)
  list[1..-1].each{|e| node << to_test(e)}
  node
end

#to_or(list) ⇒ Object



147
148
149
# File 'lib/rubasteme/parser/derived_converter.rb', line 147

def to_or(list)
  to_logical_test("or", list)
end

#to_parameterize(list) ⇒ Object



300
301
302
# File 'lib/rubasteme/parser/derived_converter.rb', line 300

def to_parameterize(list)
  not_implemented_yet("PARAMETERIZE")
end

#to_recipient(list) ⇒ Object



79
80
81
82
# File 'lib/rubasteme/parser/derived_converter.rb', line 79

def to_recipient(list)
  # <recipient> -> <expression>
  parse(list)
end

#to_test(list) ⇒ Object



74
75
76
77
# File 'lib/rubasteme/parser/derived_converter.rb', line 74

def to_test(list)
  # <test> -> <expression>
  parse(list)
end

#to_test_and_do_result(list) ⇒ Object



283
284
285
286
287
288
289
290
# File 'lib/rubasteme/parser/derived_converter.rb', line 283

def to_test_and_do_result(list)
  # ( <test> <do result> )
  # <do result> -> <sequence> | <empty>
  node = AST.instantiate(:ast_test_and_do_result)
  node.test = to_test(list[0])
  node.sequence = to_sequence(list[1..-1])
  node
end

#to_test_and_sequence(type, list) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/rubasteme/parser/derived_converter.rb', line 168

def to_test_and_sequence(type, list)
  # ( when <test> <sequence> )
  # ( unless <test> <sequence> )
  ast_type = "ast_#{type}".intern
  node = AST.instantiate(ast_type)
  node.test = to_test(list[1])
  node.sequence = to_sequence(list[2..-1])
  node
end

#to_unless(list) ⇒ Object



164
165
166
# File 'lib/rubasteme/parser/derived_converter.rb', line 164

def to_unless(list)
  to_test_and_sequence("unless", list)
end

#to_when(list) ⇒ Object



160
161
162
# File 'lib/rubasteme/parser/derived_converter.rb', line 160

def to_when(list)
  to_test_and_sequence("when", list)
end