Class: Zaid::LexerComponents::Compressor

Inherits:
Object
  • Object
show all
Includes:
Keywords
Defined in:
lib/zaid/lexer_components/compressor.rb

Constant Summary

Constants included from Keywords

Keywords::AND, Keywords::CLASS, Keywords::DIVIDE, Keywords::ELSE, Keywords::EQUALS, Keywords::FALSE, Keywords::GREATER, Keywords::IF, Keywords::IS, Keywords::IT_IS, Keywords::LESS, Keywords::METHOD, Keywords::MINUS, Keywords::NIL, Keywords::NOT, Keywords::OR, Keywords::PLUS, Keywords::RECEIVE, Keywords::THAN, Keywords::THEN, Keywords::TIMES, Keywords::TRUE, Keywords::WAS, Keywords::WHILE

Instance Method Summary collapse

Instance Method Details

#compress(tokens) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zaid/lexer_components/compressor.rb', line 10

def compress(tokens)
  compressed = []
  between_receive_and_then = false

  compression_position = 0
  while compression_position < tokens.length
    case tokens[compression_position]
    when [:RECEIVE, RECEIVE]
      between_receive_and_then = true

      compressed << tokens[compression_position]

      compression_position += 1
    when [:THEN, THEN]
      between_receive_and_then = false

      compressed << tokens[compression_position]

      compression_position += 1
    when [:IF, IF]
      compression_position += compress_if(tokens, compressed, compression_position)
    when [:WHILE, WHILE]
      compression_position += compress_while(tokens, compressed, compression_position)
    when [:GREATER, GREATER]
      compression_position += compress_greater_than_or_equals(tokens, compressed, compression_position)
    when [:LESS, LESS]
      compression_position += compress_less_than_or_equals(tokens, compressed, compression_position)
    when [:EQUALS, EQUALS]
      compression_position += compress_equals(compressed)
    when [:NOT, NOT]
      compression_position += compress_not_equals(tokens, compressed, compression_position)
    when [:OR, OR]
      compression_position += compress_or(compressed)
    when [:AND, AND]
      compression_position += compress_and(tokens, compressed, compression_position, between_receive_and_then)
    when [:PLUS, PLUS], [:MINUS, MINUS], [:TIMES, TIMES], [:DIVIDE, DIVIDE]
      compression_position += compress_arabic_arithmetic_operator(tokens[compression_position], compressed)
    else
      compressed << tokens[compression_position]

      compression_position += 1
    end
  end

  compressed
end