# calc **Repository Path**: jfiewo/calc ## Basic Information - **Project Name**: calc - **Description**: A little language. - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-12 - **Last Updated**: 2025-07-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # calcscript A little language. Run with: `python3 run.py [file_name]` REPL may be called with `python3 run.py -r`. The standard library is located at `lib/lib.calc`. --- * In this document, functions are referred to by `()`. * e.g. `(type)` the function "type", `(+)` the function "+". * Note: Division by `Same` raises an error. * To write a for loop, it's easier to count up and check for equality. * Check for a specific `(Invalid)` value: * `(any= (car (\ a)) (type (Invalid a)))` ## Notes on `(-)` * `(-)` returns * An `Int`, when lhs > rhs. * `(Same)`, when the two args are equal. * `(Invalid)`, when lhs < rhs. Thus, it can function both like an `==` operator and a `<` operator. ## Types There are three (standard) types, `Int()`, `Same()`, and `Invalid()`. * `Int` types can only hold a positive integer. * The `Same` type can only be generated by subtracting two same integers. * e.g. `(- 1 1)` prints `(Same)`. * The `Invalid` type may be caused by the following: * Subtracting a smaller integer by a larger one. * e.g. `(- 3 4)` prints `(Invalid: (-))`. * The divisor isn't a factor of the dividend. * e.g. `(/ 12 5)` prints `(Invalid: (/))`. Using `(Invalid)` in calculations raises an Error. ## Defining functions `(fn ( ...) )` Call that with e.g. `( 1 2)`. For example: ``` (fn mult (a b) (* a b)) (p (mult 3 4)) ``` ## Generating `Same` and `Invalid` values * `(Same)` - returns a value of `Same` type. * `(Invalid )` - returns a value of `Invalid` type, with message ``. * If there's no ``, the default message is `Invalid`. ## Logical operations `(Same)` is truthy, `Int`s and `(Invalid)` are falsy. * `(t )` - Logical NOT. * Turns `(Same)` into `(Invalid)`; * Turns other values into `(Same)`. * `(d ...)` - Logical AND. * `(r ...)` - Logical OR. * `(ch ...)` - Evaluate expressions in sequence. * Return value is last expression. ## Arithmetic operations CalcScript supports four arithmetic operations. * `(+ )` * `(- )` * `(* )` * `(/ )` Edge-cases are described in section "Types". ## Misc operations * `(. a)` yields the value of variable `a` (defined with `(let)`). * `(\ ...)` returns args quoted as a list. * e.g. `(\ 1 2 3)` yields `[Int(1),Int(2),Int(3)]`. * `(q ...)` is an alias for this function. * `(car )`: Returns first item of list. * If `` is an atom, or is an empty list, return `(Invalid)`. * (So, this is `(car)` and `(atom?)` packed into a single command.) * `(cdr )`: Returns remaining items of list. * If `` has less than 2 items, returns `(Invalid)`. * `(cons )`: Constructs a list. * Wraps `` in a list, then concatenates it to ``. * `(chmsg ...)` If `` is of `Invalid` type, change its message to `...`. * `(factors )` Returns all proper divisors of ``. * `(find )` * For a condition involving `i`, find all positive integers which satisfy this condition. * If the user interrupts the program, a list containing all generated values of `i` is returned. * `(for i )`: For loop. * For each integer `i` between `` and ``: * Evaluate ``. * `(len )` Returns the length of `` (a list). * Returns `Invalid('len')` otherwise. * `(let )`: * Assigns the evaluated value of `` to a variable named ``. * `` is only evaluated once. * `(msg )`: Alias for `(type)`. * Returns the error message (for Invalid-type ``). * Returns `(Invalid: (msg))` otherwise. * `(msg_of)` is an alias for `(msg)`. * `(range )`: Returns a list of `Int`s between `` and ``. * e.g. `(range 2 5)` prints: `[Int(2), Int(3), Int(4), Int(5)]`. * `(sum )`: Returns the sum of a list ``. * `(while )`: * while `` evaluates to `Same`: * Evaluate ``. * Return `Same`. ## Working with `(Same)` and `(Invalid)` * `(eq )` * if `` is `(Same)`, replace it with ``. * Otherwise, return ``. * (If there's no ``, return `(Invalid)`.) * `(invalid? )` * if `` is not `(Invalid)`, replace it with `(Same)`. * Otherwise, return `(Invalid)`. * `(any= )` * Check if any two atoms are the same. * (This includes `(Same)` and `(Invalid)`.) * `(valid? )` * Same as `(invalid?)`. * `(type )` * If type is `(Invalid)`: * Returns the error message of `(Invalid)`. * Otherwise: return `(Invalid: (type))`. * `(gtype )` returns "global type": * `(1)` - Int; `(2)` - Same; `(3)` - Invalid; `(4)` - List.