Coder Social home page Coder Social logo

carmo-evan / borgo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from borgo-lang/borgo

0.0 0.0 0.0 1.89 MB

Borgo is a statically typed language that compiles to Go.

Home Page: https://borgo-lang.github.io

JavaScript 1.17% Go 4.47% Rust 89.63% TypeScript 3.64% CSS 0.43% Nix 0.16% Just 0.50%

borgo's Introduction

The Borgo Programming Language

Borgo sits between Go and Rust


build

I want a language for writing applications that is more expressive than Go but less complex than Rust.

Go is simple and straightforward, but I often wish it offered more type safety. Rust is very nice to work with (at least for single threaded code) but it's too broad and complex, sometimes painfully so.

Borgo is a new language that transpiles to Go. It's fully compatible with existing Go packages.

Borgo syntax is similar to Rust, with optional semi-colons.

Tutorial

Check out the online playground for a tour of the language.

You can also take a look at test files for working Borgo code:

Features

Algebraic data types and pattern matching

use fmt

enum NetworkState {
    Loading,
    Failed(int),
    Success(string),
}

let msg = match state {
    NetworkState.Loading => "still loading",
    NetworkState.Failed(code) => fmt.Sprintf("Got error code: %d", code),
    NetworkState.Success(res) => res,
}

Option<T> instad of nil

// import packages from Go stdlib
use fmt
use os

let key = os.LookupEnv("HOME")

match key {
    Some(s) => fmt.Println("home dir:", s),
    None => fmt.Println("Not found in env"),
}

Result<T, E> instad of multiple return values

use fmt
use net.http

fn makeRequest() -> Result<int, error> {
    let request = http.Get("http://example.com")

    match request {
        Ok(resp) => Ok(resp.StatusCode),
        Err(err) => Err(fmt.Errorf("failed http request %w", err))
    }
}

Error handling with ? operator

use fmt
use io
use os

fn copyFile(src: string, dst: string) -> Result<(), error> {
    let stat = os.Stat(src)?

    if !stat.Mode().IsRegular() {
        return Err(fmt.Errorf("%s is not a regular file", src))
    }

    let source = os.Open(src)?
    defer source.Close()

    let destination = os.Create(dst)?
    defer destination.Close()

    // ignore number of bytes copied
    let _ = io.Copy(destination, source)?

    Ok(())
}

Guessing game example

Small game from the Rust book, implemented in Borgo.

Things to note:

  • import packages from Go stdlib
  • strconv.Atoi returns an Option<int>
  • Reader.ReadString returns a Result<string, error> (which can be unwrapped)
use bufio
use fmt
use math.rand
use os
use strconv
use strings
use time

fn main() {
    let reader = bufio.NewReader(os.Stdin)

    rand.Seed(time.Now().UnixNano())
    let secret = rand.Intn(100) + 1

    loop {
        fmt.Println("Please input your guess.")

        let text = reader.ReadString('\n').Unwrap()
        let text = strings.TrimSpace(text)

        let guess = match strconv.Atoi(text) {
            Ok(n) => n,
            Err(_) => continue,
        }

        fmt.Println("You guessed: ", guess)

        if guess < secret {
            fmt.Println("Too small!")
        } else if guess > secret {
            fmt.Println("Too big!")
        } else {
            fmt.Println("Correct!")
            break
        }
    }
}

Running locally

Borgo is written in Rust, so you'll need cargo.

To compile all .brg files in the current folder:

$ cargo run -- build

The compiler will generate .go files, which you can run as normal:

# generate a go.mod file if needed
# $ go mod init foo
$ go run .

borgo's People

Contributors

alpacaaa 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.