def read_fd(fname): func_pat = "([A-Za-z][_A-Za-z00-9]+)\((.*)\)\((.*)\)" func_table = None bias = 0 private = True # parse file f = open(fname, "r") for line in f: l = line.strip() if len(l) > 1 and l[0] != '*': # a command if l[0] == '#' and l[1] == '#': cmdline = l[2:] cmda = cmdline.split(" ") cmd = cmda[0] if cmd == "base": base = cmda[1] func_table = FuncTable(base) elif cmd == "bias": bias = int(cmda[1]) elif cmd == "private": private = True elif cmd == "public": private = False elif cmd == "end": break else: print "Invalid command:",cmda return None # a function else: m = re.match(func_pat, l) if m == None: raise IOError("Invalid FD Format") else: name = m.group(1) # create a function definition func_def = FuncDef(name, bias, private) if func_table != None: func_table.add_func(func_def) # check args args = m.group(2) regs = m.group(3) arg = args.replace(',','/').split('/') reg = regs.replace(',','/').split('/') if len(arg) != len(reg): # hack for double reg args found in mathieeedoub* libs if len(arg) * 2 == len(reg): arg_hi = map(lambda x: x + "_hi", arg) arg_lo = map(lambda x: x + "_lo", arg) arg = [x for pair in zip(arg_hi, arg_lo) for x in pair] else: raise IOError("Reg and Arg name mismatch in FD File") if arg[0] != '': num_args = len(arg) for i in range(num_args): func_def.add_arg(arg[i],reg[i]) bias += 6 f.close() return func_table
def func_def_parser(p): """ Parses Function Definitions ['define',[f x ...], p-body] generates BSL_Def f is bound to (lambda (x ...) exp-body) :param p: P-expression :return: FuncDefinition """ if p[0] != 'define': return False elif len(p) != 3: raise ParserError('p-expression must have length >= 3') elif isinstance(p[1], str): #Here, we know it is a constant if is_reserved(p[1]): raise ParserError('Variable cannot be a reserved word') else: # deals with (define x 3) # deals with (define x (lambda (y) y)) name = p[1] rhs = exp_parser(p[2]) return FuncDef(name, rhs) else: # deals with (define (f x ...) e) # as if it were (define f (lambda (x ...) e)) lst = parse_params(p[1]) name = lst[0] rhs = LambdaExpr (lst[1:], exp_parser(p[2])) return FuncDef(name, rhs)
def add_call(self, name, bias, arg, reg, is_std=False): if len(arg) != len(reg): raise IOError("Reg and Arg name mismatch in function definition") else: func_def = FuncDef(name, bias, False, is_std) self.add_func(func_def) if arg and len(arg) > 0: num_args = len(arg) for i in range(num_args): func_def.add_arg(arg[i], reg[i])
def add_call(self,name,bias,arg,reg,is_std=False): if len(arg) != len(reg): raise IOError("Reg and Arg name mismatch in function definition") else: func_def = FuncDef(name, bias, False, is_std) self.add_func(func_def) if arg and len(arg) > 0: num_args = len(arg) for i in range(num_args): func_def.add_arg(arg[i],reg[i])
def read_fd(fname): func_pat = "([A-Za-z][_A-Za-z00-9]+)\((.*)\)\((.*)\)" func_table = None bias = 0 private = True # parse file f = open(fname, "r") for line in f: l = line.strip() if len(l) > 1 and l[0] != '*': # a command if l[0] == '#' and l[1] == '#': cmdline = l[2:] cmda = cmdline.split(" ") cmd = cmda[0] if cmd == "base": base = cmda[1] func_table = FuncTable(base) elif cmd == "bias": bias = int(cmda[1]) elif cmd == "private": private = True elif cmd == "public": private = False elif cmd == "end": break else: print "Invalid command:", cmda return None # a function else: m = re.match(func_pat, l) if m == None: raise IOError("Invalid FD Format") else: name = m.group(1) # create a function definition func_def = FuncDef(name, bias, private) if func_table != None: func_table.add_func(func_def) # check args args = m.group(2) regs = m.group(3) arg = args.replace(',', '/').split('/') reg = regs.replace(',', '/').split('/') if len(arg) != len(reg): raise IOError("Reg and Arg name mismatch in FD File") else: if arg[0] != '': num_args = len(arg) for i in range(num_args): func_def.add_arg(arg[i], reg[i]) bias += 6 f.close() return func_table
def test_function_definition(self): assert func_def_parser\ (['define', ['add', 'x', 'y', 'z'], ['+', 1, 3]]) == \ FuncDef\ ('add', LambdaExpr(['x', 'y', 'z'], FuncApplication(Variable('+'), [Num(1), Num(3)]))) assert func_def_parser(['define', 'add', ['+', 1, 3]]) == \ FuncDef('add', FuncApplication(Variable('+'), [Num(1), Num(3)]))
class Constants: varx = Variable('x') vary = Variable('y') varz = Variable('z') varo = Variable('o') emptyList = [] list123 = [Num(1), Num(2), Num(3)] listadd123 = [FuncApplication(Variable('+'), list123), Num(-3)] list12True = [Num(1), Num(2), Boolean(True)] listaddsubtract123 = [ FuncApplication(Variable('-'), list123), FuncApplication('+', list123) ] listmultiply123 = [FuncApplication(Variable('*'), list123)] list84 = [Num(8), Num(4)] listdivide84 = [FuncApplication(Variable('/'), list84)] listx23 = [varx, Num(2), Num(3)] listy3 = [vary, Num(3)] list43 = [Num(4), Num(3)] list403 = [Num(4), Num(0), Num(3)] expradd1 = FuncApplication(Variable('+'), [Num(1)]) expradd123 = FuncApplication(Variable('+'), list123) expradd43 = FuncApplication(Variable('+'), list43) expradderror = FuncApplication(Variable('+'), list12True) exprsub1 = FuncApplication(Variable('-'), [Num(1)]) exprsub123 = FuncApplication(Variable('-'), list123) exprsub_add123_add123 = FuncApplication(Variable('-'), [expradd123, expradd43]) exprdiv1 = FuncApplication(Variable('/'), [Num(1)]) exprdiv84 = FuncApplication(Variable('/'), list84) exprdiv403 = FuncApplication(Variable('/'), list403) exprmul1 = FuncApplication(Variable('*'), [Num(1)]) exprmul84 = FuncApplication(Variable('*'), list84) expradd_expradd123_exprdiv84 = FuncApplication(Variable('+'), [expradd123, exprdiv84]) expraddx23 = FuncApplication(Variable('+'), listx23) expraddy3 = FuncApplication(Variable('+'), listy3) expsub_expraddx23_expradd3 = FuncApplication(Variable('-'), [expraddx23, expraddy3]) #functions func_def_varx = FuncDef("f", LambdaExpr(["x"], Variable("x"))) func_app_varx = FuncApplication(Variable('f'), [Num(4)]) list_func_app_varx = [func_app_varx, Num(2)] expradd_func_app_varx = FuncApplication(Variable('+'), list_func_app_varx) funcDef2 = FuncDef('g', LambdaExpr([], expradd_func_app_varx)) func_app_emptylist = FuncApplication(Variable('g'), []) ############### expradd_varx_vary = FuncApplication(Variable('+'), [varx, vary]) func_def_add_varx_vary = FuncDef('z', LambdaExpr(['x', 'y'], expradd_varx_vary)) func_app_varx_vary = FuncApplication(Variable('z'), [Num(7), Num(7)]) func_app_error_777 = FuncApplication( Variable('z'), [Num(7), Num(7), Num(7)]) func_app_100 = FuncApplication(Variable('d'), [Num(100)]) defs1 = Scope(()).add_definitions() defs1 = defs1.extend('x', Num(1)).extend('y', Num(4)) #set the scope by mutating to second arg of the tuple defs1 = func_def_varx.eval(defs1)[1] defs1 = funcDef2.eval(defs1)[1] defs1 = func_def_add_varx_vary.eval(defs1)[1] #structs posn_def = StructDef('posn', ['x', 'y']) defs1 = posn_def.update(defs1) zeina_def = StructDef('zeina', ['x', 'y']) defs1 = zeina_def.update(defs1) make_zeina = FuncApplication(Variable('make-posn'), [Num(10), Num(20)]) select_zeina_x = FuncApplication(Variable('zeina-x'), [make_zeina]) make_posn = FuncApplication(Variable('make-posn'), [Num(1), Num(2)]) make_posn_comp = FuncApplication(Variable('make-posn'), [make_posn, Num(2)]) is_posn = FuncApplication(Variable('posn?'), [make_posn]) is_not_posn = FuncApplication(Variable('posn?'), [Num(3)]) select_posn_x = FuncApplication(Variable('posn-x'), [make_posn]) select_posn_y = FuncApplication(Variable('posn-y'), [make_posn]) select_posn_x_comp = FuncApplication(Variable('posn-x'), [make_posn_comp]) select_posn_y_comp = FuncApplication(Variable('posn-y'), [make_posn_comp]) value_posn = Structure('posn', [('x', Num(1)), ('y', Num(2))]) value_posn_comp = Structure('posn', [('x', value_posn), ('y', Num(2))]) #functions using struct make_posn_func = FuncApplication(Variable('make-posn'), [func_app_varx, Num(1)]) func_app_varx_1 = FuncApplication(Variable('f'), [make_posn_func]) value_posn_func = Structure('posn', [('x', Num(4)), ('y', Num(1))]) posn_x_func_app_varx_1 = FuncApplication(Variable('posn-x'), [func_app_varx_1]) select_posn_x_error = FuncApplication(Variable('posn-x'), [Num(3)]) ex1 = '(ex*' ex_abc = 'abc' ex_1 = '1' exx1 = ')' exx2 = ex_abc + exx1 #And and1 = And([Boolean(True), Boolean(False)]) and2 = And( [Boolean(False), FuncApplication(Variable('/'), [Num(1), Num(0)])]) and3 = And( [Boolean(True), FuncApplication(Variable('/'), [Num(1), Num(1)])]) #equals equals34 = FuncApplication(Variable('='), [Num(3), Num(4)]) equals33 = FuncApplication(Variable('='), [Num(3), Num(3)]) equals_3_true = FuncApplication(Variable('='), [Num(3), Boolean(False)]) #bigger and less than biggerthan34 = FuncApplication(Variable('>'), [Num(3), Num(4)]) lessthan34 = FuncApplication(Variable('<'), [Num(3), Num(4)]) lessthan_error = FuncApplication(Variable('>'), [Variable('xyz'), Num(4)]) #if. if_1 = If([equals34, Num(3), Num(4)]) if_2 = If([equals33, Num(3), Num(4)])
def read_fd(fname): func_pat = "([A-Za-z][_A-Za-z00-9]+)\((.*)\)\((.*)\)" func_table = None bias = 0 private = True # parse file f = open(fname, "r") for line in f: l = line.strip() if len(l) > 1 and l[0] != '*': # a command if l[0] == '#' and l[1] == '#': cmdline = l[2:] cmda = cmdline.split(" ") cmd = cmda[0] if cmd == "base": base = cmda[1] func_table = FuncTable(base) elif cmd == "bias": bias = int(cmda[1]) elif cmd == "private": private = True elif cmd == "public": private = False elif cmd == "end": break else: print "Invalid command:", cmda return None # a function else: m = re.match(func_pat, l) if m == None: raise IOError("Invalid FD Format") else: name = m.group(1) # create a function definition func_def = FuncDef(name, bias, private) if func_table != None: func_table.add_func(func_def) # check args args = m.group(2) regs = m.group(3) arg = args.replace(',', '/').split('/') reg = regs.replace(',', '/').split('/') if len(arg) != len(reg): # hack for double reg args found in mathieeedoub* libs if len(arg) * 2 == len(reg): arg_hi = map(lambda x: x + "_hi", arg) arg_lo = map(lambda x: x + "_lo", arg) arg = [ x for pair in zip(arg_hi, arg_lo) for x in pair ] elif name == "IEEEDPSincos": # selco ugly hack for IEEEDPSincos() (Parameters is 1 + 2 registers) arg = ["fp2", "parm_hi", "parm_lo"] else: raise IOError( "Reg and Arg name mismatch in FD File") if arg[0] != '': num_args = len(arg) for i in range(num_args): func_def.add_arg(arg[i], reg[i]) bias += 6 f.close() return func_table