Giter Club home page Giter Club logo

cub's Introduction

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.

cub's People

Contributors

louisdh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cub's Issues

Highlight code ranges in diagnostics

It would be really nice if diagnostics pointed to the code they’re diagnosing, more than just Error on line 2: xxx.

Clang/Swift’s diagnostic formatting is really nice, and I’d love to see something more like that.

foo.cub:10:2 error: unexpected token
   for (), x > 12, x += 1 {
       ^

Bonus points for ANSI coloring.

Simpler way

Can you make a more simpler way of installing the interpreter? Like using brew or something.

Lexer

The lever involves some very complicated operations. You have hashing as a token mapper and all these string operations. Since String has variable lengths encoding some of the O(n) operations are secretly O(n^2). I have the same problem in my lexer for Swizzle but I use string indices rather than this so the string only has to scan starting from the current index.

No diagnostic raised for failure to return

There’s currently no diagnostic raised when a function declared as returns doesn’t return a value.

This can be tricky, as you have to prove all paths through the function body actually return.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.