]> thyrsus.com Git - mad.git/commitdiff
A step towards proper array handling.
authorEric S. Raymond <esr@thyrsus,com>
Thu, 19 Feb 2004 08:02:03 +0000 (08:02 +0000)
committerEric S. Raymond <esr@thyrsus,com>
Thu, 19 Feb 2004 08:02:03 +0000 (08:02 +0000)
Next, fully implement the format language.

mad

diff --git a/mad b/mad
index 5585677d1a102b6bd67e1c4641a26f4c523219f4..04fb71e35e954e0ffb482e54d8f0c6e6e26a58eb 100755 (executable)
--- 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: