]> thyrsus.com Git - mad.git/commitdiff
Implement screwy MAD array indexing.
authorEric S. Raymond <esr@thyrsus,com>
Thu, 19 Feb 2004 23:38:39 +0000 (23:38 +0000)
committerEric S. Raymond <esr@thyrsus,com>
Thu, 19 Feb 2004 23:38:39 +0000 (23:38 +0000)
mad

diff --git a/mad b/mad
index 0a6d7f45808ba7d5fb6354cb6936bd64a8a97674..f9a633f5ff29c3cd54945ffd0ffbf50b40b55a93 100755 (executable)
--- a/mad
+++ b/mad
@@ -736,7 +736,10 @@ def prinC1(t):
             else:
                 # More than one subscript; this is where we implement MAD's
                 # weird reliance on dope vectors.
-                subscript = 'MAD' + `len(t[2])` + '(' + dimensions[t[1]][1] + "," + ','.join(map(print_C_expr, t[2])) + ')'
+                ptr = dimensions[t[1]][1]
+                if len(dimensions[t[1]]) > 2:
+                    ptr = ptr + "+" + dimensions[t[1]][2]
+                subscript = 'MAD' + `len(t[2])` + '(' + ptr + "," + ','.join(map(print_C_expr, t[2])) + ')'
             res += '[' + subscript + ']'
         return res
     elif t[0] == 'funptr':
@@ -948,7 +951,7 @@ def print_C_statement(statement):
         return "\n" + print_C_expr(('apply', statement[1], statement[2])) + ";"
     elif statement[0] == 'for':
         (keyword, label, dummy, start, increment, predicate) = statement
-        rep = 'for (int %s = %s; %s; %s += %s) {' % (dummy, print_C_expr(start), print_C_expr(predicate), dummy, print_C_expr(increment))
+        rep = 'for (int %s = %s; !(%s); %s += %s) {' % (dummy, print_C_expr(start), print_C_expr(predicate), dummy, print_C_expr(increment))
         if mad_debug:
             rep += ' /* label %s */' % label
         return rep
@@ -1033,11 +1036,32 @@ def generate_C_code(out):
     out.write("/* generated by the MAD compiler */\n")
     for header in headers:
         out.write('#include <%s.h>\n' % header)
-    if headers:
-        out.write("\n")
+        if headers:
+            out.write("\n")
     if 'bool' in typedict.values():
         out.write("typedef int bool;\n")
 
+    # Generate indexing macros for MAD's screwy dope vectors
+    depths = []
+    for d in arraydepths.values():
+        if d not in depths:
+            depths.append(d)
+    for i in depths:
+        out.write("#define MAD%d(d, "% i)
+        out.write(", ".join(map(lambda x: "i%d" % (x+1), range(i))))
+        out.write(")   d[1] + ")
+        term = ""
+        for j in range(i):
+            term += "("
+            for k in range(i-j-1):
+                term += "d[%d]*" % (k+2)
+            if j < i-1:
+                term +="*"
+            term = term[:-1] + "((i%d)-1))" % (j+1)
+            term += " + "
+        term = term[:-3]
+        out.write(term + "\n")
+
     for (id, value) in static.items():
         if typedict[id] == 'char':
             out.write('static char *%s\t= "%s";\n' % (id,fmt_convert(value)))