Skip to main content

kore/
stdlib.rs

1//! KORE Standard Library
2
3use crate::types::ResolvedType;
4use std::collections::HashMap;
5
6/// Built-in function registry
7pub struct StdLib {
8    pub functions: HashMap<String, BuiltinFn>,
9    pub types: HashMap<String, ResolvedType>,
10}
11
12pub struct BuiltinFn {
13    pub name: &'static str,
14    pub params: Vec<(&'static str, &'static str)>,
15    pub return_type: &'static str,
16    pub doc: &'static str,
17}
18
19impl StdLib {
20    pub fn new() -> Self {
21        let mut lib = Self {
22            functions: HashMap::new(),
23            types: HashMap::new(),
24        };
25        
26        // I/O
27        lib.add_fn("print", &[("value", "Any")], "Unit", "Print value to console");
28        lib.add_fn("println", &[("value", "Any")], "Unit", "Print value with newline");
29        lib.add_fn("read_line", &[], "String", "Read line from stdin");
30        lib.add_fn("read_file", &[("path", "String")], "String", "Read file contents");
31        lib.add_fn("write_file", &[("path", "String"), ("content", "String")], "Unit", "Write to file");
32        
33        // Math
34        lib.add_fn("abs", &[("x", "Int")], "Int", "Absolute value");
35        lib.add_fn("sqrt", &[("x", "Float")], "Float", "Square root");
36        lib.add_fn("pow", &[("base", "Float"), ("exp", "Float")], "Float", "Power");
37        lib.add_fn("sin", &[("x", "Float")], "Float", "Sine");
38        lib.add_fn("cos", &[("x", "Float")], "Float", "Cosine");
39        lib.add_fn("tan", &[("x", "Float")], "Float", "Tangent");
40        lib.add_fn("floor", &[("x", "Float")], "Int", "Floor");
41        lib.add_fn("ceil", &[("x", "Float")], "Int", "Ceiling");
42        lib.add_fn("round", &[("x", "Float")], "Int", "Round");
43        lib.add_fn("min", &[("a", "Int"), ("b", "Int")], "Int", "Minimum");
44        lib.add_fn("max", &[("a", "Int"), ("b", "Int")], "Int", "Maximum");
45        lib.add_fn("clamp", &[("x", "Int"), ("lo", "Int"), ("hi", "Int")], "Int", "Clamp between bounds");
46        
47        // Vector math (for shaders)
48        lib.add_fn("vec2", &[("x", "Float"), ("y", "Float")], "Vec2", "Create 2D vector");
49        lib.add_fn("vec3", &[("x", "Float"), ("y", "Float"), ("z", "Float")], "Vec3", "Create 3D vector");
50        lib.add_fn("vec4", &[("x", "Float"), ("y", "Float"), ("z", "Float"), ("w", "Float")], "Vec4", "Create 4D vector");
51        lib.add_fn("dot", &[("a", "Vec3"), ("b", "Vec3")], "Float", "Dot product");
52        lib.add_fn("cross", &[("a", "Vec3"), ("b", "Vec3")], "Vec3", "Cross product");
53        lib.add_fn("normalize", &[("v", "Vec3")], "Vec3", "Normalize vector");
54        lib.add_fn("length", &[("v", "Vec3")], "Float", "Vector length");
55        lib.add_fn("distance", &[("a", "Vec3"), ("b", "Vec3")], "Float", "Distance between points");
56        lib.add_fn("mix", &[("a", "Float"), ("b", "Float"), ("t", "Float")], "Float", "Linear interpolation");
57        lib.add_fn("smoothstep", &[("edge0", "Float"), ("edge1", "Float"), ("x", "Float")], "Float", "Smooth step");
58        
59        // Collections
60        lib.add_fn("len", &[("collection", "Any")], "Int", "Get length");
61        lib.add_fn("push", &[("array", "Array"), ("value", "Any")], "Unit", "Push to array");
62        lib.add_fn("pop", &[("array", "Array")], "Any", "Pop from array");
63        lib.add_fn("map", &[("array", "Array"), ("fn", "Function")], "Array", "Map over array");
64        lib.add_fn("filter", &[("array", "Array"), ("fn", "Function")], "Array", "Filter array");
65        lib.add_fn("reduce", &[("array", "Array"), ("initial", "Any"), ("fn", "Function")], "Any", "Reduce array");
66        lib.add_fn("range", &[("start", "Int"), ("end", "Int")], "Array", "Create range");
67        
68        // HashMap
69        lib.add_fn("map_new", &[], "Any", "Create new map");
70        lib.add_fn("map_set", &[("map", "Any"), ("key", "String"), ("value", "Any")], "Unit", "Set map key");
71        lib.add_fn("map_get", &[("map", "Any"), ("key", "String")], "Any", "Get map value");
72        
73        // Sockets
74        lib.add_fn("socket_connect", &[("host", "String"), ("port", "Int")], "Int", "Connect TCP socket");
75        lib.add_fn("socket_send", &[("sock", "Int"), ("data", "String")], "Unit", "Send data");
76        lib.add_fn("socket_recv", &[("sock", "Int")], "String", "Receive data");
77        
78        // String
79        lib.add_fn("split", &[("s", "String"), ("sep", "String")], "Array", "Split string");
80        lib.add_fn("join", &[("arr", "Array"), ("sep", "String")], "String", "Join array to string");
81        lib.add_fn("trim", &[("s", "String")], "String", "Trim whitespace");
82        lib.add_fn("to_upper", &[("s", "String")], "String", "To uppercase");
83        lib.add_fn("to_lower", &[("s", "String")], "String", "To lowercase");
84        lib.add_fn("contains", &[("s", "String"), ("sub", "String")], "Bool", "Check contains");
85        lib.add_fn("replace", &[("s", "String"), ("from", "String"), ("to", "String")], "String", "Replace substring");
86        
87        // Conversion
88        lib.add_fn("to_string", &[("value", "Any")], "String", "Convert to string");
89        lib.add_fn("to_int", &[("value", "Any")], "Int", "Convert to int");
90        lib.add_fn("to_float", &[("value", "Any")], "Float", "Convert to float");
91        
92        // Debug
93        lib.add_fn("dbg", &[("value", "Any")], "Any", "Debug print and return");
94        lib.add_fn("assert", &[("condition", "Bool"), ("message", "String")], "Unit", "Assert condition");
95        lib.add_fn("panic", &[("message", "String")], "Never", "Panic with message");
96        
97        // Time
98        lib.add_fn("now", &[], "Float", "Current time in seconds");
99        lib.add_fn("sleep", &[("seconds", "Float")], "Unit", "Sleep for seconds");
100        
101        // Actors
102        lib.add_fn("spawn", &[("actor", "Actor")], "ActorRef", "Spawn actor");
103        lib.add_fn("send", &[("actor", "ActorRef"), ("message", "Message")], "Unit", "Send message");
104        
105        // Python FFI
106        lib.add_fn("py_eval", &[("code", "String")], "Any", "Evaluate Python expression");
107        lib.add_fn("py_exec", &[("code", "String")], "Unit", "Execute Python code");
108        lib.add_fn("py_import", &[("module", "String")], "Any", "Import Python module");
109
110        // UI
111        lib.add_fn("mount", &[("component", "Any"), ("selector", "String")], "Unit", "Mount component to DOM");
112
113        lib
114    }
115    
116    fn add_fn(&mut self, name: &'static str, params: &[(&'static str, &'static str)], ret: &'static str, doc: &'static str) {
117        self.functions.insert(name.to_string(), BuiltinFn {
118            name,
119            params: params.to_vec(),
120            return_type: ret,
121            doc,
122        });
123    }
124}
125
126impl Default for StdLib {
127    fn default() -> Self {
128        Self::new()
129    }
130}
131