Giter VIP home page Giter VIP logo

colang's Introduction

colang - CO language compiler

CO is a language designed for use in programming olympiads and contests like IOI and ACM ICPC. It features a simple C-style syntax, opinionated algorithm-oriented standard library, C++ tier performance, and many small bits that make solving tasks easier and more fun.

colang translates CO source code into C source code, so you can use CO on every judge that accepts C or C++.

Status

colang is in early development stage, so it can't do much yet. This section will be updated when most core language features will be implemented, so stay tuned!

Building

If you still want to try it out right now, grab the source, install SBT, and prepare your build environment. You need two things to successfully build colang: CO standard library and GCC binary on your PATH.

CO standard library

colang depends on the CO standard library that must be present at any of the following locations: ~/.colang-libs/, /usr/local/lib/colang, /usr/lib/colang, /lib/colang. The standard library is included in this repo (the stdlib directory), so the most simple and reliable installation method is creating a symlink from ~/.colang-libs/ to the stdlib directory. On Unix-like systems this can be done like this:

ln -s path/to/repo/stdlib ~/.colang-libs

On Windows, run cmd.exe as administrator and in your home directory (C:\Users\<you>\) execute

mklink /D .colang-libs\ path\to\repo\stdlib

Assembly

Once you have GCC and CO stdlib ready, build the compiler with sbt assembly. It will produce an executable standalone JAR file under target/scala-2.11.

Compiling and running programs

You can now compile and execute the solution to the A + B problem:

void main() {
    int a, b
    read(a)
    read(b)
    println(a + b)
}

The syntax is not final: a more convenient I/O interface will be eventually supported.

Let's go for a more fun example. At this point, you can already solve quadratic equations with CO:

double abs(double x) {
    if (x >= 0.0) return x else return -x
}

double sqr(double x) {
    return x * x
}

double EPS = 1.0e-9

//If we have no sqrt(), we can just write our own :)
double sqrt(double x) {
    double r = 10.0

    while (abs(r * r - x) > EPS) {
        r = r - (sqr(r) - x) / (2.0 * r)
    }

    return r
}

void main() {
    double a, b, c
    read(a)
    read(b)
    read(c)

    double det = sqr(b) - 4.0 * a * c
    if (det < 0.0) {
        //Can't print strings yet :(
        println(-1)
        return
    }

    double r1 = (-b - sqrt(det)) / (2.0 * a)
    double r2 = (-b + sqrt(det)) / (2.0 * a)

    println(r1)
    println(r2)
}

For the curious, the generated code looks like this.

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.