compiler for custom python based language designed to reduce the amount of typing required to code:

Code:

src=input('code: ')
code=''
def pseudo_to_real(pseudo, controls):
    lines = pseudo.split('\n')
    real_code = []
    indent_level = 0
    for line in lines:
        stripped = line.strip()
        if any(stripped.startswith(control) for control in controls):
            real_code.append(' ' * indent_level + stripped + ':')
            indent_level += 4
        elif stripped == 'end':
            indent_level -= 4
        else:
            real_code.append(' ' * indent_level + stripped)
    return '\n'.join(real_code)
controls=['while','if','for','def','class','else','elif']
def replace_for_loop(pseudo):
    lines = pseudo.split('\n')
    updated_code = []
    for line in lines:
        stripped = line.strip()
        if stripped.startswith('for('):
            has_colon = stripped.endswith(':')
            if has_colon:
                stripped = stripped[:-1]  # Remove the trailing colon
            content = stripped[4:]  # Remove 'for('
            components = content.split(',')
            if len(components) == 4:
                var, upr, lwr, step = map(str.strip, components)
                stripped = f"for {var} in range({lwr}, {upr}, {step})"
            elif len(components) == 3:
                var, upr, lwr = map(str.strip, components)
                stripped = f"for {var} in range({lwr}, {upr})"
            elif len(components) == 2:
                var, upr = map(str.strip, components)
                stripped = f"for {var} in range(0, {upr})"
            if has_colon:
                stripped += ':'
            line = line.replace(line.strip(), stripped)
        updated_code.append(line)
    return '\n'.join(updated_code)
src=src.replace(':',chr(10))
pseudo = src
src = pseudo_to_real(pseudo, controls)
src = replace_for_loop(src)
src=src.replace('`',':')
print(src)
Great work!
But can you add some instructions to it?
idk maybe if i have motivation it was mostly just an experiment
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement