From c3771eac8856639a5be02e7cab83da4cba68e023 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 19 Feb 2004 08:02:03 +0000 Subject: [PATCH] A step towards proper array handling. Next, fully implement the format language. --- mad | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/mad b/mad index 5585677..04fb71e 100755 --- a/mad +++ b/mad @@ -372,9 +372,12 @@ def p_whenever_or(t): t[0] = ('or', t[3]) # VECTOR VALUES -def p_vector_values(t): +def p_vector_values_string(t): 'statement : VECTOR VALUES NAME EQUALS STRING' t[0] = ('values',) + (t[3], t[5]) +def p_vector_values_array(t): + 'statement : VECTOR VALUES NAME EQUALS arglist' + t[0] = ('values',) + (t[3], t[5]) ## Assignment def p_statement_substitution1(t): @@ -708,11 +711,18 @@ def mode(t): sys.exit(1) def prinC1(t): + "Recursive print of expression value" if t[0] == 'variable': res = t[1] if len(t) > 2: - for dim in t[2]: - res += '[' + print_C_expr(dim) + ']' + if len(t[2]) == 1: + # One subscript, the ordinary linear-addressing case. + subscript = print_C_expr(t[2][0]) + else: + # More than one subscript; this is where we implement MAD's + # weird reliance on dope vectors. + raise IndexError + res += '[' + subscript + ']' return res elif t[0] == 'funptr': return t[1] @@ -961,6 +971,7 @@ def print_C_statement(statement): return 'printf("' + fmt + '", ' + ', '.join(map(print_C_expr, statement[2:])) + ');' elif statement[0] == 'read': (fmt, types) = fmt_convert(format[statement[1]]) + print "Foo!", (fmt, types) inlist = [] for (ispec, itype) in zip(statement[2:], types): if type(ispec) == type(""): @@ -968,7 +979,7 @@ def print_C_statement(statement): elif type(ispec)==type(()) and ispec[0]=='range' and itype=='char': inlist.append(print_C_expr(ispec)) else: - nonfatal("unsupported type in READ FORMAT") + nonfatal("unsupported type in READ FORMAT:" + `ispec`) return 'scanf("' + fmt + '", ' + ', '.join(inlist) + ');' elif statement[0] == 'remark': if len(statement) == 1: @@ -1014,11 +1025,18 @@ def generate_C_code(out): out.write("typedef int bool;\n") for (id, value) in static.items(): - out.write('static const char *%s\t= "%s";\n' % (id,fmt_convert(value))) + if typedict[id] == 'char': + out.write('static char *%s\t= "%s";\n' % (id,fmt_convert(value))) + elif typedict[id] == 'int': + out.write('static int %s\t= {%s};\n' % (id, ", ".join(map(lambda x: x[1], value)))) + else: + sys.stderr.write("mad: static %s has unknown type\n") + sys.exit(1) declarations = "" for vtype in ('double', 'int', 'bool', 'char'): vars = filter(lambda x: typedict[x] == vtype, typedict.keys()) + vars = filter(lambda x: x not in static, vars) if vars: declarations += vtype for var in vars: -- 2.43.0