Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yhirose/monkey-cpp
An interpreter for the Monkey programming language written in C++
https://github.com/yhirose/monkey-cpp
cpp interpret peg programing-language
Last synced: about 1 month ago
JSON representation
An interpreter for the Monkey programming language written in C++
- Host: GitHub
- URL: https://github.com/yhirose/monkey-cpp
- Owner: yhirose
- License: mit
- Created: 2020-03-02T21:59:36.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-12T02:15:22.000Z (over 1 year ago)
- Last Synced: 2024-11-01T22:43:00.173Z (about 2 months ago)
- Topics: cpp, interpret, peg, programing-language
- Language: C++
- Size: 552 KB
- Stars: 25
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesomecpp - Monkey - - C++ version monkey language interpreter. (Scripts)
README
# monkey
Another implementation of Monkey programming language described in [Writing An Interpreter In Go](https://interpreterbook.com/) and [Writing A Compiler In Go](https://compilerbook.com/).
This is written in C++ and uses [cpp-peglib](https://github.com/yhirose/cpp-peglib) PEG library for lexter and parser.In addition to the original Monkey language spec, this implementation supports the line comment. Macro system in Appendix A is not implemented, yet.
## Install
```bash
$ git clone --recursive https://github.com/yhirose/monkey-cpp.git
$ cd monkey-cpp && mkdir build && cd build
$ cmake ..
$ make$ ./build/cli/monkey
>> puts("hello " + "world!")
hello world!
null
>> quit$ ./build/cli/monkey ../examples/map.monkey
[2, 4, 6, 8]
15
```## PEG grammar
```
PROGRAM <- STATEMENTSSTATEMENTS <- (STATEMENT ';'?)*
STATEMENT <- ASSIGNMENT / RETURN / EXPRESSION_STATEMENTASSIGNMENT <- 'let' IDENTIFIER '=' EXPRESSION
RETURN <- 'return' EXPRESSION
EXPRESSION_STATEMENT <- EXPRESSIONEXPRESSION <- INFIX_EXPR(PREFIX_EXPR, INFIX_OPE)
INFIX_EXPR(ATOM, OPE) <- ATOM (OPE ATOM)* {
precedence
L == !=
L < >
L + -
L * /
}IF <- 'if' '(' EXPRESSION ')' BLOCK ('else' BLOCK)?
FUNCTION <- 'fn' '(' PARAMETERS ')' BLOCK
PARAMETERS <- LIST(IDENTIFIER, ',')BLOCK <- '{' STATEMENTS '}'
CALL <- PRIMARY (ARGUMENTS / INDEX)*
ARGUMENTS <- '(' LIST(EXPRESSION, ',') ')'
INDEX <- '[' EXPRESSION ']'PREFIX_EXPR <- PREFIX_OPE* CALL
PRIMARY <- IF / FUNCTION / ARRAY / HASH / INTEGER / BOOLEAN / NULL / IDENTIFIER / STRING / '(' EXPRESSION ')'ARRAY <- '[' LIST(EXPRESSION, ',') ']'
HASH <- '{' LIST(HASH_PAIR, ',') '}'
HASH_PAIR <- EXPRESSION ':' EXPRESSIONIDENTIFIER <- < [a-zA-Z]+ >
INTEGER <- < [0-9]+ >
STRING <- < ["] < (!["] .)* > ["] >
BOOLEAN <- 'true' / 'false'
NULL <- 'null'
PREFIX_OPE <- < [-!] >
INFIX_OPE <- < [-+/*<>] / '==' / '!=' >KEYWORD <- 'null' | 'true' | 'false' | 'let' | 'return' | 'if' | 'else' | 'fn'
LIST(ITEM, DELM) <- (ITEM (~DELM ITEM)*)?
LINE_COMMENT <- '//' (!LINE_END .)* &LINE_END
LINE_END <- '\r\n' / '\r' / '\n' / !.%whitespace <- ([ \t\r\n]+ / LINE_COMMENT)*
%word <- [a-zA-Z]+
```