All Projects → louisdh → Cub

louisdh / Cub

Licence: mit
The Cub Programming Language

Programming Languages

swift
15916 projects
bytecode
52 projects

Projects that are alternatives of or similar to Cub

Lioness
The Lioness Programming Language
Stars: ✭ 155 (-21.72%)
Mutual labels:  compiler, ast, lexer, parser, interpreter, virtual-machine
Webassemblyjs
Toolchain for WebAssembly
Stars: ✭ 566 (+185.86%)
Mutual labels:  compiler, ast, parser, interpreter
Charly Vm
Fibers, Closures, C-Module System | NaN-boxing, bytecode-VM written in C++
Stars: ✭ 66 (-66.67%)
Mutual labels:  compiler, ast, lexer, parser
Snapdragon
snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory.
Stars: ✭ 180 (-9.09%)
Mutual labels:  compiler, ast, lexer, parser
Tiny Compiler
A tiny compiler for a language featuring LL(2) with Lexer, Parser, ASM-like codegen and VM. Complex enough to give you a flavour of how the "real" thing works whilst not being a mere toy example
Stars: ✭ 425 (+114.65%)
Mutual labels:  compiler, ast, lexer, parser
Exprtk
C++ Mathematical Expression Parsing And Evaluation Library
Stars: ✭ 301 (+52.02%)
Mutual labels:  compiler, ast, lexer, parser
Forge
A lightweight, elegant scripting language with built-in Rust-FFI.
Stars: ✭ 153 (-22.73%)
Mutual labels:  compiler, parser, interpreter
Tiny Compiler
A tiny evaluator and compiler of arithmetic expressions.
Stars: ✭ 680 (+243.43%)
Mutual labels:  compiler, ast, interpreter
Mico
Mico ("Monkey" in catalan). Monkey language implementation done with C++. https://interpreterbook.com/
Stars: ✭ 19 (-90.4%)
Mutual labels:  lexer, parser, interpreter
Csstree
A tool set for CSS including fast detailed parser, walker, generator and lexer based on W3C specs and browser implementations
Stars: ✭ 1,121 (+466.16%)
Mutual labels:  ast, lexer, parser
Ph7
An Embedded Implementation of PHP (C Library)
Stars: ✭ 422 (+113.13%)
Mutual labels:  compiler, interpreter, virtual-machine
Cymbal
Yet another Rust implementation of the Monkey language from "Writing an Interpreter in Go" and "Writing a Compiler in Go"
Stars: ✭ 49 (-75.25%)
Mutual labels:  compiler, interpreter, virtual-machine
Rs Monkey Lang
Monkey Programming Language written in Rust.
Stars: ✭ 80 (-59.6%)
Mutual labels:  lexer, parser, interpreter
Minigo
minigo🐥is a small Go compiler made from scratch. It can compile itself.
Stars: ✭ 456 (+130.3%)
Mutual labels:  compiler, lexer, parser
Participle
A parser library for Go
Stars: ✭ 2,302 (+1062.63%)
Mutual labels:  ast, lexer, parser
Monkey Rust
An interpreter for the Monkey programming language written in Rust
Stars: ✭ 174 (-12.12%)
Mutual labels:  lexer, parser, interpreter
Go.vm
A simple virtual machine - compiler & interpreter - written in golang
Stars: ✭ 178 (-10.1%)
Mutual labels:  compiler, interpreter, virtual-machine
Ratel Core
High performance JavaScript to JavaScript compiler with a Rust core
Stars: ✭ 367 (+85.35%)
Mutual labels:  compiler, ast, parser
Php Parser
🌿 NodeJS PHP Parser - extract AST or tokens (PHP5 and PHP7)
Stars: ✭ 400 (+102.02%)
Mutual labels:  ast, lexer, parser
Graphql Go Tools
Tools to write high performance GraphQL applications using Go/Golang.
Stars: ✭ 96 (-51.52%)
Mutual labels:  ast, lexer, parser

Lioness Cub SavannaKit

Cub Logo

The Cub Programming Language

Travis build status Codecov
version 1.0.1 Carthage Compatible Swift Platform: iOS macOS tvOS watchOS Extension: .cub
Twitter Donate via PayPal

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's scripting feature. A language guide is available in OpenTerm and online. Cub was derived from 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:

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:

a = 1
n = 10
do n times {
    a += a
}
// a = 1024

More examples can be found here.

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:

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:

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 Manager

Add to your Package.swift file's dependencies section:

.Package(url: "https://github.com/louisdh/cub.git",
		         majorVersion: 1, minor: 0)

Using CocoaPods

Add the following line to your Podfile:

pod 'Cub', '~> 1.0'

Using Carthage

Add the following line to your Cartfile:

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 for instructions.

Architecture

A detailed explanation of the project's architecture can be found here.

License

This project is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].