https://github.com/gomjellie/monkey.ts
monkey language written in typescript
https://github.com/gomjellie/monkey.ts
Last synced: 3 months ago
JSON representation
monkey language written in typescript
- Host: GitHub
- URL: https://github.com/gomjellie/monkey.ts
- Owner: gomjellie
- Created: 2021-11-05T06:59:59.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-06T00:34:51.000Z (over 3 years ago)
- Last Synced: 2025-01-23T11:25:48.311Z (4 months ago)
- Language: TypeScript
- Size: 285 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `monkey`
monkey는 typescript로 작성된 node.js런타임 환경에서 실행되는 인터프리터 언어입니다.
[Writing an INTERPRETER in go](https://edu.anarcho-copy.org/Programming%20Languages/Go/writing%20an%20INTERPRETER%20in%20go.pdf)에서 소개된 간단한 장난감 언어를 Typescript로 구현하였습니다.
# EBNF Grammar
```bnf
literal: | |
boolean_literal: true | false
integer_literal: [0-9]+
string_literal: "([a-zA-Z_])*"
function_literal:
fnparameters:
( , , , ... )identifier: [a-zA-Z_]+
expression:
| |
|
|
prefix_expression:
prefix_operator: + | - | !
infix_expression:
call_expression: ()
statement: ; | |
if_statement:
if () else |
if ()let_statement:
let =...
```
# 표현식(expression) vs 명령문(statement)
명령문은 값을 생성하지 않는반면 표현식은 값을 생성해냅니다.
let a = fn(x) { x + 5 }; 은 명령문입니다.
a(10); 은 표현식입니다.
monkey언어에서 let 문과 return 문을 제외하면 모두 표현식입니다.
# 표현식 문법
## 전위 연산자가 포함된 표현식을 쓸 수 있습니다.
```js
-5;
!true;
```## 이항 연산자(binary operator)가 가능합니다.
```js
5 + 5;
5 - 5;
5 / 5;
5 * 5;
```## 비교 연산이 가능합니다.
```js
5 * (5 + 5)((5 + 5) * 5) * 5;
```## 함수 호출, 식별자(identifier)는 표현식으로 취급합니다.
```js
add(2, 3);
add(add(2, 3), add(5, 10));
max(5, add(5, 5 * 5));(foo * bar) / foobar;
add(foo, bar);
```함수는 일급 시민입니다. 따라서 함수 리터럴 역시 표현식입니다.
## if 표현식
Monkey 언어에는 if 표현식이 이라는 특이한 문법이 있습니다.
```js
let result = if (10 > 5) { true } else { false };
result // => true
```