Skip to content
/ raoul Public

Compiler for the programming languages class

Notifications You must be signed in to change notification settings

ricglz/raoul

Repository files navigation

Raoul

Imperative language done as compilers class final project

How to run it?

cargo run -- examples/filename.ra

Flags

  • -q or --quads. Shows the quadruples generated by the compiler
  • -d or --debug. Shows debugging message for the developer of the language

Documentation

Program structure

  • Assignments for global variables
  • Function declarations
  • Main

Assigning variables

a = 3;
b = "hello";
c = false;
d = 1.2;
e = [1, 2, 3];
f = read_csv("data.csv");

In raoul variable type is infer based on the first value assigned to the variable. We support the following types:

  • int
  • float
  • string
  • boolean
  • dataframes
  • arrays (only for atomic types, ie. not dataframes)

Due to this freedom, we made the rule of not being able to re-define the type of a variable. If the variable was initially assigned to a boolean type, the following assignments to this variable must be of type boolean or any type that can be cast into it.

Global variables

If you wish to make use of global variables there are 2 ways to achieve this:

// Assign before functions
a = 3;

func test(): int {
  return a * b;
}

func main(): void {
  // Use the "global" prefix
  global b = 3;
  print(test());
}

It's important to note that using the "global prefix" method sometimes may cause compilation errors that the "assign before functions" does not. Thus, if you don't want to worry about a lot about the prior is the recommended method.

Function declaration

Must be declared before main function. This is a language of good families, thus there is no polymorphism, meaning that every function's name should be unique.

func fibonacci(param: int): int {
  ...
  return result;
}

Expressions

x = 2 * (12 + x) >= a;

Language supports:

  • Arithmetic operations (+, -, *, /)
  • Compare and equality (>, <, >=, <=, ==, !=)
  • Logical operations (&&, ||, !)
  • Parenthesis for nested expressions

For-loop declaration

The upper-limit is an inclusive limit. Meaning that if the limit is equals to 5 the variable of control must be higher than five to exit.

for i = start to limit {
    ...
}

While-loop declaration

i = 0
while(i < 10) {
    ...
    i = i + 1;
}

Conditions declaration

if (cond) {
  ...
} else if (cond_2) {
  ...
} else {
  ...
}

Read from console

Variable assigned the value of input will be of type string, nevertheless, we've made sure that string can cast into most of atomic types (with the exception of boolean). Thus, you can make use of it.

a = input();

Print to console

Its possible to chain multiple string constants and expressions. At the end, this will always print a new line.

print(var, " ", func());

Dataframe declaration

There can only be one dataframe per program

read_csv("data.csv");

Dataframe shape operations

To get the amount rows and columns of a dataframe you can do the following

rows = get_rows(dataframe);
columns = get_columns(dataframe);

Dataframe operations

Returns the operation value for a given key.

Possible operations:

  • Mean: average()
  • Variance: variance()
  • Std: std()
  • Median: median()
  • Min: min()
  • Max: max()
  • Range: range()

Arguments:

  • 1st argument: must be a dataframe
  • 2nd argument: must be a value of type string, that represents the column to analyze
avg(data, "key");

Dataframe correlation

Returns correlation value for two columns

correl(data, "key1", "key2");

Plot with dataframe

Scatter plot for two columns in the dataframe, pops up in new window

plot(data, "key1", "key2");

Note. Using this command will end the execution of the program, so is recommended to be the last one

Result:

ScatterPlot

Histogram with dataframe

Histogram for a variable in the dataframe, the third argument is the number of bins for the histogram.

Plot pops up in new window.

hist(data, "key1", 10);

Note. Using this command will end the execution of the program, so is recommended to be the last one

Result

Histogram

Main declaration

func main(): void {
    ...
}

Code Examples

There is a bunch a possible valid and invalid files in the examples folder in repo.

More Documentation

For more details, check Documentation

Mini Tutorial

If you want a use in action of the compiler you can see the following video

Note The video is in Spanish