forked from OpenResearchComputation/fable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
89 lines (80 loc) · 2.5 KB
/
__init__.py
File metadata and controls
89 lines (80 loc) · 2.5 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
try:
import boost.python
except ImportError:
ext = None
else:
ext = boost.python.import_ext("fable_ext", optional=True)
# compare with fem/utils/string.hpp
def py_fem_utils_unsigned_integer_scan(code, start=0, stop=-1):
i = start
while (i < stop):
c = code[i]
if (not c.isdigit()): break
i += 1
if (i == start): return -1
return i
# compare with ext.cpp
def py_ext_get_code_stop(code, stop):
len_code = len(code)
if (stop < 0): return len_code
assert stop <= len_code
return stop
# compare with ext.cpp
def py_unsigned_integer_scan(code, start=0, stop=-1):
return py_fem_utils_unsigned_integer_scan(
code=code, start=start, stop=py_ext_get_code_stop(code, stop))
# compare with ext.cpp
def py_floating_point_scan_after_exponent_char(code, start=0, stop=-1):
code_stop = py_ext_get_code_stop(code=code, stop=stop)
i = start
if (i < code_stop):
c = code[i]
if (c == '+' or c == '-'):
i += 1
return py_unsigned_integer_scan(code=code, start=i, stop=stop)
return -1
# compare with ext.cpp
def py_floating_point_scan_after_dot(code, start=0, stop=-1):
code_stop = py_ext_get_code_stop(code=code, stop=stop)
i = py_unsigned_integer_scan(code=code, start=start, stop=stop)
if (i < 0): i = start
if (i < code_stop):
c = code[i]
if (c == 'e' or c == 'd'):
return py_floating_point_scan_after_exponent_char(
code=code, start=i+1, stop=stop)
return i
# compare with ext.cpp
def py_identifier_scan(code, start=0, stop=-1):
code_stop = py_ext_get_code_stop(code=code, stop=stop)
i = start
if (i < code_stop):
c = code[i]; i += 1
if ((c < 'a' or c > 'z') and c != '_'): return -1
while (i < code_stop):
c = code[i]; i += 1
if ( (c < 'a' or c > 'z')
and (c < '0' or c > '9') and c != '_'): return i-1
return i
return -1
def py_find_closing_parenthesis(code, start=0, stop=-1):
code_stop = py_ext_get_code_stop(code=code, stop=stop)
n_inner = 0
for i in xrange(start, code_stop):
c = code[i]
if (c == ')'):
if (n_inner == 0): return i
n_inner -= 1
elif (c == '('):
n_inner += 1
return -1
if (ext is not None):
from fable_ext import *
else:
unsigned_integer_scan = py_unsigned_integer_scan
floating_point_scan_after_exponent_char = \
py_floating_point_scan_after_exponent_char
floating_point_scan_after_dot = py_floating_point_scan_after_dot
identifier_scan = py_identifier_scan
find_closing_parenthesis = py_find_closing_parenthesis
class SemanticError(Exception): pass