r/learnpython • u/Mediocre_Command_187 • 5h ago
im wrote lacenta interpreter
hi all, im the begginner in python, so, im wroted my first interpretator. im dont wroted docs for this. please please dont kill me.
import random
def prin(e):
print(e, end='')
def g(x):
return x[1]
def p_gate(a, b, c):
w = 'Cgate('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
c[all] = []
return kend
def p_re(a, b, c):
w = 'Cre('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
now = all.split(';')
c[now[0]][int(c[now[1]])] = c[now[2]]
return kend
def p_ro(a, b, c):
w = 'Cro('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
now = all.split(';')
c[now[0]].append(c[now[1]])
return kend
def p_ra(a, b, c):
w = 'Cra('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
now = all.split(';')
c[now[0]] = c[now[1]][int(c[now[2]])]
return kend
def p_text(a, b, c):
w = 'Ctext('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
if all[0] == '*':
prin(all[1:])
else:
prin(c[all])
return kend
def p_sub(a, b, c):
w = 'Csub('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
now = a[k:kend].split(';')
src = c[now[0]]
start = int(now[1][1:]) if now[1].startswith('*') else int(c[now[1]])
count = int(now[2][1:]) if now[2].startswith('*') else int(c[now[2]])
c[now[3]] = src[start:start + count]
return kend
def p_jump(a, b, c):
w = 'Cjump('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
return a.find('@2' + all + '2@')
def p_why(a, b, c):
w = 'Cwhy?('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
c[all] = input()
return kend
def p_rnd(a, b, c):
w = 'Crnd('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
now = all.split(";")
c[now[0]] = random.randint(int(now[1]), int(now[2]))
return kend
def p_str(a, b, c):
w = 'Cstr('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
now = all.split(';')
for x in range(len(now)):
c[now[x]] = str(c[now[x]])
return kend
def p_num(a, b, c):
w = 'Cnum('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
now = all.split(';')
for x in range(len(now)):
c[now[x]] = float(c[now[x]])
return kend
def p_if(a, b, c):
w = 'Cif('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
now = all.split(';')
op2 = float(now[2][1:]) if now[2].startswith("*", 0) else c[now[2]]
op1 = float(now[0][1:]) if now[0].startswith("*", 0) else c[now[0]]
if g(now) == 'eq':
res = op1 == op2
elif g(now) == 'nq':
res = op1 != op2
elif g(now) == 'bg':
res = op1 > op2
elif g(now) == 'sm':
res = op1 < op2
return (kend, res)
def p_tor(a, b, c):
w = 'Ctor('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
now = all.split(';')
op2 = float(now[2][1:]) if now[2].startswith("*", 0) else c[now[2]]
op1 = float(now[0][1:]) if now[0].startswith("*", 0) else c[now[0]]
if g(now) == '+':
res = op1 + op2
elif g(now) == '-':
res = op1 - op2
elif g(now) == '/':
res = op1 / op2
elif g(now) == '*':
res = op1 * op2
c[now[3]] = res
return kend
def p_p(a, b, c):
w = 'Cp('
k = a.find(w, b) + len(w)
kend = a.find(')', k)
all = a[k:kend]
s = all.split(';')
c[s[0]] = s[1]
return kend
def main(a):
b = 0
c = {}
yes = 1
while b < len(a):
if yes == 1:
if a.startswith('Ctext(', b):
b = p_text(a, b, c)
elif a.startswith('Cre(', b):
b = p_re(a, b, c)
elif a.startswith('Cgate(', b):
b = p_gate(a, b, c)
elif a.startswith('Cra(', b):
b = p_ra(a, b, c)
elif a.startswith('Cro(', b):
b = p_ro(a, b, c)
elif a.startswith('Cjump(', b):
b = p_jump(a, b, c)
elif a.startswith('Cp(', b):
b = p_p(a, b, c)
elif a.startswith('Csub(', b):
b = p_sub(a, b, c)
elif a.startswith('Ctor(', b):
b = p_tor(a, b, c)
elif a.startswith('Cnum(', b):
b = p_num(a, b, c)
elif a.startswith('Cstr(', b):
b = p_str(a, b, c)
elif a.startswith('Cif(', b):
b, yes = p_if(a, b, c)
elif a.startswith('Cwhy?(', b):
b = p_why(a, b, c)
elif a.startswith('Crnd(', b):
b = p_rnd(a, b, c)
else:
n = a.find("C", b)
if n != -1:
b = n
else:
b += 1
else:
b = a.find("endif", b)
yes = 1
print('[program finished]\n')
if __name__ == '__main__':
main(input('>'))thats an a repost my first mesg, where formatting go vroom
2
Upvotes
1
u/Mediocre_Command_187 5h ago
im try to write docs