| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
VM.VMValue
Synopsis
- data VMValue
- getValueName :: VMValue -> Text
- castValue :: Word8 -> VMValue -> VMValue
- valueToString :: VMValue -> Text
- valueToInt :: VMValue -> Int
- eqValue :: VMValue -> VMValue -> Bool
- stringToValue :: Text -> VMValue
- charToValue :: Char -> VMValue
Documentation
Represents a runtime value within the Virtual Machine.
@details This data structure encompasses all possible types that can exist on the stack or in memory during the execution of the program. It bridges the gap between the static AST and the dynamic runtime.
Constructors
| VInt IntValue | Represents an integer value (wrapping the Common IntValue type). Used for all arithmetic operations. |
| VBool Bool | Represents a boolean value (#t or #f). Used for logic operations and conditional jumps. |
| VList (Vector VMValue) | Represents a dynamic list of values.
Corresponds to the |
| VStruct (Vector VMValue) | Represents an instance of a structure. Fields are stored contiguously. The compiler resolves field names to indices. |
| VClosure Int (Vector VMValue) | Represents a closure (anonymous function with captured context).
|
| VFuncPtr Int | Represents a raw function pointer without captured variables. @param Int The absolute instruction pointer address. |
| VVoid | Represents the absence of a value (Unit/Null). Used for functions that do not return data. |
getValueName :: VMValue -> Text Source #
Recursively determines the precise type name of a value.
arg val: The VMValue to inspect.
return The formatted type string (e.g., "[int]", Player, "[[char]]").
@details - Primitives: Returns "int", "bool", "void". - Chars: Distinguished from Ints via IChar/UIChar -> "char". - Structs: Returns the stored name (e.g., Player). - Lists: Recursively inspects the *first* element to build the type syntax. [] -> "[void]" [1, 2] -> "[int]" ["hi"] -> "[[char]]"
castValue :: Word8 -> VMValue -> VMValue Source #
Converts a VMValue to a specific target TypeID.
@args - typeId: The target type identifier (see ASM_SPEC). - val: The value to convert.
@details
Performs a native conversion (truncation or extension) via fromIntegral.
Supports converting between Booleans, Integers (signed/unsigned) and Chars.
valueToString :: VMValue -> Text Source #
Converts a VMValue into a human-readable Text representation.
param val The value to convert.
return A Text representing the value.
@details This function handles type-specific formatting: - **Chars**: Converted to single-character strings. - **Lists**: If the list contains only Characters, it is rendered as a raw string (e.g., Hello). Otherwise, it uses list notation (e.g., "[1, 2, #t]"). - **Structs**: Rendered with curly braces (e.g., "{10, 20}"). - **Bools**: Rendered as #t or #f (Scheme style).
valueToInt :: VMValue -> Int Source #
Helper to extract a raw integer value for casting purposes.
@details - Integers: returns the raw value. - Booleans: True -> 1, False -> 0. - Pointers: returns the address. - Others: returns 0 (safe default).
eqValue :: VMValue -> VMValue -> Bool Source #
Recursive equality check for VMValues.
@details Handles loose integer comparison inside Lists and Structs. e.g., [I8 5] == [I64 5] returns True.
stringToValue :: Text -> VMValue Source #
Converts a Text string into a VMValue.
@arg txt: The text string to convert.
@details
Transforms the Text into a VList of VInt (IChar).
This reverses the logic of valueToString for strings, creating
the standard list-based string representation used by the VM.
charToValue :: Char -> VMValue Source #
Converts a single Character to a VMValue.
@arg c: The character to convert.
@details Wraps the character's ordinal value into an IChar (Int8). Used as a helper for string conversion.