Arturo Programming Language

website·Indexed 33 days ago

https://arturo-lang.io/documentation/

Documentation for the Arturo programming language, covering language fundamentals, example programs, and the standard library with a focus on arithmetic operations, type handling, in-place modifications, attributes, and custom types.

Tokens
37.5K
Snippets
335
Records
350
Agent score
18%

What's inside Arturo

  1. Retrieve system and process information

    Arturo provides several built-in symbols to inspect the environment:

    • sys: System metadata (OS, CPU architecture, cores, endianness, hostname).
    • process: Current process details (PID, memory usage).
    • path: Path information (current, home, temp).
    • script: Metadata embedded in the script via ;; documentation comments.
    ; System info
    print sys\os
    print sys\cpu\arch
    
    ; Process info
    print process\id
    
    ; Path info
    print path\current
    print path\home
  2. Understand Arturo evaluation and precedence

    Arturo has no operator precedence rules. Expressions are evaluated from right to left. For example, 2 * 3 + 4 is evaluated as 2 * (3 + 4), resulting in 14 rather than 10.
  3. Arturo Language Overview

    Arturo is a minimalist programming language characterized by a lack of reserved keywords and a flexible syntax. Code consists of words, labels, symbols, values, and attributes. It does not require semicolons, obligatory newlines, commas, parentheses, or specific indentation rules. Every word in the language can be redefined.
  4. Perform statistical analysis on numeric collections

    The Statistics module works with blocks and ranges of numbers (integers, floating-point, and rationals). All results are returned as floating-point numbers. By default, functions calculate population statistics; use the .sample attribute for sample statistics.
  5. Understand Arturo attributes

    Attributes in Arturo are a mechanism for defining optional named parameters for functions. A key characteristic of attributes is that they can transcend between different function calls, allowing state or configuration to persist across calls.
  6. Inspect symbols and runtime information

    The Reflection module allows developers to retrieve information about symbols, functions, and the current execution state. Use info to print details about a symbol, or info.get to retrieve that data as a dictionary. inspect provides a detailed structure of a value, and stack reveals the current value stack.
    ; Detailed inspection of a value
    inspect [1 2 #[name: "John"]
    
    ; Print info for a function
    info 'print
    
    ; Get info as a dictionary
    functionInfo: info.get 'print
    
    ; View current value stack
    print stack
  7. Assign variables and symbols in Arturo

    Variables can be assigned using a colon operator or the let keyword. Arturo also supports multiple assignments in a single statement.
    ; Basic assignment
    x: 5                ; assign value to symbol
    let 'x 5            ; alternative syntax
    
    ; Multiple assignments
    [a,b,c]: 1,2,3      ; assign multiple values
  8. Parse URL components

    The extract namespace can be used to parse various components of a URL string, including scheme, host, port, path, query, and anchor.
    url: "https://example.com:8080/path/page.html?q=term#section"
    
    print extract.scheme url        ; https
    print extract.host url          ; example.com
    print extract.port url          ; 8080
    print extract.path url          ; /path/page.html
    print extract.query url         ; q=term
    print extract.anchor url        ; section
  9. Perform database transactions and retrieve IDs

    To perform multiple operations as a single transaction, pass a block of query strings to the query function. To retrieve the ID of the last inserted row, use query.id or query.id.with for parametrized inserts.
    ; Transaction: execute multiple commands
    query db [
        "INSERT INTO users (name, age) VALUES ('Bob', 35)"
        "UPDATE users SET age = 31 WHERE name = 'Alice'"
    ]
    
    ; Get last insert ID
    lastId: query.id.with: ["Carol" 28] db {!sql 
        INSERT INTO users (name, age) VALUES (?,?)
    }
  10. Create strings using different syntaxes in Arturo

    Arturo supports multiple ways to define strings, including multi-line, verbatim, and heredoc styles.
    ; basic
    str: "hello world"
    
    ; multi-line (strips common indentation)
    text: {
        This is a multi-line string
    }
    
    ; verbatim (preserves exact formatting)
    code: {:
        def main():
            print("hello")
    :}
    
    ; right smart-quote (until end of line)
    message: « This is a single line string
    
    ; heredoc-style
    data: 
    ------
    Everything after the dashes
    until the end of the file
    ------
  11. Handle errors and exceptions in Arturo

    Arturo provides a set of tools for throwing, catching, and detecting errors. Errors can be generic or categorized by specific error kinds (e.g., syntaxError, systemError, typeError, uiError, valueError, vmError).
    ; Throwing a generic error
    err: try -> throw "Page not found"
    
    ; Using a custom errorKind
    pageNotFound: to :errorKind "404: Page not Found"
    err: try -> throw.as: pageNotFound "Seems that the page does not exist"
    
    ; Throwing using only the errorKind label as the message
    err: try -> throw pageNotFound
  12. Define and call functions

    Functions are defined using the function keyword or the $ alias. They can return values using the return keyword.
    ; Declaration
    f: function [x][ 2*x ]
    f: $[x]-> 2*x
    
    ; Calling
    f 10                        ; => 20
    
    ; Complex function with return
    g: function [x][
        if x < 2 -> return 0
        res: 0
        loop 0..x 'z [ res: res + z ]
        return res
    ]