https://github.com/pickledchair/rustymonkey
The implementation of Monkey programming language (https://interpreterbook.com/) in Rust.
https://github.com/pickledchair/rustymonkey
Last synced: 2 months ago
JSON representation
The implementation of Monkey programming language (https://interpreterbook.com/) in Rust.
- Host: GitHub
- URL: https://github.com/pickledchair/rustymonkey
- Owner: PickledChair
- License: mit
- Created: 2021-03-14T11:19:07.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-04-23T11:18:55.000Z (about 5 years ago)
- Last Synced: 2025-12-27T14:10:47.947Z (6 months ago)
- Language: Rust
- Size: 190 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RustyMonkey
Thorsten Ball 氏の著書 [Writing An Interpreter In Go](https://interpreterbook.com/)(設樂 洋爾 氏による邦訳:[Go言語でつくるインタプリタ](https://www.oreilly.co.jp/books/9784873118222/))にある Monkey 言語の Rust 実装です。
## インストール
ビルドには Rust の開発環境(2018 edition)が必要です。
```
$ git clone https://github.com/PickledChair/RustyMonkey.git
$ cd RustyMonkey
$ cargo build
```
## 使い方
### REPL
```
$ cargo run
Hello ! This is the Monkey programming language!
Feel free to type in commands
>>
```
終了時は `exit` または `quit` を入力してください。
### ソースファイルの実行
```
# test.txt
puts("hello!");
```
```
$ cargo run -- test.txt
hello!
```
## 独自拡張
### いくつかのエスケープ文字への対応
```
>> "h\te\tl\tl\to\t!"
=> h e l l o !
>> "h\ne\nl\nl\no\n!"
=> h
e
l
l
o
!
>> "\"hello!\""
=> "hello!"
```
### マルチバイト文字への対応
```
>> let japanese = "こんにちは";
>> japanese
=> こんにちは
```
### 行コメント
```
>> # これはコメントです
>> # `#` 記号から改行文字までが無視されます
>> let hello = "hello"; # 式や文の後ろに書くこともできます
>> hello
=> hello
```
### 配列向けの組み込み関数や添字演算子の文字列への対応
```
>> let hello = "hello";
>> # 以下、戻り値はいずれも文字列です
>> first(hello)
=> h
>> rest(hello)
=> ello
>> last(hello)
=> o
>> hello[0]
=> h
>> hello[1]
=> e
```
### 整数に関する `<=`, `>=` 演算子の追加
```
>> 1 <= 1
=> true
>> 2 <= 1
=> false
>> 1 >= 2
=> false
>> 2 >= 1
=> true
>> 1 >= 1
=> true
```
### 追加の組み込み関数
```
>> # init 関数:rest 関数の逆です。引数が配列の場合、末尾の要素を除いた配列を返します。
>> # 引数が文字列の場合は、末尾の文字を除いた文字列を返します。
>> init([1,2,3,4,5])
=> [1, 2, 3, 4]
>> init("hello")
=> hell
>>
>> # int 関数:文字列を1つ引数にとり、整数値に変換します。
>> # 変換できなかった場合、エラーを返します。
>> int("42")
=> 42
>> int("hello")
=> ERROR: could not convert the given STRING `hello` into INTEGER
>>
>> # str 関数:引数を一つ取り、オブジェクトの文字列表現を返します。
>> str(42)
=> 42
>> str([1,2,3,4])
=> [1, 2, 3, 4]
>>
>> # print 関数:文字列を1つ引数にとり、改行なしで表示します。戻り値は null です。
>> print("hello")
hello=> null
>>
>> # readline 関数:引数なし。入力待ちになるので、文字列を入力後 Enter で確定してください。
>> # 戻り値は文字列です。
>> let input = readline();
my input
>> input
=> my input
>>
>> # writefile 関数:ファイルパスと書き込む内容の文字列を引数にとります。
>> # 指定されたファイルをテキストモードで開き、内容を書き込みます。
>> # 戻り値は成功時に null, 失敗時にエラーを返します。
>> writefile("test.txt", "ok")
=> null
>>
>> # readfile 関数:ファイルパスを引数にとります。
>> # 指定されたファイルをテキストモードで開き、ファイルの内容を文字列で返します。
>> readfile("test.txt")
=> ok
>> # ファイルが見つからない場合は null を返します。
>> readfile("hoge.txt")
=> null
```
### import 文
指定されたソースファイルをパース・評価して、得られたオブジェクトを現在の環境に追加します。
```
>> writefile("mylibrary.txt", "let myFunc = fn() { return \"This is my function!\"; };\n");
=> null
>> import "mylibrary.txt";
>> myFunc()
=> This is my function!
```
**備考**:原始的な import 文なので、互いに import し合うソースコードを評価すると無限ループに陥ります。
## License
MIT License で公開しています。