Language Keywords
AI
ai
The ai
keyword defines an AI-powered function that can interact with AI models.
ai fn summarize(text: str) -> str {
prompt "Summarize the following text in 3 sentences: {text}";
}
let summary = summarize("AIScript is a programming language designed for AI interactions...");
print(summary);
prompt
The prompt
keyword sends a text prompt to an AI model and returns the response.
// Basic usage
let response = prompt "What is the capital of France?";
// With model specification
let detailed_answer = prompt {
input: "Explain quantum computing",
model: "gpt-4",
max_tokens: 500,
temperature: 0.7,
system_prompt: "You are a quantum physics expert"
};
agent
The agent
keyword defines an AI agent with specific capabilities and instructions.
agent SearchAssistant {
instructions: "You are a search assistant. Help users find information.",
model: "claude-3-5-sonnet",
fn search(query: str) -> str {
// Implementation of search functionality
prompt f"Search for: {query}";
}
fn summarize(text: str) -> str {
prompt f"Summarize: {text}";
}
}
// Using the agent
let result = SearchAssistant.run(input: "Find information about climate change");
Control Flow
if
Executes a block of code conditionally.
let x = 10;
if x > 5 {
print("x is greater than 5");
} else if x < 5 {
print("x is less than 5");
} else {
print("x equals 5");
}
else
Used with if
to provide alternative code blocks.
for
Creates a loop that iterates over collections or ranges.
for i in 1..5 {
print(i); // Prints 1, 2, 3, 4
}
let names = ["Alice", "Bob", "Charlie"];
for name in names {
print(name);
}
while
Creates a loop that continues as long as a condition is true.
let i = 0;
while i < 5 {
print(i);
i = i + 1;
}
break
Exits the current loop.
for i in 1..10 {
if i == 5 {
break;
}
print(i); // Prints 1, 2, 3, 4
}
continue
Skips to the next iteration of a loop.
for i in 1..10 {
if i % 2 == 0 {
continue;
}
print(i); // Prints 1, 3, 5, 7, 9
}
match
Pattern matching for conditional logic.
let value = 2;
match value {
1 => print("One"),
2 => print("Two"),
3 => print("Three"),
_ => print("Other number")
}
// Match with enum variants
enum Status {
Pending,
Started,
Finished,
}
let result = Result::Pending;
match result {
Result::Pending => print("status: pending"),
Result::Started => print("status: started"),
Result::Finished => print("status: finished"),
}
Function
fn
Defines a function.
fn add(a: int, b: int) -> int {
return a + b;
}
return
Returns a value from a function.
fn is_even(num: int) -> bool {
if num % 2 == 0 {
return true;
}
return false;
}
raise
Returns an error from a function.
enum ArithError! {
DivideByZero
}
fn divide(a: int, b: int) -> float, ArithError! {
if b == 0 {
raise ArithError!::DivideByZero;
}
return a / b;
}
// Using the function with error handling
let result = divide(10, 0) |err| {
print("Division by zero");
return 0;
};
Variable Declaration
let
Declares a mutable variable.
let name = "AIScript";
name = "AI Script Language"; // Valid - can be reassigned
const
Declares an immutable constant.
const PI = 3.14159;
const MAX_USERS = 100;
// PI = 3.14; // Invalid - constants cannot be reassigned
Visibility
pub
Makes a declaration public and accessible from other modules.
pub fn add(a: int, b: int) -> int {
return a + b;
}
pub const VERSION = "1.0.0";
Type Definition
enum
Defines an enumeration type.
enum Color {
Red,
Green,
Blue
}
// Enum with values
enum HttpStatus {
OK = 200,
NotFound = 404,
ServerError = 500
}
// Enum for errors
enum NetworkError! {
ConnectionFailed,
Timeout,
InvalidResponse,
}
class
Defines a class with fields and methods.
class Person {
name: str,
age: int,
fn new(name: str, age: int) {
self.name = name;
self.age = age;
}
}
let person = Person("John", 30);
Logical Operators
and
Logical AND operator.
if x > 0 and x < 10 {
print("x is between 0 and 10");
}
or
Logical OR operator.
if x < 0 or x > 10 {
print("x is not between 0 and 10");
}
not
Logical NOT operator.
if not (x == 5) {
print("x is not 5");
}
Boolean Literals
true
Boolean literal representing a true value.
false
Boolean literal representing a false value.
Other
in
Tests for membership or is used with for
loops.
// Membership test
let numbers = [1, 2, 3, 4, 5];
print(3 in numbers); // true
// Used in for loops
for element in numbers {
print(element);
}
self
References the current instance in a class method.
class Counter {
count: int = 0,
fn increment(self) {
self.count = self.count + 1;
}
fn get_count(self) -> int {
return self.count;
}
}
super
References the parent class in a class inheritance context.
class Animal {
fn make_sound() {
print("Generic animal sound");
}
}
class Dog(Animal) {
fn make_sound() {
super.make_sound();
print("Woof!");
}
}
nil
Represents the absence of a value.
let optional = nil;
if optional == nil {
print("No value");
}
use
Imports modules or specific items from modules.
// Import a module
use std.math;
print(math.sin(0.5));
// Import multiple modules
use std.io;
use std.time;
_
Used as a wildcard in match expressions.
match value {
1 => print("One"),
2 => print("Two"),
_ => print("Other number")
}