Skip to content

bielcarpi/po

Repository files navigation

Logo PO

The PO Language Compiler

PO is a simple and intuitive programming language, fundamentally oriented towards the world of teaching. What is intended with this type of language is to achieve that it can be used as a first contact with the world of programming. The creators of the language want future programmers to be able to use it as a base language with which they can understand how the world of programming works. In this way, show a simple programming method and get more and more people encouraged to program and can participate in the community of software developers.

PO Features

  • Untyped and Dynamic (but compiled!) Language
  • Variables and Constants
  • Comments (Single '/' and Multiline '/* */')
  • Functions (fun)
  • If, Switch, While, For, Foreach support
  • Recursion
  • Compiles to MIPS Architecture
  • Optimized for MIPS Architecture
  • Support for other Architectures

User Manual & Code Examples

Operations and Expressions

func calculator(num1, num2, op) {
    var result = 0

    if op == 0 {
        result = num1 + num2
    }
    elsif op == 1 {
        result = num1 - num2
    }
    elsif op == 2 {
        if num1 <= 65000 and num2 <= 65000 {
            result = num1 * num2
        }
        else {
            print("The result can be bigger than 32 bits")
        }
    }
    elsif op == 3 {
        result = num1 / num2
    }
    else {
        if op == 4 {
            print("Mod is not supported yet")
        }
        else {
            print("OP should be between 0 and 3")
        }
    }

    ret result
}

main() {
    var num1 = 100
    var num2 = 100
    var op = 0 // 0 = sum - 1 = sub - 2 = mult - 3 = div
    var times = 0 // How many times the op will be done

    var result = calculator(num1, num2, op, times)
    print("The result is: \n")
    print(result)
    print("\n")

Recursive Factorial

func recFactorial(num) {
    var aux

    if num >= 1 {
        aux = recFactorial(num - 1)
        ret num * aux
    }
    else {
        ret 1
    }
}

main() {
    prints(" *** Factorial Calculator *** \n")
    prints("Enter a number: ")
    var num = read()
    if num <= 12 {
        prints("Calculating the factorial of ")
        print(num)
        var factorial = recFactorial(num)
        prints("\nThe factorial result is: ")
        print(factorial)
    }
    else {
        prints("The number is too big, please enter a number between 0 and 12")
    }
}

Simple Sorting Algorithm

var v1 = 5
var v2 = 2
var v3 = 15
var v4 = 50000
var v5 = 1000

func sortVars(n) {
    var i = 0
    var j = 0
    var aux = 0

    for i = 0, i < n, i++ {
        for j = 0, j < n, j++ {
            if j == 0 and v1 > v2 {
                aux = v1
                v1 = v2
                v2 = aux
            }
            if j == 1 and v2 > v3 {
                aux = v2
                v2 = v3
                v3 = aux

            }
            if j == 2 {
               if v3 > v4 {
                    aux = v3
                    v3 = v4
                    v4 = aux
                }
            }
            if j == 3 {
                if v4 > v5 {
                    aux = v4
                    v4 = v5
                    v5 = aux
                }
            }
        }
    }
}

main() {
    var n = 5 //Num of variables
    prints("Sorting the 5 variables [v1, v5]\n")
    sortVars(n)
    prints("    **********\n\t")
    print(v1)
    prints("\t")
    print(v2)
    prints("\t")
    print(v3)
    prints("\t")
    print(v4)
    prints("\t")
    print(v5)
    prints("    **********\n")
}

How To Use the Compiler

# Update the Main.java file with the path to the PO file you want to compile
private final static String file = "src/testing/integration/po_mips/inputs/recursive-factorial.po";

# Run the Main.java file

# The MIPS code will be generated in the same folder as the PO file, with the same name and extension ".asm"
# Also, an intermediate TAC file will be generated, named "file.tac", in the root folder of the project

Modify the Language Grammar

As the language is defined using a grammar.txt file, you can modify it to add new features to the language, or change it as you like. The compiler itself takes care of parsing this grammar and generating the corresponding Parsing Table (recursively finding first, follows...).

The grammar is LL(1), defined using Backus-Naur form. You can find "grammar.txt" in the root folder of the project.

Authors

Marc Geremias ([email protected])
Angel Garcia ([email protected])
Guillem Godoy ([email protected])
Armand Daussà ([email protected])
Biel Carpi ([email protected])