def test_python_function(self) -> None:
        """Python function call test (from Concat).

        Test that a normal Python is treated as if it had the stack effect
        kwargs args -- func(*args, **kwargs) when called with py_call."""
        stack = [Quotation([]), Quotation([0]), bool]
        stash: List[object] = []
        concat.stdlib.pyinterop.py_call(stack, stash)
        message = 'py_call has incorrect stack effect'
        self.assertEqual(stack, [False], msg=message)
Exemple #2
0
 def test_singleton(self) -> None:
     quotation = Quotation([
         drop,
         lambda s, _: s.append(
             Quotation([
                 drop,
                 lambda s, _: s.append(None),
                 lambda s, _: s.append(None),
             ])),
         lambda s, _: s.append(42),
     ])
     stack: List[object] = [quotation]
     concat.stdlib.types.to_py_iter(stack, [])
     actual = list(cast(Iterator[int], stack.pop()))
     self.assertEqual(actual, [42])
Exemple #3
0
 def fun(stack, stash) -> None:
     drop(stack, stash)
     n = stack.pop()
     stack.append(n == 1)
     stack.append(
         Quotation([
             lambda s, t: s.append(
                 Quotation([lambda s, t: s.extend([None, None])])),
             lambda s, t: s.append(n),
         ]))
     stack.append(
         Quotation([
             lambda s, t: s.append(n),
             lambda s, t: s.append(fun2),
             curry,
             lambda s, t: s.append(n),
         ]))
     choose(stack, stash)
Exemple #4
0
 def test_return(self) -> None:
     quotation = Quotation([
         drop,
         lambda s, _: s.append(
             Quotation([
                 drop,
                 lambda s, _: s.append(None),
                 lambda s, _: s.append('a return value'),
             ])),
         lambda s, _: s.append(42),
     ])
     stack = LoggableStack('stack', should_log=False)
     stack.extend([quotation])
     concat.stdlib.types.to_py_iter(stack, [])
     generator = cast(Iterator[object], stack.pop())
     for _ in range(10):
         try:
             next(generator)
         except StopIteration as e:
             self.assertEqual(e.value, 'a return value')
             return
     self.fail('generator did not stop')
Exemple #5
0
 def test_send(self) -> None:
     quotation = Quotation([lambda s, _: s.append(None), swap])
     stack = LoggableStack('stack', should_log=False)
     stack.extend([quotation])
     concat.stdlib.types.to_py_iter(stack, [])
     generator = cast(Generator[int, int, None], stack.pop())
     for _ in range(1):
         try:
             generator.send(42)
         except StopIteration as e:
             self.assertEqual(e.value, 42)
             return
     self.fail('generator did not stop')
Exemple #6
0
def curry(stack, stash):
    """value $fun -- $(value fun)"""
    fun, value = (stack.pop() for _ in range(2))
    stack.append(Quotation([lambda s, _: s.append(value), fun]))
Exemple #7
0
def if_not(stack: List[object], stash: List[object]) -> None:
    """flag $fun => flag $() $fun choose"""
    stack[-1:-1] = [Quotation()]
    choose(stack, stash)
Exemple #8
0
def if_then(stack: List[object], stash: List[object]) -> None:
    """flag $fun => flag $fun $() choose"""
    stack.append(Quotation())
    choose(stack, stash)