Examples
This page contains various examples and reference implementations for the Autolang compiler.
📦 getProducts
Example of registering a built-in Autolang library to fetch a mock list of vegetable products from the host environment.
window.compiler = await ACompiler.create({
// I don't want the AI to access the filesystem
addStdFile: false
});
// Limit opcode count for AI's script
window.compiler.setLimitOpcodeCount(1000000);
window.compiler.registerBuiltInLibrary("company/products", `
@import("std/json")
// Define Product class that AI can use
class Product (
val name: String,
val price: Int,
val weight: Float,
val inStock: Bool
)
class Database {
// Returns raw JSON string that is parsed to Array<Product> in next step
@native("get_products")
static private func _get_products(): String
// Parse JSON and returns an Array of Product instances
static func get_products(): Array<Product> {
val jsonStr = _get_products()
val json = Json.parse(jsonStr)
val products = json["data"]
return jsonToArrayClass<Product>(products)
}
}
`, { autoImport: true }, {
// Define native function implementation
"get_products": () => {
return JSON.stringify({
data: [
{ name: 'Dalat Carrot', price: 25, weight: 0.5, inStock: true },
{ name: 'Highland Potato', price: 30, weight: 1.0, inStock: false },
{ name: 'Broccoli', price: 45, weight: 0.4, inStock: true },
{ name: 'Cherry Tomato', price: 35, weight: 0.3, inStock: true },
{ name: 'Onion', price: 18, weight: 1.0, inStock: false },
{ name: 'Pumpkin', price: 22, weight: 1.5, inStock: true },
{ name: 'Purple Cabbage', price: 40, weight: 0.8, inStock: true },
{ name: 'Old Ginger', price: 60, weight: 0.2, inStock: false },
{ name: 'Seedless Lime', price: 15, weight: 0.5, inStock: true },
{ name: 'Lyson Garlic', price: 120, weight: 0.2, inStock: true }
]
});
}
});
// Disable lateinit and non-null assertion in AI's script
window.compiler.setMainSourceConfig({
allowLateinitKeyword: false,
allowNonNullAssertion: false
});
// If the AI tries to use
window.compiler.runAndCompile("test.atl", ...);
🤖 AI Agent Interaction
ℹ️
Model Info & System PromptIn this example, we used the model: With this setup, we found that the AI is more than capable of writing small, functional Autolang scripts to answer user queries effectively.
gemini-3.1-flash-lite-preview.To instruct the AI, we provided the entire autolang-ai-reference.md file as context, along with the following custom system prompt:You can help user if they want to know company's products. You can find all products in the company's database and filter them.
Use @import("company/products")
class Product (
val name: String,
val price: Int,
val weight: Float,
val inStock: Bool
)
class Database {
static func get_products(): Array<Product>
}Below are the actual prompts and responses from the AI using the custom company/products library we registered above.
I have $30, what can I buy from your company's products
AI
AGENT@import("company/products")
val budget = 30
val allProducts = Database.get_products()
val affordable = allProducts.filter {|p| p.inStock && p.price <= budget }
println("You can afford the following products with $30:")
affordable.forEach {|p| println("- ${p.name}: $${p.price}") }With a budget of $30, you can currently purchase any of the following items from our product catalog:
Dalat Carrot: $25
Pumpkin: $22
Seedless Lime: $15
Please note that these items are currently in stock. Since each of these products costs $30 or less, you can choose any one of them for your purchase.
how many products are still in stock
AI
AGENT@import("company/products")
val products = Database.get_products()
val inStockProducts = products.filter {|p| p.inStock }
println(inStockProducts.size())There are currently 7 products in stock.
What things
AI
AGENT@import("company/products")
val products = Database.get_products()
val inStock = products.filter {|p| p.inStock }
inStock.forEach {|p| println("- ${p.name}") }The products currently available in stock are:
Dalat Carrot
Broccoli
Cherry Tomato
Pumpkin
Purple Cabbage
Seedless Lime
Lyson Garlic
