Example #1
0
def test_case_insensitive():
    text = r"""
        program hello_world;
        
        VAR
        _num_ber, a, b, c: INTEGER;
        x: integer;
        
        BEGIN

            BEgIN
                _num_ber := 2;
                a := _Num_Ber;
                B := 10 * a + 10 * _NUM_BER div 4;
                c := a - - b
            enD;
{ this is a comment }
        x := 11
        END.
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    SemanticAnalyzer().visit(tree)
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert interpreter.GLOBAL_SCOPE == {
        "A": 2,
        "X": 11,
        "C": 27,
        "B": 25,
        "_NUM_BER": 2,
    }
Example #2
0
def test_interpreter():
    text = r"""
        PROGRAM HELLO_WORLD;
        
        VAR
        number, a, b, c: INTEGER;
        x: REAL;
        
        BEGIN
            BEGIN
                number := 2;
                a := number; { this is a comment }
                b := 10 * a + 10 * number DIV 4;
                c := a - - b
            END;
            x := 11.2;
        END.
        """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    SemanticAnalyzer().visit(tree)
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert interpreter.GLOBAL_SCOPE == {
        "A": 2,
        "X": 11.2,
        "C": 27,
        "B": 25,
        "NUMBER": 2,
    }
Example #3
0
def test_part12():
    text = """
    PROGRAM Part12;
    VAR
       a : INTEGER;
    
    PROCEDURE P1;
    VAR
       a : REAL;
       k : INTEGER;
    
       PROCEDURE P2;
       VAR
          a, z : INTEGER;
       BEGIN {P2}
          z := 777;
       END;  {P2}
    
    BEGIN {P1}
    
    END;  {P1}
    
    BEGIN {Part12}
       a := 10;
    END.  {Part12}
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()
    builtins_scope = semantic_analyzer.visit(tree)
    global_scope = builtins_scope.global_scope
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert builtins_scope._symbols == {
        "INTEGER": integer_type,
        "REAL": real_type,
        "PART12": ProcedureSymbol("PART12"),
    }

    assert global_scope._symbols == {
        "A": VarSymbol("A", integer_type),
        "P1": ProcedureSymbol("P1"),
    }

    assert global_scope.P1_scope._symbols == {
        "A": VarSymbol("A", real_type),
        "K": VarSymbol("K", integer_type),
        "P2": ProcedureSymbol("P2"),
    }

    assert global_scope.P1_scope.P2_scope._symbols == {
        "A": VarSymbol("A", integer_type),
        "Z": VarSymbol("Z", integer_type),
    }

    assert interpreter.GLOBAL_SCOPE == {"A": 10}
Example #4
0
def test_part10():
    text = r"""
        PROGRAM Part10;
        VAR
          number : INTEGER;
          a, b, c, x : INTEGER;
          y : REAL;
        
        BEGIN
          {Part10}
        BEGIN
          number := 2;
          a := number;
          b := 10 * a + 10 * number DIV 4;
          c := a - - b
        END;
          x := 11;
          x := x / 2;
          y := 20 / 7 + 3.14;
          { writeln('a = ', a); }
          { writeln('b = ', b); }
          { writeln('c = ', c); }
          { writeln('number = ', number); }
          { writeln('x = ', x); }
          { writeln('y = ', y); }
        END.  {Part10}

    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    SemanticAnalyzer().visit(tree)
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert interpreter.GLOBAL_SCOPE == {
        "A": 2,
        "C": 27,
        "B": 25,
        "NUMBER": 2,
        "Y": 5.997142857142857,
        "X": 5.5,
    }
Example #5
0
def test_formal_parameter():
    text = """
        program Main;
       var x, y: real;
    
       procedure Alpha(a : integer);
          var y : integer;
       begin
          x := a + x + y;
       end;
    
    begin { Main }
    
    end.  { Main }
    """

    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()
    builtins_scope = semantic_analyzer.visit(tree)
    global_scope = builtins_scope.global_scope
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert builtins_scope._symbols == {
        "INTEGER": integer_type,
        "REAL": real_type,
        "MAIN": ProcedureSymbol("MAIN"),
    }

    assert global_scope._symbols == {
        "X": VarSymbol("X", real_type),
        "Y": VarSymbol("Y", real_type),
        "ALPHA": ProcedureSymbol("ALPHA"),
    }

    assert global_scope.ALPHA_scope._symbols == {
        "A": VarSymbol("A", integer_type),
        "Y": VarSymbol("Y", integer_type),
    }

    assert interpreter.GLOBAL_SCOPE == {}
Example #6
0
def test_part11():
    text = """
        PROGRAM Part11;
    VAR
       number : INTEGER;
       a, b   : INTEGER;
       y      : REAL;
    
    BEGIN {Part11}
       number := 2;
       a := number ;
       b := 10 * a + 10 * number DIV 4;
       y := 20 / 7 + 3.14
    END.  {Part11}
    """
    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    semantic_analyzer = SemanticAnalyzer()
    builtins_scope = semantic_analyzer.visit(tree)
    global_scope = builtins_scope.global_scope
    interpreter = Interpreter(tree)
    interpreter.interpret()

    assert builtins_scope._symbols == {
        "INTEGER": integer_type,
        "REAL": real_type,
        "PART11": ProcedureSymbol("PART11"),
    }

    assert global_scope._symbols == {
        "NUMBER": VarSymbol("NUMBER", integer_type),
        "A": VarSymbol("A", integer_type),
        "B": VarSymbol("B", integer_type),
        "Y": VarSymbol("Y", real_type),
    }

    assert interpreter.GLOBAL_SCOPE == dict(A=2,
                                            B=25,
                                            NUMBER=2,
                                            Y=5.997142857142857)
# print(lexer.get_next_token())
# print(lexer.get_next_token())
# print(lexer.get_next_token())
# print(lexer.get_next_token())
# Token(BEGIN, 'BEGIN')
# Token(ID, 'a')
# Token(ASSIGN, ':=')
# Token(INTEGER, 2)
# Token(SEMI, ';')
# Token(END, 'END')
# Token(DOT, '.')
# Token(EOF, None)

# parse file
with open('test_full.pas', 'r') as file:
    text = file.read()

    lexer = Lexer(text)
    parser = Parser(lexer)
    interpreter = Interpreter(parser)
    interpreter.interpret()

    for k, v in sorted(interpreter.GLOBAL_SCOPE.items()):
        print('{} = {}'.format(k, v))

# a = 2
# b = 25
# c = 27
# number = 2
# x = 11
# y = 5.997142857142857
Example #8
0
 def compute(task):
     lexer = Lexer(task)
     parser = Parser(lexer)
     interpreter = Interpreter(parser)
     result = interpreter.interpret()
     return result
Example #9
0
 def compute(task):
     lexer = Lexer(task)
     parser = Parser(lexer)
     interpreter = Interpreter(parser)
     result = interpreter.interpret()
     return result