2021-02-12 14:53:52 +08:00
2021-05-08 18:54:07 +08:00
< p align = "center" >
2022-04-03 02:39:57 +08:00
< img src = "https://user-images.githubusercontent.com/41085900/161365989-f3fd47bb-7ea7-4114-8e9b-2224e1193079.png" width = "500" >
2021-05-08 18:54:07 +08:00
< / p >
2021-02-12 14:53:52 +08:00
2021-06-15 15:37:49 +08:00
**Pocketlang** is a small (~3000 semicolons) and [fast ](https://github.com/ThakeeNathees/pocketlang#performance )
2021-06-04 16:05:27 +08:00
functional language written in C. It's syntactically similar to Ruby and it can
be learned [within 15 minutes ](https://thakeenathees.github.io/pocketlang/getting-started-learn-in-15-minutes.html ).
2021-06-03 10:14:03 +08:00
Including the compiler, bytecode VM and runtime, it's a standalone executable
2021-06-15 15:37:49 +08:00
with zero external dependencies just as it's self descriptive name. The pocketlang
2021-06-03 10:14:03 +08:00
VM can be embedded in another hosting program very easily.
2021-05-08 18:54:07 +08:00
2021-06-28 21:47:19 +08:00
[Wren Language ](https://wren.io/ ) and their wonderful book [Crafting Interpreters ](http://www.craftinginterpreters.com/ )
were used as a reference to write this language.
2021-05-08 18:54:07 +08:00
2021-06-06 05:51:18 +08:00
## What pocketlang looks like
2021-02-12 14:53:52 +08:00
```ruby
2021-05-12 15:57:51 +08:00
# Python like import statement.
2021-05-19 02:59:09 +08:00
from lang import clock as now
2021-02-12 14:53:52 +08:00
2021-05-12 15:57:51 +08:00
# A recursive fibonacci function.
def fib(n)
if n < 2 then return n end
return fib(n-1) + fib(n-2)
2021-02-12 14:53:52 +08:00
end
2021-05-13 18:31:55 +08:00
# Prints all fibonacci from 0 to 10 exclusive.
for i in 0..10
print(fib(i))
2021-02-12 14:53:52 +08:00
end
```
2021-06-04 09:28:42 +08:00
## Try It Now
You can [try pocketlang on your browser ](https://thakeenathees.github.io/pocketlang/getting-started-try-it-now.html ).
It's a [WebAssembly ](https://webassembly.org/ ) build of the VM compiled using [emscripten ](https://emscripten.org/ ).
2021-06-13 04:17:44 +08:00
Note that in the webassembly version of the language, some features (input, file handling, relative import, etc.)
have disabled, has limited memory allocations, and the stdout calls might be slower.
2021-06-04 09:28:42 +08:00
## Documentation
The pocketlang documentation is hosted at https://thakeenathees.github.io/pocketlang/ which
is built from the `docs` branch generated by a little python script at `docs/generate.py` .
Note that the documentations are WIP and might not be up to date.
2021-06-01 19:50:41 +08:00
## Performance
2021-05-31 08:01:41 +08:00
2022-04-04 11:17:15 +08:00
Pocketlang uses [NaN-Boxing ](https://leonardschuetz.ch/blog/nan-boxing/ ) which is a memory efficient way to represent
dynamic types and dealing with them are much faster. It supports [tail call ](https://en.wikipedia.org/wiki/Tail_call )
[optimization ](https://wiki.c2.com/?TailCallOptimization ). When a function returns a call, the callee can re-use the
caller's stack frame, this will optimize memory from O(n) to O(1) and for [tail recursive ](https://www.youtube.com/watch?v=-PX0BV9hGZY )
it'll completely prevent stackoverflows and yet it's faster.
2021-06-13 04:17:44 +08:00
2021-06-22 16:01:06 +08:00
All benchmarks below were executed on: Windows10 (64bit), ASUS N552VX, Intel Core i7-6700HQ 2.6GHz
2021-06-01 19:50:41 +08:00
with 12GB SODIMM Ram. And the language versions are: pocketlang (pre-alpha), wren v0.3.0,
python v3.7.4, ruby v2.7.2.
2021-05-31 08:01:41 +08:00
2022-03-31 01:50:44 +08:00
![performance ](https://user-images.githubusercontent.com/41085900/120123257-6f043280-c1cb-11eb-8c20-a42153268a0f.png )
2021-05-31 08:01:41 +08:00
2021-06-22 16:01:06 +08:00
The source files used to run benchmarks can be found at `test/benchmarks/`
directory. They were executed using a small python script in the test directory.
2021-05-31 08:01:41 +08:00
## Building From Source
2021-06-18 12:42:57 +08:00
It can be build from source easily without any dependencies, or additional requirements
except for a c99 compatible compiler. It can be compiled with the following command.
2021-05-31 08:01:41 +08:00
2021-06-18 12:42:57 +08:00
#### GCC / MinGw / Clang (alias with gcc)
2021-05-31 08:01:41 +08:00
```
2021-06-20 23:28:31 +08:00
gcc -o pocket cli/*.c src/*.c -Isrc/include -lm
2021-05-31 08:01:41 +08:00
```
2021-06-01 19:50:41 +08:00
#### MSVC
2021-05-31 08:01:41 +08:00
```
cl /Fepocket cli/*.c src/*.c /Isrc/include & & rm *.obj
```
2021-06-18 12:42:57 +08:00
#### Makefile
```
make
```
2022-04-03 02:39:57 +08:00
To run the make file on windows with `mingw` , you require the GNU `make` tool which you can get
from [msys2 ](https://www.msys2.org/ ) or [cygwin ](https://www.cygwin.com/ ).
2021-06-18 12:42:57 +08:00
#### Windows batch script
```
build
```
You don't have to run the script from a Visual Studio .NET developer command prompt, It'll search
2021-06-22 18:44:05 +08:00
for the MSVS installation path and setup the build environment.
2021-06-18 12:42:57 +08:00
2021-06-01 19:50:41 +08:00
### For other compiler/IDE
1. Create an empty project file / makefile.
2. Add all C files in the src directory.
2021-06-16 02:54:30 +08:00
3. Add all C files in the cli directory (**NOT** recursively).
2021-06-02 11:45:12 +08:00
4. Add `src/include` to include path.
2021-06-01 19:50:41 +08:00
5. Compile.
2021-06-28 21:47:19 +08:00
If you weren't able to compile it, please report us by [opening an issue ](https://github.com/ThakeeNathees/pocketlang/issues/new ).
2021-06-01 19:50:41 +08:00
## References
2021-06-13 04:17:44 +08:00
- Bob Nystrom.(2021) *craftinginterpreters* [online] Available at www.craftinginterpreters.com/ (Accessed January 2021)
2021-06-01 19:50:41 +08:00
2021-06-13 04:17:44 +08:00
- Mark W. Bailey, Nathan C. Weston (June 2001) Technical report. *Performance Benefits of Tail Recursion Removal in
Procedural Languages* [online] Available at http://cs.hamilton.edu/~mbailey/pubs/techreps/TR-2001-2.pdf
2021-06-01 19:50:41 +08:00
2021-06-16 05:30:37 +08:00
- Leonard schütz.(2020) *Dynamic Typing and NaN Boxing* [online] Available at https://leonardschuetz.ch/blog/nan-boxing/ (Accessed December 2020)
2021-06-01 19:50:41 +08:00
2021-06-13 04:17:44 +08:00
- Bob Nystrom.(2011) *Pratt Parsers: Expression Parsing Made Easy* [online] Avaliable at http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/ (Accessed December 2020)
2021-05-31 08:01:41 +08:00
2021-06-13 04:17:44 +08:00
- Carol E. (Wolf of Pace University), P. Oser. *The Shunting Yard Algorithm* [online] Available at http://mathcenter.oxford.emory.edu/site/cs171/shuntingYardAlgorithm/ (Accessed September 2020)