1
Language Overview
erick-alcachofa edited this page 2025-12-30 03:08:14 +00:00

Language Overview

Summarizes the core syntax and semantics supported in the current parser-focused phase of the language.

Imports and Aliases

import std::memory;
import std::*;
import my_module::some_function;
import my_module::some_typename;

using mem = std::memory;
using malloc = mem::mem_alloc;
using my_type = my_module::some_typename;
using my_func = my_module::some_function;
  • import module::symbol; brings a specific symbol into scope.
  • import module::*; imports all direct children of module (not recursive).
  • using introduces aliases for modules, types, or functions.

Structs and Generics

struct Point<typename T> {
  x: T,
  y: T
}

struct Rectangle {
  top: Point::<i32>,
  bot: Point::<i32>
}
  • Generic definitions use <typename T>.
  • Instantiations require ::<> (turbofish) to disambiguate from comparisons.
  • Fields use name: Type syntax.

Functions and Methods

fn meaning_of_life() -> i32 {
  return 42;
}

fn scale<typename T>(lhs: *Point::<T>, rhs: T) -> Point::<T> {
  return Point::<T> {
    .x = lhs->x * rhs,
    .y = lhs->y * rhs
  };
}

fn add<typename T>(this *Point::<T>, other: *Point::<T>) {
  this->x += other->x;
  this->y += other->y;
}
  • Return types follow the parameter list via ->.
  • Methods use this <type> as the first parameter, enabling both member and free-function call styles.

Enums and Variants

enum Result<typename T, typename E> {
  Ok(T),
  Err(E)
}

return Result::<void, i32>::Err{ -1 };
return Result::<void, i32>::Ok{};
  • Result demonstrates tagged unions with data payloads.
  • Variants initialize with braces, optionally containing payloads.

Variables, Pointers, Qualifiers

let x: i32 = 10;
let answer = meaning_of_life();
def PI: f64 = 3.14159265358979;

def ptr: *i32 = &answer;
let mutable_pointer: *$i32 = &x;
let complex_pointer: *$*$i32 = &mutable_pointer;
let null_int: ?i32 = null;
  • let for mutable, def for immutable bindings.
  • Qualifiers *, $, ? apply to the immediate type to the right and can be combined to express rich pointer semantics.

Slices and Literals

let arrSlice: ?[]i32 = []i32 { 2, 4, 6, 8, 10 };

let full  = arrSlice[:];
let range = arrSlice[1:3];
let head  = arrSlice[:2];
let tail  = arrSlice[2:];

let memPtr    = arrSlice.*;
let memLength = arrSlice.#;
let newSlice  = memPtr.[memLength];
  • []Type { ... } constructs slice literals.
  • Slicing syntax mirrors Python with optional start/end.
  • Specialized suffixes:
    • expr.* raw pointer;
    • expr.# length;
    • ptr.[len] create slice from pointer + length.

Reflection

def refl_info = foo.@;
def xalign = Point::<u32>::x.@alignment;
def type_size = Point::<u32>.@size;
  • . @ yields metadata for values, types, or struct members.
  • Attributes include @alignment, @size, @typename, @offset.

Resource Management

defer cleanup();
errdefer { log_failure(); }
  • defer schedules work at scope exit (LIFO order).
  • errdefer runs only when the function returns an error variant.

These constructs appear throughout idiomatic artichoke code and are supported by the current parser.