OILS / mycpp / examples / asdl_generated.py View on Github | oilshell.org

65 lines, 28 significant
1#!/usr/bin/env python2
2"""
3Code snippets that are like ASDL generated code.
4
5Even though we may generate our own C++ code, the CALL SITES still have to be
6correctly typed-inferred from Python.
7"""
8from __future__ import print_function
9
10import os
11
12from mycpp import mylib
13from mycpp.mylib import log
14
15from typing import Optional, List
16
17# This causes problems
18#from asdl import format as fmt
19
20
21class arith_expr_t(object):
22
23 def __init__(self):
24 # type: () -> None
25 """Empty constructor for ASDL."""
26 pass
27
28
29class arith_expr__Const(arith_expr_t):
30
31 def __init__(self, i):
32 # type: (int) -> None
33 arith_expr_t.__init__(self)
34 self.i = i
35
36
37# This causes problems
38#class arith_expr(object):
39# Const = arith_expr__Const
40
41
42def run_tests():
43 # type: () -> None
44
45 #x = arith_expr.Const(5)
46 x = arith_expr__Const(5)
47 log('x = %d', x.i)
48
49
50def run_benchmarks():
51 # type: () -> None
52 i = 0
53 n = 1000000
54 while i < n:
55 x = arith_expr__Const(i)
56 i = i + 1
57 mylib.MaybeCollect()
58
59
60if __name__ == '__main__':
61 if os.getenv('BENCHMARK'):
62 log('Benchmarking...')
63 run_benchmarks()
64 else:
65 run_tests()