Basic Syntax
AIScript syntax is inspired by Python, JavaScript and Rust. If you're familiar with any of these languages, you can grasp the basic language syntax in about 10 minutes.
AIScript uses //
for comments.
// This is a comment
// Print a Hello World
print("Hello World");
Semicolon
AIScript is a semicolon-terminated language, which means ;
is required at the end of statements.
let a = 1;
let flag = true;
print(1 + 2);
Variables
AIScript uses let
to define variables. Type annotations are not supported yet.
let name = "AIScript";
let age = 18;
// Type annotation is not support yet
// let flag: bool = false;
There are two kinds of variables: global variables
and local variables
. Any variable defined in the root scope is a global variable; otherwise, it's a local scope variable.
let global_variable = "abc";
if len(global_variable) > 1 {
let local_variable = "xyz";
print("I can access global variable:", global_variable); // print: I can access global variable: abc
}
// Local variables are inaccessible outside of their local scope.
// This print statement will raise error: Undefined variable 'local_variable'.
// print(local_variable);
Constants
Constants are declared with the const
keyword and are immutable. Constants can only be declared once and must be initialized with a value; they cannot be reassigned.
const PI = 3.14;
print(PI); // print 3.14
PI = 3.1415; // Error at 'PI': Cannot assign to constant variable.
Primitive types
// nil
let nil_value = nil;
// strings
let name = "AIScript";
// integers
let age = 18;
let negative = -1;
// let hex = 0x10;
// let octal = 0o10;
// let binary = 0b10;
// let big = 1_000_000_000_000;
// floats
let pi = 3.14;
// booleans
let flag1 = true;
let flag2 = false;
// arrays
let numbers = [1, 2, 3, 4, 5];
// object literal
let address_key = "address";
let person = {
name: "AIScript",
age: 18, // double quotes are optional
is_male: true,
hobbies: ["reading", "coding", "gaming"],
[address_key]: {
// key name are computable
city: "Beijing",
street: "No. 100, Xihuan Road",
zipcode: "100000",
country: "China",
phone: "13800138000",
},
};
// tuples
let person = ("AIScript", 18, true);
Operators
// arithmetic operators
let a = 1 + 2;
let b = 1 - 2;
let c = 1 * 2;
let d = 1 / 2;
let e = 1 % 2;
// logical operators
let a = true and false;
let b = true or false;
let c = not true;
// comparison operators
let a = 1 == 2;
let b = 1 != 2;
let c = 1 > 2;
let d = 1 < 2;
let e = 1 >= 2;
let f = 1 <= 2;
// bitwise operators
let a = 1 & 2;
let b = 1 | 2;
let c = 1 ^ 2;
let d = 1 << 2;
let e = 1 >> 2;
// assignment operators
let a = 1;
a += 2;
a -= 2;
a *= 2;
a /= 2;
a %= 2;
// in operator
let a = 1 in [1, 2, 3];
let b = 1 not in [1, 2, 3];
Not supported
AIScript doesn't support increment/decrement operators like i++
, ++i
, i--
or --i
.
String
// string concatenation
let a = "AIScript";
let b = "Hello" + a + "!";
// string length
let a = "AIScript";
print(len(a)); // print 8
Formatted String Literal
AIScript supports f-strings
(formatted string literals, like Python's f-string but not identical) for string interpolation. F-strings provide a concise and readable way to embed expressions inside string literals. To create an f-string, prefix the string with f
and use curly braces {}
to include expressions.
let name = "Alice";
let greeting = f"Hello, {name}!";
print(greeting); // Hello, Alice!
let x = 10;
let y = 5;
print(f"{x} + {y} = {x + y}"); // 10 + 5 = 15
print(f"{x} - {y} = {x - y}"); // 10 - 5 = 5
print(f"{x} * {y} = {x * y}"); // 10 * 5 = 50
print(f"{x} / {y} = {x / y}"); // 10 / 5 = 2
F-strings can contain any valid AIScript expression inside the curly braces. The expressions are evaluated at runtime and their results are converted to strings. This makes f-strings particularly useful for:
- String interpolation with variables
- Embedding arithmetic expressions
- Calling functions within strings
- Formatting complex data structures
Control flow
let age = 20;
if age > 60 {
print("You are a senior");
} else if age > 18 and age <= 60 {
print("You are an adult");
} else if age > 12 and age <= 18 {
print("You are a teenager");
} else {
print("You are a child");
}
Inline if
AIScript doesn't support the ?:
ternary operator like other languages, but you can use inline if
syntax.
let score = 92;
let result = "A" if score > 90 else "B";
print(result); // "A"
Match
Use match
to evaluate expressions against different patterns:
match
also support evaluate enums, learn more in Enum chapter
- Use
|
to combine multipe case in one arm
match
arm support if
guards
let language = "AIScript";
match language {
"AIScript" => print("AIScript"),
"Rust" | "Python" => print("Rust or Python"),
"JavaScript" => print("JavaScript"),
x if x.starts_with("Z") => print("Maybe Zig?"),
_ => print("Unknown language"),
};
Functions
- Use
fn
keyword to define function; you can use ai fn
to define AI function.
- Argument and return type annotations are recommend but optional.
- Use
|
to append error types after return type, see Error handling.
fn add(a: int, b: int) -> int {
return a + b;
}
print(add(1, 2)); // 3
Closures
Functions are first-class citizens in AIScript, so you can return a function as a closure. The key difference between closures and regular functions is that closures capture variables
from their surrounding scope.
fn add(a: int) {
fn add_inner(b: int) -> int {
return a + b;
}
return add_inner;
}
let add5 = add(5);
print(add5(10)); // 15
Closure add_inner
captures the outer variable a
.
Slice Not supported yet
// array slice
let a = [1, 2, 3, 4, 5];
let b = a[0:3]; // [1, 2, 3]
let c = a[3:]; // [4, 5]
let d = a[:3]; // [1, 2, 3]
// string slice
let a = "AIScript";
let b = a[0:2]; // "AI"
let c = a[3:]; // "Script"
let d = a[:2]; // "AI"
Loops
AIScript only has two keyword for loops: for
and while
.
Regular for iteration
for let i = 0; i < 10; i = i + 1 {
print(i);
}
While loop
INFO
AIScript doesn't support do...while
.
let i = 0;
while i < 10 {
print(i);
}
while true {
// infinite loop
}
Iterate over a list Not supported yet
let numbers = [1, 2, 3, 4, 5];
for number in numbers {
print(number);
}
Iterate over a map Not supported yet
let person = {
"name": "AIScript",
"age": 18,
"gender": "male",
};
for (key, value) in person {
print(f"{key}: {value}");
}
Iterate over range Not supported yet
for i in 1..10 {
print(i);
}
Break and Continue
for let i = 0; i < 10; i += 1 {
if i % 2 == 0 {
continue;
} else if i == 9 {
print("i == 9");
break;
}
print(i);
}
// 1
// 3
// 5
// 7
// i == 9
Environment
AIScript provide a convenient way to access environment variables, the syntax is $ENV_NAME
. You can also use std.env module too.
let home = $HOME;
print(home);