OSDN Git Service

TextJoyType and inscribe command.
authorSimon Forman <sforman@hushmail.com>
Tue, 17 Jul 2018 17:35:11 +0000 (10:35 -0700)
committerSimon Forman <sforman@hushmail.com>
Tue, 17 Jul 2018 17:35:11 +0000 (10:35 -0700)
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
joy/utils/types.py

index 6fa287a..ada8da1 100644 (file)
@@ -280,6 +280,24 @@ for name, primitive in getmembers(genlib, isfunction):
 
 
 @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):
   '''Parse the string on the stack to a Joy expression.'''
index c35ffa4..90f33b5 100644 (file)
@@ -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)