Module: Parser

Included in:
SpecialGiggle
Defined in:
lib/special-giggle/parser.rb

Defined Under Namespace

Classes: ParserError, Token

Constant Summary collapse

BinaryOps =
{
  :Plus         => 50,
  :Minus        => 50,
  :Asterisk     => 60,
  :Slash        => 60,
  :Percent      => 60,
  :Bar          => 10,
  :Ampersand    => 30,
  :Caret        => 20,
  :LeftShift    => 40,
  :RightShift   => 40,
}
UnaryOps =
{
  :Tilde        => 70,
  :Minus        => 80,
}

Class Method Summary collapse

Class Method Details

.parse(tokens) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/special-giggle/parser.rb', line 72

def self.parse(tokens)
  head = Token.new(:Program)
  statement = Array.new
  tokens.each do |token|
    if token.type == :SemiColon
      head.child << parse_statement(statement)
      statement = Array.new
    else
      statement << token
    end
  end
  head
end

.parse_expression(tokens) ⇒ Object



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
56
57
58
59
60
# File 'lib/special-giggle/parser.rb', line 30

def self.parse_expression(tokens)
  if tokens.length == 1 && tokens[0].type == :Variable
    Token.new(:Variable, tokens[0].string)
  elsif tokens.length == 1 && tokens[0].type == :Number
    Token.new(:Number, tokens[0].string.to_i)
  elsif tokens.length >= 3 && tokens[0].type == :LeftBracket && tokens[-1].type == :RightBracket
    Token.new(:Bracket, parse_expression(tokens[1...-1]))
  elsif tokens.length >= 2 && UnaryOps.include?(tokens[0].type)
    if tokens[0].type == :Minus
      Token.new(:Negative, parse_expression(tokens[1..-1]))
    else
      Token.new(tokens[0].type, parse_expression(tokens[1..-1]))
    end
  elsif tokens.length >= 3
    head, precedence = nil, 100
    (tokens.length-1).downto(0) do |index|
      token = tokens[index]
      next if index == 0 || index == tokens.length - 1 || !BinaryOps.include?(token.type)
      next unless precedence.nil? || precedence > BinaryOps[token.type]
      begin
        head = Token.new(token.type, parse_expression(tokens[0..index-1]), parse_expression(tokens[index+1..-1]))
        precedence = BinaryOps[token.type]
      rescue ParserError
      end
    end
    raise ParserError, "Syntax error in statement: #{tokens.map(&:string).join(' ')}" if head.nil?
    head
  else
    raise ParserError, "Syntax error in statement: #{tokens.map(&:string).join(' ')}"
  end
end

.parse_statement(tokens) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/special-giggle/parser.rb', line 62

def self.parse_statement(tokens)
  if tokens.length >= 1 && tokens[0].type == :Print
    Token.new(:Print, parse_expression(tokens[1..-1]))
  elsif tokens.length >= 2 && tokens[0].type == :Variable && tokens[1].type == :Equal
    Token.new(:Move, Token.new(:Variable, tokens[0].string), parse_expression(tokens[2..-1]))
  else
    raise ParserError, "Syntax error in statement: #{tokens.map(&:string).join(' ')}"
  end
end