Code Monkey home page Code Monkey logo

mcdjs's People

Contributors

deepsource-io[bot] avatar e0selmy4v avatar r-bsf avatar

Stargazers

 avatar  avatar  avatar  avatar

mcdjs's Issues

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

目前,代指令是一种基于项目中所定义 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 插件,使新的语言可以被编译为旧的语言,从而实现新的语言。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.