86 lines
2.2 KiB
Markdown
86 lines
2.2 KiB
Markdown
|
|
I want a command-line interpreter with optional echo and rainbow parenthessis.
|
|
I want to read from a file, printing what was read and then the result.
|
|
|
|
I want 'define' or 'def' or some symbol creates a new global namespace/envronment
|
|
that falls back to the previous.
|
|
|
|
// this is mostly a neat calculator
|
|
|
|
// let's also read an .13rc init file. There's a command-line option not to.
|
|
|
|
// main will be a parse, eval, print and free loop.
|
|
|
|
// I want to be able to change things interactively
|
|
// using extra syntax not allowed in the files?
|
|
|
|
// I want a good interactive prompt:
|
|
// colors
|
|
// syntax errors
|
|
|
|
// in order to get that, the tokenizer will have to hand out token objects \
|
|
that are assigned colors?
|
|
|
|
// the parser will also have to gracefully handle syntax errors, by creating \
|
|
'syntax error' expressions
|
|
|
|
// main will have to scan for these? and partially color things that make \
|
|
sense and give a message for the rest?
|
|
|
|
// reading from a file will be senstive to newlines. newlines end a statement. \
|
|
that is, a statement is ended with a newline only if the statement would \
|
|
validily parse ending where it is. otherwise, ignore the newline.
|
|
// semicolons delimit multiple statements on the same line.
|
|
|
|
// a b c => (a(b))(c)
|
|
|
|
// a lambda: b c
|
|
// a(lambda: b c)
|
|
// (a(lambda: b))(c)
|
|
|
|
// lambda x: x 4
|
|
|
|
// lambda x: x (lambda y: y)
|
|
|
|
// (lambda x: x) 4
|
|
|
|
// lambda n: lambda f: lambda x: f (n f x)
|
|
// lambda n: (lambda f: (lambda x: f((n(f))(x))))
|
|
|
|
// a b c d e
|
|
// (((a(b))(c))(d))(e)
|
|
|
|
```
|
|
primary-expression
|
|
: variable-name // use of variable
|
|
| integer-literal // literal
|
|
| '(' root-expression ')' // higher operator preceidence
|
|
;
|
|
|
|
abstraction: ('lambda' | 'λ') (variable-name (',' variable-name)*)? (':' | '.') postfix-expression;
|
|
|
|
postfix-expression
|
|
: primary-expression
|
|
| postfix-expression primary-expression // function call
|
|
| postfix-expression abstraction // function call passing in a function declaration
|
|
;
|
|
|
|
lambda-expression
|
|
: postfix-expression
|
|
| abstraction
|
|
;
|
|
|
|
root-expression: lambda-expression;
|
|
|
|
expression: root-expression;
|
|
|
|
statement: (variable-name '=')* expression;
|
|
|
|
statements: statement (';' statement)* ';'?;
|
|
|
|
%start: statements;
|
|
```
|
|
|
|
|
|
|
|
|