Open meloalright opened 1 year ago
第一步 lexical analysis 词法分析:
Source Code
=>Tokens
在 Lexer REPL 的视角中就是读取到一行内容去解析为 Tokens
https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/repl/repl.go#L23-L28
而这个 NextToken()
就是一个关于当前 char
的大厚处理方法
除了 EQ
和 NOT_EQ
以及 IDENT
or NUMBER
or KEYWORD
其余的都很简单
https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L17-L86
这里的设计中 pickChar()
是一种不前进指针的预读取下一个字符 而 readChar()
是一种会有副作用前进的读取下个字符的方法
pickChar()
这里是这样实现的
https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L110-L116
在实现 EQ
和 NOT_EQ
的词法解析时会用到
https://github.com/meloalright/monkey-go/blob/9f5e5597a4ab6be8dc82a4d6cd2490a432913af1/learn/01-lexing/monkey/lexer/lexer.go#L48-L66
会在这个判断逻辑区分开 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
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.
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.
以 Token.LBRACKET
入手
以 Token.LBRACE
入手 + 配合 Token.COLON
0. Acknowledgments + Introduction
lexers
=词法分析器
parser
=语法分析器
evaluate
=eval
Tree Walking:
monkey lang 5 大金刚:
REPL:
Codes
https://interpreterbook.com/waiig_code_1.7.zip