diff options
Diffstat (limited to 'filetypes/makefilecom.py')
-rw-r--r-- | filetypes/makefilecom.py | 124 |
1 files changed, 113 insertions, 11 deletions
diff --git a/filetypes/makefilecom.py b/filetypes/makefilecom.py index 6bfd29b..e361f8c 100644 --- a/filetypes/makefilecom.py +++ b/filetypes/makefilecom.py @@ -1,10 +1,22 @@ from ply import lex from ply import yacc -def com_interp(string): +def expand(lst,variables): + newlst = [] + for item in lst: + if isinstance(item, list): + strlst = com_interp(item[0],variables) + netlst += expand(strlst,variables) + else: + newlst.append(item) + + return newlst + +def com_interp(string,variables): tokens = ( "COMMAND", "COMMA", + "COL", "EQ", "TEXT", "PERCENT", @@ -51,6 +63,10 @@ def com_interp(string): r"," return t + def t_COL(t): + r":" + return t + def t_TEXT(t): r"[^ \n\t:=\\,]+" return t @@ -69,14 +85,100 @@ def com_interp(string): #for tok in lexer: # print(tok) - tokens = 0 + tokens = [] for tok in lexer: - tokens += 1 - print("gethere") - - print(tokens) - if tokens == 1: - print("gapp") - - -com_interp("HELOO") + tokens += [tok.value] + + if len(tokens) == 1: + if tokens[0] in variables: + return variables[tokens[0]] + else: + return [] + + #YACC stuff begins here + + def p_tonewstr(p): + """ + newstr : getstr EQ TEXT PERCENT TEXT + | getstr EQ PERCENT TEXT + | getstr EQ TEXT PERCENT + | getstr EQ PERCENT + | getstr EQ TEXT + """ + newtextlist = [] + if p[1] == []: + p[0] = p[1] + elif len(p) == 6: + for text in p[1]: + newtextlist.append(p[3] + text + p[5]) + p[0] = newtextlist + + elif len(p) == 5: + if p[3] == "%": + for text in p[1]: + newtextlist.append(text + p[4]) + p[0] = newtextlist + else: + for text in p[1]: + newtextlist.append(p[3] + text) + p[0] = newtextlist + + elif p[3] == "%": + p[0] = p[1] + else: + for text in p[1]: + newtextlist.append(text + p[3]) + p[0] = newtextlist + + + def p_getstr(p): + """ + getstr : TEXT COL TEXT PERCENT TEXT + | TEXT COL PERCENT TEXT + | TEXT COL TEXT PERCENT + | TEXT COL PERCENT + | TEXT COL TEXT + """ + if not p[1] in variables: + p[0] = [] + else: + textlst = expand(variables[p[1]]): #make sure it's expanded + newtextlst = [] + + if len(p) == 6: + l1 = len(p[3]) #length of str1 + l2 = len(p[5]) + for text in textlst + if p[3] == text[0:l1] and p[5] == text[-l2:]: + newtextlst.append(text[l1:-l2]) + + p[0] = newtextlst + + elif len(p) == 5: + if p[3] == "%": + l1 = len(p[4]) + for text in textlst + if p[4] == text[-l1:] + newtextlst.append(text[:-l1]) + + p[0] = newtextlst + else: + l1 = len(p[3]) + for text in textlst + if p[3] == text[0:l1] + newtextlst.append(text[l1:]) + + p[0] = newtextlst + elif p[3] == "%": + p[0] = textlst + else: + l1 = len(p[3]) + for text in textlst + if p[3] == text[-l1:] + newtextlst.append(text[:-l1]) + + p[0] = newtextlst + + + +com_interp("HELOO",{"HELOO":["mupp"]}) |