r/learnpython • u/Mediocre_Command_187 • 13h ago
I built my own custom programming language interpreter in Python! Looking for feedback and roast.
Hey everyone!
I'm still learning, and as a fun side project, I wrote a simple interpreter for my own custom language in Python.
Every command starts with C and uses parentheses with ; as a separator (e.g., Cp(x;10) to assign a variable, Ctor(x;+;*5;y) for math, Cgate(arr) for lists, and Cjump(label) with u/2label2@ for gotos). It also supports basic Cif conditions, user input via Cwhy?(var), random numbers, and string slicing.
Here is the full code of my interpreter:
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('>'))I know the parser is pretty basic (it just moves an index through the string) and there's a lot to improve, so I'd love to hear your thoughts! What features should I add next, and how can I make the Python code cleaner?
Sorry for any mistakes, English isn't my first language!
1
u/ducksauvage 8h ago
Decent start! Although I would suggest improving readability (small one-liner comments explaining blocks, named args, more descriptive variable naming) and building an abstract syntax tree of the program first, then iterating through it. It will also make it easier to add additional operations.
Perhaps create an "operations" map or something, that acts as a registry of names to implementing functions. That'll save you the huge if-else block.
"Cwhy?(...)" - adding a question mark is an interesting choice, but definitely your own. I would probably avoid it, in case I wanted to use it for some other use in the future (e.g. denominating optional values)
1
u/No_Addition_992 7h ago
This is a strong first project, and getting variables, math, and jumps working is the hard part. A useful next step is writing a few tiny example programs that test each command, so you can tell at once when a change breaks something. Splitting the code that reads commands from the code that runs them would also make it easier to add new commands later.