JohnEarnest / ok

An open-source interpreter for the K5 programming language.
MIT License
587 stars 72 forks source link

Minus Ambiguity and Strings #16

Closed JohnEarnest closed 8 years ago

JohnEarnest commented 9 years ago

Making note of an issue brought to my attention by Clynt Pinfold via email. The hack used to deal with the ambiguity of the - operator as a numeric prefix or the verb "negate" is overeager and will alter the contents of string literals if they match the pattern:

  "x-4"
"x- 4"

The cause is clear; the best resolution less so.

refi64 commented 9 years ago

Here's the easiest solution in Python-like pseudocode (not a JavaScript person):

new_string = ''
regex = '(string_regex)|(([A-Za-z0-9\]\)])-(\d|(\.\d))|(.)'
for match in regex.findall(string):
    data = match.get_text()
    if the second and third groups were matched:
        data = '$second_group - $third_group
    new_string += data
refi64 commented 9 years ago

I can work up a PR if wanted.