lib/qlang/lexer/tokens.rb in qlang-0.0.27180000 vs lib/qlang/lexer/tokens.rb in qlang-0.0.27182000

- old
+ new

@@ -1,45 +1,78 @@ module Qlang module Lexer module Tokens - # FIRST TOKEN - NUM = '[0-9]+' - VAR = '[a-z]' - FUNCV = '[a-zA-Z]' - VARNUM = '[0-9a-z]' + # NUM + INT = /[0-9]+/ + FLO = /[0-9]+\.[0-9]+/ + E = /e/ + PI = /pi/ + NUM = /(#{FLO}|#{INT}|#{E}|#{PI})/ + + # FUNCTION + EMBEDDED_FUNC = /(sin|cos|tan|log)/ + USER_FUNC = /[a-zA-Z]/ + FUNCV = /(#{EMBEDDED_FUNC}|#{USER_FUNC})/ + + # VARIABLE + VAR = /([a-d]|[f-z])/ + #VAR_MUL2 = /(?!pi)#{VAR}{2}/ + # #VAR_MUL3 = /(?!#{EMBEDDED_FUNC})#{VAR}{3}/ + # # FIX: + #VAR_MUL = /(?!#{EMBEDDED_FUNC})#{VAR_MUL2}/ + + # # TERM + # TERM = /(#{NUM}|#{VAR_MUL}|#{VAR_MUL})/ + + + # OPE + PLS = /\+/ + SUB = /-/ + MUL = /\*/ + DIV = /\// + EXP = /(\*\*|\^)/ + OPE = /(#{PLS}|#{SUB}|#{MUL}|#{DIV}|#{EXP})/ + + VARNUM = /(#{NUM}|#{VAR})/ ANYSP = ' *' - ANYSTR = '.+' - NONL = '[^\r\n]' - LPRN = '\(' - RPRN = '\)' - LBRC = '\{' - RBRC = '\}' - CLN = '\:' - SCLN = ';' - CMA = '\,' - SP = ' ' + ANYSTR = /.+/ + NONL = /[^\r\n]/ - # SECOND TOKEN - class ::String - def line_by(char) - "#{ANYSP}#{self}(#{ANYSP}#{char}#{ANYSP}#{self})*#{ANYSP}" + LPRN = /\(/ + RPRN = /\)/ + PRN = /(#{LPRN}|#{RPRN})/ + + LBRC = /\{/ + RBRC = /\}/ + BRC = /(#{LBRC}|#{RBRC})/ + + CLN = /\:/ + SCLN = /;/ + CMA = /\,/ + SP = / / + + # TODO: what is better + class Util + def self.string_out(str, partition) + /#{ANYSP}#{str}(#{ANYSP}#{partition}#{ANYSP}#{str})*#{ANYSP}/ end - end - NUMS_BY_CMA = NUM.line_by(CMA) - VARS_BY_CMA = VAR.line_by(CMA) - VARNUMS_BY_CMA = VARNUM.line_by(CMA) - NUMS_BY_SP = NUM.line_by(SP) - # THIRD TOKEN - class ::String - def func_call - "#{FUNCV}#{LPRN}#{ANYSP}#{self}#{ANYSP}#{RPRN}" + def self.func_call(args) + /#{FUNCV}#{LPRN}#{ANYSP}#{args}#{ANYSP}#{RPRN}/ end end - FUNCCN = NUMS_BY_CMA.func_call - FUNCCV = VARS_BY_CMA.func_call - FUNCCVN = VARNUMS_BY_CMA.func_call - NUMS_BY_SP_BY_SCLN = NUMS_BY_SP.line_by(SCLN) + NUMS_BY_CMA = Util.string_out(NUM, CMA) + VARS_BY_CMA = Util.string_out(VAR, CMA) + VARNUMS_BY_CMA = Util.string_out(VARNUM, CMA) + NUMS_BY_SP = Util.string_out(NUM, SP) + + FUNCCN = Util.func_call(NUMS_BY_CMA) + FUNCCV = Util.func_call(VARS_BY_CMA) + FUNCCVN = Util.func_call(VARNUMS_BY_CMA) + + NUMS_BY_SP_BY_SCLN = Util.string_out(NUMS_BY_SP, SCLN) + + FORMULA = /(#{OPE}|#{FUNCV}|#{VAR}|#{NUM}|#{PRN}|#{ANYSP})+/ end end end