Created
September 25, 2018 21:15
-
-
Save kilon/3c1b2d746adb3fc4e1bed3eec60c43e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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