GlaDOS dev-docs
  • Introduction
  • Getting started
    • Pre-requisite
    • Installation
    • Creating our first function
  • Prism's Features
  • Prism Compiler
  • Prism Bytecode File Structure
  • Exeption Handling
  • Grammar language
  • Test Policy
    • Unit & integration tests
    • Continuous Integration and Deployment (CI/CD)
  • Appendix
Powered by GitBook
On this page
  1. Getting started

Creating our first function

In this section, we will guide you through creating your first function in Prism. We will create a file named add.pr and define a simple function called add that takes two parameters and returns their sum.

  1. Create add.pr file: Open your text editor of choice and create a new file named add.pr. You can use the following command in the terminal:

$> touch add.pr
  1. Edit add.pr and define the add function: add the following code to add.pr using your text editor:

// add.pr

// Define the add function
fn add (a, b) {
    let c = a + b;
    ret (c);
}

// Test the add function
fn main() {
    let res = add(1, 3);
    ret (res);
}

In this code, we define a function add that takes two parameters (a and b) and returns their sum using the + operator. We then test the function by calling add(1, 3).

  1. Compile and execute the function: save the add.pr file and open your terminal. Navigate to the directory containing add.pr and execute the following command:

// For compile
$> ./psc add.pr

When compiling, a file generator.out is generated. This file contains the binary instructions that correspond to the code contained in the file passed as an argument to the binary psc.

// For execute
$> ./glados
4

This command calls the Prism Virtual Machine, which treats the generator.out file to give the response you see in the terminal.

Congratulations! You've created and executed your first function in Prism. Feel free to explore more features and functionalities of Prism by defining and testing additional functions in your add.pr file.

Last updated 1 year ago