From: Eric S. Raymond Date: Sat, 14 Feb 2004 06:56:25 +0000 (+0000) Subject: Handle function pointers as function arguments. X-Git-Tag: 1.0~104 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=26f50f852f3097fb324c3f42e3d315159bcd614f;p=mad.git Handle function pointers as function arguments. --- diff --git a/mad b/mad index 7302d99..c0ab70d 100755 --- a/mad +++ b/mad @@ -473,7 +473,7 @@ def p_function_application(t): def p_expression_funcall0(t): 'expression : FUNCALL' var = t[1] - t[0] = ('variable', var) + t[0] = ('funptr', var[:-1]) refcount[var] = refcount.get(var, 0) + 1 def p_expression_name(t): @@ -745,10 +745,10 @@ def prinC1(t): elif t[0] in ('intnum', 'doublenum'): return t[1] elif type(t) == type(()) and t[0] == 'range': - if t[2] == ('intnum', '1'): + if t[2] == ('intnum', '0'): return print_C_expr(('variable', t[1])) else: - return print_C_expr(('+',('variable', t[1]),('-',t[2],('intnum', '1')))) + return print_C_expr(('+',('variable', t[1]), t[2])) else: return '***'+`t` @@ -865,14 +865,19 @@ def print_C_statement(statement): rep = "\n" + returntypes.get(currentfunc, normalmode) + " " + currentfunc[:-1] + "(" defaulted = [] if len(statement) > 2: - for (dummy, var) in statement[2]: - declarator = ("*" * arraydepths.get(var, 0)) + var - if var in typedict: - rep += typedict[var] + " " + declarator + ", " - del typedict[var] - else: - rep += normalmode + " " + declarator + ", " - defaulted.append(var) + for (vtype, var) in statement[2]: + if vtype == 'funptr': + # we lose if a passed-in function has a type other than the + # normal mode -- there's no way to deduce this. + rep += "%s (*%s)()" % (normalmode, var) + elif vtype == 'variable': + declarator = ("*" * arraydepths.get(var, 0)) + var + if var in typedict: + rep += typedict[var] + " " + declarator + ", " + del typedict[var] + else: + rep += normalmode + " " + declarator + ", " + defaulted.append(var) if rep.endswith(", "): rep = rep[:-2] rep = rep + ") {"