Skip to content

Latest commit

 

History

History
111 lines (77 loc) · 2.45 KB

index.md

File metadata and controls

111 lines (77 loc) · 2.45 KB

PIE Module System

PIE has two kinds of modules: standard library modules (bundled with the compiler) and user modules (any .pie file you write).


Standard Library Modules

Import with the import keyword at the top of the file:

import sqlite;
import json;
import http;
import date;

Multiple imports are allowed — one per line:

import sqlite;
import json;

After importing, call functions with the module name as a prefix:

import date;

int now = date.now();
string formatted = date.format_iso(now);
output(formatted, string);

Available Modules

Module Purpose Doc
sqlite SQLite database CRUD and transactions sqlite.md
json Parse and build JSON json.md
http HTTP client (GET/POST) and HTTP server http.md
date Timestamps, formatting, parsing, arithmetic date.md

User Modules

Any .pie file can be imported as a module. Mark the functions you want to expose with the export keyword.

Defining a module

mathutils.pie:

export int square(int n) {
    return n * n;
}

export float hypotenuse(float a, float b) {
    return sqrt(a * a + b * b);
}

int private_helper(int x) {
    return x + 1;
}
  • Functions marked export are visible to importers.
  • Functions without export are private to the module file.

Importing a user module

import mathutils;

void main() {
    int sq = mathutils.square(5);
    output(sq, int);

    float h = mathutils.hypotenuse(3.0, 4.0);
    output(h, float, 2);
}

Path resolution

The compiler looks for modulename.pie relative to the source file being compiled, then in the standard library paths. To import a module in a subdirectory:

import utils/helpers;

This resolves to utils/helpers.pie relative to the source file.


Module Naming Rules

  • Module names must be valid identifiers ([a-zA-Z_][a-zA-Z0-9_]*).
  • Standard library module names are lowercase (sqlite, json, http, date).
  • User module names should match their filename (without .pie).
  • Call exported functions as modulename.functionname(args).

See Also