Skip to content

Running the Compiler

Davide Paro edited this page Dec 15, 2020 · 9 revisions

Running the DPCC compiler

If you didn't already did it please you should to clone and build the compiler.

If every step was made correctly and the build successed, the final built executable will then be placed in run_tree/bin/dpcc

You can then run the compiler with

./run_tree/bin/dpcc

which will spit out it's usage help message

Using the compiler

  • ./run_tree/bin/dpcc lex <input> [-o <out>]: Lex the input and show the list of tokens composing the DPL source in either stdout or in the given file.
  • ./run_tree/bin/dpcc parse <input> [-o <out>]: Parse the program and produce a text representation of the AST (Abstract Syntax Tree) in either stdout or in the given file.
  • ./run_tree/bin/dpcc 3ac <input> [-o <out>]: Parse the program and perform additional type validations and type checking. If the program is still valid emit 3AC in either stdout or in the given file.
  • ./run_tree/bin/dpcc c <input> [-o <out>]: Same as 3AC but also emit preamble and postamble required to promote 3AC to a valid C program that can be compiled. The output is emitted in either stdout or in the given file.
  • ./run_tree/bin/dpcc gcc <input> [-o <out>]: Same as dpcc c but the generated C program is piped into GCC standard input and the final executable is either compiled in a.out or in the given filepath. This requires GCC to be in the path.
  • ./run_tree/bin/dpcc run <input>: Parse, typecheck, emit the C code, compile it and run it in one single command. The executable produced by GCC is outputted in a temp file (under /tmp), the temp executable is executed right away and then removed. The -o flag is ignored. This requires GCC to be in the path.

Write your first DPL script

Create a file named my_script.dpl wherever you like (the extension doesn't really matter but it's nice to have).

You can put the following code in it:

let len = 32;

let array = [
    15, 59, 61, 75, 12, 71,  5, 35, 44,
    6, 98, 17, 81, 56, 53, 31, 20, 11,
    45, 80,  8, 34, 71, 83, 64, 28,  3,
    88, 50, 48, 80,  5
];

print("Unsorted array\n");
print(array);

for (let cs = 1; cs < len; cs = 2 * cs) {
    for (let l = 0; l < len - 1; l = l + 2 * cs) {
        let m = len - 1;

        if ((l + cs - 1) < len - 1) {
            m = l + cs - 1;
        }

        let r = len - 1;

        if ((l + 2 * cs - 1) < len - 1) {
            r = l + 2 * cs - 1;
        }

        let n1 = m - l + 1;
        let n2 = r - m;

        let L: int[1024];
        let R: int[1024];

        for (let i = 0; i < n1; i++) {
            L[i] = array[l + i];
        }

        for (let i = 0; i < n2; i++) {
            R[i] = array[m + 1 + i];
        }


        let i = 0;
        let j = 0;
        let k = l;

        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                array[k++] = L[i++];
            } else {
                array[k++] = R[j++];
            }
        }

        while (i < n1) {
            array[k++] = L[i++];
        }
        while (j < n2) {
            array[k++] = R[j++];
        }
    }
}

print("Sorted array\n");
print(array);

which is an implementation of an iterative merge sort algorithm that shows DPL language and it strees a nice amount of language features.

You can then call ./run_tree/bin/dpcc run ./my_script.dpl assuming that it was placed at the root of this repo and you should obtain the following output:

Unsorted array
array = [ 15, 59, 61, 75, 12, 71, 5, 35, 44, 6, 98, 17, 81, 56, 53, 31, 20, 11, 45, 80, 8, 34, 71, 83, 64, 28, 3, 88, 50, 48, 80, 5 ]
Sorted array
array = [ 3, 5, 5, 6, 8, 11, 12, 15, 17, 20, 28, 31, 34, 35, 44, 45, 48, 50, 53, 56, 59, 61, 64, 71, 71, 75, 80, 80, 81, 83, 88, 98 ]
Clone this wiki locally