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

如何用JavaScript实现一门编程语言 - AST #12

Open
llwanghong opened this issue Apr 28, 2023 · 0 comments
Open

如何用JavaScript实现一门编程语言 - AST #12

llwanghong opened this issue Apr 28, 2023 · 0 comments

Comments

@llwanghong
Copy link
Owner

llwanghong commented Apr 28, 2023

整篇译文的目录章节如下:

AST

正如前面所描述的,解析器将会构建出一个能准确表达程序语义的数据结构。一个AST节点就是一个普通的JavaScript对象,该对象有一个指定节点类型的type属性和依赖具体节点类型的额外信息。

简而言之:

  • num { type: "num", value: NUMBER } // 数字型变量节点
  • str { type: "str", value: STRING } // 字符串型变量节点
  • bool { type: "bool", value: true or false } // 布尔型变量节点
  • var { type: "var", value: NAME } // 标识符节点
  • lambda { type: "lambda", vars: [ NAME... ], body: AST } // 函数声明节点
  • call { type: "call", func: AST, args: [ AST... ] } // 函数调用节点
  • if { type: "if", cond: AST, then: AST, else: AST } // 条件节点
  • assign { type: "assign", operator: "=", left: AST, right: AST } // 赋值节点
  • binary { type: "binary", operator: OPERATOR, left: AST, right: AST } // 二元表达式节点
  • prog { type: "prog", prog: [ AST... ] } // 表达式序列节点

  • let { type: "let", vars: [ VARS... ], body: AST } // 块级作用于变量节点

示例:

数字型变量Numbers("num")
number
字符串型变量Strings("str")
string
布尔型变量Booleans("bool")
bool
标识符Identifiers("var")
var
函数声明Functions("lambda")
lambda
函数调用Function calls("call")
call
条件Conditionals("if")
if
else分支是可选的:
else
赋值Assignment("assign")
assign
二元表达式Binary expressions("binary")
binary
表达式序列Sequences("prog")
prog
块级作用域变量Block scoped variables("let")
let

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant