| 1 | #!/usr/bin/env python2
|
| 2 | """
|
| 3 | Code snippets that are like ASDL generated code.
|
| 4 |
|
| 5 | Even though we may generate our own C++ code, the CALL SITES still have to be
|
| 6 | correctly typed-inferred from Python.
|
| 7 | """
|
| 8 | from __future__ import print_function
|
| 9 |
|
| 10 | import os
|
| 11 |
|
| 12 | from mycpp import mylib
|
| 13 | from mycpp.mylib import log
|
| 14 |
|
| 15 | from typing import Optional, List
|
| 16 |
|
| 17 | # This causes problems
|
| 18 | #from asdl import format as fmt
|
| 19 |
|
| 20 |
|
| 21 | class arith_expr_t(object):
|
| 22 |
|
| 23 | def __init__(self):
|
| 24 | # type: () -> None
|
| 25 | """Empty constructor for ASDL."""
|
| 26 | pass
|
| 27 |
|
| 28 |
|
| 29 | class 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 |
|
| 42 | def 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 |
|
| 50 | def 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 |
|
| 60 | if __name__ == '__main__':
|
| 61 | if os.getenv('BENCHMARK'):
|
| 62 | log('Benchmarking...')
|
| 63 | run_benchmarks()
|
| 64 | else:
|
| 65 | run_tests()
|