Skip to content

Modules

Modules in EasyLang allow you to organize code into separate files and reuse logic across programs.
You can import:

  • EasyLang modules (.elangh files)
  • Internal standard library modules (like strings, math)
  • Python modules (math, random, os, etc.)
  • Custom Python packages
  • Any module provided by ELPM (EasyLang Package Manager)

EasyLang uses a flexible and powerful module system fully integrated with the interpreter.


Bringing a Module Into Your Program

The syntax is: bring <module> as <alias>

You can bring:

  • a file module"path.elangh"
  • an EasyLang built-in modulestrings, math
  • a Python modulemath, random, os
  • a Python package installed via ELPMrequests, etc.

Examples:

bring "math.elangh" as math
bring strings as str
bring math as m
bring random as rand

Once imported, you use:

alias.function()
alias.value
alias.property

1. Importing EasyLang Modules (.elangh)

You can create modules in EasyLang using .elangh extension.

Example file: math.elangh

define square(n): do [
    return n mul n
]

define cube(n): do [
    return n mul n mul n
]

Usage:

bring "math.elangh" as math
so print math.square(5)
so print math.cube(3)

How It Works Internally

  • EasyLang loads the module file
  • It lexes, parses, and interprets it inside a fresh module scope
  • The resulting variables and functions become part of the module object
  • The module object is returned as a namespace

2. Importing Standard Library Modules

Your interpreter includes built-in modules such as: strings.elangh

with functions like:

  • upper(str)
  • lower(str)
  • strip(str)
  • substring(str, start, end)
  • contains(str, sub)

Usage:

bring strings as str
so print str.upper("hello")

math.elangh

with functions like:

  • sqrt
  • pow
  • rand
  • random
  • sin, cos, tan
  • ceil, floor

Usage:

bring math as m
so print m.sqrt(25)

These are bundled with EasyLang and loaded the same way as user modules.


3. Importing Python Modules

EasyLang can seamlessly import Python modules and wrap them so they act like EasyLang modules.

Example:

bring math as m
so print m.sqrt(2)

Another example:

bring random as r
we let x = r.randint(1, 10)
so print x

What happens internally?

The interpreter:

    1. Locates the Python module
    1. Imports it using importlib
    1. Wraps all callables, numbers, strings, and objects
    1. Converts them into EasyLang-friendly functions/properties

This feature makes EasyLang very powerful — you can use:

json
os
sys
platform
pathlib
requests (installed through ELPM)
numpy (installed through ELPM)

And many more Python modules.


4. Importing ELPM-Installed Python Packages

Since ELPM is a wrapper around pip, any installed package becomes importable:

elpm --install requests

Now in EasyLang:

bring requests as req

we let response = req.get("https://httpbin.org/get")
so print response.text


5. Module Aliases

Alias is required for modules to avoid naming conflicts.

Example:

bring math as m
bring strings as s

Then:

so print m.sqrt(16)
so print s.upper("hello")

Aliases behave like namespaces.


6. Accessing Module Attributes and Functions

Module members are accessed using the dot operator: moduleName.identifier

Examples:

math.sqrt(25)
str.upper("hello")
rand.randint(1, 20)

Wrong: sqrt(25) $ Error, must use math.sqrt()


7. Module Scope Rules

Each module has its own scope.

Internal variables: we let secret = 42

are not accessible outside the module unless returned or exposed directly.

Example:

bring "config.elangh" as cfg
so print cfg.secret   $ works only if secret is global inside config


8. Using Variables Inside Modules

Global variables inside .elangh are part of the module namespace:

math.elangh: we let PI = 3.14

Usage:

bring "math.elangh" as math
so print math.PI


9. Errors in Module Loading

EasyLang gives helpful errors if:

  • module file not found
  • Python module does not exist
  • .elangh contains syntax errors
  • alias missing

Examples:

Missing file: bring "nope.elangh" as mod

Error:

RuntimeError: Module file 'nope.elangh' not found.

Missing alias: bring "math.elangh"

Error:

SyntaxError: Expected alias after 'as'

Python module not installed: bring requests as req

If not installed:

RuntimeError: Could not import Python module: requests


10. Real-World Example

bring "math.elangh" as math
bring strings as s
bring random as r

we let x = math.square(4)
we let y = r.randint(1, 10)
we let upper = s.upper("hello")

so print x
so print y
so print upper

Output (example):

16
7
HELLO


Summary

  • bring "file.elangh" as alias loads EasyLang modules
  • bring module as alias loads built-in or Python modules
  • Modules have their own variable scope
  • Functions & variables inside modules are accessed using dot notation
  • ELPM-installed Python packages are fully compatible
  • Modules help structure large programs into reusable components

Next Steps

Continue to Standard Library to learn how Standard Library work in detail