-
-
Save lyxal/572b1509bf5fc3f38d36a4d21b153544 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A befunge-93 interpreter, written in python. | |
# | |
# I've written this interpreter to be as fast and as small as possible (it's | |
# currently under 1000 lines of code), since I want to see how small of an | |
# interactive interpreter I can write in Python. | |
# | |
# It's not as fast as C or as fast as I would like it to be, but it's fast | |
# enough for me to use it in its current form. | |
# | |
# It's not as small as I would like it to be, but it's small enough for me to | |
# use it in its current form. | |
# | |
# I've tried to be as extensible as possible, so it can be used for more than | |
# just writing a simple interpreter. The module `befunge.interpreter` contains | |
# the `Interpreter` class, which is the main object you'll want to interact | |
# with. | |
# | |
# The interpreter is designed to work with the Befunge-93 language (though it | |
# should work with a few others). | |
import sys | |
import getch | |
import operator as op | |
from functools import reduce | |
from befunge.interpreter import Interpreter | |
from befunge.field import Field | |
from befunge.field import BefungeError | |
from befunge.field import ValueError | |
from befunge.field import TypeError | |
# There's probably a better way to do this, but this'll work for now. | |
class _Getch: | |
""" | |
Gets a single character from standard input. | |
Does not echo to the screen. | |
""" | |
def __init__(self): | |
self.impl = getch.Getch() | |
def __call__(self): | |
return self.impl() | |
getch = _Getch() | |
def _str2int(string): | |
""" | |
Converts a string to an integer, if the string can be converted to an | |
integer. If the string cannot be converted, it's returned unchanged. | |
Args: | |
string (str): A string to be converted to an integer. | |
Returns: | |
str: Either the converted integer or the original string. | |
""" | |
try: | |
return int(string) | |
except ValueError: | |
return string | |
def _str2type(string): | |
""" | |
Converts a string to a type, if the string can be converted to a type. | |
If the string cannot be converted, it's returned unchanged. | |
Args: | |
string (str): A string to be converted to a type. | |
Returns: | |
str: Either the converted integer or the original string. | |
""" | |
try: | |
return eval(string) | |
except ValueError: | |
return string | |
def _str2bool(string): | |
""" | |
Converts a string to a boolean, if the string can be converted to a | |
boolean. If the string cannot be converted, it's returned unchanged. | |
Args: | |
string (str): A string to be converted to a boolean. | |
Returns: | |
str: Either the converted boolean or the original string. | |
""" | |
if string in ['True', 'true', '1', 1]: | |
return True | |
elif string in ['False', 'false', '0', 0]: | |
return False | |
else: | |
return string | |
def _str2op(string): | |
""" | |
Converts a string to an operator, if the string can be converted to an | |
operator. If the string cannot be converted, it's returned unchanged. | |
Args: | |
string (str): A string to be converted to an operator. | |
Returns: | |
str: Either the converted operator or the original string. | |
""" | |
try: | |
return op.__dict__[string] | |
except KeyError: | |
return string | |
def _str2func(string): | |
""" | |
Converts a string to a function, if the string can be converted to a | |
function. If the string cannot be converted, it's returned unchanged. | |
Args: | |
string (str): A string to be converted to a function. | |
Returns: | |
str: Either the converted function or the original string. | |
""" | |
try: | |
return eval(string) | |
except ValueError: | |
return string | |
def _eval(string, field): | |
""" | |
Evaluates a string as an integer, float, string, type, boolean, operator, | |
or function. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return _str2int(string) or _str2float(string) or \ | |
_str2type(string) or _str2bool(string) or \ | |
_str2op(string) or _str2func(string) or string | |
def _eval_op(string, field): | |
""" | |
Evaluates a string as an operator or function. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return _str2op(string) or _str2func(string) | |
def _eval_bool(string, field): | |
""" | |
Evaluates a string as a boolean. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return _str2bool(string) | |
def _eval_int(string, field): | |
""" | |
Evaluates a string as an integer. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return _str2int(string) | |
def _eval_float(string, field): | |
""" | |
Evaluates a string as a float. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return _str2float(string) | |
def _eval_type(string, field): | |
""" | |
Evaluates a string as a type. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return _str2type(string) | |
def _eval_str(string, field): | |
""" | |
Evaluates a string as a string. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return string | |
def _eval_func(string, field): | |
""" | |
Evaluates a string as a function. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return _str2func(string) | |
def _eval_val(string, field): | |
""" | |
Evaluates a string as a value. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return string | |
def _eval_value(string, field): | |
""" | |
Evaluates a string as a value. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): The field being operated on. | |
Returns: | |
str: The evaluated string. | |
""" | |
return string | |
def _eval_none(string, field): | |
""" | |
Evaluates a string as a none-value. | |
Args: | |
string (str): The string to evaluate. | |
field (Field): |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you are missing the end of the docstring L#283