Class: Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(istream) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
# File 'lib/parser.rb', line 8

def initialize(istream)
  @scan = Scanner.new(istream)
  @todo = true
end

Instance Method Details

#ExprObject



30
31
32
# File 'lib/parser.rb', line 30

def Expr() 
  return RestExpr(Term())
end

#ExprEWEObject



134
135
136
137
138
# File 'lib/parser.rb', line 134

def ExprEWE()
  result = Term()
  result = RestExpr(result)
  return result
end

#FactorObject

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/parser.rb', line 95

def Factor() 
  t = @scan.getToken

  if t.type == :number then
    return NumNode.new(t.lex.to_i)
  end

  if t.type == :identifier then

    var = t.lex.to_s
    value = $calc.recallVar(t.lex.to_s)

    return IdentifierNod.new(var, value.to_i)
  end

  if t.type == :keyword then
    if t.lex == "R" then
      return RecallNode.new
    end
    if t.lex == "C" then
      $calc.store(0)
      return RecallNode.new
    end
  end
  if t.type == :lparen then
    
    expr = Expr()
    t = @scan.getToken

    if t.type == :rparen then
      return expr
    else
      raise ParseError.new
    end
  end

  raise ParseError.new
end

#parseObject



13
14
15
# File 'lib/parser.rb', line 13

def parse()
  return Prog()
end

#ProgObject

private private public protected



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/parser.rb', line 18

def Prog()
  result = Expr()
  t = @scan.getToken()
  
  if t.type != :eof then
    print "Expected EOF. Found ", t.type, ".\n"
    raise ParseError.new
  end
  
  return result
end

#RestExpr(e) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/parser.rb', line 34

def RestExpr(e) 
  t = @scan.getToken()
  
  if t.type == :add then
       return RestExpr(AddNode.new(e,Term()))
  end
  
  if t.type == :sub then
    return RestExpr(SubNode.new(e,Term()))
  end
    
  @scan.putBackToken()
  
  return e
end

#RestTerm(e) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parser.rb', line 54

def RestTerm(e)
  t = @scan.getToken

  if t.type == :times then
    return RestTerm(TimesNode.new(e,Term()))
  end

  if t.type == :divide then
    return RestTerm(DivNode.new(e,Term()))
  end

  if t.type == :mod then
    return RestTerm(ModNode.new(e,Term()))
  end

  @scan.putBackToken
  return e
end

#StorableObject



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

def Storable()
    factor = Factor()

    t = @scan.getToken

    if t.type == :keyword then
      if t.lex == "S" then
        return StoreNode.new(factor)
      end
      if t.lex == "M" then
        return MNode.new(factor)
      end
      if t.lex == "P" then
        return PNode.new(factor)
      end
    end

    @scan.putBackToken

    factor
end

#TermObject



50
51
52
# File 'lib/parser.rb', line 50

def Term()
  return RestTerm(Storable())
end