Hacker News new | past | comments | ask | show | jobs | submit login
Creating Your Own Programming Language – Laurence Tratt [video] (youtube.com)
54 points by matt_d 58 days ago | hide | past | favorite | 6 comments



Coincidentally just saw this video a few days ago.

Interesting approach, to use RPN for expressions, including for the condition in the while statement.

I could understand most of the logic of the interpreter, except for the part about how the while statement is implemented.

I did not quite understand the parts of the code with

  case "while" 
and

  case "end
fragments.

That is, around the region of lines 11 to 14 or so, at about 18:0 in the video.

How does the code there, work?


`case while` evaluates the condition. If the condition's true it moves forward to the next line (i.e. into the body of the loop), but if it's false it moves forward several lines, until the line after the `end` statement.

`case end` simply moves back to the `while` statement and lets the `case while` code run again.

Essentially:

        while n 1 >=
        ...
        end
becomes:

    start:
        if not (n 1 >=) goto done
        ...
        goto start
    done:


It's just using the python match statement.

https://docs.python.org/3/tutorial/controlflow.html#match-st...

It's like switch in C / C++. And then it's just looking for the strings "while" or "end".

It's the same as if he had written:

    keyword = line.split(maxsplit=1)[0]
    if keyword == "while":
        ...
    elif keyword == "end":
        ...
Then depending on the statement, in the while, if it evaluates to true, it scans forward to find end and continues the program from there. If the statement is true it evaluates the lines one by one, until hitting end and then scans back to the start of the while to evaluate it again.

The `pc` variable is the line number that is currently being evaluated.


By random chance I'm working on making my own programming language and I very recently used RPN for my expressions. It's a very neat and clean way to implement order precedence and then afterwards you can translate the entire flat expression into a tree-like structure to fit into your AST and be sure that transformation doesn't mess anything up.

I'm surprised you think the condition in the while statement would be any different though, an expression is an expression, it doesn't matter if you are assigning a variable, evaluating an if condition, or doing a while loop, you still need an expression for each of them, and the expressions should behave the same way in each of them.

As for the case "while" and case "end", I think the case "end", he actually does explain them pretty well as he is writing them. It can be a little confusing because he puts ... as a placeholder and implements the code a little later in the video.


replying a couple of days late due to heavy weather conditions here ...

thanks to all those who replied, guys.

I should clarify that I did understand from the beginning, what the intention of his code was, for parsing and running that while statement. I am a dev myself. I mean, I did know that the purpose of that code part was to scan from the "while" line to the "end" line, executing the statements along the way, and then scan back up to the "while", and again execute the statements until the "end", and so on, as long as the while condition was true. and the RPN part was clear to me too, no issues there.

I mean, as somebody else commented, he says all that himself in the talk.

what I did not quite understand was some of the exact implementation details of that part. I think there were a couple of reasons for this. one, I may have spent too little time reading it, and two, as someone else again said, he jumps back and forth in the video, between writing bits of code and testing them, which makes it difficult to get a holistic view of the code at one time on the screen.

anyway I will go through it again, to make sense of the implementation details.


On the same topic, pikuma also published a new compiler course recently:

https://pikuma.com/courses/create-a-programming-language-com...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: