Module: Kapusta::Compiler::Language
- Defined in:
- lib/kapusta/compiler/language.rb
Defined Under Namespace
Classes: AccumulateForm, BindingForm, CaseForm, CatchClause, ClassForm, ConditionalBodyForm, CountedForForm, DotTarget, FaccumulateForm, FinallyClause, FunctionForm, GlobalForm, HashFnForm, ImportMacrosForm, IterationForm, LetForm, LuaIteratorForm, LuaPcallForm, LuaXpcallForm, MacroDefinitionForm, ModuleForm, OrPattern, PinPattern, SetForm, SigilForm, TryForm, TsetForm, UnpackCall, ValuesForm, WherePattern
Constant Summary
collapse
- FUNCTION_HEADS =
%w[fn lambda λ].freeze
- FUNCTION_DEFINITION_HEADS =
(FUNCTION_HEADS + %w[defn]).freeze
- MACRO_FUNCTION_HEADS =
(FUNCTION_HEADS + %w[macro]).freeze
- BINDING_HEADS =
%w[local var].freeze
%w[class module].freeze
%i[module class].freeze
- DEFINITION_SCOPES =
([:toplevel] + HEADER_SCOPES).freeze
- THREAD_HEADS =
%w[-> ->> -?> -?>>].freeze
- PIPELINE_HEADS =
(THREAD_HEADS + %w[doto]).freeze
- SHORT_PIPELINE_HEADS =
%w[-?> -?>>].freeze
- THREAD_FIRST_HEADS =
%w[-> -?>].freeze
- SEQUENCE_STATEMENT_HEADS =
%w[let while for each case match].freeze
- MULTILINE_BODY_HEADS =
%w[
if case match let try catch finally do for -> ->> -?> -?>> doto
fn lambda λ macro
].freeze
- FLAT_BODY_HEADS =
%w[
fn lambda λ macro when unless for each icollect collect fcollect
accumulate faccumulate
].freeze
- NEVER_FLAT_HEADS =
%w[let case match try catch finally do -> ->> -?> -?>> doto].freeze
- QUASI_HEADS =
%w[
quasi-sym quasi-list quasi-list-tail quasi-vec quasi-vec-tail
quasi-hash quasi-gensym
].freeze
- CORE_SPECIAL_FORMS =
%w[
fn defn lambda λ let local var global set if when unless case match
while for each do values
-> ->> -?> -?>> doto
icollect collect fcollect accumulate faccumulate
hashfn
. ?: :
..
length
require
module class end
try catch finally
raise
ivar cvar gvar
ruby
tset
and or not
= not= < <= > >=
+ - * / %
print
macro macros import-macros
quasi-sym quasi-list quasi-list-tail quasi-vec quasi-vec-tail quasi-hash quasi-gensym
].freeze
- SPECIAL_FORMS =
(CORE_SPECIAL_FORMS + LuaCompat::SPECIAL_FORMS).freeze
- CLASS_BODY_DECLARATIONS =
%w[
include extend prepend
private public protected
module_function
attr_accessor attr_reader attr_writer
].freeze
Class Method Summary
collapse
Class Method Details
209
210
211
212
|
# File 'lib/kapusta/compiler/language.rb', line 209
def binding_form?(form)
name = list_head_name(form)
!name.nil? && binding_head?(name)
end
|
.binding_head?(name) ⇒ Boolean
224
|
# File 'lib/kapusta/compiler/language.rb', line 224
def binding_head?(name) = BINDING_HEADS.include?(name)
|
.bodyless_header?(form) ⇒ Boolean
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/kapusta/compiler/language.rb', line 248
def bodyless_header?(form)
return false unless (form)
case form.head.name
when 'module'
parsed = parse_module_form(form)
parsed.body.empty? || (parsed.body.length == 1 && bodyless_header?(parsed.body[0]))
when 'class'
parse_class_form(form).body.empty?
else
false
end
end
|
.class_body_declaration?(name) ⇒ Boolean
216
|
# File 'lib/kapusta/compiler/language.rb', line 216
def class_body_declaration?(name) = CLASS_BODY_DECLARATIONS.include?(name)
|
.definition_scope?(scope) ⇒ Boolean
230
|
# File 'lib/kapusta/compiler/language.rb', line 230
def definition_scope?(scope) = DEFINITION_SCOPES.include?(scope)
|
197
|
# File 'lib/kapusta/compiler/language.rb', line 197
def defn_form?(form) = list_head?(form, 'defn')
|
195
|
# File 'lib/kapusta/compiler/language.rb', line 195
def do_form?(form) = list_head?(form, 'do')
|
193
|
# File 'lib/kapusta/compiler/language.rb', line 193
def end_form?(form) = list_head?(form, 'end')
|
328
329
330
|
# File 'lib/kapusta/compiler/language.rb', line 328
def exception_class_form?(form)
form.is_a?(Sym) && (form.name.match?(/\A[A-Z]/) || form.dotted?)
end
|
.flat_body_head?(name) ⇒ Boolean
242
|
# File 'lib/kapusta/compiler/language.rb', line 242
def flat_body_head?(name) = FLAT_BODY_HEADS.include?(name)
|
.function_definition_head?(name) ⇒ Boolean
220
|
# File 'lib/kapusta/compiler/language.rb', line 220
def function_definition_head?(name) = FUNCTION_DEFINITION_HEADS.include?(name)
|
262
263
264
|
# File 'lib/kapusta/compiler/language.rb', line 262
def function_form?(form, heads: FUNCTION_HEADS)
!parse_function_form(form, heads:).nil?
end
|
.function_head?(name) ⇒ Boolean
218
|
# File 'lib/kapusta/compiler/language.rb', line 218
def function_head?(name) = FUNCTION_HEADS.include?(name)
|
199
200
201
202
|
# File 'lib/kapusta/compiler/language.rb', line 199
def (form)
name = list_head_name(form)
!name.nil? && (name)
end
|
226
|
# File 'lib/kapusta/compiler/language.rb', line 226
def (name) = HEADER_HEADS.include?(name)
|
228
|
# File 'lib/kapusta/compiler/language.rb', line 228
def (scope) = HEADER_SCOPES.include?(scope)
|
.list_head(form) ⇒ Object
177
178
179
180
181
|
# File 'lib/kapusta/compiler/language.rb', line 177
def list_head(form)
return unless form.is_a?(List) && !form.empty?
form.head
end
|
.list_head?(form, *names) ⇒ Boolean
188
189
190
191
|
# File 'lib/kapusta/compiler/language.rb', line 188
def list_head?(form, *names)
name = list_head_name(form)
!name.nil? && names.flatten.include?(name)
end
|
.list_head_name(form) ⇒ Object
183
184
185
186
|
# File 'lib/kapusta/compiler/language.rb', line 183
def list_head_name(form)
head = list_head(form)
head.name if head.is_a?(Sym)
end
|
.macro_function_head?(name) ⇒ Boolean
222
|
# File 'lib/kapusta/compiler/language.rb', line 222
def macro_function_head?(name) = MACRO_FUNCTION_HEADS.include?(name)
|
.multiline_body_head?(name) ⇒ Boolean
240
|
# File 'lib/kapusta/compiler/language.rb', line 240
def multiline_body_head?(name) = MULTILINE_BODY_HEADS.include?(name)
|
.never_flat_head?(name) ⇒ Boolean
244
|
# File 'lib/kapusta/compiler/language.rb', line 244
def never_flat_head?(name) = NEVER_FLAT_HEADS.include?(name)
|
.parse_accumulate_args(args) ⇒ Object
502
503
504
|
# File 'lib/kapusta/compiler/language.rb', line 502
def parse_accumulate_args(args)
AccumulateForm.new(bindings: args[0], body: args[1..] || [])
end
|
506
507
508
509
510
|
# File 'lib/kapusta/compiler/language.rb', line 506
def parse_accumulate_form(form)
return unless list_head?(form, 'accumulate')
parse_accumulate_args(form.rest)
end
|
.parse_binding_args(head, args) ⇒ Object
338
339
340
341
342
|
# File 'lib/kapusta/compiler/language.rb', line 338
def parse_binding_args(head, args)
return unless args.length == 2
BindingForm.new(head:, target: args[0], value: args[1])
end
|
332
333
334
335
336
|
# File 'lib/kapusta/compiler/language.rb', line 332
def parse_binding_form(form)
return unless binding_form?(form)
parse_binding_args(form.head.name, form.rest)
end
|
.parse_case_args(args) ⇒ Object
452
453
454
|
# File 'lib/kapusta/compiler/language.rb', line 452
def parse_case_args(args)
CaseForm.new(subject: args[0], clauses: args[1..] || [])
end
|
456
457
458
459
460
|
# File 'lib/kapusta/compiler/language.rb', line 456
def parse_case_form(form)
return unless list_head?(form, %w[case match])
parse_case_args(form.rest)
end
|
.parse_catch_clause(args) ⇒ Object
320
321
322
323
324
325
326
|
# File 'lib/kapusta/compiler/language.rb', line 320
def parse_catch_clause(args)
if exception_class_form?(args[0])
CatchClause.new(klass: args[0], bind_sym: args[1], body: args[2..] || [])
else
CatchClause.new(klass: nil, bind_sym: args[0], body: args[1..] || [])
end
end
|
.parse_class_args(args) ⇒ Object
299
300
301
302
303
304
305
|
# File 'lib/kapusta/compiler/language.rb', line 299
def parse_class_args(args)
if args[1].is_a?(Vec)
ClassForm.new(name: args[0], supers: args[1], body: args[2..] || [], prefix_length: 2)
else
ClassForm.new(name: args[0], supers: nil, body: args[1..] || [], prefix_length: 1)
end
end
|
280
281
282
283
284
|
# File 'lib/kapusta/compiler/language.rb', line 280
def parse_class_form(form)
return unless list_head?(form, 'class')
parse_class_args(form.rest)
end
|
.parse_conditional_body_args(args) ⇒ Object
378
379
380
|
# File 'lib/kapusta/compiler/language.rb', line 378
def parse_conditional_body_args(args)
ConditionalBodyForm.new(condition: args[0], body: args[1..] || [])
end
|
.parse_counted_for_args(args) ⇒ Object
522
523
524
|
# File 'lib/kapusta/compiler/language.rb', line 522
def parse_counted_for_args(args)
CountedForForm.new(bindings: args[0], body: args[1..] || [])
end
|
526
527
528
529
530
|
# File 'lib/kapusta/compiler/language.rb', line 526
def parse_counted_for_form(form)
return unless list_head?(form, %w[for fcollect])
parse_counted_for_args(form.rest)
end
|
.parse_dot_target(form) ⇒ Object
366
367
368
369
370
|
# File 'lib/kapusta/compiler/language.rb', line 366
def parse_dot_target(form)
return unless list_head?(form, ':')
DotTarget.new(object: form.items[1], keys: form.items[2..] || [])
end
|
.parse_faccumulate_args(args) ⇒ Object
512
513
514
|
# File 'lib/kapusta/compiler/language.rb', line 512
def parse_faccumulate_args(args)
FaccumulateForm.new(bindings: args[0], body: args[1..] || [])
end
|
516
517
518
519
520
|
# File 'lib/kapusta/compiler/language.rb', line 516
def parse_faccumulate_form(form)
return unless list_head?(form, 'faccumulate')
parse_faccumulate_args(form.rest)
end
|
.parse_function_args(args, head: nil) ⇒ Object
272
273
274
275
276
277
278
|
# File 'lib/kapusta/compiler/language.rb', line 272
def parse_function_args(args, head: nil)
if args[0].is_a?(Vec)
FunctionForm.new(head:, name: nil, params: args[0], body: args[1..] || [], prefix_length: 1)
elsif args[0].is_a?(Sym) && args[1].is_a?(Vec)
FunctionForm.new(head:, name: args[0], params: args[1], body: args[2..] || [], prefix_length: 2)
end
end
|
266
267
268
269
270
|
# File 'lib/kapusta/compiler/language.rb', line 266
def parse_function_form(form, heads: FUNCTION_HEADS)
return unless list_head?(form, heads)
parse_function_args(form.rest, head: form.head)
end
|
.parse_global_args(args) ⇒ Object
344
345
346
347
348
|
# File 'lib/kapusta/compiler/language.rb', line 344
def parse_global_args(args)
return unless args.length == 2
GlobalForm.new(name: args[0], value: args[1])
end
|
350
351
352
353
354
|
# File 'lib/kapusta/compiler/language.rb', line 350
def parse_global_form(form)
return unless list_head?(form, 'global')
parse_global_args(form.rest)
end
|
416
417
418
419
420
|
# File 'lib/kapusta/compiler/language.rb', line 416
def parse_hashfn_form(form)
return unless list_head?(form, 'hashfn')
HashFnForm.new(body: form.rest)
end
|
.parse_import_macros_args(args) ⇒ Object
432
433
434
|
# File 'lib/kapusta/compiler/language.rb', line 432
def parse_import_macros_args(args)
ImportMacrosForm.new(destructure: args[0], module_arg: args[1])
end
|
436
437
438
439
440
|
# File 'lib/kapusta/compiler/language.rb', line 436
def parse_import_macros_form(form)
return unless list_head?(form, 'import-macros')
parse_import_macros_args(form.rest)
end
|
.parse_iteration_args(args) ⇒ Object
492
493
494
|
# File 'lib/kapusta/compiler/language.rb', line 492
def parse_iteration_args(args)
IterationForm.new(bindings: args[0], body: args[1..] || [])
end
|
496
497
498
499
500
|
# File 'lib/kapusta/compiler/language.rb', line 496
def parse_iteration_form(form)
return unless list_head?(form, %w[each collect icollect])
parse_iteration_args(form.rest)
end
|
.parse_let_args(args) ⇒ Object
442
443
444
|
# File 'lib/kapusta/compiler/language.rb', line 442
def parse_let_args(args)
LetForm.new(bindings: args[0], body: args[1..] || [])
end
|
446
447
448
449
450
|
# File 'lib/kapusta/compiler/language.rb', line 446
def parse_let_form(form)
return unless list_head?(form, 'let')
parse_let_args(form.rest)
end
|
388
389
390
391
392
393
|
# File 'lib/kapusta/compiler/language.rb', line 388
def parse_lua_iterator_form(form)
head = list_head(form)
return unless head.is_a?(Sym) && LuaCompat.iterator_form?(head.name)
LuaIteratorForm.new(head:, collection: form.items[1])
end
|
.parse_lua_pcall_args(args) ⇒ Object
395
396
397
|
# File 'lib/kapusta/compiler/language.rb', line 395
def parse_lua_pcall_args(args)
LuaPcallForm.new(callable: args[0], args: args[1..] || [])
end
|
.parse_lua_xpcall_args(args) ⇒ Object
399
400
401
|
# File 'lib/kapusta/compiler/language.rb', line 399
def parse_lua_xpcall_args(args)
LuaXpcallForm.new(callable: args[0], handler: args[1], args: args[2..] || [])
end
|
.parse_macro_definition_args(args) ⇒ Object
422
423
424
|
# File 'lib/kapusta/compiler/language.rb', line 422
def parse_macro_definition_args(args)
MacroDefinitionForm.new(name: args[0], params: args[1], body: args[2..] || [])
end
|
426
427
428
429
430
|
# File 'lib/kapusta/compiler/language.rb', line 426
def parse_macro_definition_form(form)
return unless list_head?(form, 'macro')
parse_macro_definition_args(form.rest)
end
|
.parse_module_args(args) ⇒ Object
292
293
294
295
296
297
|
# File 'lib/kapusta/compiler/language.rb', line 292
def parse_module_args(args)
rest = args[1..] || []
singleton = rest[0].is_a?(Sym) && rest[0].name == '<<'
rest = rest.drop(1) if singleton
ModuleForm.new(name: args[0], body: rest, singleton:, prefix_length: singleton ? 2 : 1)
end
|
286
287
288
289
290
|
# File 'lib/kapusta/compiler/language.rb', line 286
def parse_module_form(form)
return unless list_head?(form, 'module')
parse_module_args(form.rest)
end
|
.parse_or_pattern(pattern) ⇒ Object
474
475
476
477
478
|
# File 'lib/kapusta/compiler/language.rb', line 474
def parse_or_pattern(pattern)
return unless list_head?(pattern, 'or')
OrPattern.new(alternatives: pattern.items[1..] || [])
end
|
.parse_pin_pattern(pattern) ⇒ Object
480
481
482
483
484
|
# File 'lib/kapusta/compiler/language.rb', line 480
def parse_pin_pattern(pattern)
return unless list_head?(pattern, '=') && pattern.items.length == 2
PinPattern.new(name: pattern.items[1])
end
|
.parse_set_args(args) ⇒ Object
362
363
364
|
# File 'lib/kapusta/compiler/language.rb', line 362
def parse_set_args(args)
SetForm.new(target: args[0], value: args[1])
end
|
356
357
358
359
360
|
# File 'lib/kapusta/compiler/language.rb', line 356
def parse_set_form(form)
return unless list_head?(form, 'set')
parse_set_args(form.rest)
end
|
.parse_sigil_args(head, args) ⇒ Object
410
411
412
413
414
|
# File 'lib/kapusta/compiler/language.rb', line 410
def parse_sigil_args(head, args)
return unless %w[ivar cvar gvar].include?(head)
SigilForm.new(head:, name: args[0])
end
|
403
404
405
406
407
408
|
# File 'lib/kapusta/compiler/language.rb', line 403
def parse_sigil_form(form)
head = list_head_name(form)
return unless head
parse_sigil_args(head, form.rest)
end
|
.parse_try_args(args) ⇒ Object
307
308
309
|
# File 'lib/kapusta/compiler/language.rb', line 307
def parse_try_args(args)
TryForm.new(body: args[0], clauses: (args[1..] || []).filter_map { |clause| parse_try_clause(clause) })
end
|
.parse_try_clause(clause) ⇒ Object
311
312
313
314
315
316
317
318
|
# File 'lib/kapusta/compiler/language.rb', line 311
def parse_try_clause(clause)
return unless list_head_name(clause)
case clause.head.name
when 'catch' then parse_catch_clause(clause.rest)
when 'finally' then FinallyClause.new(body: clause.rest)
end
end
|
462
463
464
465
466
|
# File 'lib/kapusta/compiler/language.rb', line 462
def parse_try_form(form)
return unless list_head?(form, 'try')
parse_try_args(form.rest)
end
|
.parse_tset_args(args) ⇒ Object
372
373
374
375
376
|
# File 'lib/kapusta/compiler/language.rb', line 372
def parse_tset_args(args)
return unless args.length >= 3
TsetForm.new(table: args[0], key: args[1], value: args[2])
end
|
.parse_unpack_call(form) ⇒ Object
486
487
488
489
490
|
# File 'lib/kapusta/compiler/language.rb', line 486
def parse_unpack_call(form)
return unless list_head?(form, 'unpack')
UnpackCall.new(value: form.items[1])
end
|
382
383
384
385
386
|
# File 'lib/kapusta/compiler/language.rb', line 382
def parse_values_form(form)
return unless list_head?(form, 'values') && form.items.length == 3
ValuesForm.new(key: form.items[1], value: form.items[2])
end
|
.parse_where_pattern(pattern) ⇒ Object
468
469
470
471
472
|
# File 'lib/kapusta/compiler/language.rb', line 468
def parse_where_pattern(pattern)
return unless list_head?(pattern, 'where')
WherePattern.new(inner: pattern.items[1], guards: pattern.items[2..] || [])
end
|
.pipeline_head?(name) ⇒ Boolean
232
|
# File 'lib/kapusta/compiler/language.rb', line 232
def pipeline_head?(name) = PIPELINE_HEADS.include?(name)
|
.quasi_head?(name) ⇒ Boolean
246
|
# File 'lib/kapusta/compiler/language.rb', line 246
def quasi_head?(name) = QUASI_HEADS.include?(name)
|
204
205
206
207
|
# File 'lib/kapusta/compiler/language.rb', line 204
def sequence_statement_form?(form)
name = list_head_name(form)
!name.nil? && sequence_statement_head?(name)
end
|
.sequence_statement_head?(name) ⇒ Boolean
238
|
# File 'lib/kapusta/compiler/language.rb', line 238
def sequence_statement_head?(name) = SEQUENCE_STATEMENT_HEADS.include?(name)
|
.short_pipeline_head?(name) ⇒ Boolean
234
|
# File 'lib/kapusta/compiler/language.rb', line 234
def short_pipeline_head?(name) = SHORT_PIPELINE_HEADS.include?(name)
|
214
|
# File 'lib/kapusta/compiler/language.rb', line 214
def special_form?(name) = SPECIAL_FORMS.include?(name)
|
.thread_first_head?(name) ⇒ Boolean
236
|
# File 'lib/kapusta/compiler/language.rb', line 236
def thread_first_head?(name) = THREAD_FIRST_HEADS.include?(name)
|