• Stars
    star
    243
  • Rank 166,489 (Top 4 %)
  • Language
    Go
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compiler for a small language into x86-64 Assembly

Compiler

This project is a small compiler, that compiles my own little language into X86-64 Assembly. It then uses yasm and ld to assemble and link into a Linux X86-64 executable.

But why?

I've always wanted to write a compiler myself! But just never got around to do it. So the current Coronavirus quarantine situation finally gives me enough time to tackle it myself.

And I was really impressed by people, that wrote solutions for last years adventofcode.com in their own language. So that is something I'd like to achieve :)

I like to work on challenging problems and found compilers intriguing.

How to run

  • go build
  • ./compiler <source_file>
  • ./executable

The compiler will always create an executable called executable. Additionally, it will create a file source.asm that contains the generated (not optimized) assembly.

Dependencies

Everything is written from scratch, there are no code dependencies. But to assemble and link the program into an executable, you need:

  • yasm
  • ld

The resulting Assembly also has no external dependencies (No C std lib, printing is implemented in Assembly directly).

Language influences

  • Go
  • C
  • Python
  • Lua

Features

  • Strong and static type system
  • Multiple return values from functions
  • Automatic packing/unpacking of multiple arguments and/or function returns
  • Function overloading
  • Function inlining (only for system functions right now)
  • Dynamic Arrays with an internal capacity, so not every append needs a new memory allocation
  • Int, Char, String and Float types are always 64bit
  • Very Python-like array creation
  • Switch expressions match either values or general boolean expressions
  • Range-based Loops with index and element
  • Structs

Examples

See the compiler_test.go file for a lot more working examples :)

Print

// There are overloaded functions: print, println that work on int, float, char, bool and string
println(5)
println(6.543)
println('b')
println("abc")
println(true)

Assignment

// Types are derived from the expressions!
a = 4
b = 5.6
c = true
d = 'f'
e = "abc"
f = a + 5 + int(b) // 14

Functions

fun abc(i int, j float) int, float, char, string {
    return i, j, 'b', "abc"
}
// Can be overloaded
fun abc(i int, j int) int, float, char, string {
    return i, 5.5, 'a', "abc"
}
// ...
a, b, c, d = abc(5, 6.5)

Lists

// List of integers. Type derived from the expressions. Every values must have the exact same type.
list = [1, 2, 3, 4, 5]
// Strings can be explicitely defined as a list of chars or implicitely by using string literals
// Internally, they are handled the same way.
chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
str = "abcdefg"

// Empty list of integers with length 10. Type explicitely set
list2 = [](int, 10)
// Lists can naturally contain other lists
list3 = [list, list2]
// There are build-in functions, to get the length and capacity
println(len(list3))
println(cap(list3))

// You have to free them yourself
free(list)

// And some convenience functions to clear/reset the list without deallocating the memory:
// reset only resets the length, while clear overwrites the memory with 0s
reset(list)
clear(list)

// Build-in append/extend function, similar to the one in Go
// Careful ! append/extend works on the first argument. Depending on the available capacity, it will
// extend list or free list and create a completely new memory block, copy list and list2 over and return
// the new pointer!
list = append(list, 6)
list = extend(list, list2)

// Lists in functions/structs
fun abc(a []int) {
    // ...
}

Loops

list = [1,2,3,4,5]

for i = 0; i < len(list); i++ {
    // ...
}

for i,e : list {
    // i is the current index
    // e is the actual element: list[i]
}

for i, c : "abcdefg" {
    print(c)
}
// will print in: abcdefg

Conditions

a = true
b = 3 > 4
if a && !b {
    println("abc")
}

Switch

// We have no fallthrough
switch 4 {
case 1:
    println("case 1")
case 2, 3, 6:
    println("case 2")
case 5:
    println("case 3")
default:
    println("default case")
}

// switches do not need a value to check against.
// With no value provided, each case will be checked after each other for an expression that evaluates to true.
switch {
case 2 > 3:
    println("case 1")
case 2 == 3:
    println("case 2")
default:
    println("default case")
}

Structs

struct B {
    i int
    j int
}
struct A {
    i int
    j B
}

// Structs are created by calling a function with the same name and an exact match of parameters
// that match the expected types of the struct.
// Internally, this is just syntax, not a function. So there is no overhead!
a = A(1, B(3, 4))
a.j.j = 100
println(a.i)
println(a.j.j)

Type conversions

// Build-in (inline) functions: int(), float()
println(int(5.5))
println(float(5))

// Chars can be converted from and to int (ascii value) by using char() or int()
println(int('a'))
println(char(98))

More Repositories

1

Delaunay_Triangulation

My own implementation of a Delaunay triangulation and Voronoi partition in Python applied to images.
Python
255
star
2

Partikel_accelleration_on_GPU

Particle accelleration with OpenGL 4.3, using the compute shader to calculate particle movement on graphics hardware.
C
253
star
3

skiplist

A Go library for an efficient implementation of a skip list: https://godoc.org/github.com/MauriceGit/skiplist
Go
252
star
4

Simple_GLSL_Shader_Example

A very simple example of how shaders in OpenGL can be used, to color Objects or map a texture on some triangles.
C
239
star
5

Voronoi_Image_Manipulation

A system independent tool for interactive image manipulation with Voronoi and Delaunay data structures.
Go
227
star
6

Water_Simulation

Water-Simulation with real time specular reflection on the waters surface. The reflection is implemented in GLSL and runs on the GPU and in screen space. The water itself is implemented using a pressure based approach for the surface calculation.
C
149
star
7

XBox_Controller_Linux_Interface

An interface that interacts with an XBox One controller via the usb stream. With simple methods for object or camera control (i.e. for OpenGL contexts).
C
132
star
8

Cloth_Simulation

Cloth-Visualization via particle-simulation.
C
83
star
9

Advanced_Algorithms

Quick implementations of some advanced algorithms for searching, sorting and trees
Python
77
star
10

Screen_Space_Ambient_Occlusion

Giving a scene a depth-dependent shading, completely irregarding the scene complexity, in screen-space.
C
47
star
11

Quaternion_Library

A small library that capsulates most commonly used operations on Quaternions. Also a small sample implementation, that rotates an object around an axis, using Quaternions.
C
40
star
12

Energy-Dome_Terrain

A visually appealing terrain visualization from real-world data with some extras, such as an animated energy dome, LOD tessellation and multisampling
Go
29
star
13

advsearch

A Go library for advanced searching in sorted data structures
Go
22
star
14

Repeat_History

A small but useful command for a linux shell. It makes the bash history more easily accessible than cmd+r.
Shell
19
star
15

Marching_Cubes_on_GPU

A marching cube algorithm, that is executed in parallel on the GPU, using compute shaders. This will later enable a highly parallel creation of advanced landscape/terrain structures in potentially real-time (next project).
Go
9
star
16

tree23

An implementation of a balanced 2,3-tree that allows accessing next/previous elements in O(1) at all times.
Go
8
star
17

Vector_Library

Small vector library in C, containing all basic vector operations.
C
4
star
18

2D_Bin_Packing

several parallel solutions for a 2D bin packing problem for a programming contest.
Python
2
star
19

Variance_Shadow_Maps

Small example of a project, that uses variance shadow maps with hardware Multisampling.
Go
2
star
20

Voronoi_DivideAndConquer

A Go implementation of a voronoi tessellation with the divide and conquer algorithm. It is still work in progress and not yet working properly!
Go
2
star
21

mtVector

A short vector library with all necessary operations for a Delaunay triangulation, including optimized matrix multiplication
Go
1
star
22

game

JavaScript
1
star
23

Agar_Clone_Joystick_Client

A C++ client for our programming challenge (2016) supporting an X-BOX-Controller.
C++
1
star
24

FastDelaunayImages

Go
1
star
25

AdventOfCode

My solutions for the advent of code puzzles (AOC)
Python
1
star
26

Artificial_Neural_Network_Character_Classification

Implementation of an artificial neural network with backpropagation for classification of characters.
Python
1
star
27

Bachelor_Thesis_Arimaa

Implementation of the practical part of my bachelor thesis for the FH-Wedel. The thesis has been awarded with the "Wedeler Hochschulpreis" for innovation. The code implements an artificial intelligence which plays the game of Arimaa.
Java
1
star