https://github.com/oboard/moonbit-eval
MoonBit language interpreter
https://github.com/oboard/moonbit-eval
moonbit
Last synced: 5 months ago
JSON representation
MoonBit language interpreter
- Host: GitHub
- URL: https://github.com/oboard/moonbit-eval
- Owner: oboard
- License: apache-2.0
- Created: 2024-10-16T18:10:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-01-18T16:58:11.000Z (5 months ago)
- Last Synced: 2026-01-19T00:46:38.858Z (5 months ago)
- Topics: moonbit
- Language: MoonBit
- Homepage: https://mooncakes.io/docs/oboard/moonbit-eval
- Size: 3.14 MB
- Stars: 11
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MoonBit Eval
[](https://mooncakes.io/docs/oboard/moonbit-eval)
[](https://github.com/oboard/moonbit-eval/actions/workflows/check.yaml)
[](https://github.com/oboard/moonbit-eval/blob/main/LICENSE)
## Demo
đ **[REPL Demo](https://github.com/oboard/moonbit-repl/releases/)**
đ **[Notebook Demo https://moonbit-notebook.oboard.fun/](https://moonbit-notebook.oboard.fun/)**
## Introduction
MoonBit Eval is an interpreter for the MoonBit language.
Built on top of the [@moonbitlang/parser](https://github.com/moonbitlang/parser) library, it provides comprehensive and accurate MoonBit syntax support, capable of correctly parsing and executing complex MoonBit code structures including functions, structs, lambdas, loops, and more.
## Quick Start
```moonbit
let vm = MoonBitVM::new()
// Basic expressions
inspect(vm.eval("1 + 2 * 3"), content="7")
inspect(vm.eval("\"hello\" + \" world\""), content="hello world")
// Variables and functions
inspect(vm.eval("let x = 42"), content="()")
inspect(vm.eval("fn double(n: Int) -> Int { n * 2 }"), content="(n: Int) -> Int")
inspect(vm.eval("double(x)"), content="84")
// Control flow
inspect(vm.eval("if x > 40 { \"big\" } else { \"small\" }"), content="big")
// Pattern matching
inspect(vm.eval("match (1, 2) { (a, b) => a + b }"), content="3")
```
## ⨠Features
- â
**đĨŽ Mooncakes Loader**: Load Mooncakes packages at runtime
- â
**Builtin FileSystem Library**: Provides basic file system operations.
- â
**Eval Function**: Allows dynamic evaluation of MoonBit code strings.
## MoonBit Language Support
| Feature | Status | Description |
|---------|--------|-------------|
| **Core Language** | | |
| Basic Types (Int, Bool, String, Double, Char) | â
| Full support for primitive types |
| Expressions (arithmetic, logical, comparison) | â
| Complete expression evaluation |
| Variables (let, let mut) | â
| Immutable and mutable variables |
| Assignment | â
| Variable reassignment and shadowing |
| Multiline strings | â
| #\|syntax for multiline string literals |
| String interpolation | â
| \{variable} syntax in string literals |
| Type constraints | â
| (value : Type) syntax for explicit typing |
| **Control Flow** | | |
| If-else | â
| Conditional expressions |
| For loops | â
| C-style for loops with continue/break |
| While loops | â
| While loop constructs with else clause |
| Loop control | â
| Continue and break statements |
| Guard expressions | â
| guard condition else { action } syntax |
| Is expressions | â
| Pattern matching with 'is' operator |
| Defer expressions | â
| defer statement for cleanup code |
| Return expressions | â
| Early return from functions |
| Raise expressions | â
| Exception throwing with raise |
| Try-catch expressions | â
| Exception handling with try-catch |
| Loop expressions | â
| loop pattern matching with break/continue |
| **Functions** | | |
| Function definitions | â
| Named functions with parameters |
| Named parameters | â
| Named and optional parameters |
| Lambda expressions | â
| Anonymous functions (x => x * 2) |
| Closures | â
| Proper closure environment capture |
| Recursive functions | â
| Self-referencing function calls |
| Currying | â
| Higher-order function composition |
| External functions | â
| Integration with external calls |
| Embedded functions | â
| Native function integration |
| **Data Structures** | | |
| Arrays | â
| Array creation, indexing, assignment |
| Array methods | â
| length, get, push, pop, contains, slice, concat, join |
| Array boolean methods | â
| any, all operations |
| Array spread syntax | â
| [..array1, ..array2] syntax |
| Array slice operations | â
| arr[start:end], arr[start:], arr[:end] syntax |
| Array augmented assignment | â
| arr[i] += value, arr[i] *= value syntax |
| Tuples | â
| Tuple creation, access, destructuring |
| Structs | â
| Custom data types with methods |
| Mutable struct fields | â
| Field mutation support |
| Nested struct references | â
| Reference semantics for nested structures |
| Record update syntax | â
| { ..record, field: new_value } syntax |
| Map literals | â
| { "key": value } syntax for map creation |
| **Pattern Matching** | | |
| Basic patterns | â
| Constants, variables, wildcards |
| Tuple patterns | â
| Destructuring tuples |
| Array patterns | â
| Array destructuring |
| Record patterns | â
| Struct field matching |
| Range patterns | â
| Range expressions (_.. operator for function chaining |
| Function aliases | â
| Alias support (e.g., not for %bool_not) |
| Cross-package method calls | â
| Method calls across different packages |
| Error handling | â
| Result type error handling |
| Group expressions | â
| Parenthesized expressions for precedence |
| For-in loops | â
| Iterator-based loops |
| Iterator methods | â
| iter, map, filter, reduce, for_each |
| Nested iteration | â
| Complex nested loop structures |
| Iterator control flow | â
| break/continue in iterator contexts |
| **Package System** | | |
| Module imports | â
| @package.function syntax |
| Cross-package types | â
| Using types from other packages |
| Built-in packages | â
| @int, @math, @bigint, @cmp, @list support |
| Package method calling | â
| Method calls across package boundaries |
| **IO and FFI** | | |
| Print functions | â
| println and print support |
| Embedded functions | â
| Native function integration via FFI |
| External function binding | â
| Custom function registration |
| **Sorting and Collections** | | |
| List sorting | â
| Built-in sort methods for collections |
| Array sorting | â
| Sorting operations on arrays |
| Collection methods | â
| Comprehensive collection manipulation |
| **Comparison Operations** | | |
| Equality operators | â
| == and != operators |
| Relational operators | â
| <, >, <=, >= operators |
| Type-aware comparison | â
| Proper type checking in comparisons |
| **Constructor Patterns** | | |
| Single argument matching | â
| Constructor pattern with single args |
| Named field matching | â
| Constructor patterns with named fields |
| Wildcard patterns | â
| _ patterns in constructor matching |
| **Functional Programming** | | |
| Higher-order functions | â
| Functions as first-class values |
| Function composition | â
| Combining functions effectively |
| Closure environments | â
| Proper variable capture in closures |
| **Literal Overloading** | | |
| Numeric literal overloading | â
| Automatic conversion between numeric types |
| Character literal overloading | â
| Char to Int conversion in pattern matching |
| String literal overloading | â
| String to Bytes conversion |
| Array literal overloading | â
| Array to various types (Bytes, String) conversion |
| Double literal overloading | â
| Double to Float precision conversion |
| Map literal overloading | â
| Map to Json object conversion |
| Complex overloading scenarios | â
| Multi-step type conversions |
| Traits | đĄ | Interface definitions |
| Trait as expressions | đĄ | (value as Trait) syntax for trait casting |
| Packages | đĄ | Module system with @package.function syntax (no trait, trait derive, operator overloading) |
| **Attribute** | | |
| #alias | â
| Function alias |
| #external | â | External function binding |
| #callsite | â | Call site information |
| #skip | â | Skipping compilation of a function |
| #cfg | â | Conditional compilation based on configuration |
| **Not Yet Supported** | | |
| Async/await | â | Asynchronous programming |
## Contributing
We welcome contributions to the MoonBit Eval project! Whether it's bug fixes, feature additions, or documentation improvements, your contributions are valuable.
## Community
Join our community for discussions and support:
- QQ Group: **949886784**
