-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogrammingLanguage.py
More file actions
136 lines (112 loc) · 3.82 KB
/
programmingLanguage.py
File metadata and controls
136 lines (112 loc) · 3.82 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class language:
def __init__(self,fileName):
self.file = fileName
self.commands = ['print','loop','input']
self.lines = []
self.words = []
self.variables = {}
self.ignore = ['comment','end']
def readFile(self):
with open(self.file) as f:
self.lines = f.read().splitlines()
for x in self.lines:
self.words.append(x.split(' '))
def execute(self):
while self.lines:
line = self.lines.pop(0)
if self.words[0][0] == 'print':
self.words.pop(0)
print(line[5:])
elif self.words[0][0] == 'loop':
loopTime = self.words[0][1]
self.words.pop(0)
toLoop = []
toWords = []
while not self.words[0][0] == 'end':
x = self.lines.pop(0)
y = self.words.pop(0)
toLoop.append(x)
toWords.append(y)
for x in range(int(loopTime)):
self.lines = toLoop + self.lines
self.words = toWords + self.words
elif self.words[0][0] == 'variable':
if self.words[0][2] == '=':
if self.words[0][3] == 'input':
varIn = input(" ".join(self.words[0][4:]) + "\n")
self.variables[self.words[0][1]] = varIn
self.replaceVar(self.words[0][1],varIn)
else:
self.variables[self.words[0][1]] = ' '.join(self.words[0][3:])
self.replaceVar(self.words[0][1],' '.join(self.words[0][3:]))
else:
return "Error","No Variable Assignment"
self.words.pop()
elif self.words[0][0] in self.ignore:
self.words.pop(0)
pass
else:
return "Error","Unknown Command"
def replaceVar(self,variable,replacement):
for i,v in enumerate(self.lines):
w = v.split(' ')
for j,word in enumerate(w):
if word == variable:
w[j] = replacement
self.lines[i] = ' '.join(w)
self.words[i] = w
class Lexer():
tokens = { r'[a-zA-Z_][a-zA-Z0-9_]*', '^[-+]?[0-9]+$', r'\".*?\"' }
ignore = '\t '
literals = { '=', '+', '-', '/','*', '(', ')', ',', ';'}
def NUMBER(self, t):
t.value = int(t.value)
return t
def COMMENT(self, t):
pass
def newline(self, t):
self.lineno = t.value.count('\n')
class Parser():
tokens = Lexer.tokens
precedence = (
('left', '+', '-'),
('left', '*', '/'),
('right', 'UMINUS'),
)
def __init__(self):
self.env = { }
def statement(self, p):
pass
def statement(self, p):
return p.var_assign
def var_assign(self, p):
return ('var_assign', p.NAME, p.expr)
def var_assign(self, p):
return ('var_assign', p.NAME, p.STRING)
def statement(self, p):
return (p.expr)
def expr(self, p):
return ('add', p.expr0, p.expr1)
def expr(self, p):
return ('sub', p.expr0, p.expr1)
def expr(self, p):
return ('mul', p.expr0, p.expr1)
def expr(self, p):
return ('div', p.expr0, p.expr1)
def expr(self, p):
return p.expr
def expr(self, p):
return ('var', p.NAME)
def expr(self, p):
return ('num', p.NUMBER)
def main():
file = input("What file do you want compiled?")
if file == '':
file = 'programTesting.txt'
Compiler = language(file)
Compiler.readFile()
errors = Compiler.execute()
if errors != None and errors[0] == 'Error':
print("Error:" + errors[1])
if __name__ == "__main__":
main()