Carp icon indicating copy to clipboard operation
Carp copied to clipboard

register can't register symbols that start with numerals

Open scolsen opened this issue 3 years ago • 4 comments

to reproduce:

(register 1Foo Int "1Foo")

scolsen avatar Jan 07 '22 18:01 scolsen

I think this is because it’s parsed as (register 1 Foo Int "1Foo").

See also:

> (+ 1x)
Can't find symbol 'x' at REPL:3:5.

Traceback:
  (+ 1 x) at REPL:3:1.

> (defdynamic x 1)
> (+ 1x)
=> 2

hellerve avatar Jan 07 '22 19:01 hellerve

ahhh good catch--we probably shouldn't inject the whitespace right? Or is there good reason to keep that behavior?

scolsen avatar Jan 07 '22 22:01 scolsen

I think this an outcome of

https://github.com/carp-lang/Carp/blob/3522b0ad982006cd3ea577d1f23d8f033a0cad96/src/Parsing.hs#L590-L593

which is the only occurrence of whitespaceOrNothing. So it’s definitely intentional. My guess is that this is to allow for things such as (define x 1)(+ x 1) (notice that there is no space between the two forms). Some forms, like arrays and lists do not need them to be logically separated. Some do. I’m not sure how to best resolve this; maybe switch to two classes of parse expressions like this?

needsWhitespace :: Parsec.Parsec String ParseState XObj
needsWhitespace = do
  x <- Parsec.choice [ref, deref, copy, quote, quasiquote, unquoteSplicing, unquote,  atom]
  _ <- whitespace
  pure x

selfClosing :: Parsec.Parsec String ParseState XObj
selfClosing = do
  x <- Parsec.choice [list, staticArray, array, dictionary]
  _ <- whitespaceOrNothing
  pure x

sexpr :: Parsec.Parsec String ParseState XObj
sexpr = Parsec.choice [selfClosing, needsWhitespace]

hellerve avatar Jan 08 '22 20:01 hellerve

That makes sense, could we solve it with lookahead? if the next char is not any of ()[], parse it as a symbol?

Or rather the rule for symbols could just be any number of chars until whitespace occurs.

scolsen avatar Jan 09 '22 00:01 scolsen