1 Star 1 Fork 0

曾昭源/Monkey-Interpreter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

interpreter-go

介绍

使用go语言自制解释器 该解释器会解析源代码,构建抽象语法树(AST),然后对这棵树求值。这类解释器称为树遍历(tree-walking)解释器,因为其工作方式是在AST上遍历并解释。

For example:
let five = 5;
let ten = 10;
let add = fn(x, y) {
    x + y;
};
let result = add(five, ten);

Monkey 基本语法

1.类C语法
2.变量绑定
  let age = 1;
  let name = "Monkey";
  let result = 10 * (20 / 2);
3.整型和布尔型
4.算术表达式
   -5
   !true
   !false
   5 + 5
   5 - 5
   5 / 5
   5 * 5
   foo == bar
   foo != bar
   foo < bar
   foo > bar
   5 * (5 + 5)
   ((5 + 5) * 5) * 5
   add(2, 3)
   add(add(2, 3), add(5, 10))
   max(5, add(5, (5 * 5)))
   foo * bar / foobar
   add(foo, bar)
   let add = fn(x, y) { return x + y };
   f(x, y) {return x + y}(5, 5)
   (fn(x) { return x }(5) + 10) * 10
   let result = if (10 > 5) { true } else { false };
   result // => true
5.内置函数
6.头等函数和高阶函数
  let add = fn(a, b) { return a + b; };
  let add = fn(a, b) { a + b; };
  add(1, 2);
  let fibonacci = fn(x) {
      if (x == 0) {
        0
      } else {
        if (x == 1) {
          1
        } else {
          fibonacci(x - 1) + fibonacci(x - 2);
        }
      }
  }
  let twice = fn(f, x) {
      return f(f(x));
  };
  let addTwo = fn(x) {
      return x + 2;
  }
  twice(addTwo, 2); // => 6
7.闭包
8.字符串数据结构
9.数组数据结构
  let myArray = [1, 2, 3, 4, 5];
  myArray[0]    // => 1
10.哈希数据结构
  let thorsten = {"name":"Thorsten", "age":28}
  thorsten["name"]    // => "Thorsten"

Monkey解释器主要包含了以下几个主要部分:

1.词法分析器
  let x = 5 + 5;
  [
      LET,
      IDENTIFER("x"),
      EQUAL_SIGN,
      INTEGER(5),
      PLUS_SIGN,
      INTEGER(5),
      SEMICOLON
  ]
2.语法分析器
  普特拉解析法,没有将解析函数与语法规则相关联,
  而是将这些函数与单个词法单元类型相关联。这个
  想法的关键是,每种词法单元类型都可以具有两个
  与之相关联的解析函数,具体取决于词法单元的位
  置,比如是中缀还是前缀。
3.抽象语法树(AST)
4.内部对象系统
5.求值器

GOPATH=xxx/interpreter-go

MIT License Copyright (c) 2023 曾昭源 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

使用go语言自制解释器 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者 (1)

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zeng-zhaoyuan/interpreter-go.git
git@gitee.com:zeng-zhaoyuan/interpreter-go.git
zeng-zhaoyuan
interpreter-go
Monkey-Interpreter
master

搜索帮助