squiggle/packages/website/docs/Internal/Grammar.md
Quinn Dougherty 7035b581a6 2/3 todos
2022-04-22 14:00:31 -04:00

1.6 KiB

title author
Grammar
Quinn Dougherty

Formal grammar specification, reference material for parser implementation.

In all likelihood the reference will have to be debugged as we see what tests pass and don't pass during implementation.

Lexical descriptions of constants and identifiers

<number> ::= [-]? [0-9]+ (.[0-9]+)? | [-]? [0-9]+ (.[0-9]+)? [e] [-]? [0-9]+
<symbol> ::= [a-zA-Z]+ [a-zA-Z0-9]?
<bool> ::= true | false

Expressions

The following gives no typing information. You can obey the grammar and still write nonsensical code.

Think of javascript's list unpacking notation to read our variable-argument function mixture.

<expr> ::= <term> + <expr> | <term> - <expr> | <expr> .+ <expr> | <expr> .- <expr> | <term>
<term> ::= <power> * <term> | <power> <term> | <power> / <term> | <power> .* <term> | <power ./ <term> | <power>
<power> ::= <factor> ^ <power> | <factor> .^ <power> | <factor>
<factor> ::= <number> | <bool> | <symbol> | ( <expr> ) | <symbol>(<symbol>)

Data structures

<array> ::= [] | [<expr>] | [<expr>, <expr>] | ...
<record> ::= {} | {<symbol>: <expr>} | {<symbol>: <expr>, <symbol>: <expr>} | ...

Statements

<statement> ::= <assign> | <assignFunction>
<assign> ::= <symbol> = <expr>
<assignFunction> ::= <symbol>(<symbol>) = <expr>

A squiggle file

To be valid and raise no errors as of current (apr22) interpreter,

<delim> ::= ; | \n
<code> ::= <expr> | <statement> <delim> <expr> | <statement> <delim> <statement> <delim> <expr> | ...

This isn't strictly speaking true; the interpreter allows expressions outside of the final line.