-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
81 lines (79 loc) · 2.41 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from util import utilStr
from stacktrace import error
from String import concatenation
from Type import determine
from Integer import maths
import shlex
def parse(raw):
rawList = shlex.split(raw, posix=False)
varDict = {}
i = 0
while (i < len(rawList)):
curr = rawList[i]
if (utilStr.equals_caseless(curr,"say")):
temp = concatenation.on(rawList,(i + 1),varDict)
print(temp[0])
i = temp[1]
elif (utilStr.equals_caseless(curr,"comment")):
try:
i = rawList.index("\\n",i) - 1
except:
i = len(rawList)
elif (utilStr.equals_caseless(curr,"block-comment")):
j = i + 1
if (not utilStr.equals_caseless(rawList[j],"is")):
error.missingIs();
while (j < len(rawList)):
if (utilStr.equals_caseless(rawList[j],"and")):
if (utilStr.equals_caseless(rawList[j+1],"end")):
if (utilStr.equals_caseless(rawList[j+2],"block-comment")):
j += 2
break
else:
j += 2
else:
j += 1
j += 1
if (j < len(rawList)):
i = j
else:
error.missingIs()
elif (utilStr.equals_caseless(rawList[i + 1],"is")):
testBool = False
try:
testBool = rawList[i + 2] == "\\n" or utilStr.equals_caseless(rawList[i + 2],"and")
except:
testBool = False
if not testBool:
i += 2
checkType = determine.Type(rawList,i,varDict)
definition = ""
if (checkType == "String"):
definition = concatenation.on(rawList,i,varDict)[0]
elif (checkType == "Integer"):
definition = maths.calc(rawList,i,varDict)
elif (checkType == "Variable"):
if (not utilStr.checkInt(varDict[rawList[i]])):
definition = concatenation.on(rawList,i,varDict)[0]
else:
definition = maths.calc(rawList,i,varDict)
elif (checkType == "Input"):
rawList[i] = "\"" + input() + "\""
definition = concatenation.on(rawList,i,varDict)[0]
else:
error.unrecognizedVar()
varDict[rawList[i-2]] = definition
else:
error.invalidSyntax()
i = determineNextIndex(rawList,i)
def determineNextIndex(rawList,i):
testBool = False
try:
testBool = rawList[i + 1] == "\\n" or utilStr.equals_caseless(rawList[i + 1],"and")
except:
testBool = False
if testBool:
i += 2
else:
i += 1
return i