Example #1
0
 def sexp(self, elements):
     if len(elements) > 1 and elements[-2] == PtObject.intern('.'):
         assert len(elements) > 2
         final = elements[-1]
         elements = elements[:-2]
     else:
         final = PtObject.nil
     return PtObject.list(elements, final)
Example #2
0
def test_let():
    check_result('(let ())', PtObject.nil)
    check_result('(let () 1)', PtObject(1))
    check_result('(let (a) a)', PtObject.nil)
    check_result('(let ((a)) a)', PtObject.nil)
    check_result('(let ((a nil)) a)', PtObject.nil)
    check_result('(let ((a 3)) a)', PtObject(3))
    check_result('(let ((a 1)) (let ((b a)) b))', PtObject(1))
Example #3
0
def _codegen_cons(node, bld, mod, lib, ns):
    if not bool(node):
        return _obj_ptr(bld, PtObject.nil)
    head, tail = node.car, node.cdr
    if head == PtObject.intern('quote'):
        return codegen_copy(tail.car, bld, mod, lib, ns)
    if head == PtObject.intern('begin'):
        nodes = list(tail) or [PtObject.nil]
        for node in nodes:
            retval = codegen(node, bld, mod, lib, ns)
        return retval
    if head == PtObject.intern('if'):
        return _codegen_if(tail, bld, mod, lib, ns)
    if head == PtObject.intern('let'):
        return _codegen_let(tail, bld, mod, lib, ns)
    return _codegen_funcall(node, bld, mod, lib, ns)
Example #4
0
def test_literals():
    check_result('0', PtObject(0))
    check_result('-1', PtObject(-1))
    check_result('0xf', PtObject(15))

    check_result('2.1', PtObject(2.1))
    check_result('-0.0', PtObject(0.0))

    check_result('""', PtObject(''))
    check_result('"alpha"', PtObject('alpha'))
Example #5
0
def test_symbol():
    obj = PtObject.symbol('abc')
    assert str(obj) == 'abc'
    assert obj != PtObject.symbol('abc')

    obj = PtObject.intern('abc')
    assert obj != PtObject.symbol('abc')
    assert obj == PtObject.intern('abc')
    assert obj is PtObject.intern('abc')

    ns = {
        PtSymbol('a', 0): 'a',
        PtSymbol('b', 1): 'b',
    }
    assert ns[PtSymbol('a', 0)] == 'a'
    assert ns[PtSymbol('a', 1)] == 'b'
    assert ns[PtSymbol('b', 0)] == 'a'
Example #6
0
 def bquot(self, value):
     return PtObject.list([PtObject.intern('backquote'), value])
Example #7
0
 def double(self, value):
     return PtObject(float(value))
Example #8
0
def test_parsing():
    parser = PaltryParser(semantics=PaltrySemantics())
    parse = lambda s: parser.parse(s, 'exp')

    assert parse('quux') == PtObject.intern('quux')
    assert parse('"alpha"') == PtObject('alpha')
    assert parse('""') == PtObject('')
    assert parse('120') == PtObject(120)
    assert parse('-2') == PtObject(-2)
    assert parse('120.0e1') == PtObject(1200.0)
    assert parse('-3.14') == PtObject(-3.14)
    assert parse('0xf') == PtObject(15)
    assert parse('-0xf') == PtObject(-15)
    assert parse('0o7') == PtObject(7)
    assert parse('-0o7') == PtObject(-7)
    assert parse('0b1') == PtObject(1)
    assert parse('-0b1') == PtObject(-1)
    assert parse('(a b c d "e")') == PtObject.list([
        PtObject.intern('a'),
        PtObject.intern('b'),
        PtObject.intern('c'),
        PtObject.intern('d'),
        PtObject('e'),
    ])
    assert parse("'quoted") == PtObject.list(
        [PtObject.intern('quote'),
         PtObject.intern('quoted')])
    assert parse("'(a list goes here)") == PtObject.list([
        PtObject.intern('quote'),
        PtObject.list([
            PtObject.intern('a'),
            PtObject.intern('list'),
            PtObject.intern('goes'),
            PtObject.intern('here'),
        ]),
    ])
    assert parse('`(a list goes here)') == PtObject.list([
        PtObject.intern('backquote'),
        PtObject.list([
            PtObject.intern('a'),
            PtObject.intern('list'),
            PtObject.intern('goes'),
            PtObject.intern('here'),
        ]),
    ])
    assert parse(',sym') == PtObject.list([
        PtObject.intern('unquote'),
        PtObject.intern('sym'),
    ])
    assert parse(',(a b)') == PtObject.list([
        PtObject.intern('unquote'),
        PtObject.list([
            PtObject.intern('a'),
            PtObject.intern('b'),
        ])
    ])
    assert parse(',@sym') == PtObject.list([
        PtObject.intern('unquote-splice'),
        PtObject.intern('sym'),
    ])
    assert parse(',@(a b)') == PtObject.list([
        PtObject.intern('unquote-splice'),
        PtObject.list([
            PtObject.intern('a'),
            PtObject.intern('b'),
        ])
    ])
Example #9
0
def test_cons():
    assert str(PtObject.nil) == 'nil'
    assert PtObject.nil == PtObject.nil

    obj = PtObject.cons(PtObject.intern('def'), PtObject.intern('ghi'))
    assert str(obj) == '(def . ghi)'
    assert obj == PtObject.cons(PtObject.intern('def'), PtObject.intern('ghi'))
    assert obj != PtObject.cons(PtObject.symbol('def'), PtObject.intern('ghi'))
    assert obj != PtObject.cons(PtObject.intern('def'), PtObject.symbol('ghi'))

    obj = PtObject.cons(
        PtObject.intern('a'),
        PtObject.cons(
            PtObject.intern('b'),
            PtObject.cons(
                PtObject.intern('c'),
                PtObject.intern('d'))))
    assert str(obj) == '(a b c . d)'
    assert obj == PtObject.cons(
        PtObject.intern('a'),
        PtObject.cons(
            PtObject.intern('b'),
            PtObject.cons(
                PtObject.intern('c'),
                PtObject.intern('d'))))

    obj = PtObject.cons(
        PtObject.intern('a'),
        PtObject.cons(
            PtObject.intern('b'),
            PtObject.cons(
                PtObject.intern('c'),
                PtObject.nil)))
    assert str(obj) == '(a b c)'
    assert obj == PtObject.cons(
        PtObject.intern('a'),
        PtObject.cons(
            PtObject.intern('b'),
            PtObject.cons(
                PtObject.intern('c'),
                PtObject.nil)))

    obj = PtObject.list([
        PtObject.intern('a'),
        PtObject.intern('b'),
        PtObject.intern('c'),
        PtObject.intern('d'),
        PtObject.intern('e'),
    ])
    assert str(obj) == '(a b c d e)'
    assert obj == PtObject.list([
        PtObject.intern('a'),
        PtObject.intern('b'),
        PtObject.intern('c'),
        PtObject.intern('d'),
        PtObject.intern('e'),
    ])
Example #10
0
def test_string():
    obj = PtObject('abc')
    assert str(obj) == '"abc"'
    assert obj == PtObject('abc')
    assert obj != PtObject('abcc')
Example #11
0
 def symbol(self, name):
     return PtObject.intern(name)
Example #12
0
def test_if():
    check_result('(if t 1)', PtObject(1))
    check_result('(if nil 1)', PtObject.nil)
    check_result('(if nil 1 2)', PtObject(2))
    check_result('(if nil 1 2 3)', PtObject(3))
Example #13
0
def test_begin():
    check_result('(begin)', PtObject.nil)
    check_result('(begin 1)', PtObject(1))
    check_result('(begin 1 2 3)', PtObject(3))
Example #14
0
def test_quote():
    check_result("'symbol", PtObject.intern('symbol'))
    check_result(
        "''symbol",
        PtObject.list([PtObject.intern('quote'),
                       PtObject.intern('symbol')]))
    check_result("'(a b c)",
                 PtObject.list([PtObject.intern(s) for s in ['a', 'b', 'c']]))
    check_result("'(a . b)",
                 PtObject.cons(PtObject.intern('a'), PtObject.intern('b')))
    check_result(
        "'(a b . c)",
        PtObject.cons(
            PtObject.intern('a'),
            PtObject.cons(PtObject.intern('b'), PtObject.intern('c'))))
Example #15
0
 def unquot(self, value):
     return PtObject.list([PtObject.intern('unquote'), value])
Example #16
0
 def unquot_splice(self, value):
     return PtObject.list([PtObject.intern('unquote-splice'), value])
Example #17
0
def test_runtime():
    check_result('(intern "alpha")', PtObject.intern('alpha'))
Example #18
0
def test_float():
    obj = PtObject(3.0)
    assert str(obj) == '3.0'
    assert obj == PtObject(3.0)
    assert obj != PtObject(-3.0)
Example #19
0
 def string(self, value):
     value = codecs.escape_decode(bytes(value[1:-1],
                                        'utf-8'))[0].decode('utf-8')
     return PtObject(value)
Example #20
0
 def hex_integer(self, value):
     sign = ''
     if value[0] in '+-':
         sign, value = value[0], value[1:]
     return PtObject(int(sign + value[2:], 16))
Example #21
0
 def dec_integer(self, value):
     return PtObject(int(value))
Example #22
0
def test_integer():
    obj = PtObject(3)
    assert str(obj) == '3'
    assert obj == PtObject(3)
    assert obj != PtObject(4)
Example #23
0
def intern(name):
    return PtObject.intern(name.string)