Coder Social home page Coder Social logo

wabiverse / galah Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stackotter/galah

0.0 0.0 0.0 5.98 MB

A scripting language with the goal of being lightweight and embeddable in Swift applications.

Home Page: https://stackotter.github.io/galah/

Shell 0.51% JavaScript 36.41% Swift 63.02% HTML 0.06%

galah's Introduction

galah

A scripting language with the goal of being lightweight and embeddable in Swift applications.

Trying it out

Online playground

The Galah playground uses WASM to run a Galah interpreter in your browser! Visit it in a modern browser to try out Galah without the need to install it locally.

Building locally

The following commands build the interpreter's CLI from source and run the sample.galah file included in this repository,

git clone https://github.com/stackotter/galah
cd galah
swift run galah ./sample.galah

Syntax (WIP, mostly not implemented yet)

This syntax is all bound to change! Nothing is set in stone, and even the style of the language isn't completely decided yet.

The following example program is strongly typed but relies heavily on type inference instead of type annotations. Still not sure whether omitting return type annotations is a good idea or not. With good enough LSP support it should be fine, but LSPs won't always be that useful when using user-provided built-in functions etc (there's a limit to how much the LSP can know without some sort of interface file).

struct Citizen {
    let name: Str
    let age: Int

    // `name` is inferred to be of type `Str`.
    init(name) {
        self.name = name
        self.age = 0
    }

    fn randomName() -> Str {
        ["Steve Apple", "John Smith"].randomChoice()
    }

    // Return type is inferred as `(Str, Optional<Str>)`
    fn parsedName(self) {
        let parts = self.name.split(" ", maxSplits: 1)
        return (parts[0], parts.last)
    }

    // We could infer that it throws for the user, but that might not be a good idea. Note that
    // this method definitely shouldn't throw in any sane software, this is just an example.
    fn incrementAge(mut self) throws {
        self.age += 1
        if self.age > 100 || Int.random(0, 80) == 0 {
            throw "Citizen died"
        }
    }
}

fn main() {
    let stackotter = Citizen("stackotter")
    for _ in 0..<80 {
        do {
            try stackotter.incrementAge()
        } catch {
            eprint("stackotter died at {stackotter.age}")
            exit(1)
        }
    }

    print("stackotter is {stackotter.age}")
    let (first, last) = stackotter.parsedName()
    print("stackotter's first name is {first} and his last name is \(last ?? "unknown")")
}

Here's the same program but written in Python as a comparison,

from typing import Optional

class Citizen:
    name: Str
    age: Int

    def init(self, name: str, age: int):
        self.name = name
        self.age = age

    @classmethod
    def randomName(cls) -> str {
        return ["Steve Apple", "John Smith"].randomChoice()

    def parsedName(self) -> (str, Optional[str]):
        let parts = self.name.split(" ", 1)
        return (parts[0], parts[1] if len(parts) == 2 else None)

    def incrementAge(self):
        self.age += 1
        if self.age > 100 or random.randrange(0, 80) == 0:
            raise Exception("Citizen died")

if __name__ == "__main__":
    stackotter = Citizen("stackotter")
    for _ in range(80):
        try:
            try stackotter.incrementAge()
        catch Exception:
            print("stackotter died at {stackotter.age}")
            exit(1)

    print(f"stackotter is {stackotter.age}")
    first, last = stackotter.parsedName()
    print(f"stackotter's first name is {first} and his last name is \(last ?? 'unknown')")

galah's People

Contributors

stackotter avatar furby-tm avatar

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.