-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.grammar
72 lines (67 loc) · 2.05 KB
/
calc.grammar
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* First entry defines the entire thing parsed */
expression: multicompare;
/* These don't short-circuit, partly b/c the expression evaluator ALWAYS
parses all variables before evaluating anything */
multicompare: compare;
multicompare: compare "&&" compare {binop};
multicompare: compare "||" compare {binop};
/* Convenience aliases */
multicompare: compare "&" compare {binop};
multicompare: compare "|" compare {binop};
/* Note that "1 < 2 < 3" is not supported, to allow future expansion
with possible Python-style semantics */
compare: add;
compare: add "<" add {binop};
compare: add "<=" add {binop};
compare: add "==" add {binop};
compare: add "!=" add {binop};
compare: add ">=" add {binop};
compare: add ">" add {binop};
/* Convenience aliases */
compare: add "=" add {binop};
compare: add "=>" add {binop};
add: multiply;
add: add "+" multiply {binop};
add: add "-" multiply {binop};
multiply: expon;
multiply: multiply "*" expon {binop};
multiply: multiply "x" expon {binop}; /* Humans like to write 3x4 for multiplication */
multiply: multiply "/" expon {binop};
multiply: multiply "%" expon {binop};
expon: value;
expon: value "**" expon {binop};
expon: value "^" expon {binop};
digit: "0";
digit: "1";
digit: "2";
digit: "3";
digit: "4";
digit: "5";
digit: "6";
digit: "7";
digit: "8";
digit: "9";
integer: digit;
integer: digit integer {stitch};
float: integer "." {stitch};
float: "." integer {stitch};
float: integer "." integer {stitch};
signedint: integer;
signedint: "+" integer {stitch};
signedint: "-" integer {stitch};
signedfloat: float;
signedfloat: "+" float {stitch};
signedfloat: "-" float {stitch};
value: signedint {makeint};
value: signedfloat {makefloat};
value: "string";
value: "(" expression ")" {parens};
value: "word" "(" ")" {func_nullary};
value: "word" "(" arglist ")" {func};
value: "@" "word" {varlookup};
value: "@" "string" {varlookup};
value: "@" "=" "word" "=" value {varassign};
value: "@" "=" "string" "=" value {varassign};
arglist: expression {make_array};
arglist: expression "," {make_array};
arglist: expression "," arglist {prepend_array};