I am creating a programming language. For this language, I am creating a program that compiles it into Python. I don't need a lexer, because most of the grammar can be converted into Python with regexes.
Here's what I have so far:
import re
infile = input()
output = open(infile + ".py","w")
input = open(infile + ".hlx")
# I'm aware the .hlx extension is already taken, but it doesn't really matter.
for line in input:
output.write(re.sub(r'function (\S+) (\S+) =', r'def \1(\2):', line))
for line in input:
output.write(re.sub(r'print(.+)', r'print(\1)', line))
for line in input:
output.write(re.sub(r'call (\S+) (\S+)', r'\1(\2)', line))
# More regexes go here, eventually.
input.close()
output.close()
I had to put each regex in a separate for statement because if I put them together, it would replace each line 3 times.
The problem here is that it only performs one of the regexes, which is the first one. The order doesn't really matter here, but I still need the program to perform all of the regexes. How would I do this?
By the way, here's the code I want to replace in my language:
function hello input =
print "Hello, ", input, "!"
hello "world"
And here's the code I want to replace it with in Python:
def hello(input):
print("Hello, " + input + "!")
hello("world")
seek()to the beginning of the file. Also, why don't you assign the output of eachre.subcall to a variable so you can call eachre.subon the same line before you have to write it.