Skip to content

YaoXiang Reference Documentation

This documentation is under construction...

YaoXiang is currently in the experimental verification phase, and the standard library and API are being gradually improved.

Language Specification

Current Status

ModuleStatusDescription
std.io🔨 In progressInput/Output
std.string🔨 In progressString operations
std.list🔨 In progressList operations
std.dict📋 PlannedDictionary operations
std.math🔨 In progressMath functions
std.net📋 PlannedNetwork operations
std.concurrent📋 PlannedConcurrency primitives

Built-in Types

Primitive Types

TypeDescriptionExample
VoidVoid/null return()
BoolBoolean valuetrue, false
IntInteger42, -10
FloatFloating-point number3.14, -0.5
CharCharacter'a', '中'
StringString"hello"

Composite Types

TypeDescriptionExample
List(T)List of same-type elements[1, 2, 3]
Tuple(T1, T2, ...)Tuple of different elements(1, "hello")
Dict(K, V)Key-value map{"a": 1}
(Args) -> RetFunction type(Int) -> Int

User-Defined Types

yaoxiang
// Record type (struct)
Point: Type = { x: Float, y: Float }

// Enum type
Result: (T: Type, E: Type) -> Type = { ok: (T) -> Result(T, E), err: (E) -> Result(T, E) }

// Interface type (all fields are functions)
Callable: Type = { call: (String) -> Void }

Built-in Functions

Output

yaoxiang
print(value)           // Print without newline
println(value)         // Print with newline

Conversion

yaoxiang
to_string(value)       // Convert to string
to_int(value)          // Convert to integer
to_float(value)        // Convert to floating-point

Type Checking

yaoxiang
typeof(value)         // Return type name
is_type(value, type)  // Check type

Keywords

KeywordDescription
TypeMeta type
spawnMark spawn function
spawn forParallel loop
spawn {}Spawn block
if / else if / elseConditional branch
matchPattern matching
while / forLoop
returnReturn value
refCreate reference
mutMutable marker

Syntax Quick Reference

Variable Declaration

yaoxiang
// Immutable variable (default)
x: Int = 42
y = 42                 // Type inference

// Mutable variable
mut count: Int = 0
count = count + 1

Function Definition

yaoxiang
// Regular function
add: (a: Int, b: Int) -> Int = a + b

// Spawn function (automatic concurrency)
fetch: (url: String) -> JSON spawn = HTTP.get(url).json()

// Generic function
identity: [T](x: T) -> T = x

Control Flow

yaoxiang
// Conditional
if x > 0 {
    print("positive")
} else if x < 0 {
    print("negative")
} else {
    print("zero")
}

// Pattern matching
match result {
    ok(value) => print("success: " + value),
    err(error) => print("error: " + error),
}

// Loop
for i in 0..10 {
    print(i)
}

Error Handling

yaoxiang
// ? operator propagates error
data = fetch_file(path)?

Operator Precedence

PrecedenceOperator
Highest( ) Function call
. Field access
[ ] Index
unary - Unary minus
* / % Mul/Div/Mod
+ - Add/Subtract
== != < > <= >= Comparison
and or Logical ops
Lowest= Assignment

Standard Library Usage Examples

yaoxiang
// Import standard library
use std.io.{print, println}

// List operations
use std.list.{list_push, list_pop, list_len}

// Math functions
use std.math.{sqrt, sin, cos, PI}

// Usage
println("Hello, YaoXiang!")
result = sqrt(16.0)  // 4.0

Command Line Tools

bash
# Run script
yaoxiang run hello.yx

# Build bytecode
yaoxiang build hello.yx -o hello.42

# Interpret and execute
yaoxiang eval 'println("Hello")'

# View help
yaoxiang --help

Complete Example

yaoxiang
// Calculate Fibonacci sequence
fib: (n: Int) -> Int = if n <= 1 {
    n
} else {
    fib(n - 1) + fib(n - 2)
}

// Main function
main: () -> Void = {
    print("Fibonacci(10) = " + fib(10).to_string())
}

Contribution Guide

The standard library is under construction, contributions are welcome!

  1. Choose a module (e.g., std.io, std.net)
  2. Implement functions in src/std/
  3. Add documentation comments
  4. Submit PR