InterpreterFlow provides code interpreters to run code given the language of code and the code content. It returns the execution results of the code.
code, language -> InterpreterFlow -> execution results
*Expected Input*:
- `code`
- `language`
*Expected Output*:
- `interpreter_output`: output of the code interpreter
# Table of Contents
* [run](#run)
* [InterpreterAtomicFlow](#InterpreterAtomicFlow)
* [InterpreterAtomicFlow](#InterpreterAtomicFlow.InterpreterAtomicFlow)
* [set\_up\_flow\_state](#InterpreterAtomicFlow.InterpreterAtomicFlow.set_up_flow_state)
* [\_\_init\_\_](#__init__)
* [code\_interpreters](#code_interpreters)
* [code\_interpreters.language\_map](#code_interpreters.language_map)
* [code\_interpreters.create\_code\_interpreter](#code_interpreters.create_code_interpreter)
* [code\_interpreters.languages.javascript](#code_interpreters.languages.javascript)
* [preprocess\_javascript](#code_interpreters.languages.javascript.preprocess_javascript)
* [code\_interpreters.languages.shell](#code_interpreters.languages.shell)
* [preprocess\_shell](#code_interpreters.languages.shell.preprocess_shell)
* [add\_active\_line\_prints](#code_interpreters.languages.shell.add_active_line_prints)
* [code\_interpreters.languages.python](#code_interpreters.languages.python)
* [preprocess\_python](#code_interpreters.languages.python.preprocess_python)
* [add\_active\_line\_prints](#code_interpreters.languages.python.add_active_line_prints)
* [AddLinePrints](#code_interpreters.languages.python.AddLinePrints)
* [insert\_print\_statement](#code_interpreters.languages.python.AddLinePrints.insert_print_statement)
* [process\_body](#code_interpreters.languages.python.AddLinePrints.process_body)
* [visit](#code_interpreters.languages.python.AddLinePrints.visit)
* [code\_interpreters.languages.html](#code_interpreters.languages.html)
* [code\_interpreters.languages.r](#code_interpreters.languages.r)
* [R](#code_interpreters.languages.r.R)
* [preprocess\_code](#code_interpreters.languages.r.R.preprocess_code)
* [code\_interpreters.languages.powershell](#code_interpreters.languages.powershell)
* [preprocess\_powershell](#code_interpreters.languages.powershell.preprocess_powershell)
* [add\_active\_line\_prints](#code_interpreters.languages.powershell.add_active_line_prints)
* [wrap\_in\_try\_catch](#code_interpreters.languages.powershell.wrap_in_try_catch)
* [code\_interpreters.languages](#code_interpreters.languages)
* [code\_interpreters.languages.applescript](#code_interpreters.languages.applescript)
* [AppleScript](#code_interpreters.languages.applescript.AppleScript)
* [preprocess\_code](#code_interpreters.languages.applescript.AppleScript.preprocess_code)
* [add\_active\_line\_indicators](#code_interpreters.languages.applescript.AppleScript.add_active_line_indicators)
* [detect\_active\_line](#code_interpreters.languages.applescript.AppleScript.detect_active_line)
* [detect\_end\_of\_execution](#code_interpreters.languages.applescript.AppleScript.detect_end_of_execution)
* [code\_interpreters.subprocess\_code\_interpreter](#code_interpreters.subprocess_code_interpreter)
* [SubprocessCodeInterpreter](#code_interpreters.subprocess_code_interpreter.SubprocessCodeInterpreter)
* [preprocess\_code](#code_interpreters.subprocess_code_interpreter.SubprocessCodeInterpreter.preprocess_code)
* [code\_interpreters.base\_code\_interpreter](#code_interpreters.base_code_interpreter)
* [BaseCodeInterpreter](#code_interpreters.base_code_interpreter.BaseCodeInterpreter)
# run
# InterpreterAtomicFlow
## InterpreterAtomicFlow Objects
```python
class InterpreterAtomicFlow(AtomicFlow)
```
This flow is used to run the code passed from the caller.
*Input Interface*:
- `code`
- `language`
*Output Interface*:
- `interpreter_output`: output of the code interpreter
*Configuration Parameters*:
- None
**Full credits to open-interpreter (https://github.com/KillianLucas/open-interpreter)
for the usage of code interpreters (package `code_interpreters`) and the function truncate_output()**
I'm extracting the code interpreter part from open-interpreter because the litellm version of open-interpreter
is not compatible with that of the current version of aiflows(v.0.1.7).
#### set\_up\_flow\_state
```python
def set_up_flow_state()
```
class-specific flow state: language and code,
which describes the programming language and the code to run.
# \_\_init\_\_
# code\_interpreters
# code\_interpreters.language\_map
# code\_interpreters.create\_code\_interpreter
# code\_interpreters.languages.javascript
#### preprocess\_javascript
```python
def preprocess_javascript(code)
```
Add active line markers
Wrap in a try catch
Add end of execution marker
# code\_interpreters.languages.shell
#### preprocess\_shell
```python
def preprocess_shell(code)
```
Add active line markers
Wrap in a try except (trap in shell)
Add end of execution marker
#### add\_active\_line\_prints
```python
def add_active_line_prints(code)
```
Add echo statements indicating line numbers to a shell string.
# code\_interpreters.languages.python
#### preprocess\_python
```python
def preprocess_python(code)
```
Add active line markers
Wrap in a try except
Add end of execution marker
#### add\_active\_line\_prints
```python
def add_active_line_prints(code)
```
Add print statements indicating line numbers to a python string.
## AddLinePrints Objects
```python
class AddLinePrints(ast.NodeTransformer)
```
Transformer to insert print statements indicating the line number
before every executable line in the AST.
#### insert\_print\_statement
```python
def insert_print_statement(line_number)
```
Inserts a print statement for a given line number.
#### process\_body
```python
def process_body(body)
```
Processes a block of statements, adding print calls.
#### visit
```python
def visit(node)
```
Overridden visit to transform nodes.
# code\_interpreters.languages.html
# code\_interpreters.languages.r
## R Objects
```python
class R(SubprocessCodeInterpreter)
```
#### preprocess\_code
```python
def preprocess_code(code)
```
Add active line markers
Wrap in a tryCatch for better error handling in R
Add end of execution marker
# code\_interpreters.languages.powershell
#### preprocess\_powershell
```python
def preprocess_powershell(code)
```
Add active line markers
Wrap in try-catch block
Add end of execution marker
#### add\_active\_line\_prints
```python
def add_active_line_prints(code)
```
Add Write-Output statements indicating line numbers to a PowerShell script.
#### wrap\_in\_try\_catch
```python
def wrap_in_try_catch(code)
```
Wrap PowerShell code in a try-catch block to catch errors and display them.
# code\_interpreters.languages
# code\_interpreters.languages.applescript
## AppleScript Objects
```python
class AppleScript(SubprocessCodeInterpreter)
```
#### preprocess\_code
```python
def preprocess_code(code)
```
Inserts an end_of_execution marker and adds active line indicators.
#### add\_active\_line\_indicators
```python
def add_active_line_indicators(code)
```
Adds log commands to indicate the active line of execution in the AppleScript.
#### detect\_active\_line
```python
def detect_active_line(line)
```
Detects active line indicator in the output.
#### detect\_end\_of\_execution
```python
def detect_end_of_execution(line)
```
Detects end of execution marker in the output.
# code\_interpreters.subprocess\_code\_interpreter
## SubprocessCodeInterpreter Objects
```python
class SubprocessCodeInterpreter(BaseCodeInterpreter)
```
#### preprocess\_code
```python
def preprocess_code(code)
```
This needs to insert an end_of_execution marker of some kind,
which can be detected by detect_end_of_execution.
Optionally, add active line markers for detect_active_line.
# code\_interpreters.base\_code\_interpreter
## BaseCodeInterpreter Objects
```python
class BaseCodeInterpreter()
```
.run is a generator that yields a dict with attributes: active_line, output