Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def parseFunctionText(s):
def parseFunction(s, linenumber):
parsed = re.search(functionRegex, s)
name, args, text = parsed.group(1), parsed.group(2).split(","), parsed.group(3)
if verbose: print '<parser:' + str(linenumber) + '> parsed function ' + name + ' -> ' + str(args)
if verbose: print('<parser:' + str(linenumber) + '> parsed function ' + name + ' -> ' + str(args))
functions[name] = args, parseFunctionText(text)

def parseDefinition(s, linenumber):
parsed = re.search(definitionRegex, s)
name, value = parsed.group(1), parsed.group(2)
if verbose: print '<parser:' + str(linenumber) + '> parsed definition ' + name + ' -> ' + value
if verbose: print('<parser:' + str(linenumber) + '> parsed definition ' + name + ' -> ' + value)
definitions[name] = value

def parseFunctionCall(s):
Expand Down Expand Up @@ -57,13 +57,12 @@ def transformFile(inputFilename):

def preprocessLine(s, linenumber):
# if, else, endif block
global preprocessorState
global verbose
global preprocessorState, verbose
modified = preprocessorState
if re.match(ifDefRegex, s) != None or re.match(ifNDefRegex, s) != None:
invert = re.match(ifNDefRegex, s) != None
if preprocessorState != 'initial':
print str(linenumber) + ': nested #ifdef/#ifndef is not supported!'
print (str(linenumber) + ': nested #ifdef/#ifndef is not supported!')
sys.exit(-1)
keyword = re.search((ifNDefRegex if invert else ifDefRegex), s).group(1)
if keyword in definitions:
Expand All @@ -76,17 +75,17 @@ def preprocessLine(s, linenumber):
elif preprocessorState == 'ifdef_true':
preprocessorState = 'else_false'
else:
print str(linenumber) + ': invalid #else statement'
print(str(linenumber) + ': invalid #else statement')
sys.exit(-1)
return None
elif re.match(endifDefRegex, s) != None:
if preprocessorState == 'initial':
print 'invalid #endif statement at line ' + str(linenumber)
print('invalid #endif statement at line ' + str(linenumber))
sys.exit(-1)
preprocessorState = 'initial'

if preprocessorState != modified:
if verbose: print '<pre:' + str(linenumber) + '> ' + preprocessorState + ': \t' + s.rstrip()
if verbose: print('<pre:' + str(linenumber) + '> ' + preprocessorState + ': \t' + s.rstrip())
return None

if re.match(includeRegex, s) != None:
Expand All @@ -99,18 +98,18 @@ def preprocessLine(s, linenumber):
return None

# definition expansion
for name, value in definitions.items():
for name, value in list(definitions.items()):
if name in s:
s = s.replace(name, value)

# function call expansion
if isFunctionCall(s):
linename, lineargs = parseFunctionCall(s)
if linename in functions:
if verbose: print '<pre:' + str(linenumber) + '> matched ' + linename + str(lineargs)
if verbose: print('<pre:' + str(linenumber) + '> matched ' + linename + str(lineargs))
args, text = functions[linename]
if len(args) != len(lineargs):
print 'error: call to ' + linename + ' needs ' + str(len(args)) + ' arguments'
print('error: call to ' + linename + ' needs ' + str(len(args)) + ' arguments')
sys.exit(-1)
for i, a in enumerate(args):
text = text.replace(a, lineargs[i])
Expand Down
8 changes: 4 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ def checkFile(A, B):

files = [x for x in os.listdir(subdirectory) if not x.endswith(expectedSuffix)]
if len(files) == 0:
print 'no tests found'
print('no tests found')

for i, filename in enumerate(files):
pair = (subdirectory + filename, subdirectory + filename + outputSuffix)
preprocessor.preprocessFile(*pair)
checkPair = (subdirectory + filename + outputSuffix, subdirectory + filename + expectedSuffix)
if checkFile(*checkPair) != True:
print 'check failed for ' + str(checkPair)
print(('check failed for ' + str(checkPair)))
sys.exit(-1)
print 'checked ' + str(i + 1) + ' of ' + str(len(files)) + ': ' + str(pair[0])
print(('checked ' + str(i + 1) + ' of ' + str(len(files)) + ': ' + str(pair[0])))

print 'all checks ok'
print('all checks ok')

for p in [x for x in os.listdir(subdirectory) if x.endswith('.p')]:
os.remove(subdirectory + p)
4 changes: 2 additions & 2 deletions tpypp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
pass_verbose = False

if not 2 <= len(sys.argv) <= 4:
print helpstring
print(helpstring)
sys.exit(-1)
if len(sys.argv) == 3 and sys.argv[1] == sys.argv[2]:
print 'input file cannot be same as output file'
print('input file cannot be same as output file')
sys.exit(-1)
if (len(sys.argv) == 3 and sys.argv[2] == '--v') or (len(sys.argv) == 4 and sys.argv[3] == '--v'):
pass_verbose = True
Expand Down