eggshell.py

Created by chick-chicky

Created on August 22, 2023

3.05 KB

A shell program for my language (https://my.numworks.com/python/chick-chicky/egg)


"""
A basic shell program for EGG
"""
import egg

progs = {}
prog = None

state = 'shell'

stdlib = '\n'.join([
    'f p >> @ end',
    'f pi 3.141592653589793 <# end'
    'f e 2.718281828459045 <# end'
])

while True:
    
    if state == 'shell':
    
        x = input('SHEL> ')
        c = egg.tokenize(x)
        
        if len(c) == 0: continue
        
        if c[0] == 'e' or c[0] == 'enter':
            if len(c) == 1:
                c.append('main')
                print('[?] Defaulting to `main`')
            if c[1] in progs.keys():
                print('[!] Program already exists')
            else:
                prog = c[1]
                progs[prog] = ''
                state = 'prog'
            
        if c[0] == 'l' or c[0] == 'ls' or c[0] == 'list':
            for p in progs.keys():
                print('[.] '+('*' if p == prog else ' ')+' '+p)
                
        if c[0] == 'd' or c[0] == 'del' or c[0] == 'delete':
            if len(c) == 1:
                c.append(prog)
                print('[?] Defaulting to `%s`'%(prog,))
            if c[1] in progs.keys():
                prog = c[1]
                v = input('[/] Are you sure you want to delete `%s` ? (y/N) '%(c[1],))
                if v.lower() == 'y':
                    del progs[prog]
                    print('[.] Deleted `%s`'%(c[1],))
            else:
                print('[!] Could not find `%s`'%(c[1],))
                
        if c[0] == 's' or c[0] == 'select':
            if len(c) == 1:
                c.append('main')
                print('[?] Defaulting to `main`')
            if c[1] in progs.keys():
                prog = c[1]
                print('[.] Selected `%s`'%(c[1],))
            else:
                print('[!] Could not find `%s`'%(c[1],))
                
        if c[0] == 'r' or c[0] == 'run':
            if prog == None:
                print('[!] Select a program to run')
                continue
            print('[.] Running `%s`'%(prog,))
            r = None
            try:
                r = egg.EggRunner(stdlib+'\n'+progs[prog])
            except Exception as e:
                print('[!] Exception while parsing:')
                for l in str(e).split('\n'):
                    print('[!] : %s'%(l,))
                continue
            try:
                r.run()
            except Exception as e:
                print('[!] Exception while running:')
                for l in str(e).split('\n'):
                    print('[!] : %s'%(l,))
                continue
        
        if c[0] == 'x' or c[0] == 'exit':
            print('[.] Byyyeee !')
            break
            
    elif state == 'prog':
        
        x = input('YOLK: ')
        
        if x.startswith('%%'):
            c = egg.tokenize(x[2:])
            if len(c) == 0: continue
            if c[0] == 'x' or c[0] == 'exit': 
                state = 'shell'
                print('[.] Saved program `%s`'%(prog,))
            else:
                print('[!] Unknown command `%s`'%(c[0],))
            continue
        
        progs[prog] += x+'\n'

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.