コード例 #1
0
ファイル: HCS_interpreter.py プロジェクト: helton/hcs-lang
def interpret_loop():

    def print_welcome():
        size = 50
        print("=" * size)
        print("HCS Programming Language Interpreter")
        print("=" * size)
        print("Author: Helton Carlos de Souza")
        print("Type 'quit' or 'exit' to end.")
        print("=" * size)

    print_welcome()
    hcs = HCS()
    command_count = 1
    while True:
        print("[%03d]>> " % (command_count), end="")
        try:
            command = input()
            command_count += 1
        except EOFError as e:
            print()
            return            
        if command in ['quit', 'exit']:
            return
        try:
            print(hcs.evaluate(command))            
        except Exception as e:
            #print("%s: %s" % (e.__class__.__name__, str(e)))
            traceback.print_exc()
コード例 #2
0
ファイル: HCS_tests.py プロジェクト: helton/hcs-lang
def test_anonymous_functions():
    hcs = HCS()
    assert(hcs.evaluate("""
        (def(a, b, c) { 
            sum(a, b, c)
        })(123, 456, 789)
    """)) == 1368
コード例 #3
0
ファイル: HCS_tests.py プロジェクト: helton/hcs-lang
def test_named_functions():
    hcs = HCS()
    assert(hcs.evaluate("""
        def sum_this(a, b, c) { 
            sum(a, b, c);
        };
        x = sum_this(123, 456, 789);
    """)) == 1368
コード例 #4
0
ファイル: HCS_tests.py プロジェクト: helton/hcs-lang
def test_prefix_posfix_operators():
    hcs = HCS()
    hcs.evaluate("a = 1; b = 1") # preparing variables to test
    assert hcs.evaluate("c = ++a") == 2
    assert hcs.evaluate("a") == 2
    assert hcs.evaluate("d = b++") == 1
    assert hcs.evaluate("b") == 2
コード例 #5
0
ファイル: HCS_tests.py プロジェクト: helton/hcs-lang
def test_statements():
    hcs = HCS()
    assert hcs.evaluate("a = 3; a; 123; 23423; b = a + 10") == 13
    source = """add_two = def(a, b) {
                            a + b
                         };
                x = add_two(10, 99);
                x + 1000
                """
    assert hcs.evaluate(source) == 1109
    
    source = """
       add_two = def(a, b) {
                    a + b;
                    c = 8;
                    avg_three = def(x, y, z) { (x + y + z) / 3 };
                    x = avg_three(a, b, c)
                 };
        y = add_two(7, 9);                
        y + 1000
    """
    assert hcs.evaluate(source) == 1008
    
    source = """
        add_two = def(a, b) {
                     a + b;
                     c = 8;
                     avg_three = def(x, y, z) { (x + y + z) / 3 };
                     x = avg_three(a, b, c);
                  };
        y = add_two(7, 9);                
        y = y + 1000;
        (def(r) { 
            r + 2000
        })(y);
    """ 
    assert hcs.evaluate(source) == 3008

    #without semicolons
    source = """
        add_two = def(a, b) {
                     a + b
                     c = 8
                     avg_three = def(x, y, z) { (x + y + z) / 3 }
                     x = avg_three(a, b, c)
                  };
        y = add_two(7, 9)
        y = y + 1000
        (def(r) { 
            r + 2000
        })(y)
    """ 
    assert hcs.evaluate(source) == 3008    
コード例 #6
0
ファイル: HCS_tests.py プロジェクト: helton/hcs-lang
def test_function_calls():
    hcs = HCS()
    assert hcs.evaluate('add_ten = def(a) { a + 10 }; add_ten(2)') == 12
    assert hcs.evaluate("add_nums = def(a, b) { a + b } ; add_nums(4, 5)") == 9
    assert hcs.evaluate("add_two = def(a, b) { a + b } ; add_two(10, 99)") == 109