refactored a bit so that the file will not grow out of bounds

This commit is contained in:
Shautvast 2025-11-10 18:23:47 +01:00
parent 1315b2878a
commit f7230a30f4
5 changed files with 47 additions and 32 deletions

View file

@ -0,0 +1,33 @@
use crate::value::Value;
use std::collections::HashMap;
use std::sync::LazyLock;
use crate::builtins::string::string_methods;
use crate::errors::RuntimeError;
pub(crate) type MethodFn = fn(Value, Vec<Value>) -> Result<Value, RuntimeError>;
pub(crate) type MethodMap = HashMap<String, MethodFn>;
pub(crate) type MethodTable = HashMap<String, MethodMap>;
const METHODS: LazyLock<MethodTable> = LazyLock::new(|| {
let mut table: MethodTable = HashMap::new();
table.insert("string".to_string(), string_methods());
table
});
pub(crate) fn insert(m: &mut MethodMap, name: &str, method: MethodFn) {
m.insert(name.to_string(), method);
}
pub fn call_builtin(
type_name: &str,
method_name: &str,
self_val: Value,
args: Vec<Value>,
) -> Result<Value, RuntimeError> {
METHODS
.get(type_name)
.and_then(|methods| methods.get(method_name))
.ok_or_else(|| RuntimeError::FunctionNotFound(format!("{}.{}",type_name, method_name)))?
(self_val, args)
}

2
src/builtins/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod builtin_functions;
mod string;

View file

@ -1,37 +1,16 @@
use crate::value::Value;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::LazyLock; use crate::builtins::builtin_functions::{insert, MethodMap};
use crate::errors::RuntimeError; use crate::errors::RuntimeError;
use crate::value::Value;
type MethodFn = fn(Value, Vec<Value>) -> Result<Value, RuntimeError>; pub(crate) fn string_methods() -> MethodMap {
type MethodMap = HashMap<String, MethodFn>;
type MethodTable = HashMap<String, MethodMap>;
const METHODS: LazyLock<MethodTable> = LazyLock::new(|| {
let mut table: MethodTable = HashMap::new();
let mut string_methods: MethodMap = HashMap::new(); let mut string_methods: MethodMap = HashMap::new();
string_methods.insert("len".to_string(), string_len); let m = &mut string_methods;
string_methods.insert("to_uppercase".to_string(), string_to_uppercase); insert(m, "len", string_len);
string_methods.insert("contains".to_string(), string_contains); insert(m, "to_uppercase", string_to_uppercase);
string_methods.insert("reverse".to_string(), string_reverse); insert(m, "contains", string_contains);
insert(m, "reverse", string_reverse);
table.insert("string".to_string(), string_methods); string_methods
table
});
pub fn call_builtin(
type_name: &str,
method_name: &str,
self_val: Value,
args: Vec<Value>,
) -> Result<Value, RuntimeError> {
METHODS
.get(type_name)
.and_then(|methods| methods.get(method_name))
.ok_or_else(|| RuntimeError::FunctionNotFound(format!("{}.{}",type_name, method_name)))?
(self_val, args)
} }
fn string_len(self_val: Value, _args: Vec<Value>) -> Result<Value, RuntimeError> { fn string_len(self_val: Value, _args: Vec<Value>) -> Result<Value, RuntimeError> {
@ -56,6 +35,7 @@ fn string_contains(self_val: Value, args: Vec<Value>) -> Result<Value, RuntimeEr
_ => Err(RuntimeError::ExpectedType("string".to_string())), _ => Err(RuntimeError::ExpectedType("string".to_string())),
} }
} }
fn string_reverse(self_val: Value, _: Vec<Value>) -> Result<Value, RuntimeError> { fn string_reverse(self_val: Value, _: Vec<Value>) -> Result<Value, RuntimeError> {
match self_val { match self_val {
Value::String(s) => { Value::String(s) => {

View file

@ -23,7 +23,7 @@ mod symbol_builder;
mod tokens; mod tokens;
mod value; mod value;
pub mod vm; pub mod vm;
mod builtin_functions; mod builtins;
pub fn compile_sourcedir(source_dir: &str) -> Result<HashMap<String, Chunk>, CrudLangError> { pub fn compile_sourcedir(source_dir: &str) -> Result<HashMap<String, Chunk>, CrudLangError> {
let mut registry = HashMap::new(); let mut registry = HashMap::new();

View file

@ -7,7 +7,7 @@ use arc_swap::Guard;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use tracing::debug; use tracing::debug;
use crate::builtin_functions::call_builtin; use crate::builtins::builtin_functions::call_builtin;
pub struct Vm { pub struct Vm {
ip: usize, ip: usize,