Skip to content

Commit

Permalink
Wrap code.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDiBernardo committed Nov 11, 2015
1 parent a36dd8b commit e5c4426
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
22 changes: 15 additions & 7 deletions interpreter/interpreter.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,11 @@ Python exposes a boatload of its internals at run time, and we can access them r

```python
>>> cond.__code__.co_code # the bytecode as raw bytes
b'd\x01\x00}\x00\x00|\x00\x00d\x02\x00k\x00\x00r\x16\x00d\x03\x00Sd\x04\x00Sd\x00\x00S'
b'd\x01\x00}\x00\x00|\x00\x00d\x02\x00k\x00\x00r\x16\x00d\x03\x00Sd\x04\x00Sd\x00
\x00S'
>>> list(cond.__code__.co_code) # the bytecode as numbers
[100, 1, 0, 125, 0, 0, 124, 0, 0, 100, 2, 0, 107, 0, 0, 114, 22, 0, 100, 3, 0, 83, 100, 4, 0, 83, 100, 0, 0, 83]
[100, 1, 0, 125, 0, 0, 124, 0, 0, 100, 2, 0, 107, 0, 0, 114, 22, 0, 100, 3, 0, 83,
100, 4, 0, 83, 100, 0, 0, 83]
```

When we just print the bytecode, it looks unintelligible --- all we can tell is that it's a series of bytes. Luckily, there's a powerful tool we can use to understand it: the `dis` module in the Python standard library.
Expand Down Expand Up @@ -410,7 +412,8 @@ class VirtualMachine(object):

def run_code(self, code, global_names=None, local_names=None):
""" An entry point to execute code using the virtual machine."""
frame = self.make_frame(code, global_names=global_names, local_names=local_names)
frame = self.make_frame(code, global_names=global_names,
local_names=local_names)
self.run_frame(frame)

```
Expand Down Expand Up @@ -484,7 +487,9 @@ The implementation of the `Function` object is somewhat twisty, and most of the

```python
class Function(object):
"""Create a realistic function object, defining the things the interpreter expects."""
"""
Create a realistic function object, defining the things the interpreter expects.
"""
__slots__ = [
'func_code', 'func_name', 'func_defaults', 'func_globals',
'func_locals', 'func_dict', 'func_closure',
Expand Down Expand Up @@ -515,7 +520,8 @@ class Function(object):
def __call__(self, *args, **kwargs):
"""When calling a Function, make a new frame and run it."""
callargs = inspect.getcallargs(self._func, *args, **kwargs)
# Use callargs to provide a mapping of arguments: values to pass into the new frame.
# Use callargs to provide a mapping of arguments: values to pass into the new
# frame.
frame = self._vm.make_frame(
self.func_code, callargs, self.func_globals, {}
)
Expand Down Expand Up @@ -573,7 +579,8 @@ class VirtualMachine(object):
f.last_instruction += 1
byte_name = dis.opname[byteCode]
if byteCode >= dis.HAVE_ARGUMENT:
arg = f.code_obj.co_code[f.last_instruction:f.last_instruction+2] # index into the bytecode
# index into the bytecode
arg = f.code_obj.co_code[f.last_instruction:f.last_instruction+2]
f.last_instruction += 2 # advance the instruction pointer
arg_val = arg[0] + (arg[1] * 256)
if byteCode in dis.hasconst: # Look up a constant
Expand Down Expand Up @@ -680,7 +687,8 @@ class VirtualMachine(object):
def unwind_block(self, block):
"""Unwind the values on the data stack corresponding to a given block."""
if block.type == 'except-handler':
offset = 3 # The exception itself is on the stack as type, value, and traceback.
# The exception itself is on the stack as type, value, and traceback.
offset = 3
else:
offset = 0

Expand Down
22 changes: 15 additions & 7 deletions tex/interpreter.tex
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,11 @@

\begin{verbatim}
>>> cond.__code__.co_code # the bytecode as raw bytes
b'd\x01\x00}\x00\x00|\x00\x00d\x02\x00k\x00\x00r\x16\x00d\x03\x00Sd\x04\x00Sd\x00\x00S'
b'd\x01\x00}\x00\x00|\x00\x00d\x02\x00k\x00\x00r\x16\x00d\x03\x00Sd\x04\x00Sd\x00
\x00S'
>>> list(cond.__code__.co_code) # the bytecode as numbers
[100, 1, 0, 125, 0, 0, 124, 0, 0, 100, 2, 0, 107, 0, 0, 114, 22, 0, 100, 3, 0, 83, 100, 4, 0, 83, 100, 0, 0, 83]
[100, 1, 0, 125, 0, 0, 124, 0, 0, 100, 2, 0, 107, 0, 0, 114, 22, 0, 100, 3, 0, 83,
100, 4, 0, 83, 100, 0, 0, 83]
\end{verbatim}

When we just print the bytecode, it looks unintelligible --- all we can
Expand Down Expand Up @@ -741,7 +743,8 @@
def run_code(self, code, global_names=None, local_names=None):
""" An entry point to execute code using the virtual machine."""
frame = self.make_frame(code, global_names=global_names, local_names=local_names)
frame = self.make_frame(code, global_names=global_names,
local_names=local_names)
self.run_frame(frame)
\end{verbatim}

Expand Down Expand Up @@ -830,7 +833,9 @@

\begin{verbatim}
class Function(object):
"""Create a realistic function object, defining the things the interpreter expects."""
"""
Create a realistic function object, defining the things the interpreter expects.
"""
__slots__ = [
'func_code', 'func_name', 'func_defaults', 'func_globals',
'func_locals', 'func_dict', 'func_closure',
Expand Down Expand Up @@ -861,7 +866,8 @@
def __call__(self, *args, **kwargs):
"""When calling a Function, make a new frame and run it."""
callargs = inspect.getcallargs(self._func, *args, **kwargs)
# Use callargs to provide a mapping of arguments: values to pass into the new frame.
# Use callargs to provide a mapping of arguments: values to pass into the new
# frame.
frame = self._vm.make_frame(
self.func_code, callargs, self.func_globals, {}
)
Expand Down Expand Up @@ -942,7 +948,8 @@
f.last_instruction += 1
byte_name = dis.opname[byteCode]
if byteCode >= dis.HAVE_ARGUMENT:
arg = f.code_obj.co_code[f.last_instruction:f.last_instruction+2] # index into the bytecode
# index into the bytecode
arg = f.code_obj.co_code[f.last_instruction:f.last_instruction+2]
f.last_instruction += 2 # advance the instruction pointer
arg_val = arg[0] + (arg[1] * 256)
if byteCode in dis.hasconst: # Look up a constant
Expand Down Expand Up @@ -1079,7 +1086,8 @@
def unwind_block(self, block):
"""Unwind the values on the data stack corresponding to a given block."""
if block.type == 'except-handler':
offset = 3 # The exception itself is on the stack as type, value, and traceback.
# The exception itself is on the stack as type, value, and traceback.
offset = 3
else:
offset = 0
Expand Down

0 comments on commit e5c4426

Please sign in to comment.