]> thyrsus.com Git - mad.git/commitdiff
Implemented NORMAL MODE IS.
authorEric S. Raymond <esr@thyrsus,com>
Mon, 9 Feb 2004 19:46:21 +0000 (19:46 +0000)
committerEric S. Raymond <esr@thyrsus,com>
Mon, 9 Feb 2004 19:46:21 +0000 (19:46 +0000)
mad

diff --git a/mad b/mad
index 25b33e7573f9621080475007e11a8a70549f394e..7be96cd079e413badd80b2f810e20bb0d67d7ff5 100755 (executable)
--- a/mad
+++ b/mad
@@ -2,9 +2,10 @@
 
 reserved = ('BOOLEAN', 'CONDITIONAL', 'CONTINUE', 'DIMENSION', 'END',
             'ENTRY', 'EXECUTE', 'EXTERNAL', 'FOR', 'FORMAT',
-            'FUNCTION', 'INTEGER', 'INTERNAL', 'OF', 'PAUSE', 'PRINT',
-            'PROGRAM', 'READ', 'RETURN', 'THROUGH', 'TO', 'TRANSFER',
-            'VALUES', 'VECTOR', 'WHENEVER',
+            'FUNCTION', 'INTEGER', 'INTERNAL', 'IS', 'MODE',
+            'NORMAL', 'OF', 'PAUSE', 'PRINT', 'PROGRAM', 'READ',
+            'RETURN', 'THROUGH', 'TO', 'TRANSFER', 'VALUES', 'VECTOR',
+            'WHENEVER',
             # This is a pseudo-keyword generated early in compilation.
             'COMMENT')
 
@@ -110,6 +111,7 @@ funcnames = []
 typedict = {}
 arraydepths = {}
 returntypes = {}
+normalmode = 'double'
 
 def initfunc(f):
     global currentfunc
@@ -229,6 +231,17 @@ def p_internal(t):
     t[0] = ('internal', t[3], tuple(t[4]), t[6])
     funcnames.append(t[3])
 
+## NORMAL MODE IS
+def p_normal(t):
+    "statement : NORMAL MODE IS typeval"
+    t[0] = ('normal', t[1])
+def p_typeval_int(t):
+    "typeval : INTEGER"
+    t[0] = 'int'
+def p_typeval_bool(t):
+    "typeval : BOOLEAN"
+    t[0] = 'bool'
+
 ## PAUSE
 def p_pause(t):
     "statement : PAUSE expression"
@@ -456,6 +469,9 @@ def analyze():
         elif statement[0] == 'integer':
             for var in statement[1:]:
                 typedict[currentfunc][var] = 'int'
+        elif statement[0] == 'normal':
+            global normalmode
+            normalmode = statement[1]
         elif statement[0] == 'values':
             var = statement[1]
             if var in refcount:
@@ -484,7 +500,7 @@ def analyze():
                     typedict[currentfunc][var] = type
     # Type inference loop.  We initialized for this by deducing types
     # from READ FORMAT statements. INTEGER, and BOOLEAN declarations
-    # above.  Now propagate int and bool type markers through
+    # above.  Now propagate markers of non-normalmode type through
     # expression evaluation until we can't deduce any more int or bool
     # type bindings for variables.  Everything else just defaults to
     # double.
@@ -502,13 +518,13 @@ def analyze():
                 # different modes.
                 m = mode(statement[1])
                 returntypes[currentfunc] = m
-                if m in ('int','bool') and returntypes[currentfunc] != m:
+                if m != normalmode and returntypes[currentfunc] != m:
                     deductions += 1
             elif statement[0] == 'substitution':
                 # Propagate type markers through assignment
                 m = mode(statement[2])
                 typedict[currentfunc][statement[1]] = m
-                if m in ('int','bool') and typedict[currentfunc][statement[1]]!=m:
+                if m != normalmode and typedict[currentfunc][statement[1]]!=m:
                     deductions += 1
         if deductions == 0:
             break
@@ -532,7 +548,7 @@ def mode(t):
     #print "->mode", t
     if t[0] == 'variable':
         #print "<-mode of ", t[1]
-        return typedict[currentfunc].get(t[1], 'double')
+        return typedict[currentfunc].get(t[1], normalmode)
     elif t[0] in ('UMINUS', 'UPLUS', 'POW', 'ABS'):
         #print "<-mode of ", t[1]
         return mode(t[1])
@@ -552,7 +568,7 @@ def mode(t):
         #print "<-mode of string ", t[1]
         return 'int'
     elif t[0] == 'apply':
-        return returntypes.get(t[1], "double")
+        return returntypes.get(t[1], normalmode)
     elif t[0] in logops:
         #print "<-bool (logical operator)", t[1]
         return 'bool'
@@ -699,6 +715,7 @@ def print_statement(statement):
                         'dimension',
                         'external_function',
                         'integer',
+                        'normal',
                         'values'):
         pass
     elif statement[0] == 'continue':
@@ -709,7 +726,7 @@ def print_statement(statement):
         func = statement[1]
         types = typedict[func]
         depths = arraydepths[currentfunc]
-        rep = "\n" + returntypes.get(func, "double") + " " + func[:-1] + "("
+        rep = "\n" + returntypes.get(func, normalmode) + " " + func[:-1] + "("
         defaulted = []
         if statement[2]:
             for (dummy, var) in statement[2]:
@@ -718,7 +735,7 @@ def print_statement(statement):
                     rep += types[var] + " " + declarator + ", "
                     del types[var]
                 else:
-                    rep += "double " + declarator + ", "
+                    rep += normalmode + " " + declarator + ", "
                     defaulted.append(var)
             rep = rep[:-2]
         rep  = rep + ") {\n"
@@ -745,7 +762,7 @@ def print_statement(statement):
         return 'if (' + print_expr(statement[1]) + ') ' + print_statement(statement[2])
     elif statement[0] == 'internal':
         return '    %s %s {return %s;}' % \
-                      (returntypes.get(statement[1], "double"), print_expr(('apply', statement[1], statement[2])), print_expr(statement[3]))
+                      (returntypes.get(statement[1], normalmode), print_expr(('apply', statement[1], statement[2])), print_expr(statement[3]))
     elif statement[0] == 'pause':
         return('printf("%o", ' + print_expr(statement[1]) + '); getchar();')
     elif statement[0] == 'read':