Skip to content

Instantly share code, notes, and snippets.

@kilon kilon/talos.py
Created Sep 25, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.