squiggle/packages/website/docs/Internal/Grammar.md

1.8 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> ) | <array> | <record> | <record>.<symbol> | <symbol> => <expr> | <symbol>(<symbol>) | <symbol>(<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> | <symbol>(<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.