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



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

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



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

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)


685
686
687
688
# File 'lib/kaiser_ruby/parser.rb', line 685

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)


680
681
682
683
# File 'lib/kaiser_ruby/parser.rb', line 680

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)


690
691
692
693
694
695
# File 'lib/kaiser_ruby/parser.rb', line 690

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)


697
698
699
# File 'lib/kaiser_ruby/parser.rb', line 697

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

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

Returns:

  • (Boolean)


705
706
707
708
709
710
# File 'lib/kaiser_ruby/parser.rb', line 705

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)


701
702
703
# File 'lib/kaiser_ruby/parser.rb', line 701

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



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

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



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

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



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

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



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

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



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

def parse_break
  { break: nil }
end

#parse_common_variable(string) ⇒ Object



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

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}:#{@lnum+1}" 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



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

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



223
224
225
# File 'lib/kaiser_ruby/parser.rb', line 223

def parse_continue
  { continue: nil }
end

#parse_decrement(line) ⇒ Object



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

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



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

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



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

def parse_else
  { else: nil }
end

#parse_empty_lineObject



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

def parse_empty_line
  { empty_line: nil }
end

#parse_equality(string) ⇒ Object



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

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



346
347
348
349
350
351
# File 'lib/kaiser_ruby/parser.rb', line 346

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



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

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



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

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



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

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



555
556
557
558
559
# File 'lib/kaiser_ruby/parser.rb', line 555

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

#parse_gt(string) ⇒ Object



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

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



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

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



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

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



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

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



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

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
110
111
# File 'lib/kaiser_ruby/parser.rb', line 96

def parse_line(line)
  line = line.strip

  if line.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



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
163
164
# File 'lib/kaiser_ruby/parser.rb', line 120

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}:#{@lnum+1}"
end

#parse_listenObject



182
183
184
# File 'lib/kaiser_ruby/parser.rb', line 182

def parse_listen
  { listen: nil }
end

#parse_listen_to(line) ⇒ Object



175
176
177
178
179
180
# File 'lib/kaiser_ruby/parser.rb', line 175

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



587
588
589
# File 'lib/kaiser_ruby/parser.rb', line 587

def parse_pronoun
  { pronoun: nil }
end

#parse_proper_variable(string) ⇒ Object



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

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}:#{@lnum+1}" 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



186
187
188
189
190
191
# File 'lib/kaiser_ruby/parser.rb', line 186

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



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

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)


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

def parse_type_literal(string)
  words = string.split /\s/
  raise SyntaxError, "too many words in poetic type literal: #{string}:#{@lnum+1}" 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}:#{@lnum+1}"
  end
end

#parse_type_value(string) ⇒ Object



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

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: #{string}:#{@lnum+1}" if words.count > 1
    { type: 'nil' }
  elsif matches_first?(words, NULL_TYPE)
    raise KaiserRuby::RockstarSyntaxError, "extra words are not allowed after literal type keyword: #{string}:#{@lnum+1}" if words.count > 1
    { type: 'null' }
  elsif matches_first?(words, TRUE_TYPE)
    raise KaiserRuby::RockstarSyntaxError, "extra words are not allowed after literal type keyword: #{string}:#{@lnum+1}" if words.count > 1
    { type: 'true' }
  elsif matches_first?(words, FALSE_TYPE)
    raise KaiserRuby::RockstarSyntaxError, "extra words are not allowed after literal type keyword: #{string}:#{@lnum+1}" 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



332
333
334
335
336
337
# File 'lib/kaiser_ruby/parser.rb', line 332

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



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

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



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

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



339
340
341
342
343
344
# File 'lib/kaiser_ruby/parser.rb', line 339

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



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

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



674
675
676
677
678
# File 'lib/kaiser_ruby/parser.rb', line 674

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



669
670
671
672
# File 'lib/kaiser_ruby/parser.rb', line 669

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

#update_nesting(object) ⇒ Object



113
114
115
116
117
118
# File 'lib/kaiser_ruby/parser.rb', line 113

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