Skip to content

solarity-lab/MercuryLang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1,361 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

- [WARNING] This programming language is still under active development and is not stable.
- Using it may cause unexpected behavior, memory leaks, or other critical issues.
- Many features are incomplete or experimental.
- It is strongly advised not to use this language in production or for important projects until development is fully completed and thoroughly validated.

๐Ÿช Mercury Programming Language

GitHub Stars GitHub Forks Repo Size Issues License C++

MercuryLang Logo


MercuryLang is a lightweight, free, and open-source programming language written in C++, inspired by the simplicity of Lua and Python. It provides clean syntax, dynamic typing, powerful standard libraries, and a fast interpreter โ€” ideal for beginners and language creators alike.


๐Ÿš€ Features

  • ๐Ÿ”น Python-like clean syntax
  • ๐Ÿ”น First-class functions
  • ๐Ÿ”น Built-in math & list support
  • ๐Ÿ”น Dynamic variables and control flow
  • ๐Ÿ”น Interactive REPL + .mer file runner
  • ๐Ÿ”น C++ API to embed or customize MercuryLang

๐Ÿ”จ Build from source

1. Navigate to the MercuryLang-main folder.

2. Open Command Prompt.

3. Run make to build from source, and mer, merc, mpi will be in MercuryLang-main/Bin/

4. Rename MercuryLang-main into MercuryLang and then move it to C:/

5. Edit environment path to C:/MercuryLang/Bin/


โš™๏ธ Getting Started

Prerequisites

  • C++ Compiler (MSVC / GCC / Clang)
  • CMake 3.12+

Running Mercury

mer           # Launch REPL
mer main.mer  # Run file

๐Ÿง  Syntax Guide and Help

๐Ÿ“ค Output

import "IO" # standard IO library

print("hello world") # output: hello world

๐Ÿ”ข Variables

import "IO"

let x = 10
print(x)

๐Ÿ“‹ String

import "IO"

let a = "hello, "
let b = "world"
print(a + b)

๐Ÿ“‹ Lists

import "IO"

let fruits = [["apple, "orange", "pineapple"], "banana", "water"]
fruit *= 2
print(fruits[0][1])

frutis[0][2] = "watermelon"

let other_food = ["fish", "egg"]
print(fruits + other_food)

๐Ÿง  Lambda

import "IO"

let f(x) = [1, 2, 3, 4, x]
print(f(3)[0])

๐Ÿง  Functions

import "IO"

func add(x, y) do
    return x + y
end

print(add(2, 3))

๐Ÿ”€ While loop

import "IO"

let x = 0
while x < 100 do
    print(x)
    x += 1
end

๐Ÿ”€ For loop

import "IO"

for i in range(0, 1000) do
    print(i)
end

๐Ÿ”€ Do loop

import "IO"

let x = 0
loop
    print(x)
    x += 1

    if x > 100 then
        break
    end
end

๐Ÿ”€ If-elif-else statement

import "IO"

let a = 1

if a == 1 then
    print("a = 1")
end

elif a == 2 then
    print("a = 2")
end

else
    print("other value")
end

๐Ÿ“‹ Classes

class Animal do
    let name = ""
    let age = 0

    func init_name(n, a) do
        this.name = n
        this.age = a
    end
end

class Dog extends Animal do
    name = "Dog"
    age = 12

    func bark() do
        print("woof")
    end
end

class Cat extends Animal do
    name = "Cat"
    age = 15
    
    func meow() do
        print("meow")
    end
end

let pup = Dog()
let kirby = Cat()

pup.init_name("Pup Dog", 16)
kirby.init_name("LongTail Cat", 20)

print(pup.name)
print(kirby.name)

๐Ÿง  Call C++ Functions from MercuryLang

MercuryLang supports calling C++ functions from dynamic libraries (.dll on Windows) via the dll_load function.

dll_load("<path_to_dll>", "<function_name>", [arg1, arg2, ...])
  • <path_to_dll>: Path to your compiled DLL or shared library.
  • <function_name>: Name of the exported C++ function.
  • [arg1, arg2, ...]: Arguments passed to the C++ function (as a list).

๐Ÿ›  Example: Extending Lists or Strings

1๏ธโƒฃ C++ Implementation

#include <Mercury.h>  // Include Mercury API

START_DLL  // Required macro for Mercury DLL

// Exported function must return table* and accept table* args[]
DLL_EXPORT table* extend(table* args[]) {
    table *item1 = args[0];
    table *item2 = args[1];

    if (item1->is_list && item2->is_list) {
        item1->list_v->args.insert(item1->list_v->args.end(),
                                   item2->list_v->args.begin(),
                                   item2->list_v->args.end());
        item1->list_v->size += item2->list_v->size;
        return item1;
    }

    else if (item1->is_str && item2->is_str) {
        item1->f_str_v->buff.insert(item1->f_str_v->buff.end(),
                                    item2->f_str_v->buff.begin(),
                                    item2->f_str_v->buff.end());
        item1->f_str_v->size += item2->f_str_v->size;
        return item1;
    }

    else {
        item1->cval += item2->cval;
        item1->value += item2->value;
        return item1;
    }
}

END_DLL  // Required macro

2๏ธโƒฃ MercuryLang Side

Compile the C++ file into a .dll (Windows), then call it from Mercury:

func extend(item1, item2) do
    return dll_load("init.dll", "extend", [item1, item2])
end

extend([1, 2, 3], [4, 5, 6])  # => [1, 2, 3, 4, 5, 6]
extend("Hello, ", "World!")   # => "Hello, World!"

๐Ÿ“œ Other syntax

MercuryLang also supports many other syntax features such as break, return, continue, and, or, not, is, include, import, do, end, if, elif, else, while, for, loop, and more.

๐ŸŽฎ Game

  • MercuryLang has a library for graphic and game (MGL)
  • This code here is a example for a simple jumping game using MGL
  • If you want to use the library graphic and game download SDL3.dll and then move it into ..\Bin
import "MGL" # import Mercury Game Library

screen_width = 500
screen_height = 500

screen_init(screen_width, screen_height)

cube_width = 50
cube_height = 50

cube_x = 0
cube_y = screen_height - cube_height

cube_vel = 5

cube_color = [255, 255, 255]

while true do
    screen_fill(0, 0, 0)

    screen_draw(cube_x, cube_y, cube_width, cube_height, cube_color[0], cube_color[1], cube_color[2])

    let key = get_key_char(key_pressed())

    if same(key, "a") then
        cube_x -= cube_vel
    end

    if same(key, "d") then
        cube_x += cube_vel
    end

    if cube_x >= screen_width - cube_width then
        cube_x = screen_width - cube_width
    end

    if cube_x <= 0 then
        cube_x = 0
    end

    screen_flip()
end

screen_quit()

๐Ÿงฌ Embedding MercuryLang in C++

#include <Mercury.h>

int main() {
    let_S   = "gan";
    PRINT_S = "in";
    WHILE_S = "lap";
    IF_S    = "neu";
    THEN_S  = "thi";
    END_S   = "ketthuc";

    LANGUAGE = "MercuryLangTiengViet";
    AUTHOR   = "Dinh Son Hai";

    prompt();
}

๐Ÿ“• library installation

If Python has pip, so MercuryLang has mpi


๐Ÿ” What is mpi?

  • mpi (Mercury Package Installer) is the official tool for installing external libraries in MercuryLang โ€” just like pip in Python.

  • Unlike Python or Lua which use centralized package markets,
    MercuryLang uses GitHub as the main source for libraries.


๐Ÿš€ How to install a library?

Installing a library with mpi is simple. Just type:

mpi --install https://github.com/author/repo

Example:

mpi --install https://github.com/dinhsonhai132/math

When done just import it:

import "math"

Replace author/repo with the actual GitHub repository of the library you want to install.


๐Ÿ“ Required Files in a Library Repo

To be recognized as a valid MercuryLang library, the repository must contain:

1. main.mer

  • This is the entry point of the library.
  • If itโ€™s missing or named incorrectly, mpi will reject the repo.

2. mpi.json

  • A metadata file that describes the library.
  • Example format:
{
  "name": "your-library-name",
  "version": "1.0.0",
  "description": "A short description of your library.",
  "author": "YourName"
}

Made by dinhsonhai132

About

๐Ÿš€๐Ÿช๐™ˆ๐™š๐™ง๐™˜๐™ช๐™ง๐™ฎ๐™‡๐™–๐™ฃ๐™œ ๐Ÿฎ.๐Ÿฎ.๐Ÿญ ๐Ÿš€๐Ÿช

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors