1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 | """
|
4 | houston-fp demo
|
5 | """
|
6 |
|
7 | import sys
|
8 |
|
9 | from demo_asdl import Token, expr_t, word_part_t, value, value_t
|
10 |
|
11 | from typing import List, Optional, cast
|
12 |
|
13 |
|
14 | def main(argv):
|
15 | # type: (List[str]) -> None
|
16 |
|
17 | # Better example:
|
18 | #
|
19 | # 1. Lexer() returning Token
|
20 | # 2. Mututally recursive parsers storing Token as first class variants:
|
21 | # WordParser
|
22 | # ArithParser
|
23 | # BoolParser
|
24 | # ExprParser
|
25 | #
|
26 | # 3. Evaluators using Token
|
27 | #
|
28 | # 4. Errors bubbled up to top-level, using Token
|
29 | #
|
30 | # ...
|
31 | #
|
32 | # TypeScript example: https://github.com/oilshell/yaks
|
33 |
|
34 | tok = Token(3, 4, 5, 'foo')
|
35 | print(tok)
|
36 |
|
37 | val = None # type: Optional[value_t]
|
38 | ival = value.Int(42)
|
39 |
|
40 | val = ival
|
41 |
|
42 | my_word_part = None # type: Optional[word_part_t]
|
43 | my_expr = None # type: Optional[expr_t]
|
44 |
|
45 | my_word_part = tok
|
46 | my_expr = tok
|
47 |
|
48 | # Errors
|
49 | #my_word_part = val
|
50 |
|
51 | #val = tok
|
52 |
|
53 |
|
54 | if __name__ == '__main__':
|
55 | try:
|
56 | main(sys.argv)
|
57 | except RuntimeError as e:
|
58 | print('FATAL: %s' % e, file=sys.stderr)
|
59 | sys.exit(1)
|