-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
166 lines (156 loc) · 14.2 KB
/
index.html
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="all" name="robots" />
<title>pydrogen</title>
<link rel="shortcut icon" type="image/x-icon" href="data:image/x-icon;base64,iVBORw0KGgo="><!-- Suppress browser favicon.ico request. -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/vs.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/highlight.min.js"></script>
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style>
html, body { height:100%; margin:0; color:#000000; font-family:'Open Sans',sans-serif; }
body #content { z-index:100; min-height:100%; width:900px; margin:0 auto -90px; }
body #content #title { margin-left:-2px; padding-top:60px; padding-bottom:20px; /*text-align:center;*/ color:#326B9A; font-family:'Open Sans',sans-serif; font-size:36px; }
body #content #description { padding-bottom:40px; font-size:16px; }
body #content #sheet { margin:0 auto; padding-bottom:40px; color:#000000; background-color:white; }
body #content #sheet .section { padding-top:20px; font-family:'Open Sans',sans-serif; font-size:16px; }
body #content #sheet .section pre code { padding:10px; font-size:14px; }
body #content #sheet .section code { background-color:#F1F1F1; }
body #content #sheet .section h2 { color:#888888; font-family:'Open Sans',sans-serif; font-weight:normal; font-size:28px; }
body #content #sheet .section ul { list-style:none; padding:8px 0px 8px 0px; margin:0; }
body #content #sheet .section li { padding-left:1.5em; text-indent:-1em; }
body #content #sheet .section li:before { content:"4"; color:#888888; font-family:'Webdings'; }
body #content #sheet .section a { color:#CAAC3A; text-decoration:none; }
body #content #sheet .section a:hover { text-decoration:underline; }
body #content #footer_spacer { height:70px; }
body #footer { width:900px; height:70px; margin:0 auto; padding-top:15px; font-family:'Open Sans',sans-serif; font-size:13px; }
body #footer hr { border:0px; border-top:1px solid #DFDFDF; }
body #footer a { color:#000000; text-decoration:none; }
body #footer a:hover { text-decoration:underline; }
</style>
<script>
var favIcon = "AAABAAEAICAAAAEAIACoEAAAFgAAACgAAAAgAAAAQAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADr49oA6+PaAOvj2gDr5NsA6+PZAMexlgqmhFp4lGs51JFmMvKSaDXaon9SkcStkR3KyMYAvb2+AL+/vwC1tbUAwMDAAL+/vwC+vr4Avr6+AL29vQC6uroAvr6+AMDAwAC/v78AwcHBAL29vQDFxcUA2NjYANfX1wDX19cA19fXAOXbzwDl288A5dvPAObd0QDj2csHpIFWsZBjLf+wiFb/wJhp/7WOXP+WajT/mnNE0ry2r2G3uLqGtbW1p7GxsbS1tbWytra2ore3t4S6urpUvLy8Grq6ugC/v78AwMDAAL+/vwDBwcEAvb29AMXFxQDY2NgA19fXANfX1wDX19cAzrujAM67owDOu6MA0cCpALaad2mQZC7/w51u/9u3i//ZtYr/27eM/82nev+XajP/moFi/7G0uP+wsbH/sLCw/LCwsP2wsLD/r6+v/6+vr/+wsLD6s7Ozvrm5uWe/v78PwMDAAMHBwQC9vb0AxcXFANjY2ADX19cA19fXANfX1wDKtpwAyracAMq3nQDLuJ4AoHtOsaR6R//ZtIn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3NAM3NzQDNzc0Azc3NAM3NzQDNzc0Azs7OAL+/vwC+vr4Avb29Gra2tqCvr6/9r6+v/7Ozs9O3t7d9ubm5PLy8vBG9vb0EysrKANPT0wC8vLwCv7+/DLu7uzK3t7d0s7Ozza+vr/+wsLD9t7e3nb6+vhm+vr4Au7u7AMzMzADNzc0Azc3NAM3NzQDNzc0Azc3NAM3NzQDOzs4Av7+/ALy8vAC+vr4Av7+/ALi4uDS1tbWesbGx8K+vr/+vr6//sbGx9rOzs9O3t7fBurq6wLOzs8qxsbHvr6+v/6+vr/+wsLD2tra2pb29vTTBwcEAwMDAAL29vQC7u7sAzMzMAM3NzQDNzc0Azc3NAM3NzQDNzc0Azc3NAM7OzgC/v78AvLy8AL6+vgC+vr4Au7u7AL29vQC8vLwSurq6Uba2tpCzs7PAsbGx5bCwsPawsLD6sLCw7bOzs822trafubm5X7q6uhm/v78AwcHBAMDAwAC/v78Avb29ALu7uwDMzMwAzc3NAM3NzQDNzc0A/h////gIH//4AAP/8Afw//AH/D/wB/8f8A//z+Af/+fH///jz/gf85/gB/mfwAP5n4AB/T+AAfw/AAH8PwAA/D8AAPw/AAD8PwAB/D+AAfy/gAH9n8AD+Z/gB/nP+B/zz///8+f//+fz///P+f//n/x//j/+H/h//4AB///wD/8=";
var docHead = document.getElementsByTagName('head')[0];
var newLink = document.createElement('link');
newLink.rel = 'shortcut icon';
newLink.type = 'image/x-icon';
newLink.href = 'data:image/png;base64,'+favIcon;
docHead.appendChild(newLink);
hljs.initHighlightingOnLoad();
</script>
<base target="_parent">
</head>
<body onload="$('code').each(function(){ $(this).html(($(this).html().trim()));});">
<div id="content">
<div id="title">pydrogen</div>
<div id="description">
Python library for building embedded languages within Python that have alternative operational semantics or abstract interpretations.
</div>
<div id="sheet">
<div class="section"><h2>Purpose</h2>
This library allows programmers to free the building blocks that constitute Python syntax from the default Python semantics. It is designed to allow programmers to define quickly and to use immediately one or more alternative abstract interpretation or operational semantics for Python.
</div>
<div class="section"><h2>Repository and Package</h2>
The <a href="http://github.com/lapets/pydrogen">official repository</a> is maintained on GitHub. The package is also <a href="http://pypi.python.org/pypi/Pydrogen">available on PyPI</a>.
<pre><code>
pip install pydrogen
</code></pre>
Note that this library is only compatible (i.e., tested) with Python 3. It depends on the <a href="http://docs.python.org/3/library/ast.html"><code>ast</code></a> and <a href="http://docs.python.org/3/library/inspect.html"><code>inspect</code></a> modules.
</div>
<div class="section"><h2>Examples</h2>
A user would normally import the module in the usual way.
<pre><code class="py">
import pydrogen
</code></pre>
To create an alternative interpretation for Python syntax, it is only necessary to extend the <code>Pydrogen</code> class and to provide definitions for those functions that handle the abstract syntax nodes that are of interest. The complete Python abstract syntax can be found in the documentation of the Python <a href="http://docs.python.org/3/library/ast.html"><code>ast</code> module</a>.
<br/><br/>
Below, we define a simple type checking algorithm.
<pre><code class="py">
class Ty(pydrogen.Pydrogen):
def Statements(self, ss): return ss.post()[-1] # Last statement.
def Module(self, ss): return ss.post()
def FunctionDef(self, ss): return ss.post()
def Return(self, e): return e.post()
def True_(self): return 'Bool'
def False_(self): return 'Bool'
def BoolOp(self, es): return 'Bool'if frozenset(es.post()) == {'Bool'} else 'Error'
def Not(self, e): return 'Bool' if e.post() == 'Bool' else 'Error'
def Num(self, n): return 'Int'
def Add(self, e1, e2): return 'Int' if e1.post() == 'Int' and e2.post() == 'Int' else 'Error'
def Sub(self, e1, e2): return 'Int' if e1.post() == 'Int' and e2.post() == 'Int' else 'Error'
def Mult(self, e1, e2): return 'Int' if e1.post() == 'Int' and e2.post() == 'Int' else 'Error'
def USub(self, e): return 'Int' if e.post() == 'Int' else 'Error'
</code></pre>
Given the above definition, we can use <code>@Ty</code> as a decorator on any function. This will make it possible to query that function's type at a later point using the name of the class extension defined above.
<pre><code class="py">
@Ty
def correct():
return -1 + 2 - 3 * 4
print("The type of 'correct' is " + str(correct.Ty) + ".")
@Ty
def incorrect():
return 123 and False
print("The type of 'incorrect' is " + str(incorrect.Ty) + ".")
</code></pre>
The example below illustrates how multiple interpretations can be used together by defining a running time approximation algorithm for a very small subset of Python.
<pre><code class="py">
class Size(pydrogen.Typical):
def List(self, elts): return len(elts.post())
def Num(self, n): return 1
class Time(pydrogen.Typical):
def Statements(self, ss): return sum(ss.post())
def Assign(self, targets, e): return 1 + e.post()
def For(self, target, itr, ss, orelse): return Size(itr.pre()) * (1 + ss.post())
def BoolOp(self, es): return 1 + sum(es.post())
def BinOp(self, e1, e2): return 1 + e1.post() + e2.post()
def Call(self, func, args): return 1 + sum(args.post())
def Num(self, n): return 0
def NameConstant(self): return 0
@Time
def example():
for x in [1,2,3,4,5,6,7]:
print(True and False)
print(123 + 456)
print("The approximate running time of example is " + str(example.Time) + ".")
</code></pre>
The example below illustrates how the optional context parameter can be used to create environments. Note that because only the statement handlers return <code>(env, ty)</code> tuples, we only need to handle such tuple results when they appear (while not worrying about them when dealing with results from expression handlers, which do not return an environment).
<pre><code class="py">
class Ty2(pydrogen.Typical):
def Statements(self, ss, env):
(tys, env) = ss.post(env)
return tys[-1]
def Assign(self, targets, value, env):
var = targets.pre()[0].id # Only single variable assignment.
envNew = env.copy()
envNew[var] = value.post(env)
return ('Void', envNew)
def Name(self, x, env):
return env[x.pre()] if x.pre() in env else 'Error'
def Num(self, n, env):
return 'Int'
def Add(self, e1, e2, env):
return 'Int' if {e1.post(env), e2.post(env)} == {'Int'} else 'Error'
@Ty2
def example2():
y = 0
x = 1 + 2 + 3
return x + y
print("The type of example2() is " + str(example2.Ty2) + ".")
</code></pre>
</div>
</div><!-- /#sheet -->
<div id="footer_spacer"></div>
</div><!-- /#content -->
<div id="footer">
<hr/>
<a href="http://pypi.python.org/pypi/Pydrogen">Package</a>
<a href="http://www.github.com/lapets/pydrogen">Repository</a>
<a href="mailto:[email protected]">Contact</a>
</div><!-- /#footer -->
</body>
</html>