Exemplo n.º 1
0
def test_parse_error_in_msg_slot_value_assignment():
    try:
        lex_and_parse('(| asd: a Bsd: b = 1 |)')
    except AssertionError:
        return

    raise AssertionError("KW slot value assignment shouldn't be allowed!")
Exemplo n.º 2
0
def test_parallelism():
    one_plus_one_ast = lex_and_parse("""(|
        add = (|| 1 + 1)
    |) add""")
    two_plus_two_ast = lex_and_parse("""(|
        add = (|| 2 + 2)
    |) add""")

    one_plus_one_ctx = one_plus_one_ast[0].compile(CodeContext())
    two_plus_two_ctx = two_plus_two_ast[0].compile(CodeContext())

    interpreter = Interpreter(universe=get_primitives())
    one_plus_one_process = interpreter.add_process(one_plus_one_ctx)
    two_plus_two_process = interpreter.add_process(two_plus_two_ctx)

    assert not one_plus_one_process.finished
    assert not one_plus_one_process.result
    assert not two_plus_two_process.finished
    assert not two_plus_two_process.result

    interpreter.interpret()

    assert one_plus_one_process.finished
    assert one_plus_one_process.result == PrimitiveIntObject(2)
    assert two_plus_two_process.finished
    assert two_plus_two_process.result == PrimitiveIntObject(4)
Exemplo n.º 3
0
def test_parens_for_priority():
    result = lex_and_parse('(|| 1 > (2 xex: 1) ifTrue: [] )')

    assert result == [
        Object(code=[
            Send(obj=Send(obj=IntNumber(1),
                          msg=BinaryMessage(
                              name='>',
                              parameter=Send(obj=IntNumber(2),
                                             msg=KeywordMessage(
                                                 name='xex:',
                                                 parameters=[IntNumber(1)])))),
                 msg=KeywordMessage(name='ifTrue:', parameters=[Block()]))
        ])
    ]

    # and now without parens
    result = lex_and_parse('(|| 1 > 2 xex: 1 ifTrue: [] )')

    assert result == [
        Object(code=[
            Send(obj=Send(obj=IntNumber(1),
                          msg=BinaryMessage(name='>', parameter=IntNumber(2))),
                 msg=KeywordMessage(name='xex:',
                                    parameters=[
                                        Send(obj=IntNumber(1),
                                             msg=KeywordMessage(
                                                 name='ifTrue:',
                                                 parameters=[Block()]))
                                    ]))
        ])
    ]
Exemplo n.º 4
0
def test_self_kw():
    result = lex_and_parse('(|| self)')

    assert result == [Object(code=[Self()])]

    result = lex_and_parse('(|| self xe)')

    assert result == [Object(code=[Send(Self(), Message("xe"))])]
Exemplo n.º 5
0
def test_argument_parser():
    result = lex_and_parse('(| :a |)')
    assert result == [Object(params=["a"])]

    result = lex_and_parse('(| :a. |)')
    assert result == [Object(params=["a"])]

    result = lex_and_parse('(| :a. :b |)')
    assert result == [Object(params=["a", "b"])]
Exemplo n.º 6
0
def test_parse_string():
    result = lex_and_parse('"asd"')

    assert isinstance(result[0], String)
    assert result[0].value == "asd"

    result = lex_and_parse("'asd'")
    assert result[0].value == "asd"

    result = lex_and_parse('""')
    assert result[0].value == ""
Exemplo n.º 7
0
def test_parse_multiple_slot_assignment():
    result = lex_and_parse('(asd <- 2. bsd <- 4 |)')
    assert result == [
        Object(slots=rw_slots(
            OrderedDict((("asd", IntNumber(2)), ("bsd", IntNumber(4))))))
    ]

    result = lex_and_parse('(| asd <- 2. bsd <- 4. |)')
    assert result == [
        Object(slots=rw_slots(
            OrderedDict((("asd", IntNumber(2)), ("bsd", IntNumber(4))))))
    ]
Exemplo n.º 8
0
def test_parse_object_with_nil_slot():
    result = lex_and_parse('(| asd |)')
    assert result == [Object(slots=_rw_slot("asd", Nil()))]

    result = lex_and_parse('(| asd. |)')
    assert result == [Object(slots=_rw_slot("asd", Nil()))]

    result = lex_and_parse('(asd |)')
    assert result == [Object(slots=_rw_slot("asd", Nil()))]

    result = lex_and_parse('( asd. |)')
    assert result == [Object(slots=_rw_slot("asd", Nil()))]
Exemplo n.º 9
0
def test_parse_chained_messages_kw():
    result = lex_and_parse('2 minus ifTrue: []')

    assert result == [
        Send(obj=Send(obj=IntNumber(2), msg=Message('minus')),
             msg=KeywordMessage(name='ifTrue:', parameters=[Block()]))
    ]
Exemplo n.º 10
0
def test_parse_binary_message():
    result = lex_and_parse('1 + 1')

    assert result == [
        Send(obj=IntNumber(1),
             msg=BinaryMessage(name='+', parameter=IntNumber(1)))
    ]
Exemplo n.º 11
0
def test_parse_keyword_message_to_obj_with_parameters():
    result = lex_and_parse('asd set: 1')

    assert result == [
        Send(obj=Send(obj=Self(), msg=Message('asd')),
             msg=KeywordMessage(name='set:', parameters=[IntNumber(1)]))
    ]
Exemplo n.º 12
0
def test_parse_object_with_multiple_nil_slots():
    expected_result = [
        Object(slots=OrderedDict((("asd", Nil()), ("asd:",
                                                   AssignmentPrimitive()),
                                  ("bsd", Nil()), ("bsd:",
                                                   AssignmentPrimitive()))))
    ]

    result = lex_and_parse('(| asd. bsd |)')
    assert result == expected_result

    result = lex_and_parse('(| asd. bsd. |)')
    assert result == expected_result

    result = lex_and_parse('(asd. bsd. |)')
    assert result == expected_result
Exemplo n.º 13
0
def test_obj_with_code_with_dot():
    result = lex_and_parse('(| a | a printLine.)')

    assert result == [
        Object(slots=_rw_slot("a", Nil()),
               code=[Send(Send(Self(), Message("a")), Message("printLine"))])
    ]
Exemplo n.º 14
0
def test_object_with_parents():
    result = lex_and_parse('(| p* = traits | a printLine)')

    assert result == [
        Object(parents={"p": Send(Self(), Message("traits"))},
               code=[Send(Send(Self(), Message("a")), Message("printLine"))])
    ]
Exemplo n.º 15
0
def run_interactive():
    _, interpreter = virtual_machine("()")

    while True:
        line = stdin_readline(":> ")

        if len(line) == 0 or line.strip() == "exit":
            writeln()
            return 0

        try:
            for expr in lex_and_parse(line):
                code = expr.compile(CodeContext())
                process = interpreter.add_process(code)
                interpreter.interpret()

                if process.finished_with_error:
                    print "Error:", process.result.__str__()
                    print
                    print "Code object:"
                    print
                    print code.debug_repr()
                else:
                    if process.result != NIL:
                        print process.result.__str__()

        except ParsingError as e:
            ewriteln("Parse error.")
            if e.message:
                ewriteln(e.message)
            continue

    return 0
Exemplo n.º 16
0
def test_parse_multiline_comment():
    result = lex_and_parse('''
    # this is example
    # of the multiline comment
        (|| self) # xe
    ''')

    assert result == [Object(code=[Self()])]
Exemplo n.º 17
0
def test_parse_multiple_op_slot_assignments():
    result = lex_and_parse('(| + b = (). - a = () |)')
    assert result == [
        Object(slots={
            "+": Object(params=["b"]),
            "-": Object(params=["a"]),
        })
    ]
Exemplo n.º 18
0
def test_parse_cascade_to_self():
    result = lex_and_parse('a; b')

    assert result == [
        Cascade(obj=Self(), msgs=[
            Message("a"),
            Message("b"),
        ])
    ]
Exemplo n.º 19
0
def test_parse_multiple_slots_assignments():
    result = lex_and_parse('(| asd: a Bsd: b = (). a: p = () |)')

    assert result == [
        Object(slots=OrderedDict((
            ("asd:Bsd:", Object(params=["a", "b"])),
            ("a:", Object(params=["p"])),
        )))
    ]
Exemplo n.º 20
0
def test_return_in_object():
    result = lex_and_parse('(|| a printLine. a print. ^test)')

    assert result == [
        Object(code=[
            Send(Send(Self(), Message("a")), Message("printLine")),
            Send(Send(Self(), Message("a")), Message("print")),
            Return(Send(Self(), Message("test")))
        ])
    ]
Exemplo n.º 21
0
def test_return_in_block():
    result = lex_and_parse('[ a printLine. a print. ^test]')

    assert result == [
        Block(code=[
            Send(Send(Self(), Message("a")), Message("printLine")),
            Send(Send(Self(), Message("a")), Message("print")),
            Return(Send(Self(), Message("test")))
        ])
    ]
Exemplo n.º 22
0
def test_block_with_just_code():
    result = lex_and_parse('[ a printLine. a print. test]')

    assert result == [
        Block(code=[
            Send(Send(Self(), Message("a")), Message("printLine")),
            Send(Send(Self(), Message("a")), Message("print")),
            Send(Self(), Message("test"))
        ])
    ]
Exemplo n.º 23
0
def test_block_empty_slots_and_code():
    result = lex_and_parse('[|| a printLine. a print. test]')

    assert result == [
        Block(code=[
            Send(Send(Self(), Message("a")), Message("printLine")),
            Send(Send(Self(), Message("a")), Message("print")),
            Send(Self(), Message("test"))
        ])
    ]
Exemplo n.º 24
0
def test_parse_cascade_kw_to_self():
    result = lex_and_parse('a: 1; b')

    assert result == [
        Cascade(obj=Self(),
                msgs=[
                    KeywordMessage("a:", [IntNumber(1)]),
                    Message("b"),
                ])
    ]
Exemplo n.º 25
0
def test_parse_cascade():
    result = lex_and_parse('a b; c')

    assert result == [
        Cascade(obj=Send(Self(), Message("a")),
                msgs=[
                    Message("b"),
                    Message("c"),
                ])
    ]
Exemplo n.º 26
0
def test_parse_chained_messages():
    result = lex_and_parse('2 minus minus')

    assert result == [
        Send(obj=Send(
            obj=IntNumber(2),
            msg=Message('minus'),
        ),
             msg=Message('minus'))
    ]
Exemplo n.º 27
0
def test_objects_with_objects_in_slots():
    result = lex_and_parse("""(|
    append: s = (||"Str: ")
|)""")

    assert result == [
        Object(slots=OrderedDict([
            ("append:", Object(params=["s"], code=[String("Str: ")])),
        ]))
    ]
Exemplo n.º 28
0
def test_multiple_statements_make_code():
    result = lex_and_parse('''
        xe.
        (|| self).
        1''')

    assert result == [
        Send(obj=Self(), msg=Message('xe')),
        Object(code=[Self()]),
        IntNumber(1)
    ]
Exemplo n.º 29
0
def test_multiple_cascades():
    result = lex_and_parse('obj a; b; c')

    assert result == [
        Cascade(obj=Send(Self(), Message("obj")),
                msgs=[
                    Message("a"),
                    Message("b"),
                    Message("c"),
                ])
    ]
Exemplo n.º 30
0
def test_parse_cascade_kw():
    result = lex_and_parse('s a: 1 B: 2; b')

    assert result == [
        Cascade(obj=Send(Self(), Message("s")),
                msgs=[
                    KeywordMessage("a:B:",
                                   [IntNumber(1), IntNumber(2)]),
                    Message("b"),
                ])
    ]