SPACE = /[ \t\v\n\r]+/
IDENT = /[_a-zA-Z][_a-zA-Z0-9]*/
BRACES = /[\(\)\[\]\{\}]/
- OPERATORS = /=/
+ OPERATORS = /[*\/=+-]/
INTEGER = /[0-9]+/
ID_TYPES = {
"requires" => :requires,
unary: 8,
call: 9,
primary: 10,
- eof: 11
+ eof: 11,
}
+ LVLNAMES = LEVELS.keys
+
RULES = {
- "(" => { prefix: :grouping, infix: :func_call, level: :call },
- :int => { prefix: :constant, infix: nil, level: :none },
+ "(" => { prefix: :grouping, infix: :func_call, level: :call },
+ "+" => { prefix: :unary, infix: :binary, level: :term },
+ "-" => { prefix: :unary, infix: :binary, level: :term },
+ "*" => { prefix: nil, infix: :binary, level: :factor },
+ "/" => { prefix: nil, infix: :binary, level: :factor },
+ :int => { prefix: :constant, infix: nil, level: :none },
}
def initialize(path)
expect("=")
value = expression()
end
- p "value", value
+ pp value
end
def declaration(syms)
def func_call(ast)
expect(")")
- ast
+ AST::Apply.new(nil, ast, [])
+ end
+
+ def unary()
+ type = @prev.type
+ AST::Apply.new(nil, @prev.type, [parseLevel(:unary)])
+ end
+
+ def nextLevel(tok)
+ level = getRule(@prev)[:level]
+ index = LVLNAMES.find_index(level)
+ LVLNAMES[index + 1]
+ end
+
+ def binary(left)
+ type = @prev.type
+ nlvl = nextLevel(type)
+ right = parseLevel(nlvl)
+ AST::Apply.new(nil, type, [left, right])
end
def error(str)
end
def dump
- require 'pp'
pp @definitions
end
end