From 00296563518ff25a0c1c49cc5a4f87f863340ea6 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 17 Jul 2018 10:35:11 -0700 Subject: [PATCH] TextJoyType and inscribe command. I took the plunge and added the meta-command "inscribe" to the library. This is, of course, a very dangerous and powerful command. Use it wisely. --- joy/library.py | 18 ++++++++++++++++++ joy/utils/types.py | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/joy/library.py b/joy/library.py index 6fa287a..ada8da1 100644 --- a/joy/library.py +++ b/joy/library.py @@ -279,6 +279,24 @@ for name, primitive in getmembers(genlib, isfunction): inscribe(SimpleFunctionWrapper(primitive)) +@inscribe +@FunctionWrapper +def inscribe_(stack, expression, dictionary): + ''' + Create a new Joy function definition in the Joy dictionary. A + definition is given as a string with a name followed by a double + equal sign then one or more Joy functions, the body. for example: + + sqr == dup mul + + If you want the definition to persist over restarts, enter it into + the definitions.txt resource. + ''' + definition, stack = stack + DefinitionWrapper.add_def(definition, dictionary) + return stack, expression, dictionary + + @inscribe @SimpleFunctionWrapper def parse(stack): diff --git a/joy/utils/types.py b/joy/utils/types.py index c35ffa4..90f33b5 100644 --- a/joy/utils/types.py +++ b/joy/utils/types.py @@ -63,6 +63,11 @@ class IntJoyType(FloatJoyType): prefix = 'i' +class TextJoyType(FloatJoyType): + accept = basestring + prefix = 't' + + class StackJoyType(AnyJoyType): accept = tuple @@ -283,6 +288,7 @@ N = n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 = map(NumberJoyType, _R) S = s0, s1, s2, s3, s4, s5, s6, s7, s8, s9 = map(StackJoyType, _R) F = f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 = map(FloatJoyType, _R) I = i0, i1, i2, i3, i4, i5, i6, i7, i8, i9 = map(IntJoyType, _R) +T = t0, t1, t2, t3, t4, t5, t6, t7, t8, t9 = map(TextJoyType, _R) def defs(): @@ -297,6 +303,7 @@ def defs(): dupd = __(a2, a1), __(a2, a2, a1) dupdd = __(a3, a2, a1), __(a3, a3, a2, a1) first = __((a1, s1),), __(a1,) + inscribe = __(t1), __() over = __(a2, a1), __(a2, a1, a2) pop = __(a1), __() popd = __(a2, a1,), __(a1)