Welcome to the NG programming language! This guide will provide a comprehensive introduction to the language, from the basics to more advanced topics. Whether you are a beginner or an experienced programmer, this guide will help you get started with NG.
NG is a modern, statically-typed programming language that is designed to be simple, efficient, and easy to learn. It is a general-purpose language that can be used for a wide range of applications, from scripting to systems programming.
The design of NG is guided by the following principles:
Before you can start using NG, you will need to have the following tools installed on your system:
Clone the repository:
git clone https://github.com/ng-lang/ng.git
cd ng
Create a build directory:
mkdir build
cd build
Configure the project with CMake:
cmake -GNinja ..
Build the project:
ninja
Let’s start with a simple “Hello, World!” program.
// This is a comment.
print("Hello, World!");
To run this program, save it to a file called hello.ng
and run the following command from the build
directory:
./ngi ../hello.ng
This will print “Hello, World!” to the console. The print
function is a built-in function that prints its argument to the standard output.
NG supports single-line comments using //
.
// This is a single-line comment.
Variables are declared using the val
keyword. By default, variables are mutable, which means their values can be changed after they are declared.
val x = 1; // x is 1
x = 2; // now x is 2
NG is a statically-typed language, which means that every variable has a type that is known at compile time. NG has a rich set of built-in data types.
i8
, i16
, i32
, i64
(signed) and u8
, u16
, u32
, u64
(unsigned).f32
, f64
.true
, false
."hello"
, 'world'
.unit
, which represents the absence of a value.You can explicitly specify the type of a variable using a type annotation.
val x: i32 = 1;
val name: string = "NG";
NG supports a variety of operators.
+
, -
, *
, /
, %
==
, !=
, >
, <
, >=
, <=
<<
is
There are no logical operators like &&
or ||
. Instead, you can use the not
function from the standard prelude.
NG is an expression-oriented language, which means that most things are expressions that evaluate to a value. A statement is a piece of code that performs an action but does not produce a value.
if/else
statements are used for conditional execution.
if (x > 0) {
print("positive");
} else if (x < 0) {
print("negative");
} else {
print("zero");
}
NG has a loop
construct for iteration.
fun sum(n: i32) -> i32 {
val s = 0;
loop i = 0 {
s = s + i;
if (i < n) {
next i + 1;
}
}
return s;
}
The next
keyword is used to continue the loop, optionally with a new value for the loop variable.
Functions are defined with the fun
keyword.
fun add(x: i32, y: i32) -> i32 {
return x + y;
}
For single-expression functions, you can use the =>
shorthand.
fun add(x: i32, y: i32) -> i32 => x + y;
Functions can call themselves, which is called recursion.
fun factorial(n: i32) -> i32 {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
Functions can be marked as native
, which means they are implemented in the host language.
fun my_native_function(arg: i32) -> unit = native;
Arrays are collections of elements of the same type.
val arr = [1, 2, 3, 4, 5];
print(arr[0]); // 1
arr[0] = 10;
print(arr[0]); // 10
arr << 6; // append 6 to the end
You can define your own custom types using the type
keyword.
type Person {
property firstName: string;
property lastName: string;
fun name() -> string {
return self.firstName + " " + self.lastName;
}
}
val person = new Person {
firstName: "John",
lastName: "Doe"
};
print(person.name()); // "John Doe"
Each file in NG is a module. You can control what is visible outside the module using the export
keyword.
// my_module.ng
module my_module exports *;
export fun my_fun() { ... }
You can import other modules using the import
statement.
// main.ng
import my_module (*);
my_fun();
NG has a small standard library that provides basic functionalities.
The std.prelude
module is implicitly imported into every module. It provides the following functions:
print(value)
: Prints a value to the console.assert(condition)
: Asserts that a condition is true.not(value: bool)
: Returns the logical negation of a boolean value.NG is a statically-typed language. This means that the type of every variable is known at compile time, which helps to catch errors early and improve performance.
NG uses automatic memory management, which means you don’t have to manually allocate and deallocate memory. The compiler takes care of this for you.
We welcome contributions from the community! If you are interested in contributing to the NG programming language, please read our Contribution Guide to get started.
Join the NG community to ask questions, share your ideas, and collaborate with other developers.