aboutsummaryrefslogtreecommitdiff
path: root/crates/sloth_vm/src/sloth_std/stdio.rs
blob: f56b6041776508bd08bc5b0af162583aa4644ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::io::{stdin, BufRead};

use crate::native::{self, NativeFunction, NativeFunctionResult};
use crate::value::{Object, ObjectType, Primitive};
use crate::VM;

fn write(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult {
    let Some(Primitive::Object(ptr)) = args.get(0).cloned() else {
        return Err(native::Error::InvalidArgument);
    };

    let object = vm
        .objects()
        .get(ptr as usize)
        .ok_or(native::Error::InvalidArgument)?;

    let ObjectType::String(str) = &object.typ else {
        return Err(native::Error::InvalidArgument);
    };

    print!("{str}");

    Ok(Primitive::Empty)
}

pub const WRITE_FUNCTION: NativeFunction = NativeFunction {
    name: "write",
    function: write,
    arity: 1,
    returns_value: false,
    doc: Some(
        "NativeFunction write: \n\targs: string (str)\n\tdesc: Writes <string> to the \
         terminal.\n\tExample: `write(\"I'm sleepy...\"); # Output: I'm sleepy...`",
    ),
};

fn writeln(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult {
    let Some(Primitive::Object(ptr)) = args.get(0).cloned() else {
        return Err(native::Error::InvalidArgument);
    };

    let object = vm
        .objects()
        .get(ptr as usize)
        .ok_or(native::Error::InvalidArgument)?;

    let ObjectType::String(str) = &object.typ else {
        return Err(native::Error::InvalidArgument);
    };

    println!("{str}");

    Ok(Primitive::Empty)
}

pub const WRITELN_FUNCTION: NativeFunction = NativeFunction {
    name: "writeln",
    function: writeln,
    arity: 1,
    returns_value: false,
    doc: Some(
        "NativeFunction writeln: \n\targs: string (str)\n\tdesc: Writes <string> to the terminal \
         and starts a new line.\n\tExample: `writeln(\"I'm sleepy...\"); # Output: I'm \
         sleepy...\n # This is a new line`",
    ),
};

fn read(vm: &mut VM, _args: &[Primitive]) -> NativeFunctionResult {
    let mut line = String::new();
    stdin()
        .lock()
        .read_line(&mut line)
        .map_err(|it| native::Error::Unknown(it.to_string()))?;

    let object = Object::new(ObjectType::String(line));
    let ptr = vm.objects_mut().allocate(object);

    Ok(Primitive::Object(ptr as u32))
}

pub const READ_FUNCTION: NativeFunction = NativeFunction {
    name: "read",
    function: read,
    arity: 0,
    returns_value: true,
    doc: Some(
        "NativeFunction read:\n\tdesc: Reads input from the terminal and returns what was \
         read.\n\tExample: `var input = read(); # Hello World <execute code> input = 'Hello \
         World'`",
    ),
};