meloalright / monkey-go

Book reading about monkey lang in go
Other
0 stars 0 forks source link

阅读记录 #1

Open meloalright opened 1 year ago

meloalright commented 1 year ago

0. Acknowledgments + Introduction

lexers = 词法分析器 parser = 语法分析器 evaluate = eval

Tree Walking:

生成 ast 树的解释器都可以叫 tree-walking 类解释器

monkey lang 5 大金刚:

REPL:

A read–eval–print loop (REPL), also termed an interactive toplevel or language shell

Codes

https://interpreterbook.com/waiig_code_1.7.zip

meloalright commented 1 year ago

1. Lexing

第一步 lexical analysis 词法分析: Source Code => Tokens

核心就是一个 NextToken() 方法

在 Lexer REPL 的视角中就是读取到一行内容去解析为 Tokens https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/repl/repl.go#L23-L28

而这个 NextToken() 就是一个关于当前 char 的大厚处理方法 除了 EQNOT_EQ 以及 IDENT or NUMBER or KEYWORD 其余的都很简单 https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L17-L86

pickChar() vs readChar()

这里的设计中 pickChar() 是一种不前进指针的预读取下一个字符 而 readChar() 是一种会有副作用前进的读取下个字符的方法

pickChar() 这里是这样实现的 https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L110-L116

在实现 EQNOT_EQ 的词法解析时会用到 https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L48-L66

IDENT or NUMBER or KEYWORD

会在这个判断逻辑区分开 IDENT or NUMBER or KEYWORD https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L70-L81

通过这个方法读取到这个单词字面量 https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L88-L94

通过这个看看这个单词是不是关键字 https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/token/token.go#L62-L67

通过这个读取到数字 https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L118-L124

meloalright commented 1 year ago

Parsing

Ast 实现了一堆结构

https://github.com/meloalright/monkey-go/blob/4463a8080312f794987268c97a257edbfc104c4d/learn/02-parser/monkey/ast/ast.go#L51-L274

Parser 和核心就是 Parse 3 大类 Statement

https://github.com/meloalright/monkey-go/blob/4463a8080312f794987268c97a257edbfc104c4d/learn/02-parser/monkey/parser/parser.go#L91-L115

3 大类 Statement 也不复杂 有更多嵌套罢了

https://github.com/meloalright/monkey-go/blob/4463a8080312f794987268c97a257edbfc104c4d/learn/02-parser/monkey/parser/parser.go#L117-L167

Parse Expression 稍微复杂一点 有运算优先级问题

https://github.com/meloalright/monkey-go/blob/4463a8080312f794987268c97a257edbfc104c4d/learn/02-parser/monkey/parser/parser.go#L214-L235

各种 Token 都被归类为 Prefix 和 Infix 运算了

https://github.com/meloalright/monkey-go/blob/4463a8080312f794987268c97a257edbfc104c4d/learn/02-parser/monkey/parser/parser.go#L59-L80

meloalright commented 1 year ago

Evaluation

Traverse the AST, visit each node and do what the node signifies. Interpreters working this way are called “tree-walking interpreters” and are the archetype of interpreters.

就是个大"自举" 大部分方法都是直接实现 包括上下文闭包

https://github.com/meloalright/monkey-go/blob/efbb65ce40b94e755b1df0d78a2a0578ded4a1f7/learn/03-evaluation/monkey/evaluator/evaluator.go#L17-L329

meloalright commented 1 year ago

Extending

Built-in: In this section we’re going to add built-in functions to our interpreter. They’re called “built-in”, because they’re not defined by a user of the interpreter and they’re not Monkey code - they are built right into the interpreter, into the language itself.

Array

Token.LBRACKET 入手

Hash

Token.LBRACE 入手 + 配合 Token.COLON