• R/O
  • HTTP
  • SSH
  • HTTPS

Thun: Commit

Interpreter and library.


Commit MetaInfo

Révision2fb27971f1cf942a43c3b1695e2a6089af85e2c6 (tree)
l'heure2022-04-06 02:02:01
AuteurSimon Forman <sforman@hush...>
CommiterSimon Forman

Message de Log

Two wrappers

One for math ops, the other for Boolean.

Fixes: https://todo.sr.ht/~sforman/thun-der/13

Change Summary

Modification

--- a/implementations/Python/joy/joy.py
+++ b/implementations/Python/joy/joy.py
@@ -140,7 +140,7 @@ def interp(stack=(), dictionary=None):
140140 except NotAnIntError:
141141 print('Not an integer.')
142142 except NotAListError as e:
143- print(e) # 'Not a list.'
143+ print(e)
144144 except:
145145 print_exc()
146146 print(stack_to_string(stack))
--- a/implementations/Python/joy/library.py
+++ b/implementations/Python/joy/library.py
@@ -141,9 +141,9 @@ def SimpleFunctionWrapper(f):
141141 return inner
142142
143143
144-def BinaryBuiltinWrapper(f):
144+def BinaryMathWrapper(f):
145145 '''
146- Wrap functions that take two arguments and return a single result.
146+ Wrap functions that take two numbers and return a single result.
147147 '''
148148 @FunctionWrapper
149149 @wraps(f)
@@ -152,13 +152,33 @@ def BinaryBuiltinWrapper(f):
152152 (a, (b, stack)) = stack
153153 except ValueError:
154154 raise StackUnderflowError('Not enough values on stack.')
155- # Boolean predicates like "or" fail here. :(
156-## if ( not isinstance(a, int)
157-## or not isinstance(b, int)
158-## or isinstance(a, bool) # Because bools are ints in Python.
159-## or isinstance(b, bool)
155+ if ( not isinstance(a, int)
156+ or not isinstance(b, int)
157+ # bool is int in Python.
158+ or isinstance(a, bool)
159+ or isinstance(b, bool)
160+ ):
161+ raise NotAnIntError
162+ result = f(b, a)
163+ return (result, stack), expression, dictionary
164+ return inner
165+
166+
167+def BinaryLogicWrapper(f):
168+ '''
169+ Wrap functions that take two numbers and return a single result.
170+ '''
171+ @FunctionWrapper
172+ @wraps(f)
173+ def inner(stack, expression, dictionary):
174+ try:
175+ (a, (b, stack)) = stack
176+ except ValueError:
177+ raise StackUnderflowError('Not enough values on stack.')
178+## if (not isinstance(a, bool)
179+## or not isinstance(b, bool)
160180 ## ):
161-## raise NotAnIntError
181+## raise NotABoolError
162182 result = f(b, a)
163183 return (result, stack), expression, dictionary
164184 return inner
@@ -1331,27 +1351,27 @@ for F in (
13311351
13321352 #divmod_ = pm = __(n2, n1), __(n4, n3)
13331353
1334- BinaryBuiltinWrapper(operator.eq),
1335- BinaryBuiltinWrapper(operator.ge),
1336- BinaryBuiltinWrapper(operator.gt),
1337- BinaryBuiltinWrapper(operator.le),
1338- BinaryBuiltinWrapper(operator.lt),
1339- BinaryBuiltinWrapper(operator.ne),
1340-
1341- BinaryBuiltinWrapper(operator.xor),
1342- BinaryBuiltinWrapper(operator.lshift),
1343- BinaryBuiltinWrapper(operator.rshift),
1344-
1345- BinaryBuiltinWrapper(operator.and_),
1346- BinaryBuiltinWrapper(operator.or_),
1347-
1348- BinaryBuiltinWrapper(operator.add),
1349- BinaryBuiltinWrapper(operator.floordiv),
1350- BinaryBuiltinWrapper(operator.mod),
1351- BinaryBuiltinWrapper(operator.mul),
1352- BinaryBuiltinWrapper(operator.pow),
1353- BinaryBuiltinWrapper(operator.sub),
1354-## BinaryBuiltinWrapper(operator.truediv),
1354+ BinaryMathWrapper(operator.eq),
1355+ BinaryMathWrapper(operator.ge),
1356+ BinaryMathWrapper(operator.gt),
1357+ BinaryMathWrapper(operator.le),
1358+ BinaryMathWrapper(operator.lt),
1359+ BinaryMathWrapper(operator.ne),
1360+
1361+ BinaryMathWrapper(operator.xor),
1362+ BinaryMathWrapper(operator.lshift),
1363+ BinaryMathWrapper(operator.rshift),
1364+
1365+ BinaryLogicWrapper(operator.and_),
1366+ BinaryLogicWrapper(operator.or_),
1367+
1368+ BinaryMathWrapper(operator.add),
1369+ BinaryMathWrapper(operator.floordiv),
1370+ BinaryMathWrapper(operator.mod),
1371+ BinaryMathWrapper(operator.mul),
1372+ BinaryMathWrapper(operator.pow),
1373+ BinaryMathWrapper(operator.sub),
1374+## BinaryMathWrapper(operator.truediv),
13551375
13561376 UnaryBuiltinWrapper(bool),
13571377 UnaryBuiltinWrapper(operator.not_),
--- a/implementations/Python/joy/utils/stack.py
+++ b/implementations/Python/joy/utils/stack.py
@@ -179,10 +179,10 @@ def concat(quote, expression):
179179 # In-lining is slightly faster (and won't break the
180180 # recursion limit on long quotes.)
181181
182+ if not isinstance(quote, tuple):
183+ raise NotAListError('Not a list.')
182184 temp = []
183185 while quote:
184- if not isinstance(quote, tuple):
185- raise NotAListError(repr(quote))
186186 item, quote = quote
187187 temp.append(item)
188188 for item in reversed(temp):
Afficher sur ancien navigateur de dépôt.