Skip to content

Sempai-07/MyLang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MyLang

Welcome to MyLang, a modern programming language designed for flexibility, simplicity, and power. This documentation provides an extensive set of examples and explanations for all key features of MyLang to help you master its capabilities.


Table of Contents

  1. Introduction
  2. Getting Started
  3. Core Features
  4. Import System
  5. Operators
  6. Examples
  7. CLI Commands

Introduction

MyLang is a dynamically typed, versatile programming language built to be developer-friendly and performant. It supports advanced constructs like pipeline operations, modular imports, flexible functions, and much more.


Getting Started

Installation

  1. Clone the repository:

    git clone https://github.com/Sempai-07/mylang.git
  2. Install dependencies:

    npm install
  3. Link the CLI tool:

    npm link

Running a File

Execute a MyLang file using the CLI:

mylang <your-file.ml>

Core Features

Functions

Basic Function

func add(a, b) {
  return a + b;
}

coreio.print("Sum:", add(5, 10)); // Sum: 15

Function with Default Parameters

func greet(name = "Guest") {
  return "Hello, " + name;
}

coreio.print(greet());           // Hello, Guest
coreio.print(greet("Alice"));    // Hello, Alice

Using arguments

import "arrays";

func concatStrings() {
  return arraya.join(arguments, " ");
}

coreio.print(concatStrings("My", "Lang", "is", "awesome")); // My Lang is awesome

Variables

Declaring Variables

var x = 10;
var y;

coreio.print(x, y); // 10, nil

Modifying Variables

var counter = 0;

counter += 1;
counter--;

coreio.print(counter); // 0

Constant Variables (as const)

Using as const, you can declare variables or objects that cannot be reassigned. This ensures immutability for the variable itself but allows modification of its inner elements if it's an array or object.

var constant = 10 as const;

// constant = 5; // Error: Cannot reassign a constant variable

var arr = [] as const; // Declaring an immutable array
arr[0] = 100; // Allowed: Modifying elements of the array

// arr = {}; // Error: Cannot reassign the array

Readonly Variables (as readonly)

Using as readonly, you can declare variables where neither the variable itself nor its elements can be modified.

var arr1 = [1, 2, 3] as readonly; // Declaring a fully immutable array

// arr1 = {};      // Error: Cannot reassign a readonly variable
// arr1[0] = 0;    // Error: Cannot modify elements of a readonly array

This behavior also works for objects:

var obj = { a: 1, b: 2 } as readonly;

// obj.a = 10;  // Error: Cannot modify properties of a readonly object
// obj = {};    // Error: Cannot reassign a readonly object

Using as const and as readonly

var config = {
  appName: "MyLang",
  version: "1.0.0"
} as const;

config.appName = "NewName"; // Allowed: Modifying propertie of the object

var data = [10, 20, 30] as readonly;

// data[0] = 100; // Error: Cannot modify elements of a readonly array

Objects

Object Declaration

var person = {
  name: "John",
  age: 30,
  greet: func() {
    return "Hi, I'm " + this.name;
  }
};

coreio.print(person.greet()); // Hi, I'm John

Nested Objects

var nested = {
  outer: {
    inner: {
      value: 42
    }
  }
};

coreio.print(nested.outer.inner.value); // 42

Control Structures

If-Else

var x = 10;

if (x > 5) {
  coreio.print("x is greater than 5");
} else {
  coreio.print("x is less than or equal to 5");
}

While Loop

var count = 5;

while (count > 0) {
  coreio.print("Countdown:", count);
  count--;
}

For Loop

for (var i = 0; i < 3; i++) {
  coreio.print("Iteration:", i);
}

Error Handling

Try-Catch

try {
  var result = import("nonexistent");
} catch (err) {
  coreio.print("Error:", err);
}

Finally

try {
  coreio.print("Try block");
} finally {
  coreio.print("Finally block");
}

Import System

MyLang supports importing modules from local files or URLs.

Local Import

import "./utils.ml";

coreio.print(utils.add(5, 15)); // 20

HTTP Import

import("http://example.com/module.ml");

Import Details

coreio.print(import.base); // Project root directory
coreio.print(import.cache); // Cached modules
coreio.print(import.resolve("module.ml")); // Resolve file path

Operators

Arithmetic Operators

coreio.print(5 + 3, 5 - 3, 5 * 3, 5 / 3, 5 % 3);
// 8, 2, 15, 1.6666666666666667, 2

Comparison Operators

coreio.print(5 > 3, 5 == 3, 5 != 3);
// true, false, true

Logical Operators

coreio.print(true && false, true || false, !true);
// false, true, false

Examples

Object with Methods

var car = {
  make: "Toyota",
  model: "Corolla",
  getDetails: func() {
    return this.make + " " + this.model;
  }
};

coreio.print(car.getDetails()); // Toyota Corolla

Pipeline Operations

var result = 10
  |> func(x) { return x * 2 }
  |> func(x) { return x + 5 };

coreio.print(result); // 25

Array Iteration

var fruits = ["Apple", "Banana", "Orange"];

for (var i = 0; i < fruits.length; i++) {
  coreio.print(fruits[i]);
}

Enum Methods

coreio.print(Status.getByName("Offline")); // ["Offline", { value: 1, description: "Offline status user" }]
coreio.print(Status.getByName("NonExistent")); // nil

Enum with Mixed Content

enum Action {
  SempaiJS = 7;
  Angel = {};
  Mz = func main() {};
}

// Action = "Invalid"; // Error: Enums are immutable
coreio.print(Action.SempaiJS); // 7
coreio.print(Action.Angel);    // {}

Auto-Incrementing Enums

enum Number {
  One = 1;
  Two;    // 2
  Three;  // 3
}

coreio.print(Number.One, Number.Two, Number.Three); // 1, 2, 3

Pattern Matching (match)

MyLang provides a powerful match expression for pattern matching, similar to a switch statement in other languages.

func findInt(number) {
  match(number) {
    case (1): return "One";
    case (2): return "Two";
    case (3):
    case (4):
    case (5): return "3, 4 and 5";
    default: return "unknown";
  }
}

coreio.print(
  findInt(1), // One
  findInt(2), // Two
  findInt(3), // 3, 4 and 5
  findInt(6)  // unknown
);

Using Enums and Match Together

func getStatusDescription(status) {
  match(status) {
    case (Status.Online): return Status.Online.description;
    case (Status.Offline): return Status.Offline.description;
    default: return "Unknown status";
  }
}

coreio.print(
  getStatusDescription(Status.Online),  // Online status
  getStatusDescription(Status.Offline), // Offline status user
  getStatusDescription(nil)             // Unknown status
);

Enums

Enums in MyLang allow you to define a set of named constants and associated values or methods. Enums can contain simple values, objects, or even functions.

Declaring an Enum

enum Status {
  Online = { value: 0, description: "Online status" };
  Offline = { value: 1, description: "Offline status user" };

  func getByName(val) {
    var allStatus = objects.entries(this);
    for (var i = 0; i < arrays.count(allStatus) - 1; i++) {
      if (allStatus[i][0] == val) {
        return allStatus[i];
      }
    }
    return nil;
  };
}

Accessing Enum Values

coreio.print(Status.Online); // { value: 0, description: "Online status" }
coreio.print(Status.Offline); // { value: 1, description: "Offline status user" }

CLI Commands

Command Overview

  • Run a File
    mylang <file.ml>
  • Initialize Project
    mylang init
  • Start REPL
    mylang repl
  • Version
    mylang version

Contributing

We welcome contributions!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published