douglascrockford / howjavascriptworks

Programs from the book _How JavaScript Works_
319 stars 53 forks source link

Function parsing seems to fail in Neo #9

Closed AlienKevin closed 5 years ago

AlienKevin commented 5 years ago

I passed this source string to parse(tokenize(source))

def bar: f a {
    let a: a + 1
}

and parser returns an error object:

{ id: '(error)',
  zeroth:
   { id: 'f a',
     alphameric: true,
     line_nr: 0,
     column_nr: 9,
     column_to: 12 },
  wunth: 'expected a variable' }

Do I have the wrong function syntax or the parser breaks?

AlienKevin commented 5 years ago

I also don't understand how you disambiguate function expressions in Neo. Let's say we are parsing a variable named 'f'

if 'f' is defined in scope:
    **'f' may be a variable or start of function expression**
    there is no way to disambiguate this case
    because variable names can contain spaces
    and function arguments immediately follows 'f' keyword
else:
    'f' must be start of function expression

An example of ambiguity would be:

var f: f n (n * 2)
def bar: f (10)

I can say bar is a

  1. function with zero parameters and returns 10
  2. function call of previously defined function f which will double the input and return 20.
AlienKevin commented 5 years ago

I think maybe 'f' should be the only reserved word in Neo. That means no variable names can be 'f' or a spaced separated name starting with 'f'. Otherwise, parsing is going to be very complex.

douglascrockford commented 5 years ago

Use 'ƒ' to make functions.