Giter Club home page Giter Club logo

go-compiler's Introduction

The GoLF Language

This is a stack-based, multi-pass compiler written in C++ for a programming language called GoLF. This was created as part of a term project for a course at the University of Calgary (CPSC 411) taught by John Aycock. If you are reading this and he is still teaching the course, enroll in it. Right now. Seriously.

Features

You can read through the whole specification here. Otherwise, here are some short snippets of what you can do in GoLF:

  • GoLF has built-in functions:

    • printb(b bool): To print a boolean to the console,
    • printc(c int): To print a character to the console,
    • printb(i int): To print a number to the console,
    • printb(s string): To print a string to the console,
    • getchar(): To receive user input,
    • len(s string): To get the length of a string,
    • halt(): To halts the execution of the program,
  • GoLF has support for arithmetic operators: +, -, /, *, %

  • GoLF has support for comparison operators: ==, <, <=, >=, >

  • GoLF has support for binary operators: &&, ||

  • GoLF has support for unary operators: !, -

  • GoLF supports the break statement in loops

Building

In order to build and compile, you need to install Make plus the toolchain of your choice, e.g. g++ or clang. After you setup Make with your toolchain of choice, clone the repository and build the project as follows:

git clone https://github.com/abodthedude25/Go-Compiler.git
cd golf/<YOUR_LANGUAGE_OF_CHOICE>
make

replace YOUR_LANGUAGE_OF_CHOICE with either src_c for the compiler written in C or src_c++ for the compiler written in C++.

Hello World

To run a "hello world" program, create a file named hello-world.golf on your computer, and place the following inside it:

func main() {
    prints("Hello, world!\n")
}

Then, in a terminal window, navigate to the unzipped folder and run:

$ golf hello-world.golf

The Classic Fibonacci Number Calculator:

func main() {
    var i int
    i = 0

    // Anything larger than 47 overflows a 32-bit int...
    for i <= 47 {
        prints("fib(")
        printi(i)
        prints(") = ")
        printi(fib(i))
        prints("\n")
        i = i + 1
    }
}

func fib(n int) int {
    if n == 0 { return 0 }
    if n == 1 { return 1 }
    return fib(n-1) + fib(n-2)
}

Lifecycle of a GoLF Program

GoLF programs get executed in four separate steps: lexing, parsing, semantic analysis, and code generation.

  • Lexing: The process of breaking down the source code into a stream of tokens, which represent the smallest units of meaning in the programming language.

  • Parsing: The process of analyzing the sequence of tokens to construct an abstract syntax tree, which represents the structure of the program according to the rules of the programming language's grammar.

  • Semantic Analysis: Performs a series of checks on the abstract syntax tree to ensure that it conforms to the rules of the programming language, such as type checking, scoping, and name resolution.

  • Code Generation: Transforms the abstract syntax tree into executable code, generating machine instructions or bytecode for a virtual machine.

go-compiler's People

Contributors

abodthedude25 avatar

Watchers

 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.