在Sweet.js中访问父母变量 [英] access parents variable in Sweet.js
本文介绍了在Sweet.js中访问父母变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个新的面向对象的系统,我需要访问宏的父级的变量.我有以下内容:
I'm creating a new object orientation system and I need to access the variables of the parent of the macro. I have the following:
macro module {
rule { $i:ident { $e ... } } => {
var $i = {
$e ...
}
}
}
macro fn {
rule { $i:ident { $e ... } } => {
$e ...
}
}
module x {
fn name {
}
}
我希望能够在fn macro
中提供module
名称(在本例中为x
),因为也许我想做类似$parentModule.prototype.myFunc
的操作.但是,如果执行$e$e
,它将无法正常工作.有可能吗?
I want be able to, in fn macro
, have available the module
name, in this case, x
, because maybe I want to do something like $parentModule.prototype.myFunc
. But if I do $e$e
it doesn't works properly. Is it possible?
推荐答案
There's a few ways to do this but I think the simplest thing to do is use named patterns:
macro module {
rule {
$i:ident {
$mbody:(fn $name:ident { $body ...}) ...
}
} => {
$i.prototype.$mbody$name ...
}
}
module x {
fn name {
}
}
这篇关于在Sweet.js中访问父母变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文