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.
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.
-
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.
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.
To install Deno on Windows, open PowerShell and run the following command:
irm https://deno.land/install.ps1 | iex
To install Deno on macOS, open Terminal and run the following command:
curl -fsSL https://deno.land/install.sh | sh
To install Deno on Linux, open Terminal and run the following command:
curl -fsSL https://deno.land/install.sh | sh
You can install HopeLang via npm using the following command:
npm install -g hopelang
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
After installation, you can use the hope
command to run HopeLang scripts or open the REPL.
To run a HopeLang script, use the following command:
hope <filename.hl>
Replace <filename.hl> with the path to your HopeLang script file.
You can also use the interactive REPL to experiment with HopeLang code. Simply type hope
without any arguments to enter the REPL mode.
hope
Here's an overview of the syntax in HopeLang:
let x = 10;
function greet(x) {
print(x)
}
const result = greet("Alice");
const sum = 10 + 5; // Addition
const difference = 10 - 5; // Subtraction
const product = 10 * 5; // Multiplication
const quotient = 10 / 5; // Division
const isTrue = true;
const isFalse = !isTrue; // Logical NOT
const bothTrue = true && true; // Logical AND
const eitherTrue = true || false; // Logical OR
Create object literals to represent structured data:
const person = {
name: "Alice",
age: 30,
greet() {
"Hello, " + this.name + "!";
}
};
const greeting = person.greet(); // "Hello, Alice!"
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
Create arrays to store collections of values:
const numbers = [1, 2, 3, 4, 5];
const firstNumber = numbers[0]; // 1
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);
}
Handle errors using try
, catch
, and throw
:
try {
// Code that might throw an error
} catch (error) {
// Handle the error
print(error);
}
HopeLang provides several built-in functions that perform common tasks:
-
print
- Description: Prints the given arguments to the console.
- Syntax:
print(arg1, arg2, ...)
- Example:
print(var); // Output: Hello world!
-
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
-
Mathematical Functions HopeLang provides various mathematical functions for common operations:
sqrt
: Square rootpower
: Exponentiationmin
: Minimum value among argumentsmax
: Maximum value among argumentsround
: Rounds the number to the nearest integerabs
: Absolute valuerand
: Generates a random number between 0 and 1ceil
: Rounds up to the nearest integerfloor
: Rounds down to the nearest integer
-
Trigonometric Functions
sin
: Sine functioncos
: Cosine functiontan
: Tangent functioncot
: Cotangent functionsec
: Secant functioncsc
: Cosecant functionasin
: Arcsine functionacos
: Arccosine functionatan
: Arctangent function
-
Logarithmic Functions
log
: Natural logarithmlogten
: Base-10 logarithmexp
: Exponential function
// 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.