Skip to content

Commit c5e838b

Browse files
author
Jonathan Feinberg
committed
Sketching out mode-detection.
1 parent 34d5987 commit c5e838b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import ast
2+
import re
3+
4+
"""
5+
Determines the sketch mode, namely:
6+
7+
"ACTIVE" if the sketch uses draw() and/or setup() functions;
8+
"STATIC" otherise.
9+
"MIXED" if the user seems to have erroneously both declared a draw()
10+
function and called drawing functions outside draw().
11+
"""
12+
13+
# If you define any of these functions, you're in ACTIVE mode.
14+
activeModeFunc = re.compile(r"""
15+
^(
16+
draw
17+
|
18+
setup
19+
|
20+
key(Pressed|Released|Typed)
21+
|
22+
mouse(Clicked|Dragged|Moved|Pressed|Released|Wheel)
23+
)$
24+
""", re.X)
25+
26+
# If you're in ACTIVE mode, you can't call any of these functions
27+
# outside a function body.
28+
illegalActiveModeCall = re.compile(r"""
29+
^(
30+
bezier(Detail|Point|Tangent)?
31+
|
32+
curve(Detail|Point|Tangent|Tightness)?
33+
|
34+
arc|ellipse|line|point|quad|rect|triangle
35+
|
36+
box|sphere(Detail)?
37+
(begin|end)(Countour|Shape)
38+
|
39+
(quadratic|bezier|curve)?Vertex
40+
|
41+
(apply|pop|print|push|reset)Matrix
42+
|
43+
rotate[XYZ]?
44+
|
45+
(ambient|directional|point|spot)Light
46+
|
47+
light(Fallof|Specular|s)
48+
|
49+
noLights
50+
|
51+
normal
52+
|
53+
ambient|emissive|shininess|specular
54+
|
55+
(load|update)Pixels
56+
|
57+
background|clear|(no)?(Fill|Stroke)
58+
)$
59+
""", re.X)
60+
61+
module = ast.parse(__processing_source__ + "\n\n", filename=__file__)
62+
mode = 'STATIC'
63+
for node in module.body:
64+
if isinstance(node, ast.FunctionDef):
65+
if activeModeFunc.match(name):
66+
mode = 'ACTIVE'
67+
break
68+
69+
if mode == 'STATIC':
70+
return mode
71+
72+
for node in module.body:
73+
if not isinstance(node, ast.Expr):
74+
continue
75+
e = node.value
76+
if not isinstance(e, ast.Call):
77+
continue
78+
f = e.func
79+
if illegalActiveModeCall.match(f.id):
80+
return 'MIXED'
81+
82+
return mode

0 commit comments

Comments
 (0)