From aa0b01fdcc613689214dad9f051b9f17aa6f2e8c Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Wed, 24 Nov 2021 22:01:09 -0800 Subject: [PATCH] Load defs from files into dictionary. --- joy/__main__.py | 8 ++++++-- joy/library.py | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/joy/__main__.py b/joy/__main__.py index f6f2909..cdf0f41 100644 --- a/joy/__main__.py +++ b/joy/__main__.py @@ -18,7 +18,7 @@ # along with Thun. If not see . # import argparse, io, pkg_resources -from .library import initialize, inscribe +from .library import initialize, inscribe, Def from .joy import repl, interp from .utils.pretty_print import trace @@ -68,4 +68,8 @@ under certain conditions; type "sharing" for details. Type "words" to see a list of all words, and "[] help" to print the docs for a word. ''') -stack = j(dictionary=initialize()) + +dictionary=initialize() +for def_stream in args.defs: + Def.load_definitions(def_stream, dictionary) +stack = j(dictionary=dictionary) diff --git a/joy/library.py b/joy/library.py index 47d919b..90d7a9c 100644 --- a/joy/library.py +++ b/joy/library.py @@ -323,6 +323,14 @@ class Def(DefinitionWrapper): self.__doc__ = expression_to_string(body) self._compiled = None + @classmethod + def load_definitions(class_, stream, dictionary): + for line in stream: + if line.lstrip().startswith('#'): + continue + name, body = text_to_expression(line) + inscribe(class_(name, body), dictionary) + def _text_to_defs(text): return (