sourcefile = "" # Current source file for error messages
sourceline = 0 # Current source line for error messages
deck = [] # List of tuples generated by parsing
-scopes = [] # THROUGH scopes that are pending
targets = {} # Dictionary of targets of TRANSFERs
headers = [] # Required .h file basenames
funcnames = [] # List of function names
global headers
op = t[2][1:-1].lower()
t[0] = (op, t[1], t[3])
- if op == 'pow' and "math" not in headers:
+ if op == 'p' and "math" not in headers:
headers.append("math")
def p_expression_not(t):
def fmt_convert(str, write=True):
"Convert MAD format string to C printf/scanf"
#print "->fmt_convert(%s)" % str
+ fmt = str
res = ''
types = []
while True:
elif statement[0] == 'continue':
return "/* continue */;"
elif statement[0] in ('end_program', 'end_function', 'end_conditional'):
- return "}"
+ return "} /* %s */" % statement[0]
elif statement[0] == 'entry':
currentfunc = statement[1]
types = typedict[currentfunc]
return "\n" + print_expr(('apply', statement[1], statement[2])) + ";"
elif statement[0] == 'for':
(keyword, label, dummy, start, increment, predicate) = statement
- return 'for (%s = %s; %s; %s += %s) {' % (dummy, print_expr(start), print_expr(predicate), dummy, print_expr(increment))
+ return 'for (%s = %s; %s; %s += %s) { /* label %s */ ' % (dummy, print_expr(start), print_expr(predicate), dummy, print_expr(increment), label)
elif statement[0] == 'goto':
return 'goto ' + statement[1] + ';'
elif statement[0] == 'if':
- scopes.append(statement[1])
return 'if (' + print_expr(statement[1]) + ') ' + print_statement(statement[2])
elif statement[0] == 'internal':
return '%s %s {return %s;}' % \
(returntypes.get(statement[1], normalmode), print_expr(('apply', statement[1], statement[2])), print_expr(statement[3]))
elif statement[0] == 'map':
global dummycount
- scopes.append(statement[1])
count = len(statement[3])
dummy = "d_%d" % dummycount
dummycount += 1
def generate(out):
out.write("/* generated by the MAD compiler */\n")
for header in headers:
- out.write('#include <%s.h>\n\n' % header)
+ out.write('#include <%s.h>\n' % header)
+ if headers:
+ out.write("\n")
needbool = False
for funvars in typedict.values():
if 'bool' in funvars.values():
indent = 0
global sourceline
+ scopes = [] # THROUGH scopes that are pending
for (file, sourceline, label, statement) in deck:
if label in targets:
out.write(" " + label+':\n')
+ if statement[0] == 'for':
+ scopes.append(statement[1])
rep = print_statement(statement)
if rep:
indentchange = rep.count('{') - rep.count('}')
indent += indentchange
while scopes and label == scopes[-1]:
indent -= 1
- out.write((' '*indent) + '}\n')
+ out.write((' '*indent) + '} /* closing ' + label + '*/\n')
scopes.pop()
if __name__ == '__main__':