r/learnpython • • 9m ago

lacenta interpreter

hi, im just beginning to learn python, so please go easy on me. (by the way, my english is very bad.)

im wroted the small interpreter for my mini-lang lacenta.

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('>'))

syntax:

C - command. it must be in start any command. dont write it to comments!

text() - print. text(*Hello, World!) print a leteral, because startswith *. else search var with this name

p - var declerate. ; - splitter

Cp(x;10) Ctext(x)

or if you create a file reader for this lang (just import * from this and do main with f.open):

Cp(x;10)

Ctext(x)

next num and str

num() - uhhhhh.... im forgot word.... ok, it took a var and just retake this var to float().

Cp(x;10) <- STR!!!! Cnum(x) <- now x float

also can took more than 1 vars

Cp(x;10) Cp(y;2) num(x;y)

its also translate to float them all

if - just if. can do eq, nq, bg, sm. if false, just jumping at endif. * also use for INT OR FLOAT LITERAL

Cp(z;10) Cif(z;bg;*5) text(*bigger) endif

someone day im write a full doc's, but im hate doc's, so, please, watch code

2 Upvotes

3 comments sorted by

•

u/Mediocre_Command_187 5m ago

sorry for the formatting

•

u/Available_Safe1818 3m ago

What the heck is going on here?

•

u/Mediocre_Command_187 1m ago

mmmm.... my code? whats wrong? pls, give an feedback