Skip to content

GeekyVed/HopeLang

Repository files navigation

HopeLang

TypeScript Deno npm GitHub

HopeLang is a programming language designed for simplicity and ease of learning. Built on TypeScript, it offers a smooth transition for developers familiar with TypeScript syntax. Dive into HopeLang to explore its intuitive constructs and enhance your programming skills.

About HopeLang

HopeLang was originally created as a personal project to gain a deeper understanding of system software components, including interpreters, compilers, lexers, parsers, and abstract syntax trees (ASTs). Designed with simplicity and ease of learning in mind, HopeLang serves as a beginner-friendly programming language built on TypeScript syntax.

Features

  • Simplicity: HopeLang is designed to be easy to learn, making it perfect for beginners and those new to programming.

  • TypeScript-Based: Built on TypeScript, HopeLang provides a familiar syntax for developers already comfortable with TypeScript.

  • Clear Syntax: HopeLang's syntax is clear and concise, reducing the learning curve for new programmers.

  • Interactive REPL: Includes an interactive REPL (Read-Eval-Print Loop) for experimenting with code snippets and learning interactively.

  • Support for Basic Data Types: HopeLang supports basic data types such as numbers, booleans, and objects.

  • Functions: Define and call functions to organize code into reusable blocks.

  • Mathematical Operations: Built-in support for common mathematical operations like addition, subtraction, multiplication, and division.

  • Logical Operations: Perform logical operations such as AND, OR, and NOT.

  • Error Handling: Gracefully handles errors and provides informative error messages for easy debugging.

Installation

To use HopeLang, you can install it via npm or directly from GitHub. First, make sure you have Deno installed on your system. You can install Deno by following the instructions here.

Install Deno

Windows:

To install Deno on Windows, open PowerShell and run the following command:

irm https://deno.land/install.ps1 | iex

macOS:

To install Deno on macOS, open Terminal and run the following command:

curl -fsSL https://deno.land/install.sh | sh

Linux:

To install Deno on Linux, open Terminal and run the following command:

curl -fsSL https://deno.land/install.sh | sh

Via npm

You can install HopeLang via npm using the following command:

npm install -g hopelang

Via GitHub

Alternatively, you can install HopeLang directly from GitHub using the following command:

deno install -n hope --allow-read --allow-write https://raw.githubusercontent.com/GeekyVed/HopeLang/main/index.ts

Usage

After installation, you can use the hope command to run HopeLang scripts or open the REPL.

Running Scripts

To run a HopeLang script, use the following command:

hope <filename.hl>

Replace <filename.hl> with the path to your HopeLang script file.

Interactive REPL

You can also use the interactive REPL to experiment with HopeLang code. Simply type hope without any arguments to enter the REPL mode.

hope

Syntax

Here's an overview of the syntax in HopeLang:

Variables

let x = 10;

Functions

function greet(x) {
     print(x)
}

const result = greet("Alice");

Mathematical Operations

const sum = 10 + 5; // Addition
const difference = 10 - 5; // Subtraction
const product = 10 * 5; // Multiplication
const quotient = 10 / 5; // Division

Logical Operations

const isTrue = true;
const isFalse = !isTrue; // Logical NOT
const bothTrue = true && true; // Logical AND
const eitherTrue = true || false; // Logical OR

Object Literals

Create object literals to represent structured data:

const person = {
    name: "Alice",
    age: 30,
    greet() {
         "Hello, " + this.name + "!";
    }
};

const greeting = person.greet(); // "Hello, Alice!"

Member Functions

Define member functions within object literals to encapsulate behavior:

const person = {
    name: "Alice",
    age: 30,
    greet() {
         "Hello, " + this.name + "!";
    },
    celebrateBirthday() {
        this.age++;
    }
};

person.celebrateBirthday();
print(person.age); // 31

Arrays

Create arrays to store collections of values:

const numbers = [1, 2, 3, 4, 5];
const firstNumber = numbers[0]; // 1

Control Flow

Use control flow statements such as if, else, while, and for:

let count = 0;

while (count < 5) {
    print(count);
    count++;
}
for (let i = 0; i < 5; i++) {
    print(i);
}

Error Handling

Handle errors using try, catch, and throw:

try {
    // Code that might throw an error
} catch (error) {
    // Handle the error
    print(error);
}

Built-in Functions

HopeLang provides several built-in functions that perform common tasks:

  1. print

    • Description: Prints the given arguments to the console.
    • Syntax: print(arg1, arg2, ...)
    • Example:
      print(var); // Output: Hello world!
  2. time

    • Description: Returns the current timestamp in milliseconds since the Unix epoch.
    • Syntax: time()
    • Example:
      const currentTime = time();
      print(currentTime); // Output: Current timestamp in milliseconds
  3. Mathematical Functions HopeLang provides various mathematical functions for common operations:

    • sqrt: Square root
    • power: Exponentiation
    • min: Minimum value among arguments
    • max: Maximum value among arguments
    • round: Rounds the number to the nearest integer
    • abs: Absolute value
    • rand: Generates a random number between 0 and 1
    • ceil: Rounds up to the nearest integer
    • floor: Rounds down to the nearest integer
  4. Trigonometric Functions

    • sin: Sine function
    • cos: Cosine function
    • tan: Tangent function
    • cot: Cotangent function
    • sec: Secant function
    • csc: Cosecant function
    • asin: Arcsine function
    • acos: Arccosine function
    • atan: Arctangent function
  5. Logarithmic Functions

    • log: Natural logarithm
    • logten: Base-10 logarithm
    • exp: Exponential function

Usage Example

// Import required functions
import { print, time, sqrt, power, min, max, round, abs, rand, ceil, floor, sin, cos, tan, cot, sec, csc, asin, acos, atan, log, logten, exp } from "hopelang";

// Example usage
print("Square root of 16:", sqrt(16));
print("2 to the power of 3:", power(2, 3));
print("Minimum of 10, 20, 30:", min(10, 20, 30));
print("Maximum of 10, 20, 30:", max(10, 20, 30));
print("Rounded value of 5.6:", round(5.6));
print("Absolute value of -10:", abs(-10));
print("Random number:", rand());
print("Ceiling of 5.3:", ceil(5.3));
print("Floor of 5.8:", floor(5.8));
print("Sine of 30 degrees:", sin(30));
print("Cosine of 45 degrees:", cos(45));
print("Tangent of 60 degrees:", tan(60));
print("Cotangent of 45 degrees:", cot(45));
print("Secant of 30 degrees:", sec(30));
print("Cosecant of 45 degrees:", csc(45));
print("Arcsine of 0.5:", asin(0.5));
print("Arccosine of 0.5:", acos(0.5));
print("Arctangent of 1:", atan(1));
print("Natural logarithm of 2:", log(2));
print("Base-10 logarithm of 100:", logten(100));
print("Exponential function of 2:", exp(2));

Get started with HopeLang today and embark on your journey to mastering programming with ease!

For more information and examples, check out the documentation and GitHub repository.

Made with ❤️ for everyone

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published