I am working on a fun little language using regex matching lines in a file. Here is what I have so far:
import re
code=open("code.txt", "r").read()
outputf=r'output (.*)'
inputf=r'(.*) = input (.*)'
intf=r'int (.*) = (\d)'
floatf=r'float (.*) = (\d\.\d)'
outputq=re.match(outputf, code)
if outputq:
print "Executing OUTPUT query"
exec "print %s" %outputq.group(1)
inputq=re.match(inputf, code)
if inputq:
print "Executing INPUT query"
exec "%s=raw_input(%s)"%(inputq.group(1), inputq.group(2))
intq=re.match(intf, code)
if intq:
exec "%s = %s"%(intq.group(1), intq.group(2))
exec "print %s"%(intq.group(1))
else:
print "Invalid syntax"
The code works in matching say:
int x = 1
But it will only match the first line and stop matching and ignore the rest of the code that I want to match. How can I match every line in the file to my regex definitions?