Estou criando minha própria linguagem de programação em Python

I am creating my own programming language in Python

我正在用 Python 创建属于我自己的编程语言

For some time now, I have been working on a project called Exper. The idea was simple: to learn more about interpreters and language design by creating my own programming language. 一段时间以来,我一直在进行一个名为 Exper 的项目。这个想法很简单:通过创建自己的编程语言来深入了解解释器和语言设计。

In the beginning, it was extremely simple, but gradually it gained features such as: Variables, Functions, Structs, Loops, Conditionals, Lists, and String interpolation. Today, Exper is already capable of running relatively large programs. 起初,它非常简单,但逐渐增加了诸如变量、函数、结构体(Structs)、循环、条件判断、列表和字符串插值等功能。如今,Exper 已经能够运行相对大型的程序了。

What has helped me the most in development are not small examples, but a text-based RPG I am creating to test the language. This RPG forces the implementation of real features: Inventory, Objects, Combat, Data structures, Pass-by-reference, and List manipulation. 在开发过程中,对我帮助最大的不是那些小示例,而是我为了测试该语言而正在编写的一款文字 RPG 游戏。这款 RPG 迫使我必须实现一些实际功能:物品栏、对象、战斗、数据结构、引用传递以及列表操作。

Curiously, most bugs only appear when we try to build something real. For example, I recently discovered problems related to pass-by-reference and mutability while implementing an inventory system for potions. This type of problem would hardly appear in simple calculator examples. 有趣的是,大多数 Bug 只有在我们尝试构建真实事物时才会显现。例如,我最近在实现药水物品栏系统时,发现了与引用传递和可变性相关的问题。这类问题在简单的计算器示例中几乎不会出现。

Language Example

语言示例

struct Player {
  name,
  hp = 100
}

player = Player()
player.name = "Callebe"
console.log("{player.name} possui {player.hp} HP")

What I have learned so far

到目前为止我学到了什么

Creating a language is much harder than it looks. When you believe you are finished: A scope problem arises. Then a mutability problem. Then a parsing problem. Then a list problem. Then a reference problem. But that is precisely what makes the project interesting. 创建一门语言比看起来要困难得多。当你以为大功告成时:作用域问题出现了。接着是可变性问题。然后是解析问题。再然后是列表问题。最后是引用问题。但恰恰是这些,让这个项目变得如此有趣。

Next steps

下一步计划

Improve method support, Compound operators, Module system, Classes, and Exceptions. 改进方法支持、复合运算符、模块系统、类以及异常处理。

The goal is not to create a competitor to Python or JavaScript. The goal is to learn how these languages work internally. And, so far, Exper has been an excellent teacher. 我的目标不是创造一个 Python 或 JavaScript 的竞争对手,而是学习这些语言在内部是如何运作的。到目前为止,Exper 一直是一位出色的老师。