Beispiel #1
0
    def test_parse_and_convert(self):
        fullc_ast = parse_file('./c_files/minic.c')
        converted = ctoc.transform(fullc_ast)
        self.failUnless(isinstance(converted, mast.FileAST))
        self.assertEqual(len(converted.ext), 2)
        self.failUnless(isinstance(converted.ext[0], mast.FuncDef))

        funcdef_mss = converted.ext[0]
        self.assertEqual(funcdef_mss.decl.name, 'mss')
        mss_body = funcdef_mss.body
        self.assertTrue(isinstance(mss_body, mast.Block))
        self.assertEqual(len(mss_body.block_items), 5)
        self.failUnless(isinstance(mss_body.block_items[0], mast.Decl))
        self.failUnless(isinstance(mss_body.block_items[1], mast.Decl))
        self.failUnless(isinstance(mss_body.block_items[2], mast.Decl))
        self.failUnless(isinstance(mss_body.block_items[3], mast.For))
        forstmt = mss_body.block_items[3]
        print(forstmt.next)
        self.assertTrue(isinstance(forstmt.next, mast.Assignment))
        self.failUnless(isinstance(mss_body.block_items[4], mast.Return))

        funcdef_main = converted.ext[1]
        self.assertEqual(funcdef_main.decl.name, 'main')
        main_body = funcdef_main.body
        self.assertEqual(len(main_body.block_items), 4)
        self.failUnless(isinstance(main_body.block_items[0], mast.Decl))
        self.failUnless(isinstance(main_body.block_items[1], mast.Decl))
        self.failUnless(isinstance(main_body.block_items[2], mast.Assignment))
        self.failUnless(isinstance(main_body.block_items[3], mast.Return))
Beispiel #2
0
def get_output(directory_path):
    output_c = function_wrapper(directory_path)
    mast = transform(parse_file(output_c))
    print("File: {} \nInput:".format(directory_path.split('/'[-1])))
    with open(directory_path, 'r') as fin:
        print(fin.read())
    ftranslator = FunctionalTranslator(mast, True)
    print("Output:\n{}\n----------".format(ftranslator))
Beispiel #3
0
import sys
import os
sys.path.extend(['.', '..'])

from pycparser import parse_file
from minic.c_ast_to_minic import transform
from transform_func import *
from func_utils import function_wrapper

if __name__ == "__main__":
    directory_path = "./inputs/checkin4_inputs"
    directory = os.fsencode(directory_path)
    for file in os.listdir(directory):
        filename = os.fsdecode(file)
        if not filename.endswith("_out.c"):
            output_c = function_wrapper(os.path.join(directory_path, filename))
            mast = transform(parse_file(output_c))
            ftranslator = FunctionalTranslator(mast)
            print("File: {} \nInput:".format(filename))
            with open(os.path.join(directory_path, filename), 'r') as fin:
                print(fin.read())
            print("Output:\n{}\n----------".format(ftranslator))
Beispiel #4
0
    # Return Statement
    def visit_Return(self, Return):
        self.nodes.append(Return)
        if len(self.nodes) != 0:
            for node in self.nodes:
                if isinstance(node, For) or isinstance(node, While) or isinstance(node, If):
                    variable = Return.expr.name
                    self.livevariables.append(variable)
    # For Loop
    def visit_For(self, For):
        self.nodes.append(For)

    # For While Loop
    def visit_While(self, While):
        self.nodes.append(While)

    # Needs to be checked but for If statement
    def visit_If(self, If):
        self.nodes.append(If)


if __name__ == "__main__":
    sys.path.extend(['.', '..'])
    for i in range(1, 5):
        # AST to work with
        ast = parse_file("./project1inputs/p1_input{}.c".format(i))
        # convert to minic ast
        m_ast = transform(ast)
        print("Live Variable Analysis for" + " p1_input{}.txt".format(i))
        FunctionDefVisitor().visit(m_ast)
Beispiel #5
0

class LHSPrinter(NodeVisitor):
    def __init__(self):
        self.written_set = set()
        self.var_set = set()

    def visit_Assignment(self, assignment):
        if isinstance(assignment.lvalue, ID):
            self.written_set.add(assignment.lvalue.name)
            self.var_set.add(assignment.lvalue.name)

        if isinstance(assignment.rvalue, BinaryOp):
            bov = BinaryOpVistor()
            bov.visit(assignment.rvalue)
            self.var_set.update(bov.var_set)

        if isinstance(assignment.rvalue, ArrayRef):
            self.var_set.add(assignment.rvalue.name.name)


if __name__ == "__main__":
    for i in range(1, 4):
        function_wrapper('./p3_input{}.txt'.format(i))
        mast = transform(parse_file('./p3_input{}_out.c'.format(i)))
        lhsp = LHSPrinter()
        print("p3_input{}.txt: ".format(i))
        lhsp.visit(mast)
        print("Written Variables: {} ".format(', '.join(lhsp.written_set)))
        print("All Variables: {} ".format(', '.join(lhsp.var_set)))
 def test_visit(self):
     ast = ctoc.transform(parse_file('./c_files/minic.c'))
     vs = TestVisitor()
     vs.visit(ast)
     self.assertEqual(vs.assignment_counter, 5)
     self.assertEqual(vs.forl_counter, 1)
Beispiel #7
0
 def test_visit(self):
     ast = ctoc.transform(parse_file('./c_files/minic.c'))
     vs = TestVisitor()
     vs.visit(ast)
     self.assertEqual(vs.assignment_counter, 5)
     self.assertEqual(vs.forl_counter, 1)