前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用Go实现一门解释型语言

用Go实现一门解释型语言

作者头像
李海彬
发布2018-08-16 15:34:27
4620
发布2018-08-16 15:34:27
举报
文章被收录于专栏:Golang语言社区Golang语言社区

原文作者:高峰

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.

代码语言:javascript
复制
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 ;.

代码语言:javascript
复制
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.

代码语言:javascript
复制
 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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
 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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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
  • ...

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-08-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 Introduction
  • 2 Syntax
    • 2.1 Definition
      • 2.2 Arithmetic operations
        • 2.3 Builtin containers
          • 2.4 Builtin functions
            • 2.5 Function
              • 2.5 If-else statements
                • 2.6 For-loop statements
                • 3 Extensions
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档