kai desu.

Implementing a range operator into Ghost

2020-10-26
1 min

I had recently come across the PHP Internals website, which is dedicated to detailing and explaining how PHP is parsed and executed. One of their blog posts went over how to implement a new range operator in PHP, and I thought this was a great idea to showcase the internal workings of the language.

Implementing a range operator was already on my list for Ghost, so I took this opportunity to write a similar post on a simple implementation. This turned out to be really beneficial as it not only shows how easy it is to extend and add new native functionality to Ghost itself, but also acts as "walk-through" documentation of the process.

In the post I wrote, we look at how to implement the following range operator (..).

1 .. 10

Our range operator is specifically an infix operator, meaning it sits inbetween two operands, 1 and 10 in our example. We then simply use these values to produce a new List object containing the full range of numbers.

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

This is very useful when performing loop operations.

I enjoyed writing this out, and I'd like to maybe do a couple more in the near future with some more advanced implementations that touch on different parts of the language (object and the abstract syntax tree for example).

Continue reading [ghostlang.org]