DEV Community

JavaCode7
JavaCode7

Posted on • Updated on

Improving the esolang

So I wrote an article making an esolang and got this comment:
The dev.to comment that instigated the writing of this article
Thanks to @olus2000 for this comment.

Here is the full code so far:

value = 0
code = input("Enter your loo code here please > ")

lexed = []
for i in code:
    lexed.append(i)

for j in lexed:
    if j == "+":
        value += 1
    elif j == "-":
        value -= 1
    elif j == "#":
        print(chr(value))
    elif j == ";":
        quit()
Enter fullscreen mode Exit fullscreen mode

In this guide, I am going to add a quine (quine prints out the code), a way to specify how much you want to increment or decrement by putting a number between 1 and 9 inclusive before + or - and finally, I am going to add numeric printing.

Step 1: quine

The quine functionality can easily be implemented by adding another branch to the if statement.

elif j == "q":
    print(code)
Enter fullscreen mode Exit fullscreen mode

Step 2: improving + and -

To make the changes to + and -, we are going to have to modify the for loop. This must be done to make sure we can access the index. This will be useful later.

for k in range(0, len(lexed) - 1, 1):
    j = lexed[k]
    if j == "+":
        value += 1
    elif j == "-":
        value -= 1
    elif j == "#":
        print(chr(value))
    elif j == ";":
        quit()
    elif j == "q":
        print(code)
Enter fullscreen mode Exit fullscreen mode

From here, we then have to modify the + and - branches to make the changes

for k in range(0, len(lexed) - 1, 1):
    j = lexed[k]
    if j == "+":
        scale = int(lexed[k - 1])
        value += scale
    elif j == "-":
        scale = int(lexed[k - 1])
        value -= scale
    elif j == "#":
        print(chr(value))
    elif j == ";":
        quit()
    elif j == "q":
        print(code)
Enter fullscreen mode Exit fullscreen mode

Step 3: numeric printing

After the changes made in step 2, it is now possible to read other tokens given an offset. We are going to use this in numeric printing as well.

for k in range(0, len(lexed) - 1, 1):
    j = lexed[k]
    if j == "+":
        scale = int(lexed[k - 1])
        value += scale
    elif j == "-":
        scale = int(lexed[k - 1])
        value -= scale
    elif j == "#":
        if lexed[k - 1] == "|":
            print(chr(value))
        else: print(value)
    elif j == ";":
        quit()
    elif j == "q":
        print(code)
Enter fullscreen mode Exit fullscreen mode

And there we have it! The esolang has been improved. I hope you like this article.

Discussion (0)