lib/qlang/parser/formula_parser.rb in qlang-0.0.27182000 vs lib/qlang/parser/formula_parser.rb in qlang-0.0.27182100
- old
+ new
@@ -1,36 +1,36 @@
+require 'qlang/lexer/formula_lexer'
+
module Qlang
module Parser
- # FIX:
module FormulaParser
- def execute(lexed)
- ss = StringScanner.new(lexed)
- result = ''
- until ss.eos?
- { EXP: /\^/, BFUNC: /sin|cos|tan|log/, MUL: /(pi|[1-9a-z]){2,}/, SNGL: /(pi|[1-9a-z])/, OTHER: /([^\^1-9a-z]|^pi)+/ }.each do |token , rgx|
- if ss.scan(rgx)
- item = case token
- when :EXP
- $meta_info.lang == :ruby ? '**' : '^'
- when :MUL
- sss = StringScanner.new(ss[0])
- ary = []
- until sss.eos?
- [/pi/, /[1-9a-z]/].each do |rgx2|
- ary << sss[0] if sss.scan(rgx2)
- end
- end
- ary.join(' * ')
- else
- ss[0]
+ include Lexer::Tokens
+
+ def self.execute(str)
+ lexed = Lexer::FormulaLexer.new(str)
+ time = Time.now
+ loop do
+ fail "I'm so sorry, something wrong. Please feel free to report this." if Time.now > time + 10
+ case lexed.token_str
+ when /:EXP(\d+)/
+ new_ope = $meta_info.lang == :ruby ? '**' : '^'
+ lexed.parsed!(new_ope, $1)
+ when /:MUL(\d+)/
+ sss = StringScanner.new(lexed.get_value($1))
+ ary = []
+ until sss.eos?
+ [/pi/, /[1-9a-z]/].each do |rgx2|
+ ary << sss[0] if sss.scan(rgx2)
end
- result += item
- break
end
+ parsed = ary.join(' * ')
+
+ lexed.parsed!(parsed, $1)
+ else
+ break
end
end
- result
+ lexed.values.join
end
- module_function :execute
end
end
end