Ejemplo n.º 1
0
    def setUp(self):
        with open('tests/input/file', 'w') as file:
            file.write("program{int x;};")
        self.s: Scanner = Scanner('tests/input/file')
        tokens: list = self.s.scan_file()

        self.p: Parser = Parser(tokens)
Ejemplo n.º 2
0
 def __init__(self):
     self.lexer = Lexer()
     self.parser = Parser()
     self.typechecker = Typechecker()
Ejemplo n.º 3
0
 def __init__(self):
     self.lexer_instance = Lexer()
     self.parser_instance = Parser()
Ejemplo n.º 4
0
import unittest
from rply import Token

from compiler.parser.parser import Parser
import compiler.parser.ast as ast


parser = Parser()


class TestExpr(unittest.TestCase):
    def test_add(self):
        '''Can parse a simple addition: `1 + 1;`'''
        given = iter([
            Token('INTEGER', '1'),
            Token('PLUS', '+'),
            Token('INTEGER', '1'),
            Token('SEMI', ';')
        ])

        expected = ast.Block([
            ast.Statement(ast.BinOp('+', ast.Integer(1), ast.Integer(1)))
        ])

        result = parser.parse(given)

        assert expected == result

    def test_op_order(self):
        '''Enforces the right op. precedence: `1 + 5 * 20;`'''
        given = iter([
Ejemplo n.º 5
0
from compiler.scanner.scanner import Scanner
from compiler.parser.parser import Parser

DEBUG_TEST_FILE = "compiler/samples/sample_2"

args_len: int = len(sys.argv)

if args_len >= 2:
    s = Scanner(sys.argv[1])
else:
    s = Scanner(DEBUG_TEST_FILE)

token_sequence: list = s.scan_file()

print("----------\nScanner\n----------")
if args_len >= 3:
    s.log(sys.argv[2])
else:
    s.log()

print("----------\nParser\n----------")
p: Parser = Parser(token_sequence)
result: bool = p.parse()

if args_len >= 3:
    p.log_results(sys.argv[2])
    print('----------\n'+str(result))
else:
    p.log_results()
    print('----------\nResult: ' + str(result))