实施“此” /" quot&quot用自定义解释的编程语言
我正在为自定义口译员开心;) 到目前为止,我的目的是分配变量,定义和调用功能,数组,循环,如果块等... 我已经开始将OOP元素添加到我的语言中,并且在实现“ this” /“ self”关键字方面遇到困难。 我只是找不到解决这个问题的解决方案。 我已经考虑过一些解决方案:
- 类存储的实例(作为字典),当出现“此”关键字时,解释器忽略了“私人”和“ public”关键字(从公共方法中调用私人方法) ,请呼叫“此”引用的方法并返回。
- “此”关键字是引擎盖本身的引用,但是该调用的方法访问修饰符已删除。这与第一个类似,但我想不出更好的东西。
同样,如果您知道C#(原始)或C ++,那就太好了,因为我很大程度上依靠OOP来创建我的解释器。
这是我想实现的一小部分代码样本:
struct Dog has
pub prop age;
prv def bark() do
println "woof woof";
end
pub def run(dist) do
loop 0 to $dist with "d" do
println ("the dog ran " + string $d) + " meters";
$self.bark();
end
end
end
def main(args) do
new Dog -> $dog;
7 -> $dog.age;
println $dog.age;
$dog.run 30;
end
请注意,Bark()方法是“ prv”(私有)。我想知道如何使struct意识到它的实例,以便每次运行狗的树皮(从公共方法调用私有方法)。
提前致谢
I'm working on a custom interpreter for fun ;)
What I have so far is assigning variables, defining and calling functions, arrays, loops, if blocks etc...
I've started adding OOP elements to my language and I'm having trouble implementing the "this" / "self" keyword.
I just cannot figure out a solution to this problem.
I've thought about a few solutions:
- a class stores it's instances (as a dictionary) and when the "this" keyword appears, the interpreter ignores "private" and "public" keywords (to call a private method from a public one), creates a call to the method referenced by "this" and returns.
- the "this" keyword is a reference which under the hood is the instance itself, but the method access modifiers are dropped for that call. this is similar to the first one, but I cannot think of anything better.
Also it would be nice if you knew C# (primarly) or C++, since I'm heavily relying on OOP to create my interpreter.
heres a small sample of code that I would like to implement:
struct Dog has
pub prop age;
prv def bark() do
println "woof woof";
end
pub def run(dist) do
loop 0 to $dist with "d" do
println ("the dog ran " + string $d) + " meters";
$self.bark();
end
end
end
def main(args) do
new Dog -> $dog;
7 -> $dog.age;
println $dog.age;
$dog.run 30;
end
notice that the bark() method is "prv" (private). I would like to know how can I make the struct be aware of it's instances so I can make the dog bark each time it runs (calling a private method from a public method).
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的解释器将需要保留执行堆栈和环境(否则您将不知道方法完成后返回哪里,等等)。
在此执行堆栈/env中,除了功能指针/签名之外,您还将保留该方法正在调用的对象(如果有的话,请记住有关静态函数)。
当删除参考
$ this
时,您将存储的此引用也将被访问。实际上,这是您想法的第1部分和第2部分 - 您可以在其中放置某种检查,例如方法等。祝你好运,听起来像一个有趣的项目!
Your interpreter is going to need to keep an execution stack and environment (otherwise you wouldn't know where to return to after a method is finished, etc.).
In this execution stack/env, apart from a function pointer/signature, you'd also keep the object the method is being invoked on (if any, remember about static-like functions).
This reference that you'd store would then be also accessed when de-referencing
$this
. And actually this is both part 1 and 2 of your idea - you could put some kind of checks like visibility of methods etc. there.Good luck, sounds like a fun project!