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

57 lines
1.8 KiB
Markdown
Raw Normal View History

2022-04-22 04:15:30 +00:00
---
title: Grammar
author:
- 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
2022-04-22 18:00:31 +00:00
```
<number> ::= [-]? [0-9]+ (.[0-9]+)? | [-]? [0-9]+ (.[0-9]+)? [e] [-]? [0-9]+
<symbol> ::= [a-zA-Z]+ [a-zA-Z0-9]?
<bool> ::= true | false
```
2022-04-22 04:15:30 +00:00
## Expressions
2022-04-22 16:42:05 +00:00
The following gives no typing information. You can obey the grammar and still write nonsensical code.
2022-04-22 04:15:30 +00:00
Think of javascript's list unpacking notation to read our variable-argument function `mixture`.
```
2022-04-22 18:00:31 +00:00
<expr> ::= <term> + <expr> | <term> - <expr> | <expr> .+ <expr> | <expr> .- <expr> | <term>
2022-04-22 04:15:30 +00:00
<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>) | ...
2022-04-22 04:15:30 +00:00
```
## Data structures
```
<array> ::= [] | [<expr>] | [<expr>, <expr>] | ...
<record> ::= {} | {<symbol>: <expr>} | {<symbol>: <expr>, <symbol>: <expr>} | ...
```
## Statements
```
2022-04-22 16:42:05 +00:00
<statement> ::= <assign> | <assignFunction>
<assign> ::= <symbol> = <expr>
<assignFunction> ::= <symbol>(<symbol>) = <expr> | <symbol>(<symbol>, <symbol>) = <expr> | ...
2022-04-22 04:15:30 +00:00
```
## A squiggle file
To be valid and raise no errors as of current (apr22) interpreter,
```
<delim> ::= ; | \n
2022-04-22 16:42:05 +00:00
<code> ::= <expr> | <statement> <delim> <expr> | <statement> <delim> <statement> <delim> <expr> | ...
2022-04-22 04:15:30 +00:00
```
This isn't strictly speaking true; the interpreter allows expressions outside of the final line.