Basic Syntax & Data Types

Comments and Basic Data Types
# Comments start with #
# Basic data types
let
  # Strings
  name = "Nixi";
  greeting = 'Hello World';
  
  # Numbers
  integer = 42;
  float = 3.14;
  
  # Booleans
  isTrue = true;
  isFalse = false;
  
  # Lists (arrays)
  numbers = [1, 2, 3, 4];
  names = ["Alice", "Bob", "Charlie"];
  
  # Objects/Records
  person = {
    name: "Alice";
    age: 30;
    city: "New York"
  };
in
  echo "Basic types defined!"
Functions & Lambda Expressions
# Function definitions
let
  # Simple function (curried form)
  add = x: y: x + y;
  
  # Function with record parameters
  greet = { name, age }:
    "Hello " + name + ", you are " + toString(age) + " years old";
  
  # Function application
  result1 = add 5 3;  # Returns 8
  result2 = greet { name = "Alice"; age = 30 };
  
  # Higher-order function
  map = f: list:
    if length(list) == 0
    then []
    else [f (head(list))] ++ map f (tail(list));
  
  doubled = map (x: x * 2) [1, 2, 3];  # [2, 4, 6]
in
  result2
Built-in Functions
# System operations (Bash-like)
let
  # File operations
  files = ls(".");           # List files in current directory
  currentDir = pwd();        # Get current working directory
  cd("/tmp");              # Change directory
  
  # Output functions
  echo "Hello World";     # Print to console
  print "Debug info";     # Alternative print function
  
  # String operations
  message = "Hello" + " " + "World";  # String concatenation
  upper = toUpperCase(message);       # Convert to uppercase
  lower = toLowerCase(message);       # Convert to lowercase
  
  # Mathematical operations
  sum = add(5, 3);           # Addition
  product = multiply(4, 6);    # Multiplication
  sqrt_val = sqrt(16);        # Square root
  
  # List operations
  first = head([1, 2, 3]);     # First element: 1
  rest = tail([1, 2, 3]);     # All but first: [2, 3]
  len = length([1, 2, 3]);  # Length: 3
in
  echo "Built-in functions demo completed"

GUI Components

HTML-like GUI Components
# HTML-like GUI components
let
  # Basic elements
  Title = h1 { 
    text: "Welcome to Nixi";
    class: "title"
  };
  
  Paragraph = p {
    text: "This is a paragraph of text.";
    class: "content"
  };
  
  # Container with children
  Container = div {
    class: "container";
    children: [
      Title;
      Paragraph;
      span { text: "Inline text" }
    ]
  };
  
  # Interactive elements
  Button = button {
    text: "Click Me!";
    class: "btn";
    onClick: echo "Button clicked!"
  };
  
  # Form elements
  Input = input {
    type: "text";
    placeholder: "Enter your name";
    class: "form-input"
  };
  
  # Lists and tables
  List = ul {
    class: "item-list";
    children: [
      li { text: "Item 1" };
      li { text: "Item 2" };
      li { text: "Item 3" }
    ]
  };
in
  saveHTML(Container, "gui-demo.html", "GUI Components Demo")
CSS Styling System
# Define styles using the style keyword
style "container" {
  max-width: "800px";
  margin: "0 auto";
  padding: "20px";
  font-family: "Arial, sans-serif";
  background: "linear-gradient(135deg, #667eea 0%, #764ba2 100%)";
}

style "card" {
  background: "white";
  border-radius: "10px";
  padding: "20px";
  margin: "10px 0";
  box-shadow: "0 4px 6px rgba(0, 0, 0, 0.1)";
  transition: "transform 0.3s ease";
}

# Hover effects and pseudo-classes
style "button" {
  background: "#007bff";
  color: "white";
  border: "none";
  padding: "12px 24px";
  border-radius: "6px";
  cursor: "pointer";
  font-weight: "600";
}

# Media queries for responsive design
style "responsive-grid" {
  display: "grid";
  grid-template-columns: "repeat(auto-fit, minmax(300px, 1fr))";
  gap: "20px";
}

# Animation keyframes
style "animated" {
  animation: "fadeIn 0.5s ease-in";
}

# Now use these styles in components
let
  App = div {
    class: "container";
    children: [
      div {
        class: "card animated";
        children: [
          h2 { text: "Styled Card" };
          button {
            class: "button";
            text: "Styled Button";
            onClick: echo "Styled button clicked!"
          }
        ]
      }
    ]
  };
in
  saveHTML(App, "styled-app.html", "CSS Styling Demo")

Control Flow & Pattern Matching

Conditional Expressions
# Conditional expressions
let
  # If-then-else expressions
  absolute = x:
    if x >= 0
    then x
    else -x;
  
  # Pattern matching on records
  describePerson = person:
    if person.age < 18
    then person.name + " is a minor"
    else if person.age >= 65
    then person.name + " is a senior"
    else person.name + " is an adult";
  
  # List processing with recursion
  sumList = list:
    if length(list) == 0
    then 0
    else head(list) + sumList(tail(list));
  
  # Filter function
  filter = predicate: list:
    if length(list) == 0
    then []
    else if predicate(head(list))
    then [head(list)] ++ filter predicate (tail(list))
    else filter predicate (tail(list));
  
  # Usage examples
  abs5 = absolute -5;  # Returns 5
  description = describePerson { name = "Alice"; age = 25 };
  sum = sumList([1, 2, 3, 4, 5]);  # Returns 15
  evens = filter (x: x % 2 == 0) [1, 2, 3, 4, 5, 6];  # [2, 4, 6]
in
  description
Component Composition & Reusability
# Reusable component definitions
let
  # Generic card component
  Card = { title, content, class }: div {
    class: "card " + class;
    children: [
      h3 { text: title };
      p { text: content }
    ]
  };
  
  # Button component with variants
  Button = { text, variant, onClick }: button {
    class: "btn btn-" + variant;
    text: text;
    onClick: onClick
  };
  
  # List component for rendering arrays
  List = { items, renderItem }: ul {
    class: "list";
    children: map(renderItem, items)
  };
  
  # User profile component
  UserProfile = { user }: div {
    class: "user-profile";
    children: [
      img {
        src: user.avatar;
        alt: "Avatar of " + user.name;
        class: "avatar"
      };
      div {
        class: "user-info";
        children: [
          h4 { text: user.name };
          p { text: user.email };
          span { text: "Role: " + user.role }
        ]
      }
    ]
  };
  
  # Data for our components
  users = [
    { name = "Alice"; email = "alice@example.com"; role = "Developer"; avatar = "alice.jpg" };
    { name = "Bob"; email = "bob@example.com"; role = "Designer"; avatar = "bob.jpg" }
  ];
  
  # Main application using composed components
  App = div {
    class: "app";
    children: [
      Card {
        title: "User Management";
        content: "Manage your application users";
        class: "header-card"
      };
      List {
        items: users;
        renderItem: UserProfile
      };
      Button {
        text: "Add User";
        variant: "primary";
        onClick: echo "Add user clicked"
      }
    ]
  };
in
  saveHTML(App, "components.html", "Component Composition Demo")

Error Handling & Debugging

Error Handling Patterns
# Error handling patterns
let
  # Safe division with error handling
  safeDivide = numerator: denominator:
    if denominator == 0
    then {
      success: false;
      error: "Division by zero";
      value: null
    }
    else {
      success: true;
      error: null;
      value: numerator / denominator
    };
  
  # Safe list access
  safeHead = list:
    if length(list) == 0
    then null
    else head(list);
  
  # Debug logging function
  debug = message: value:
    let
      _ = echo ("[DEBUG] " + message + ": " + toString(value));
    in value;
  
  # Validation functions
  validateEmail = email:
    let
      hasAt = indexOf(email, "@") > -1;
      hasDot = indexOf(email, ".") > indexOf(email, "@");
    in hasAt && hasDot;
  
  # Result type for error handling
  Result = { success, value, error };
  
  # Safe file operations
  safeReadFile = filename:
    let
      fileExists = fileExists(filename);
    in
      if fileExists
      then Result {
        success: true;
        value: readFile(filename);
        error: null
      }
      else Result {
        success: false;
        value: null;
        error: "File not found: " + filename
      };
  
  # Usage examples
  result1 = safeDivide 10 2;  # Success case
  result2 = safeDivide 10 0;  # Error case
  
  debugResult = debug "Division result" result1;
  emailValid = validateEmail "user@example.com";
in
  echo "Error handling demo completed"