Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将一种使用 JS 语法的全新语言作为代指令 #1

Closed
E0SelmY4V opened this issue Apr 4, 2023 · 0 comments
Closed

将一种使用 JS 语法的全新语言作为代指令 #1

E0SelmY4V opened this issue Apr 4, 2023 · 0 comments
Labels
讨论 有个想法,讨论讨论
Milestone

Comments

@E0SelmY4V
Copy link
Member

E0SelmY4V commented Apr 4, 2023

目前,代指令是一种基于项目中所定义 API 的 JS 的内部 DSL ,例如

If(select('@e'))
  .Then(() => {
    Command.say('a');
  })
  .Else(() => {
    Command.say('b');
  });

我觉得由此导致的一个较为明显的问题就是代指令过于繁琐。
我在这里把这门语言叫做“旧的语言”。

所以本人希望将一种使用 JS 语法的全新语言作为代指令。
希望效果如下

if ('@e') {
  say('a');
} else {
  say('b');
}

我觉得这门语言暂且可以叫做“新的语言”。

如何实现

对于新的语言的解析,我认为完全可以使用 babylon 、 recast 等现成的 JS 解析器进行解析,而这些解析器得到的 AST 基本是 ESTree 格式。
鉴于开发到目前(2023/6/27),本项目的转译依然是基于一种较为“特别”的 AST 格式,所以我觉得第一个需要做出抉择的问题就是使用了新的语言之后,对于 AST 的操作是继续使用目前的这种特别的格式,还是转为使用 ESTree 格式。

——为了方便,在我写下这些的时候,我粗浅地给目前使用的这个“特别”的格式起了个名字叫做 McdJS APIs Generated AST 简称 MagAST 。

  • 改为使用 ESTree

    若我们改为使用 ESTree ,则首先对于 MagAST 的生成和转译等部分要全部重写——基本相当于重写整个项目了。
    其次,考虑到指令究竟跟 JS 不一样,我觉得在符合 ESTree 标准的情况下得到一个可以表示每个命令方块串内每个命令的结构是极为困难的。
    我觉得这也代表通过编写 babel 插件来实现 McdJS 的功能不太现实,因为基本无法通过访问器模式在不破坏 ESTree 标准的情况下进行转译操作。
    因为源 AST 的格式和目标格式究竟是不同的,这大概意味着对树的遍历方式大概是可变的,所以貌似从头实现一个访问器也不简单。

    所以我认为改用 ESTree 是不太现实的。

  • 继续使用 MagAST

    若继续使用 MagAST ,就需要考虑把新的语言生成的 ESTree 转为 MagAST 。
    可行的做法本人想到了两种:

    1. 分析整个 ESTree 然后直接转为 MagAST 。
    2. 操作 ESTree 得到旧的语言,再运行旧的语言来得到 MagAST 。

    鉴于目前对 API 的开发已经达到了一定程度,我认为在这里第二种方法更好。
    操作 AST 生成新的 JS 代码正是 babel 等编译器的工作,所以理论上我们只需要开发一个 babel 插件,就可将新的语言编译为旧的语言,简直是皆大欢喜了。

于此,我个人得到的结论是在基于 MagAST 的转译和旧的语言的基础上,可以通过开发一个 babel 插件,使新的语言可以被编译为旧的语言,从而实现新的语言。

@E0SelmY4V E0SelmY4V added the 讨论 有个想法,讨论讨论 label Apr 4, 2023
@E0SelmY4V E0SelmY4V pinned this issue Apr 4, 2023
@E0SelmY4V E0SelmY4V unpinned this issue Apr 4, 2023
@E0SelmY4V E0SelmY4V pinned this issue Apr 4, 2023
@E0SelmY4V E0SelmY4V assigned E0SelmY4V and unassigned E0SelmY4V Apr 5, 2023
@E0SelmY4V E0SelmY4V added this to the 原型机 milestone Apr 5, 2023
@n9gc n9gc locked and limited conversation to collaborators Jun 27, 2023
@E0SelmY4V E0SelmY4V closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2023
@n9gc n9gc unlocked this conversation Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
讨论 有个想法,讨论讨论
Projects
None yet
Development

No branches or pull requests

1 participant