]> thyrsus.com Git - mad.git/commitdiff
Only include math.h conditionally.
authorEric S. Raymond <esr@thyrsus,com>
Fri, 30 Jan 2004 22:31:50 +0000 (22:31 +0000)
committerEric S. Raymond <esr@thyrsus,com>
Fri, 30 Jan 2004 22:31:50 +0000 (22:31 +0000)
mad

diff --git a/mad b/mad
index 5b7aa7497830ac824a9e6597604101fec5eda22c..88139700bae8e448298fca1aa07a20d681cc355d 100755 (executable)
--- a/mad
+++ b/mad
@@ -105,6 +105,7 @@ deck = []
 scopes = []
 targets = {}
 untyped = []
+headers = []
 
 # Program level
 
@@ -296,7 +297,11 @@ def p_expression_binop2(t):
                   | expression NE expression
                   | expression POW expression
                   '''
-    t[0] = (t[2][1:-1].lower(), t[1], t[3])
+    global headers
+    op = t[2][1:-1].lower()
+    t[0] = (op, t[1], t[3])
+    if op == 'pow':
+        headers.append("math")
 
 def p_expression_not(t):
     'expression : NOT expression %prec NOT'
@@ -335,6 +340,9 @@ def p_expression_subscript(t):
 def p_function_application(t):
     'expression : FUNCALL application'
     t[0] = ('apply', t[1], tuple(t[2]))
+    global headers
+    if t[1] in ('elog.', 'sqrt.'):
+        headers.append("math")
 
 def p_expression_application(t):
     'application : LPAREN arglist RPAREN'
@@ -381,6 +389,10 @@ def declare(var, lst):
         untyped.remove(var)
     lst.append(var)
 
+def croak(t, msg):
+    (file, line, label, statement) = t
+    sys.stderr.write('"%s", line %d: %s\n' % (file, line, msg))
+
 def analyze():
     "Gather variable types and determine function boundaries."
     global integers, floats, bools, deck
@@ -419,12 +431,16 @@ def analyze():
         if statement[0] == 'entry' and type(statement[1]) == type(""):
             name = statement[1]
             for j in range(i):
-                if deck[i-j-1][3][0] == 'external_function':
-                    deck[i] = (file, line, label,
-                               ('entry', ('apply', name, deck[i-j-1][3][1])))
+                prev = deck[i-j-1][3]
+                if prev[0] == 'external_function':
+                    deck[i] = (file, line, label, ('entry',
+                                                   ('apply', name, prev[1])))
                     break
+                elif prev[0] == 'entry':
+                    croak(prev, "multiple ENTRY points are not supported")
             else:
-                sys.stderr.write('"%s", line %d: no EXTERNAL declaration matches this ENTRY\n' % (file, line))
+                croak(deck[i], 'no EXTERNAL declaration matches this ENTRY')
+                
 
 def prin1(t):
     if t[0] == 'variable':
@@ -600,7 +616,9 @@ def print_statement(statement):
         return('// ' + `statement`)
 
 def generate(out):
-    out.write('#include <math.h>\n\n')
+    out.write("/* generated by the MAD compiler */\n\n")
+    if headers:
+        out.write('#include <math.h>\n\n')
 
     if integers:
         out.write('int ' + ', '.join(integers) + ';\t/* INTEGER */\n')
@@ -682,6 +700,7 @@ if __name__ == '__main__':
 
     # Perform global analysis
     analyze()
+    print "Baz!", headers
 
     if (dump_tuples):
         for t in deck: