Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anonymous proc definition not allowed in statement position #1429

Open
mrgaturus opened this issue Aug 23, 2024 · 1 comment
Open

anonymous proc definition not allowed in statement position #1429

mrgaturus opened this issue Aug 23, 2024 · 1 comment
Labels
compiler/lex-parse Parsing and lexing subsystem of the compiler compiler/sem Related to semantic-analysis system of the compiler

Comments

@mrgaturus
Copy link
Contributor

creating a proc from a template using this elegant syntax is not possible due parser limitations

Example

type
  MyProc = proc(a, b: int32): int32 {.nimcall.}

template mad(c: int32): MyProc =
  proc (a, b: int32): int32 {.nimcall.} = # -- Syntax Error? --
    (a + b) * c

let fn = mad(2)
echo fn(10, 10) # 40

Actual Output

Error: identifier expected, but found '('

Expected Output

40

Possible Solution

a workaround requires an extra symbol

template mad(c: int32): MyProc =
  let result =
    proc (a, b: int32): int32 {.nimcall.} =
      (a + b) * c
  # Extra Symbol Involved
  result

References

works on nim

@mrgaturus mrgaturus changed the title parser: syntax error in elegant proc creation from template parser: syntax error when using an elegant syntax for proc creation from template Aug 23, 2024
@zerbina zerbina added the compiler/lex-parse Parsing and lexing subsystem of the compiler label Aug 23, 2024
@zerbina zerbina changed the title parser: syntax error when using an elegant syntax for proc creation from template anonymous proc definition not allowed in statement position Aug 23, 2024
@zerbina
Copy link
Collaborator

zerbina commented Aug 23, 2024

Anonymous proc definitions being a syntax error not only affects templates (I've updated the title to reflect this), but other contexts as well.

The current grammar (and parser) state that a routine definition where a complexOrSimpleStmt is expected must be named, though it would make sense to lift this restriction.

@zerbina zerbina added the compiler/sem Related to semantic-analysis system of the compiler label Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/lex-parse Parsing and lexing subsystem of the compiler compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

No branches or pull requests

2 participants