Skip to content

Instantly share code, notes, and snippets.

@kilon
Created September 25, 2018 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kilon/3c1b2d746adb3fc4e1bed3eec60c43e7 to your computer and use it in GitHub Desktop.
Save kilon/3c1b2d746adb3fc4e1bed3eec60c43e7 to your computer and use it in GitHub Desktop.
import re
import sys
def talos_lex_source_lines(source):
return re.split("\n",source)
def talos_lex_source_tokens(source_line):
return re.split("\s",source_line)
def convert_to_c(tokens):
c_source_line = ""
for token in tokens:
if token == "int":
c_source_line= c_source_line + token + " "
continue
if re.match(".*:",token):
c_source_line= c_source_line + "(" + " "
continue
if re.match(".*[^:]",token):
c_source_line= c_source_line + token + " "
continue
c_source_line = c_source_line + "\n"
return c_source_line
if __name__ == '__main__':
filename = sys.argv[1]
file = open(filename)
source = file.read()
source_lines = talos_lex_source_lines(source)
print("\nBeginning parsing source file: {}".format(filename))
print("....................................")
source_line_number = 0
for source_line in source_lines:
source_line_number = source_line_number + 1
if source_line is not "":
tokens = talos_lex_source_tokens(source_line)
print("{} : {}".format(source_line_number,source_line))
print("line {} has tokens: {}".format(source_line_number,str(tokens)))
print("converted to c source code: {}".format(convert_to_c(tokens)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment