DEV Community

sma
sma

Posted on • Edited on

2 2

Lets build a simple interpreter from scratch in python, pt.01

Hi!.
In this blog series we will see that will i be sucessful or not in my attempt to build a programming language, an interpreter, a parser, a compiler and a virtual machine. For any questions starting with 'why' the answer is always 'why not?' except theese:

  • Why Python? Python lets you focus on problem rather than fighting with language itself.
  • Why not Rust? Rust is causing hair loss while fighting against it.
  • Why not C? It explodes many times in your hands. You lose your hands.

If code that i write is not idiomatic and tastefull for you please ignore it.
Lets get started.

class Interpreter:
    def __init__(self):
        pass

    def run(self,code):
        for xs in code:
            self.eval(xs)


    # This is our magic function. It evauates xs parameter 
    # according to its first element  and calls appropriate 
    # member function and passes the parameter to that function.
    # If xs is not a list then returns xs itself:

    def eval(self,xs):
        if isinstance(xs,list):
            return self.__getattribute__(xs[0])(xs)
        return xs


    # Our first function (or opcode) is Print. If last item 
    # is a comma it doesn't print newline:  

    def Print(self,xs):
        if len(xs)==1:
            print()
            return
        l=len(xs)-1
        for i,x in enumerate(xs[1:]):
            e=self.eval(x)
            if i<l-1:
                print(e,end="")
            else:
                if e!=",":
                    print(e)
                else:
                    print(e,end="")

# This is AST (Abstract Syntax Tree) generated by 
# parser. Consisting of recursive lists
# (No, not lisp :P )
code=[
    ["Print","Hello World!","Sky is blue"],
    ["Print",1,","],
    ["Print",2,","],
    ["Print",3],
    ["Print","a"],
    ["Print","b"],
    ["Print"],
    ["Print","c"],
]

interpreter=Interpreter()

interpreter.run(code)
Enter fullscreen mode Exit fullscreen mode

Output is:

Hello World!Sky is blue
1,2,3
a
b

c
Enter fullscreen mode Exit fullscreen mode

Part 2: Basic Arithmetic Functions

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay