Skip to content

Lite AST JSON 对象

duangsuse edited this page May 9, 2018 · 16 revisions

Lite AST JSON 序列化方式

前言:为什么要支持 AST 的 JSON 序列化

最重要的理由是 Lite 目前使用 ohm.js 作为自己的解析器,而在 JavaJavaScript 之间使用 Android WebView 只能找到传递字符串的方法,选择一个序列化方法来传递语法树数据结构是最好的解决方案,JSON 作为 JavaScript 内建的序列化方式,是 Lite AST 最好的序列化选择

其他的理由可能是使用 JSON 能 提高已转换脚本的加载速度 ,以及进一步 模块化解释器和缩减不必要的体积 ,另外还有 ohm.js 的匹配结果和真正的 AST 还有出入,需要再次处理以构建真正的 Lite AST

字段命名风格

snake_case

包含一组数据的,不使用复数形式,尽可能不使用缩写

AST 数据结构

node 数据结构应该包含 type, source, line, column 字段,也就是说每个 AST 树节点都有自己的 _类型/载入文件/行/列号信息

在 Lite 1.1 完成设计时, AST 树的组成成分(继承了 LiteNode 类):

  • block: (string[])arg_name, (node[])child_node
  • while_loop: (node)condition, (block)block
  • scope: (block)block
  • index_let: (node)lhs, (node)index, (node)value
  • index: (node)lhs, (node)index
  • if: (node)condition, (block)branch_if, (elif_branch)branch_elif, (block)branch_else

elif_branch: (node)condition, (block)block

  • identifier: (boolean)is_local, (string)label
  • for: (identifier)variable, (node)expression, (block)block
  • def: (identifier)label, (block)block_
  • call: (identifier)label, (node[])args
  • assign: (identifier)label, (node)value
  • binary: (node)lhs, (string)operator, (node)rhs

binary operators: ____

  • unary: (node)lhs, (string)operator
  • list: (node[])initializer
  • table: (kv_pair)initializer

kv_pair: (identifier)key, (node)value

  • value:

value can be true, false, nil, number, string

  • arrow: (node)lhs, (string)rhs, (node)value

Clone this wiki locally