Joy Jupyter kernel loads defs.txt

This commit is contained in:
Simon Forman 2021-11-25 11:12:02 -08:00
parent 770cd783fa
commit b0b9a71fd9
3 changed files with 16 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import sys import sys
from ipykernel.kernelbase import Kernel from ipykernel.kernelbase import Kernel
from joy.library import initialize, inscribe from joy.library import default_defs, initialize, inscribe
from joy.joy import run from joy.joy import run
from joy.utils.stack import stack_to_string from joy.utils.stack import stack_to_string
from joy.utils.pretty_print import trace from joy.utils.pretty_print import trace
@ -45,6 +45,7 @@ class JoyKernel(Kernel):
def __init__(self, *a, **b): def __init__(self, *a, **b):
self.D = initialize() self.D = initialize()
default_defs(self.D)
self.S = () self.S = ()
super(JoyKernel, self).__init__(*a, **b) super(JoyKernel, self).__init__(*a, **b)

View File

@ -87,6 +87,7 @@ sum [+] step_zero
swapd [swap] dip swapd [swap] dip
swons swap cons swons swap cons
swoncat swap concat swoncat swap concat
sqr dup mul
tailrec [i] genrec tailrec [i] genrec
take [] roll> [shift] times pop take [] roll> [shift] times pop
ternary binary popd ternary binary popd

View File

@ -23,11 +23,14 @@ functions. Its main export is a Python function initialize() that
returns a dictionary of Joy functions suitable for use with the joy() returns a dictionary of Joy functions suitable for use with the joy()
function. function.
''' '''
from pkg_resources import resource_stream
from io import TextIOWrapper
from inspect import getdoc, getmembers, isfunction from inspect import getdoc, getmembers, isfunction
from functools import wraps from functools import wraps
from itertools import count from itertools import count
import operator, math import operator, math
from . import __name__ as _joy_package_name
from .parser import text_to_expression, Symbol from .parser import text_to_expression, Symbol
from .utils import generated_library as genlib from .utils import generated_library as genlib
from .utils.errors import ( from .utils.errors import (
@ -44,6 +47,14 @@ from .utils.stack import (
) )
def default_defs(dictionary):
def_stream = TextIOWrapper(
resource_stream(_joy_package_name, 'defs.txt'),
encoding='UTF_8',
)
Def.load_definitions(def_stream, dictionary)
HELP_TEMPLATE = '''\ HELP_TEMPLATE = '''\
==== Help on %s ==== ==== Help on %s ====
@ -190,7 +201,8 @@ class Def(object):
if line.lstrip().startswith('#'): if line.lstrip().startswith('#'):
continue continue
name, body = text_to_expression(line) name, body = text_to_expression(line)
inscribe(class_(name, body), dictionary) if name not in dictionary:
inscribe(class_(name, body), dictionary)
# #