Class: KaiserRuby::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/kaiser_ruby/parser.rb

Constant Summary collapse

POETIC_STRING_KEYWORDS =
%w(says)
POETIC_NUMBER_KEYWORDS =
%w(is was were are 's 're)
POETIC_NUMBER_CONTRACTIONS =
%w('s 're)
POETIC_TYPE_KEYWORDS =
%w(is)
%w(say whisper shout scream)
LISTEN_TO_KEYWORDS =
['listen to']
LISTEN_KEYWORDS =
['listen']
BREAK_KEYWORDS =
['break', 'break it down']
CONTINUE_KEYWORDS =
['continue', 'take it to the top']
RETURN_KEYWORDS =
['give back']
INCREMENT_FIRST_KEYWORDS =
%w(build)
INCREMENT_SECOND_KEYWORDS =
%w(up)
DECREMENT_FIRST_KEYWORDS =
%w(knock)
DECREMENT_SECOND_KEYWORDS =
%w(down)
ASSIGNMENT_FIRST_KEYWORDS =
%w(put)
ASSIGNMENT_SECOND_KEYWORDS =
%w(into)
FUNCTION_KEYWORDS =
%w(takes)
FUNCTION_SEPARATORS =
['and', ', and', "'n'", '&', ',']
FUNCTION_CALL_KEYWORDS =
%w(taking)
FUNCTION_CALL_SEPARATORS =
[', and', "'n'", '&', ',']
IF_KEYWORDS =
%w(if)
UNTIL_KEYWORDS =
%w(until)
WHILE_KEYWORDS =
%w(while)
ELSE_KEYWORDS =
%w(else)
NULL_TYPE =
%w(null nothing nowhere nobody gone empty)
TRUE_TYPE =
%w(true yes ok right)
FALSE_TYPE =
%w(false no lies wrong)
NIL_TYPE =
%w(mysterious)
POETIC_TYPE_LITERALS =
NIL_TYPE + NULL_TYPE + TRUE_TYPE + FALSE_TYPE
COMMON_VARIABLE_KEYWORDS =
%w(a an the my your)
PRONOUN_KEYWORDS =
%w(he him she her it its they them)
ADDITION_KEYWORDS =
%w(plus with)
SUBTRACTION_KEYWORDS =
%w(minus without)
MULTIPLICATION_KEYWORDS =
%w(times of)
DIVISION_KEYWORDS =
%w(over)
MATH_OP_KEYWORDS =
ADDITION_KEYWORDS + SUBTRACTION_KEYWORDS + MULTIPLICATION_KEYWORDS + DIVISION_KEYWORDS
EQUALITY_KEYWORDS =
%w(is)
INEQUALITY_KEYWORDS =
%w(isn't isnt ain't aint is\ not)
GT_KEYWORDS =
['is higher than', 'is greater than', 'is bigger than', 'is stronger than']
GTE_KEYWORDS =
['is as high as', 'is as great as', 'is as big as', 'is as strong as']
LT_KEYWORDS =
['is lower than', 'is less than', 'is smaller than', 'is weaker than']
LTE_KEYWORDS =
['is as low as', 'is as little as', 'is as small as', 'is as weak as']
COMPARISON_KEYWORDS =
EQUALITY_KEYWORDS + INEQUALITY_KEYWORDS + GT_KEYWORDS + GTE_KEYWORDS + LT_KEYWORDS + LTE_KEYWORDS
FUNCTION_RESTRICTED_KEYWORDS =
MATH_OP_KEYWORDS + ['(?<!, )and', 'is', 'or', 'into', 'nor']
AND_KEYWORDS =
%w(and)
OR_KEYWORDS =
%w(or)
NOR_KEYWORDS =
%w(nor)
NOT_KEYWORDS =
['(?<!is )not']
LOGIC_KEYWORDS =
AND_KEYWORDS + OR_KEYWORDS + NOT_KEYWORDS + NOR_KEYWORDS
RESERVED_KEYWORDS =
LOGIC_KEYWORDS + MATH_OP_KEYWORDS + POETIC_TYPE_LITERALS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



65
66
67
68
# File 'lib/kaiser_ruby/parser.rb', line 65

def initialize(input)
  @raw_input = input
  @lines = input.split /\n/
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



3
4
5
# File 'lib/kaiser_ruby/parser.rb', line 3

def lines
  @lines
end

#raw_inputObject (readonly)

Returns the value of attribute raw_input.



3
4
5
# File 'lib/kaiser_ruby/parser.rb', line 3

def raw_input
  @raw_input
end

#treeObject (readonly)

Returns the value of attribute tree.



3
4
5
# File 'lib/kaiser_ruby/parser.rb', line 3

def tree
  @tree
end

Instance Method Details

#add_to_tree(object) ⇒ Object

private



657
658
659
660
661
662
663
664
665
# File 'lib/kaiser_ruby/parser.rb', line 657

def add_to_tree(object)
  object.extend(Hashie::Extensions::DeepLocate)

  if @nesting > 0
    object[:nesting_start_line] = @nesting_start_line
    object[:nesting] = @nesting
  end
  @tree << object
end

#consume_function_calls(string) ⇒ Object



351
352
353
354
355
356
357
358
359
360
# File 'lib/kaiser_ruby/parser.rb', line 351

def consume_function_calls(string)
  if string =~ prepared_regexp(FUNCTION_CALL_KEYWORDS)
    words = string.split prepared_regexp(FUNCTION_RESTRICTED_KEYWORDS)
    found_string = words.select { |w| w =~ (/\btaking\b/) }.first
    @function_temp << found_string
    string = string.gsub(found_string, " func_#{@function_temp.count - 1} ")
  end

  string
end

#matches_all?(words, rxp) ⇒ Boolean

Returns:

  • (Boolean)


683
684
685
686
# File 'lib/kaiser_ruby/parser.rb', line 683

def matches_all?(words, rxp)
  regexp = rxp.is_a?(Regexp) ? rxp : prepared_regexp(rxp)
  words.all? { |w| w =~ regexp }
end

#matches_any?(words, rxp) ⇒ Boolean

Returns:

  • (Boolean)


678
679
680
681
# File 'lib/kaiser_ruby/parser.rb', line 678

def matches_any?(words, rxp)
  regexp = rxp.is_a?(Regexp) ? rxp : prepared_regexp(rxp)
  words.any? { |w| w =~ regexp }
end

#matches_consecutive?(words, first_rxp, second_rxp) ⇒ Boolean

Returns:

  • (Boolean)


688
689
690
691
692
693
# File 'lib/kaiser_ruby/parser.rb', line 688

def matches_consecutive?(words, first_rxp, second_rxp)
  first_idx = words.index { |w| w =~ prepared_regexp(first_rxp) }
  second_idx = words.index { |w| w =~ prepared_regexp(second_rxp) }

  second_idx != nil && first_idx != nil && second_idx.to_i - first_idx.to_i == 1
end

#matches_first?(words, rxp) ⇒ Boolean

Returns:

  • (Boolean)


695
696
697
# File 'lib/kaiser_ruby/parser.rb', line 695

def matches_first?(words, rxp)
  words.index { |w| w =~ prepared_regexp(rxp) } == 0
end

#matches_separate?(words, first_rxp, second_rxp) ⇒ Boolean

Returns:

  • (Boolean)


703
704
705
706
707
708
# File 'lib/kaiser_ruby/parser.rb', line 703

def matches_separate?(words, first_rxp, second_rxp)
  first_idx = words.index { |w| w =~ prepared_regexp(first_rxp) }
  second_idx = words.index { |w| w =~ prepared_regexp(second_rxp) }

  second_idx != nil && first_idx != nil && second_idx.to_i > first_idx.to_i
end

#matches_several_first?(line, rxp) ⇒ Boolean

Returns:

  • (Boolean)


699
700
701
# File 'lib/kaiser_ruby/parser.rb', line 699

def matches_several_first?(line, rxp)
  (line =~ prepared_regexp(rxp)) == 0
end

#parseObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kaiser_ruby/parser.rb', line 70

def parse
  @tree = []

  @tree.extend(Hashie::Extensions::DeepLocate)
  @function_temp = []
  @nesting = 0
  @nesting_start_line = 0
  @lnum = 0

  # parse through lines to get the general structure (statements/flow control/functions/etc) out of it
  @lines.each_with_index do |line, lnum|
    @lnum = lnum
    parse_line(line)
  end

  func_calls = @tree.deep_locate(:passed_function_call)
  func_calls.each do |func|
    str = func[:passed_function_call]
    num = Integer(str.split('_').last)

    func[:passed_function_call] = parse_function_call(@function_temp[num])
  end

  @tree
end

#parse_addition(string) ⇒ Object



606
607
608
609
610
611
612
# File 'lib/kaiser_ruby/parser.rb', line 606

def parse_addition(string)
  words = string.rpartition prepared_regexp(ADDITION_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { addition: { left: left, right: right } }
end

#parse_and(string) ⇒ Object



434
435
436
437
438
439
440
# File 'lib/kaiser_ruby/parser.rb', line 434

def parse_and(string)
  words = string.rpartition prepared_regexp(AND_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { and: { left: left, right: right } }
end

#parse_argument(string) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/kaiser_ruby/parser.rb', line 370

def parse_argument(string)
  str = parse_literal_string(string)
  return str if str

  exp = parse_logic_operation(string)
  return exp if exp

  cmp = parse_comparison(string)
  return cmp if cmp

  math = parse_math_operations(string)
  return math if math

  fcl2 = pass_function_calls(string)
  return fcl2 if fcl2

  fcl = parse_function_call(string)
  return fcl if fcl

  vals = parse_value_or_variable(string)
  return vals if vals
end

#parse_assignment(line) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/kaiser_ruby/parser.rb', line 314

def parse_assignment(line)
  match_rxp = prepared_capture(ASSIGNMENT_FIRST_KEYWORDS, ASSIGNMENT_SECOND_KEYWORDS)
  right = parse_argument(line.match(match_rxp).captures.first.strip)
  left = parse_variables(line.match(match_rxp).captures.last.strip)
  left[:type] = :assignment
  { assignment: { left: left, right: right } }
end

#parse_breakObject



217
218
219
# File 'lib/kaiser_ruby/parser.rb', line 217

def parse_break
  { break: nil }
end

#parse_common_variable(string) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/kaiser_ruby/parser.rb', line 559

def parse_common_variable(string)
  words = string.split(/\s/)

  copied = words.dup
  copied.shift
  copied.each do |w|
    raise SyntaxError, "invalid common variable name: #{string}" if w =~ /[[:upper:]]/
  end

  words = words.map { |e| e.chars.select { |c| c =~ /[[:alpha:]]/ }.join }
  { variable_name: words.map { |w| w.downcase }.join('_') }
end

#parse_comparison(string) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/kaiser_ruby/parser.rb', line 468

def parse_comparison(string)
  return false if string.strip.start_with?('"') && string.strip.strip.end_with?('"') && string.count('"') == 2
  words = string.split(/\s/)

  if string =~ prepared_regexp(GT_KEYWORDS)
    return parse_gt(string)
  elsif string =~ prepared_regexp(GTE_KEYWORDS)
    return parse_gte(string)
  elsif string =~ prepared_regexp(LT_KEYWORDS)
    return parse_lt(string)
  elsif string =~ prepared_regexp(LTE_KEYWORDS)
    return parse_lte(string)
  elsif string =~ prepared_regexp(INEQUALITY_KEYWORDS)
    return parse_inequality(string)
  elsif string =~ prepared_regexp(EQUALITY_KEYWORDS)
    return parse_equality(string)
  end

  return false
end

#parse_continueObject



221
222
223
# File 'lib/kaiser_ruby/parser.rb', line 221

def parse_continue
  { continue: nil }
end

#parse_decrement(line) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/kaiser_ruby/parser.rb', line 200

def parse_decrement(line)
  match_rxp = prepared_capture(DECREMENT_FIRST_KEYWORDS, DECREMENT_SECOND_KEYWORDS)
  var = line.match(match_rxp).captures.first.strip
  capture = parse_variables(var)

  capture[:amount] = line.split(var).last.scan(/\bdown\b/i).count
  { decrement: capture }
end

#parse_division(string) ⇒ Object



630
631
632
633
634
635
636
# File 'lib/kaiser_ruby/parser.rb', line 630

def parse_division(string)
  words = string.rpartition prepared_regexp(DIVISION_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { division: { left: left, right: right } }
end

#parse_elseObject



209
210
211
# File 'lib/kaiser_ruby/parser.rb', line 209

def parse_else
  { else: nil }
end

#parse_empty_lineObject



213
214
215
# File 'lib/kaiser_ruby/parser.rb', line 213

def parse_empty_line
  { empty_line: nil }
end

#parse_equality(string) ⇒ Object



489
490
491
492
493
494
495
# File 'lib/kaiser_ruby/parser.rb', line 489

def parse_equality(string)
  words = string.rpartition prepared_regexp(EQUALITY_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { equality: { left: left, right: right } }
end

#parse_function(line) ⇒ Object



344
345
346
347
348
349
# File 'lib/kaiser_ruby/parser.rb', line 344

def parse_function(line)
  words = line.split prepared_regexp(FUNCTION_KEYWORDS)
  funcname = parse_function_name(words.first.strip)
  argument = parse_function_definition_arguments(words.last.strip)
  { function: { name: funcname, argument: argument } }
end

#parse_function_call(line) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/kaiser_ruby/parser.rb', line 247

def parse_function_call(line)
  words = line.split /\s/
  if matches_any?(words, FUNCTION_CALL_KEYWORDS)
    words = line.split prepared_regexp(FUNCTION_CALL_KEYWORDS)
    left = parse_function_name(words.first.strip)
    right = parse_function_call_arguments(words.last.strip)
    { function_call: { left: left, right: right } }
  else
    return false
  end
end

#parse_function_call_arguments(string) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/kaiser_ruby/parser.rb', line 237

def parse_function_call_arguments(string)
  words = string.split(Regexp.new(FUNCTION_CALL_SEPARATORS.join('|')))
  arguments = []
  words.each do |w|
    arguments << parse_value_or_variable(w.strip)
  end

  { argument_list: arguments }
end

#parse_function_definition_arguments(string) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/kaiser_ruby/parser.rb', line 225

def parse_function_definition_arguments(string)
  words = string.split Regexp.new(FUNCTION_SEPARATORS.join('|'))
  arguments = []
  words.each do |w|
    arg = parse_value_or_variable(w.strip)
    arg[:local_variable_name] = arg.delete(:variable_name)
    arguments << arg
  end

  { argument_list: arguments }
end

#parse_function_name(string) ⇒ Object



553
554
555
556
557
# File 'lib/kaiser_ruby/parser.rb', line 553

def parse_function_name(string)
  fname = parse_variables(string)
  fname[:function_name] = fname.delete(:variable_name)
  fname
end

#parse_gt(string) ⇒ Object



505
506
507
508
509
510
511
# File 'lib/kaiser_ruby/parser.rb', line 505

def parse_gt(string)
  words = string.rpartition prepared_regexp(GT_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { gt: { left: left, right: right } }
end

#parse_gte(string) ⇒ Object



513
514
515
516
517
518
519
# File 'lib/kaiser_ruby/parser.rb', line 513

def parse_gte(string)
  words = string.rpartition prepared_regexp(GTE_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { gte: { left: left, right: right } }
end

#parse_if(line) ⇒ Object



322
323
324
325
326
327
328
# File 'lib/kaiser_ruby/parser.rb', line 322

def parse_if(line)
  words = line.split prepared_regexp(IF_KEYWORDS)

  arg = consume_function_calls(words.last.strip)
  argument = parse_argument(arg)
  { if: { argument: argument } }
end

#parse_increment(line) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/kaiser_ruby/parser.rb', line 191

def parse_increment(line)
  match_rxp = prepared_capture(INCREMENT_FIRST_KEYWORDS, INCREMENT_SECOND_KEYWORDS)
  var = line.match(match_rxp).captures.first.strip
  capture = parse_variables(var)

  capture[:amount] = line.split(var).last.scan(/\bup\b/i).count
  { increment: capture }
end

#parse_inequality(string) ⇒ Object



497
498
499
500
501
502
503
# File 'lib/kaiser_ruby/parser.rb', line 497

def parse_inequality(string)
  words = string.rpartition prepared_regexp(INEQUALITY_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { inequality: { left: left, right: right } }
end

#parse_line(line) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kaiser_ruby/parser.rb', line 96

def parse_line(line)
  if line.strip.empty?
    if @nesting > 0
      @nesting -= 1
      @nesting_start_line = nil
    end

    add_to_tree(parse_empty_line)
  else
    obj = parse_line_content(line)
    add_to_tree obj
    update_nesting obj
  end
end

#parse_line_content(line) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/kaiser_ruby/parser.rb', line 118

def parse_line_content(line)
  words = line.split /\s/

  if matches_first?(words, IF_KEYWORDS)
    return parse_if(line)
  elsif matches_first?(words, ELSE_KEYWORDS)
    return parse_else
  elsif matches_first?(words, WHILE_KEYWORDS)
    return parse_while(line)
  elsif matches_first?(words, UNTIL_KEYWORDS)
    return parse_until(line)
  elsif matches_separate?(words, ASSIGNMENT_FIRST_KEYWORDS, ASSIGNMENT_SECOND_KEYWORDS)
    return parse_assignment(line)
  elsif matches_several_first?(line, RETURN_KEYWORDS)
    return parse_return(line)
  elsif matches_first?(words, PRINT_KEYWORDS)
    return parse_print(line)
  else
    if matches_any?(words, POETIC_STRING_KEYWORDS)
      return parse_poetic_string(line)
    elsif matches_any?(words, POETIC_NUMBER_KEYWORDS)
      return parse_poetic_type_all(line)
    else
      return(parse_listen_to(line)) if matches_several_first?(line, LISTEN_TO_KEYWORDS)
      return(parse_listen) if matches_first?(words, LISTEN_KEYWORDS)
      return(parse_break) if matches_several_first?(line, BREAK_KEYWORDS)
      return(parse_continue) if matches_several_first?(line, CONTINUE_KEYWORDS)
      return(parse_function_call(line)) if matches_any?(words, FUNCTION_CALL_KEYWORDS)

      if matches_separate?(words, INCREMENT_FIRST_KEYWORDS, INCREMENT_SECOND_KEYWORDS)
        return parse_increment(line)
      end

      if matches_separate?(words, DECREMENT_FIRST_KEYWORDS, DECREMENT_SECOND_KEYWORDS)
        return parse_decrement(line)
      end

      if matches_any?(words, FUNCTION_KEYWORDS)
        return(parse_function(line))
      end
    end
  end

  raise KaiserRuby::RockstarSyntaxError, "couldn't parse line: #{line}"
end

#parse_listenObject



180
181
182
# File 'lib/kaiser_ruby/parser.rb', line 180

def parse_listen
  { listen: nil }
end

#parse_listen_to(line) ⇒ Object



173
174
175
176
177
178
# File 'lib/kaiser_ruby/parser.rb', line 173

def parse_listen_to(line)
  words = line.split prepared_regexp(LISTEN_TO_KEYWORDS)
  arg = parse_variables(words.last.strip)
  arg[:type] = :assignment
  { listen_to: arg }
end

#parse_literal_number(string) ⇒ Object



646
647
648
649
650
651
652
653
# File 'lib/kaiser_ruby/parser.rb', line 646

def parse_literal_number(string)
  num = Float(string) rescue string
  if num.is_a?(Float)
    { number: num }
  else
    return false
  end
end

#parse_literal_string(string) ⇒ Object



638
639
640
641
642
643
644
# File 'lib/kaiser_ruby/parser.rb', line 638

def parse_literal_string(string)
  if string.strip.start_with?('"') && string.strip.end_with?('"') && string.count('"') == 2
    { string: string }
  else
    return false
  end
end

#parse_logic_operation(string) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/kaiser_ruby/parser.rb', line 419

def parse_logic_operation(string)
  testable = string.partition(prepared_regexp(LOGIC_KEYWORDS))
  return false if testable.first.count('"').odd? || testable.last.count('"').odd?

  if string =~ prepared_regexp(AND_KEYWORDS)
    return parse_and(string)
  elsif string =~ prepared_regexp(OR_KEYWORDS)
    return parse_or(string)
  elsif string =~ prepared_regexp(NOR_KEYWORDS)
    return parse_nor(string)
  end

  return false
end

#parse_lt(string) ⇒ Object



521
522
523
524
525
526
527
# File 'lib/kaiser_ruby/parser.rb', line 521

def parse_lt(string)
  words = string.rpartition prepared_regexp(LT_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { lt: { left: left, right: right } }
end

#parse_lte(string) ⇒ Object



529
530
531
532
533
534
535
# File 'lib/kaiser_ruby/parser.rb', line 529

def parse_lte(string)
  words = string.rpartition prepared_regexp(LTE_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { lte: { left: left, right: right } }
end

#parse_math_operations(string) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/kaiser_ruby/parser.rb', line 589

def parse_math_operations(string)
  return false if string.strip.start_with?('"') && string.strip.end_with?('"') && string.count('"') == 2
  words = string.split(/\s/)

  if matches_any?(words, MULTIPLICATION_KEYWORDS)
    return parse_multiplication(string)
  elsif matches_any?(words, DIVISION_KEYWORDS)
    return parse_division(string)
  elsif matches_any?(words, ADDITION_KEYWORDS)
    return parse_addition(string)
  elsif matches_any?(words, SUBTRACTION_KEYWORDS)
    return parse_subtraction(string)
  end

  return false
end

#parse_multiplication(string) ⇒ Object



622
623
624
625
626
627
628
# File 'lib/kaiser_ruby/parser.rb', line 622

def parse_multiplication(string)
  words = string.rpartition prepared_regexp(MULTIPLICATION_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { multiplication: { left: left, right: right } }
end

#parse_nor(string) ⇒ Object



451
452
453
454
455
456
457
458
# File 'lib/kaiser_ruby/parser.rb', line 451

def parse_nor(string)
  words = string.rpartition prepared_regexp(NOR_KEYWORDS)

  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { nor: { left: left, right: right } }
end

#parse_not(string) ⇒ Object



460
461
462
463
464
465
466
# File 'lib/kaiser_ruby/parser.rb', line 460

def parse_not(string)
  return false if string !~ /(?<!is )\bnot\b/i
  words = string.split prepared_regexp(NOT_KEYWORDS)
  argument = parse_argument(words.last.strip)

  { not: argument }
end

#parse_or(string) ⇒ Object



442
443
444
445
446
447
448
449
# File 'lib/kaiser_ruby/parser.rb', line 442

def parse_or(string)
  words = string.rpartition prepared_regexp(OR_KEYWORDS)

  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { or: { left: left, right: right } }
end

#parse_poetic_number_value(string) ⇒ Object



410
411
412
413
414
415
416
417
# File 'lib/kaiser_ruby/parser.rb', line 410

def parse_poetic_number_value(string)
  num = parse_literal_number(string)
  if num
    return num
  else
    return { number_literal: string.strip }
  end
end

#parse_poetic_string(line) ⇒ Object



259
260
261
262
263
264
265
# File 'lib/kaiser_ruby/parser.rb', line 259

def parse_poetic_string(line)
  words = line.partition prepared_regexp(POETIC_STRING_KEYWORDS)
  left = parse_variables(words.first.strip)
  right = { string: "\"#{words.last.strip}\"" }
  left[:type] = :assignment
  { poetic_string: { left: left, right: right } }
end

#parse_poetic_type_all(line) ⇒ Object



267
268
269
270
271
272
273
# File 'lib/kaiser_ruby/parser.rb', line 267

def parse_poetic_type_all(line)
  words = line.partition prepared_regexp(POETIC_NUMBER_KEYWORDS)
  left = parse_variables(words.first.strip)
  right = parse_type_value(words.last.strip)
  left[:type] = :assignment
  { poetic_type: { left: left, right: right } }
end

#parse_print(line) ⇒ Object

statements



165
166
167
168
169
170
171
# File 'lib/kaiser_ruby/parser.rb', line 165

def parse_print(line)
  words = line.partition prepared_regexp(PRINT_KEYWORDS)
  arg = consume_function_calls(words.last.strip)
  argument = parse_argument(arg)

  { print: argument }
end

#parse_pronounObject



585
586
587
# File 'lib/kaiser_ruby/parser.rb', line 585

def parse_pronoun
  { pronoun: nil }
end

#parse_proper_variable(string) ⇒ Object



572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/kaiser_ruby/parser.rb', line 572

def parse_proper_variable(string)
  words = string.split(/\s/)

  copied = words.dup
  copied.shift
  copied.each do |w|
    raise SyntaxError, "invalid proper variable name: #{string}" unless w =~ /\A[[:upper:]]/
  end

  words = words.map { |e| e.chars.select { |c| c =~ /[[:alpha:]]/ }.join }
  { variable_name: words.map { |w| w.downcase }.join('_') }
end

#parse_return(line) ⇒ Object



184
185
186
187
188
189
# File 'lib/kaiser_ruby/parser.rb', line 184

def parse_return(line)
  words = line.split prepared_regexp(RETURN_KEYWORDS)
  arg = consume_function_calls(words.last.strip)
  argument = parse_argument(arg)
  { return: argument }
end

#parse_subtraction(string) ⇒ Object



614
615
616
617
618
619
620
# File 'lib/kaiser_ruby/parser.rb', line 614

def parse_subtraction(string)
  words = string.rpartition prepared_regexp(SUBTRACTION_KEYWORDS)
  left = parse_argument(words.first.strip)
  right = parse_argument(words.last.strip)

  { subtraction: { left: left, right: right } }
end

#parse_type_literal(string) ⇒ Object

Raises:

  • (SyntaxError)


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/kaiser_ruby/parser.rb', line 297

def parse_type_literal(string)
  words = string.split /\s/
  raise SyntaxError, "too many words in poetic type literal: #{string}" if words.size > 1

  if matches_first?(words, NIL_TYPE)
    { type: 'nil' }
  elsif matches_first?(words, NULL_TYPE)
    { type: 'null' }
  elsif matches_first?(words, TRUE_TYPE)
    { type: 'true' }
  elsif matches_first?(words, FALSE_TYPE)
    { type: 'false' }
  else
    raise SyntaxError, "unknown poetic type literal: #{string}"
  end
end

#parse_type_value(string) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/kaiser_ruby/parser.rb', line 275

def parse_type_value(string)
  words = string.split /\s/

  if matches_first?(words, NIL_TYPE)
    raise KaiserRuby::RockstarSyntaxError, "extra words are not allowed after literal type keyword" if words.count > 1
    { type: 'nil' }
  elsif matches_first?(words, NULL_TYPE)
    raise KaiserRuby::RockstarSyntaxError, "extra words are not allowed after literal type keyword" if words.count > 1
    { type: 'null' }
  elsif matches_first?(words, TRUE_TYPE)
    raise KaiserRuby::RockstarSyntaxError, "extra words are not allowed after literal type keyword" if words.count > 1
    { type: 'true' }
  elsif matches_first?(words, FALSE_TYPE)
    raise KaiserRuby::RockstarSyntaxError, "extra words are not allowed after literal type keyword" if words.count > 1
    { type: 'false' }
  elsif string.strip.start_with?('"') && string.strip.end_with?('"')
    parse_literal_string(string)
  else
    parse_poetic_number_value(string)
  end
end

#parse_until(line) ⇒ Object



330
331
332
333
334
335
# File 'lib/kaiser_ruby/parser.rb', line 330

def parse_until(line)
  words = line.split prepared_regexp(UNTIL_KEYWORDS)
  arg = consume_function_calls(words.last.strip)
  argument = parse_argument(arg)
  { until: { argument: argument } }
end

#parse_value_or_variable(string) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/kaiser_ruby/parser.rb', line 393

def parse_value_or_variable(string)
  nt = parse_not(string)
  return nt if nt

  str = parse_literal_string(string)
  return str if str

  num = parse_literal_number(string)
  return num if num

  vars = parse_variables(string)
  return vars if vars

  tpl = parse_type_literal(string)
  return tpl if tpl
end

#parse_variables(string) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/kaiser_ruby/parser.rb', line 537

def parse_variables(string)
  words = string.split /\s/
  words = words.map { |e| e.chars.select { |c| c =~ /[[:alnum:]]|\./ }.join }
  string = words.join(' ')

  if string =~ prepared_regexp(PRONOUN_KEYWORDS)
    return parse_pronoun
  elsif matches_first?(words, COMMON_VARIABLE_KEYWORDS)
    return parse_common_variable(string)
  elsif matches_all?(words, /\A[[:upper:]]/) && string !~ prepared_regexp(RESERVED_KEYWORDS)
    return parse_proper_variable(string)
  end

  return false
end

#parse_while(line) ⇒ Object



337
338
339
340
341
342
# File 'lib/kaiser_ruby/parser.rb', line 337

def parse_while(line)
  words = line.split prepared_regexp(WHILE_KEYWORDS)
  arg = consume_function_calls(words.last.strip)
  argument = parse_argument(arg)
  { while: { argument: argument } }
end

#pass_function_calls(string) ⇒ Object



362
363
364
365
366
367
368
# File 'lib/kaiser_ruby/parser.rb', line 362

def pass_function_calls(string)
  if string.strip =~ /func_\d+\Z/
    { passed_function_call: string }
  else
    return false
  end
end

#prepared_capture(farr, sarr) ⇒ Object



672
673
674
675
676
# File 'lib/kaiser_ruby/parser.rb', line 672

def prepared_capture(farr, sarr)
  frxp = farr.map { |a| '\b' + a + '\b' }.join('|')
  srxp = sarr.map { |a| '\b' + a + '\b' }.join('|')
  Regexp.new(frxp + '(.*?)' + srxp + '(.*)', Regexp::IGNORECASE)
end

#prepared_regexp(array) ⇒ Object



667
668
669
670
# File 'lib/kaiser_ruby/parser.rb', line 667

def prepared_regexp(array)
  rxp = array.map { |a| '\b' + a + '\b' }.join('|')
  Regexp.new(rxp, Regexp::IGNORECASE)
end

#update_nesting(object) ⇒ Object



111
112
113
114
115
116
# File 'lib/kaiser_ruby/parser.rb', line 111

def update_nesting(object)
  if i(if function until while).include? object.keys.first
    @nesting += 1
    @nesting_start_line = @lnum
  end
end