• Stars
    star
    220
  • Rank 174,684 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The Cub Programming Language

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

  • Structs
  • Completion suggestions (given an incomplete source string and insertion point)
  • Breakpoint support in interpreter
  • Stdlib documentation
  • Compiler warnings
  • Compiler optimizations
  • Faster Lexer (without regex)
  • Support emoticons for identifier names
  • guard statement
  • A lot more unit tests
  • 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.

More Repositories

1

panelkit

A UI framework that enables panels on iOS.
Swift
3,800
star
2

openterm

OpenTerm, a sandboxed command line interface for iOS
Swift
1,614
star
3

savannakit

A high-performance, protocol oriented, framework for creating native IDEs for iOS and macOS, written in Swift
Swift
870
star
4

source-editor

A native source editor for iOS and macOS, written in Swift
Swift
740
star
5

textor

A plain text editor for iOS
Swift
587
star
6

huekit

A UI framework for iOS that provides components and utilities for building color pickers.
Swift
208
star
7

pointerkit

A proof of concept framework to use a pointing device on iOS
Objective-C
170
star
8

lioness

The Lioness Programming Language
Swift
167
star
9

bezierpath-polygons

Adds a convenience initalizer to UIBezierPath for generating n-sided regular polygon paths โ€“ with rounded corners support โ€“ in Swift!
Swift
156
star
10

bezierpath-length

A simple API to get the length of a CGPath, UIBezierPath or NSBezierPath, written in Swift.
Swift
85
star
11

savanna

A native iOS & macOS IDE for the Cub programming language
Swift
73
star
12

icns2png

A Swift command line script that converts .icns files to .png files at various sizes
Swift
34
star
13

microcontroller-kit

Value types, operators and functions for tinkering with microcontrollers in software.
Swift
20
star
14

KeyCommandAlertController

UIAlertController wrapper to add keyboard shortcuts easily
Swift
19
star
15

markdown-table-gen

A script that generates a Markdown table from an array of strings for a given number of columns
Swift
17
star
16

java-class-quick-look

Quick Look plugin to display Java class files as bytecode
C
12
star
17

silverfox-gen

Generator for the static content of silverfox.be
Swift
9
star
18

sierpinski-curve-swift

Generates a Sierpiล„ski curve using a turtle mechanism in Swift.
Swift
9
star
19

cmd-almanac

A list of commands I frequently use
6
star
20

panelkit-handle-view

IBDesignables to play around with corner radii of the resize handles in PanelKit.
Swift
5
star
21

openterm-scripts-library

Example scripts for OpenTerm, written in Cub
Swift
4
star
22

cub-guide

A guide for the Cub programming language
HTML
3
star
23

sniper

A basic 2D physics engine written in C#
C#
1
star
24

subnet-manager-java

A subnet manager written in Java (college assignment)
Java
1
star
25

silverfox-site

The HTML, CSS & JavaScript of silverfox.be
JavaScript
1
star
26

silverfox-news

The news items of silverfox.be
1
star