Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/louisdh/cub
The Cub Programming Language
https://github.com/louisdh/cub
ast bytecode compiler interpreter lexer parser virtual-machine
Last synced: about 1 month ago
JSON representation
The Cub Programming Language
- Host: GitHub
- URL: https://github.com/louisdh/cub
- Owner: louisdh
- License: mit
- Created: 2018-02-04T22:19:44.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-25T19:18:22.000Z (over 6 years ago)
- Last Synced: 2024-12-11T10:37:19.328Z (about 2 months ago)
- Topics: ast, bytecode, compiler, interpreter, lexer, parser, virtual-machine
- Language: Swift
- Size: 473 KB
- Stars: 224
- Watchers: 9
- Forks: 17
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Lioness •
Cub •
SavannaKit
The Cub Programming Language
Cub is an interpreted, dynamically typed, scripting language inspired by Swift. This project includes a lexer, parser, compiler and interpreter, all written in Swift.
Cub is used for [OpenTerm](https://github.com/louisdh/openterm)'s scripting feature. A language guide is available in OpenTerm and [online](https://silverfox.be/cub/guide/index.html).
Cub was derived from [Lioness](https://github.com/louisdh/lioness) (my first programming language).The standard library (abbreviated: stdlib) contains basic utility functions, for example to convert from/to dates.
## Source examples
The following Cub code calculates factorials recursively:```swift
func factorial(x) returns {
if x > 1 {
return x * factorial(x - 1)
}
return 1
}a = factorial(5) // a = 120
```The following Cub code uses a ```do times``` loop:
```swift
a = 1
n = 10
do n times {
a += a
}
// a = 1024
```*More examples can be found [here](Source%20examples).*
## External functions
An important feature Cub has is the ability to define external functions. These functions are implemented in native code (for example Swift) and thus allows Cub to call native code.An external function pauses the interpreter, executes the native code, and resumes the interpreter when the native code is executed.
The following example implements a print function:
```swift
let runner = Runner(logDebug: true, logTime: true)
runner.registerExternalFunction(name: "print", argumentNames: ["input"], returns: true) { (arguments, callback) in
for (name, arg) in arguments {
print(arg)
}
callback(nil)
}```
External functions are called like any other global functions in Cub, the print function from the example above could be called like this:
```swift
print("Hello world")
```## Features
* Minimalistic, yet expressive, syntax
* No type system, language is dynamic
* 5 basic operators: ```+```, ```-```, ```/```, ```*``` and ```^```
* ```^``` means "to the power of", e.g. ```2^10``` equals 1024
* all operators have a shorthand, e.g. ```+=``` for ```+```
* Numbers
* All numbers are floating point
* Booleans
* Can be evaluated from comparison
* Can be defined by literal: ```true``` or ```false```
* Strings
* Can be concatenated with the + operator
* Arrays
* Can contain any type, including other arrays
* Functions
* Supports parameters, returning and recursion
* Can be declared inside other functions
* Structs
* Can contain any type, including other structs
* Loops
* ```for```
* ```while```
* ```do times```
* ```repeat while```
* ```break```
* ```continue```
* ```if``` / ```else``` / ```else if``` statements## Running
Since the project does not rely on any dependencies, running it requires no setup.### macOS
Open ```Cub.xcworkspace``` (preferably in the latest non-beta version of Xcode) and run the ```macOS Example``` target. The example will run the code in ```A.cub```. The output will be printed to the console.## Installing framework
### Using Swift Package ManagerAdd to your `Package.swift` file's `dependencies` section:
```swift
.Package(url: "https://github.com/louisdh/cub.git",
majorVersion: 1, minor: 0)
```### Using [CocoaPods](http://cocoapods.org)
Add the following line to your ```Podfile```:
```ruby
pod 'Cub', '~> 1.0'
```### Using [Carthage](https://github.com/Carthage/Carthage)
Add the following line to your ```Cartfile```:```ruby
github "louisdh/cub" ~> 1.0
```
Run ```carthage update``` to build the framework and drag the built ```Cub.framework``` into your Xcode project.## Roadmap
- [x] Structs
- [x] Completion suggestions (given an incomplete source string and insertion point)
- [ ] Breakpoint support in interpreter
- [x] Stdlib documentation
- [ ] Compiler warnings
- [ ] Compiler optimizations
- [x] Faster Lexer (without regex)
- [x] Support emoticons for identifier names
- [ ] ```guard``` statement
- [ ] A lot more unit tests
- [x] Linux support## Xcode file template
Cub source files can easily be created with Xcode, see [XcodeTemplate.md](XcodeTemplate.md) for instructions.## Architecture
A detailed explanation of the project's architecture can be found [here](docs/Architecture.md).## License
This project is available under the MIT license. See the LICENSE file for more info.