Skip to content

lxkast/Glory

Repository files navigation

Glory

A compiler front-end and back-end built from scratch in 1 month.

Note

Please bear in mind Glory is a proof-of-concept, rather than a language that is intended to be used. There are no intentions of updating Glory at the moment.

Windows Installation

Glory currently only supports NASM output for Windows 10. This means an external assembler and linker need to be installed onto your system to use Glory:

  • Install MinGW to C:\MinGW.

  • In MinGW installation manager, install mingw-32-base bin.

image

  • Hit Apply Changes

image

  • Install NASM to C:\MinGW\bin.

  • Go to Advanced System Settings on Windows.

  • Click on Environmental Variables.

image

  • Select Path under User Variables and click the Edit button.

  • Click on New then write C:\MinGW\bin.

  • Check if it works by opening cmd and running nasm --version and gcc --version.

Assembling

  • Create a file called helloworld.asm.
global    _main                
extern    _printf              

segment  .data
	message: db   'Hello world', 10, 0

section .text
_main:                            
        push    message           
        call    _printf 
        add     esp, 4           
        ret 
  • Cd to where you saved the asm file.

  • Assemble using

nasm -f win32 helloworld.asm
  • Link with
gcc helloworld.obj -o helloworld.exe
  • Run the exe from command prompt and check if it works.

image

Glory Installation

Once the assembler and linker is setup on your system, you can install Glory from the Releases section of the repository.

It is recommended to add glorycompiler.exe as an environment variable, as this allows you to run the compiler from any directory.

Usage

  • Create a new file ending with a .glr file extension.

image

image

  • Run the compiler using:
glorycompiler [path to glr file]

image

  • Run the compiled EXE through the command prompt.

image

Features

  • Multidimensional arrays
  • Function call indexing
  • Dead code elimination
  • Recursion
  • Order of operations (1 + 2 * 3 is treated as 1 + (2 * 3))
  • While loops

Limitations

  • No floats, strings or other data types/structures
  • No exponentiation operator
  • No bound checking for arrays
  • No array literals
  • Only able to print integers using printInt()

Example programs

Iterative Factorial

int factorial(int n)
{
    int answer = 1;
    while n > 0
    {
        answer *= n;
        n -=1 ;
    }
    return answer;
}
printInt(factorial(6));

Output: 720

Recursive Factorial

int factorial(int n)
{
	if n == 0
	{
		return 1;
	}
	else
	{
		return n * factorial(n-1);
	}
}

printInt(factorial(5));

Output: 120

Bubble Sort

int[8] sort(int[8] arr)
{
    int i = 0;
    while i < 8
    {
        int j = 0;
        while j < 8 - i - 1
        {
            if arr[j] > arr[j + 1]
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
            j += 1;
        }
        i += 1;
    }
    return arr;
}

int[8] myArr; # = [10,2,5,3,0,10,120,25];
# The pain of having no array literals
myArr[0] = 10; myArr[1] = 2; myArr[2] = 5; myArr[3] = 3;
myArr[4] = 0; myArr[5] = 10; myArr[6] = 120; myArr[7] = 25;

int[8] sorted = sort(myArr);

int i = 0;
while(i < 8)
{
    printInt(sorted[i]);
    i+= 1;
}

Output: 0235101025120