class Calculator::Lexer
This lexser creates the tokens splitting the input string according to the operators (OP => see Calculator ) or brackets it returns nil if an unaccepted char (non-number or non-OP or non-bracket) is found
- Author
-
Massimiliano Dal Mas (max.codeware@gmail.com)
- License
-
Distributed under MIT license
Public Instance Methods
tokenize(string)
click to toggle source
It creates the tokens according to `OP` or '(' and ')'
-
*argument*: the string that needs to be tokenized
-
*returns*: array of tokens if all the chars are correct;
nil
else
# File lib/linmeric/Calculator.rb, line 65 def tokenize(string) stream = [] temp = "" for i in 0...string.size if OP.include? string[i] or ["(",")"].include? string[i] then stream << Token.new(temp) unless temp == "" stream << Token.new(string[i]) temp = "" elsif string[i].number? then temp += string[i] else return nil end end stream << Token.new(temp) unless temp == "" return stream end