-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr-print.fs
executable file
·30 lines (25 loc) · 956 Bytes
/
expr-print.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#! /usr/bin/env gforth
require bnf.fs
bnf: {DIGIT} t' 0 | t' 1 | t' 2 | t' 3 | t' 4 |
t' 5 | t' 6 | t' 7 | t' 8 | t' 9 ;bnf
bnf: <DIGIT> @token 48 - {DIGIT} $$ ! {{ 10 * $$ @ + }} ;bnf
bnf: <NUMBER'> <DIGIT> <NUMBER'> | <DIGIT> ;bnf
bnf: <NUMBER> {{ 0 }} <NUMBER'> {{ . }} ;bnf
defer <'EXPRESSION>
bnf: <ELEMENT> t' ( <'EXPRESSION> t' ) | <NUMBER> ;bnf
bnf: <PRIMARY> t' - <PRIMARY> {{ ." negate " }}
| <ELEMENT> ;bnf
bnf: <FACTOR> <PRIMARY> t' ^ <FACTOR> {{ ." POWER " }}
| <PRIMARY> ;bnf
bnf: <T'> t' * <FACTOR> <T'> {{ ." * " }}
| t' / <FACTOR> <T'> {{ ." / " }}
| ;bnf
bnf: <TERM> <FACTOR> <T'> ;bnf
bnf: <E'> t' + <TERM> <E'> {{ ." + " }}
| t' - <TERM> <E'> {{ ." - " }}
| ;bnf
bnf: <EXPRESSION> <TERM> <E'> ;bnf
' <EXPRESSION> is <'EXPRESSION>
: parse-it <parse <EXPRESSION> <EOL> parse> ;
parse-it 123+(45+8*1111^2)
cr bye