Esempio n. 1
0
    def test_use_print(self):
        str = 'while x>0:\n'\
            'print "1";\n'\
              'endwhile;\n'
        stat = find_vars(synt(lex(str)))
        assert_equal(set(stat.vars), set(['x']))
        assert_equal(set(stat.strs), set(['"1"']))
        assert_true(stat.use_print)
        assert_false(stat.use_read)

        str = 'if x>0:\n'\
            'print "1";\n'\
              'else:\n'\
            'print "2";\n'\
              'endif;\n'
        stat = find_vars(synt(lex(str)))
        assert_equal(set(stat.vars), set(['x']))
        assert_equal(set(stat.strs), set(['"1"', '"2"']))
        assert_true(stat.use_print)
        assert_false(stat.use_read)

        str = 'read i;\nprint i;'
        stat = find_vars(synt(lex(str)))
        assert_equal(set(stat.vars), set(['i']))
        assert_true(stat.use_print)
        assert_true(stat.use_read)
Esempio n. 2
0
def main(argc, argv):
    visual = False
    native = False
    if "-v" in argv:
        visual = True
    if "-n" in argv:
        native = True
    try:
        f_content = read_all(argv[argv.index("-f") + 1])
    except:
        print("Usage: python main.py [-v] -f file_name")
        exit()
    tokens = lex(f_content)
    parsed = parse(tokens)
    # print(json.dumps(parsed, indent=4))

    question_facts = parsed["question_facts"]
    Rules = parsed["rules"]
    check_rules(Rules)
    Facts = parsed["facts"]
    Graph = nx.Graph(parsed["graph_body"])

    config.init(Rules, Facts, Graph, visual, native)

    init_pos()
    if config.visual:
        draw_graph()
    solve()
    if config.visual:
        draw_graph()

    # Result
    for q in question_facts:
        print(q, Facts[q])
Esempio n. 3
0
    def test_if(self):
        str = "if x>0:\n" 'print "1";\n' "else:\n" 'print "2";\n' "endif;\n"
        tree = [
            (A_BLOCK, [(A_IF, [[(">", ["x", "0"])], (A_BLOCK, [(A_PRINT, '"1"')]), (A_BLOCK, [(A_PRINT, '"2"')])])])
        ]

        assert_equal(synt(lex(str)), tree)
Esempio n. 4
0
    def test_print(self):
        str = "print i;\n"
        tree = [(A_BLOCK, [(A_PRINT, "i")])]
        assert_equal(synt(lex(str)), tree)

        stat = find_vars(tree)
        assert_equal(set(stat.vars), set(["i"]))
        assert_true(stat.use_print)
        assert_false(stat.use_read)
Esempio n. 5
0
    def test_print(self):
        str = 'print i;\n'
        tree = [(A_BLOCK, [(A_PRINT, "i")])]
        assert_equal(synt(lex(str)), tree)

        stat = find_vars(tree)
        assert_equal(set(stat.vars), set(['i']))
        assert_true(stat.use_print)
        assert_false(stat.use_read)
Esempio n. 6
0
    def test_use_print(self):
        str = "while x>0:\n" 'print "1";\n' "endwhile;\n"
        stat = find_vars(synt(lex(str)))
        assert_equal(set(stat.vars), set(["x"]))
        assert_equal(set(stat.strs), set(['"1"']))
        assert_true(stat.use_print)
        assert_false(stat.use_read)

        str = "if x>0:\n" 'print "1";\n' "else:\n" 'print "2";\n' "endif;\n"
        stat = find_vars(synt(lex(str)))
        assert_equal(set(stat.vars), set(["x"]))
        assert_equal(set(stat.strs), set(['"1"', '"2"']))
        assert_true(stat.use_print)
        assert_false(stat.use_read)

        str = "read i;\nprint i;"
        stat = find_vars(synt(lex(str)))
        assert_equal(set(stat.vars), set(["i"]))
        assert_true(stat.use_print)
        assert_true(stat.use_read)
Esempio n. 7
0
    def test_smoke(self):
        str = "i = 10+1;\n" "j = 5*i;\n"
        tree = [
            (A_BLOCK, list(reversed([(A_ASSIGN, ["i", [("+", ["10", "1"])]]), (A_ASSIGN, ["j", [("*", ["5", "i"])]])])))
        ]
        assert_equal(synt(lex(str)), tree)

        stat = find_vars(tree)
        assert_equal(set(stat.vars), set(["i", "j"]))
        assert_false(stat.use_print)
        assert_false(stat.use_read)
Esempio n. 8
0
    def test_smoke(self):
        str = "i = 10+1;\n"\
              "j = 5*i;\n"
        tree = [
            (A_BLOCK,
             list(
                 reversed([
                     (A_ASSIGN, ['i', [('+', ['10', '1'])]]),
                     (A_ASSIGN, ['j', [('*', ['5', 'i'])]]),
                 ]))),
        ]
        assert_equal(synt(lex(str)), tree)

        stat = find_vars(tree)
        assert_equal(set(stat.vars), set(['i', 'j']))
        assert_false(stat.use_print)
        assert_false(stat.use_read)
Esempio n. 9
0
    def test_if(self):
        str = 'if x>0:\n'\
            'print "1";\n'\
              'else:\n'\
            'print "2";\n'\
              'endif;\n'
        tree = [
            (A_BLOCK, [
                (
                    A_IF,
                    [[('>', ['x', '0'])], (A_BLOCK, [(A_PRINT, '"1"')]),
                     (A_BLOCK, [(A_PRINT, '"2"')])],
                ),
            ]),
        ]

        assert_equal(synt(lex(str)), tree)
Esempio n. 10
0
    def test_loop(self):
        str = 'while x>0:\n'\
            'print "1";\n'\
              'endwhile;\n'
        tree = [
            (A_BLOCK, [
                (
                    A_WHILE,
                    [
                        [('>', ['x', '0'])],
                        (A_BLOCK, [(A_PRINT, '"1"')]),
                    ],
                ),
            ]),
        ]

        assert_equal(synt(lex(str)), tree)
Esempio n. 11
0
    def compile(self, f_name):
        src = file(os.path.join(os.getcwd(), 'tests/test_set/', f_name)).read()
        tree = synt(lex(src))
        asm = os.path.join(os.getcwd(), 'tests/test_set/', f_name) + '.asm'
        p = gen_code(tree, find_vars(tree))

        lines = gen_real_asm(p, 'TEST')

        f = file(asm, 'w')
        for line in lines:
            print>>f, line
        f.close()

        o = os.path.join(os.getcwd(), 'tests/test_set/', f_name) + '.o'
        bin = os.path.join(os.getcwd(), 'tests/test_set/', f_name) + '.bin'
        subprocess.check_call('nasm -f elf %s -o %s' % (asm, o), shell=True)
        subprocess.check_call('ld -s -dynamic-linker /lib/ld-linux.so.2 -lc %s -o %s' %
                              (o, bin), shell=True)
        os.remove(asm)
        os.remove(o)
        return bin
Esempio n. 12
0
    def compile(self, f_name):
        src = file(os.path.join(os.getcwd(), 'tests/test_set/', f_name)).read()
        tree = synt(lex(src))
        asm = os.path.join(os.getcwd(), 'tests/test_set/', f_name) + '.asm'
        p = gen_code(tree, find_vars(tree))

        lines = gen_real_asm(p, 'TEST')

        f = file(asm, 'w')
        for line in lines:
            print >> f, line
        f.close()

        o = os.path.join(os.getcwd(), 'tests/test_set/', f_name) + '.o'
        bin = os.path.join(os.getcwd(), 'tests/test_set/', f_name) + '.bin'
        subprocess.check_call('nasm -f elf %s -o %s' % (asm, o), shell=True)
        subprocess.check_call(
            'ld -s -dynamic-linker /lib/ld-linux.so.2 -lc %s -o %s' % (o, bin),
            shell=True)
        os.remove(asm)
        os.remove(o)
        return bin
Esempio n. 13
0
 def test_combined_vars(self):
     s = 'x1 = 1 + y2'
     assert_equal(lex(s), ['x1', '=', '1', '+', 'y2'])
Esempio n. 14
0
 def test_comment(self):
     s = 'x #text\n 1'
     assert_equal(lex(s), ['x', '1'])
Esempio n. 15
0
 def test_unclosed_quotes(self):
     s = 'x "text\n'
     lex(s)
Esempio n. 16
0
 def test_comment_in_quotes(self):
     s = '"x; # 10"'
     assert_equal(lex(s), [s])
Esempio n. 17
0
 def test_quotes(self):
     s = '"x;  123"'
     assert_equal(lex(s), [s])
Esempio n. 18
0
 def test_smoke(self):
     s = ' v 1; "1  10 2" \nx=10;'
     assert_equal(lex(s), ['v', '1', ';', '"1  10 2"', 'x', '=', '10', ';'])
Esempio n. 19
0
    def test_loop(self):
        str = "while x>0:\n" 'print "1";\n' "endwhile;\n"
        tree = [(A_BLOCK, [(A_WHILE, [[(">", ["x", "0"])], (A_BLOCK, [(A_PRINT, '"1"')])])])]

        assert_equal(synt(lex(str)), tree)