From 366740ee83c12e7b28ea1c18627c9086ccc927b3 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Tue, 10 Feb 2004 19:11:28 +0000 Subject: [PATCH] Next try to fix scope closing. --- mad | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/mad b/mad index 84a0158..280aec2 100755 --- a/mad +++ b/mad @@ -108,7 +108,6 @@ refcount = {} sourcefile = "" # Current source file for error messages sourceline = 0 # Current source line for error messages deck = [] # List of tuples generated by parsing -scopes = [] # THROUGH scopes that are pending targets = {} # Dictionary of targets of TRANSFERs headers = [] # Required .h file basenames funcnames = [] # List of function names @@ -370,7 +369,7 @@ def p_expression_binop2(t): global headers op = t[2][1:-1].lower() t[0] = (op, t[1], t[3]) - if op == 'pow' and "math" not in headers: + if op == 'p' and "math" not in headers: headers.append("math") def p_expression_not(t): @@ -693,6 +692,7 @@ def print_expr(t): def fmt_convert(str, write=True): "Convert MAD format string to C printf/scanf" #print "->fmt_convert(%s)" % str + fmt = str res = '' types = [] while True: @@ -777,7 +777,7 @@ def print_statement(statement): elif statement[0] == 'continue': return "/* continue */;" elif statement[0] in ('end_program', 'end_function', 'end_conditional'): - return "}" + return "} /* %s */" % statement[0] elif statement[0] == 'entry': currentfunc = statement[1] types = typedict[currentfunc] @@ -810,18 +810,16 @@ def print_statement(statement): return "\n" + print_expr(('apply', statement[1], statement[2])) + ";" elif statement[0] == 'for': (keyword, label, dummy, start, increment, predicate) = statement - return 'for (%s = %s; %s; %s += %s) {' % (dummy, print_expr(start), print_expr(predicate), dummy, print_expr(increment)) + return 'for (%s = %s; %s; %s += %s) { /* label %s */ ' % (dummy, print_expr(start), print_expr(predicate), dummy, print_expr(increment), label) elif statement[0] == 'goto': return 'goto ' + statement[1] + ';' elif statement[0] == 'if': - scopes.append(statement[1]) return 'if (' + print_expr(statement[1]) + ') ' + print_statement(statement[2]) elif statement[0] == 'internal': return '%s %s {return %s;}' % \ (returntypes.get(statement[1], normalmode), print_expr(('apply', statement[1], statement[2])), print_expr(statement[3])) elif statement[0] == 'map': global dummycount - scopes.append(statement[1]) count = len(statement[3]) dummy = "d_%d" % dummycount dummycount += 1 @@ -876,7 +874,9 @@ def print_statement(statement): def generate(out): out.write("/* generated by the MAD compiler */\n") for header in headers: - out.write('#include <%s.h>\n\n' % header) + out.write('#include <%s.h>\n' % header) + if headers: + out.write("\n") needbool = False for funvars in typedict.values(): if 'bool' in funvars.values(): @@ -889,9 +889,12 @@ def generate(out): indent = 0 global sourceline + scopes = [] # THROUGH scopes that are pending for (file, sourceline, label, statement) in deck: if label in targets: out.write(" " + label+':\n') + if statement[0] == 'for': + scopes.append(statement[1]) rep = print_statement(statement) if rep: indentchange = rep.count('{') - rep.count('}') @@ -902,7 +905,7 @@ def generate(out): indent += indentchange while scopes and label == scopes[-1]: indent -= 1 - out.write((' '*indent) + '}\n') + out.write((' '*indent) + '} /* closing ' + label + '*/\n') scopes.pop() if __name__ == '__main__': -- 2.43.0