用Go实现一门解释型语言

原文作者:高峰

A interpreter language implementation in Go

1 Introduction

Write an Interpreter in Go source code.

Monkey interpreter language which is implemented Go language. After typing monkey in terminal, you get into the monkeyprogramming language.

1 $ ./monkey
2 Hello $Username! This is Monkey programming language!
3 Feel free to type in commnd
4 Enter "exit()" or CTRL+C to quit command interface
5 >>>

2 Syntax

2.1 Definition

using let as keyword, each line ends with ;.

1 >>>let a = 3;
2 >>>let b = 1.2;
3 >>>a+b
4 4.2

2.2 Arithmetic operations

monkey supports all basic arithmetic operation of int and float types. int type is represented by int64 and float type is represented by float64.

 1 >>> let a = 3;
 2 >>> let b = 1.2;
 3 >>> a + b
 4 4.2
 5 >>> a - b
 6 1.8
 7 >>> a * b 
 8 3.6
 9 >>> a / b 
10 2.5

2.3 Builtin containers

monkey contains two builtin containers: array and map.

  • array

array is a list which organizes items by linear sequence. But types of items can be different from each other.

1 >>> let a = [1, 2.3, "array"];
2 >>> a 
3 [1, 2.3, array]
4 >>> let b = push(a, "another");
5 >>> b 
6 [1, 2.3, array, another]
  • map

map is treated as key-value container. please attention to that only boolean, int and string types can be used as key.

 1 >>> let a = {"name":"moneky", true:1, 7:"sevent"};
 2 >>> a
 3 {name: monkey, true: 1, 7: seven}
 4 >>> a["name"]
 5 monkey
 6 >>> a[true]
 7 1
 8 >> a[7]
 9 seven
10 >>>let b = set(a, 8, "eight");
11 >>> b 
12 {name: moneky, true: 1, 7: sevent, 8: eight}

2.4 Builtin functions

  • len

yield the length of builtin containers.

  • first

yield the first element of array.

  • last

yield the last element of array.

  • rest

yield an array which excludes the first element.

  • push

push an elements into the array.

  • set

insert key value pair into the map

  • puts

print literal value of objects.

2.5 Function

monkey use fn as the definition of function. Apart from regular function using, monkey also includes high order function.

1 >>>let add = fn(a, b) { return a + b;};
2 >>> add(1,2)
3 3
4 >>>let addTwo = fn(a,b, f) { return 2 + f(a, b);};
5 >>>addTwo(1,2, add)
6 5

2.5 If-else statements

monkey supports if-else statements.

1 >>> let max = fn(a, b) { if (a > b) { return a;} else { return b; } };
2 >>> max(1, 2)
3 2

2.6 For-loop statements

monkey support for-loop statement.

1 >>> let sum = fn(x) { let i = 1; let sum = 0; for (i < x) { let sum = sum + i; let i = i+1; } return sum; };
2 >>> sum(100)
3 4950

3 Extensions

To make monkey interpreter language to be more powerfull, it is deserved improving it.

  • float type
  • unicode literal
  • loop branch
  • translate into Chinese
  • ...

版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。

原文发布于微信公众号 - Golang语言社区(Golangweb)

原文发表时间:2018-08-07

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏JadePeng的技术博客

Docker+Jenkins持续集成环境(5): android构建与apk发布

项目组除了常规的java项目,还有不少android项目,如何使用jenkins来实现自动构建呢?本文会介绍安卓项目通过jenkins构建的方法,并设计开发一个...

4928
来自专栏hotqin888的专栏

engineercms利用pdf.js制作连续看图功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hotqin888/article/det...

1691
来自专栏跟着阿笨一起玩NET

C# 地磅串口编程

然后最近有一个项目用到了地磅,这里也是通过串口通讯方式进行数据交互,说实话,地磅这东西,实在有点不方便。

1692
来自专栏Java架构师学习

分布式消息队列Apache RocketMQ源码剖析-Producer分析正文总结

正文 首先我们看一下Producer的继承结构: ? image.png MQAdmin主要包含一些管理性的接口,比如创建topic、查询某个特定消息以方便排查...

4037
来自专栏java 成神之路

spring 之 import标签、alias标签、beans标签 解析

55410
来自专栏一个会写诗的程序员的博客

CodeMirror 代码渲染神器的极简入门实例

1.8K1
来自专栏学海无涯

Java Web之Servlet获取表单值

Java Web开发中,最常用的就是在后台获取前台的参数,经典的案例就是 JSP 表单传值到后台的 Servlet,然后在 doGet 或者 doPost 中获...

3424
来自专栏技术记录

前端插件——头像截图上传插件的使用(带后台)

效果图:实现上传头像,右边是预览,有三个大小,可以对头像进行裁剪 ? HTML: toParentData 和 img 返回的是图片裁剪后的base64编码。其...

1K5
来自专栏Pythonista

牛掰的python与unix

  加载subprocess模块仅仅是将可以使用的代码文件加载进来。也可以创建自己的模块或文件,拱以后重复使用,这与加载subprocess模块的方法相同。IP...

1092
来自专栏移动开发面面观

OpenGL学习笔记——上色

1394

扫码关注云+社区

领取腾讯云代金券