refactored a bit so that the file will not grow out of bounds
This commit is contained in:
parent
1315b2878a
commit
f7230a30f4
5 changed files with 47 additions and 32 deletions
33
src/builtins/builtin_functions.rs
Normal file
33
src/builtins/builtin_functions.rs
Normal 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
2
src/builtins/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod builtin_functions;
|
||||
mod string;
|
||||
|
|
@ -1,37 +1,16 @@
|
|||
use crate::value::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::LazyLock;
|
||||
use crate::builtins::builtin_functions::{insert, MethodMap};
|
||||
use crate::errors::RuntimeError;
|
||||
use crate::value::Value;
|
||||
|
||||
type MethodFn = fn(Value, Vec<Value>) -> Result<Value, RuntimeError>;
|
||||
type MethodMap = HashMap<String, MethodFn>;
|
||||
type MethodTable = HashMap<String, MethodMap>;
|
||||
|
||||
const METHODS: LazyLock<MethodTable> = LazyLock::new(|| {
|
||||
let mut table: MethodTable = HashMap::new();
|
||||
|
||||
pub(crate) fn string_methods() -> MethodMap {
|
||||
let mut string_methods: MethodMap = HashMap::new();
|
||||
string_methods.insert("len".to_string(), string_len);
|
||||
string_methods.insert("to_uppercase".to_string(), string_to_uppercase);
|
||||
string_methods.insert("contains".to_string(), string_contains);
|
||||
string_methods.insert("reverse".to_string(), string_reverse);
|
||||
|
||||
table.insert("string".to_string(), 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)
|
||||
let m = &mut string_methods;
|
||||
insert(m, "len", string_len);
|
||||
insert(m, "to_uppercase", string_to_uppercase);
|
||||
insert(m, "contains", string_contains);
|
||||
insert(m, "reverse", string_reverse);
|
||||
string_methods
|
||||
}
|
||||
|
||||
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())),
|
||||
}
|
||||
}
|
||||
|
||||
fn string_reverse(self_val: Value, _: Vec<Value>) -> Result<Value, RuntimeError> {
|
||||
match self_val {
|
||||
Value::String(s) => {
|
||||
|
|
@ -23,7 +23,7 @@ mod symbol_builder;
|
|||
mod tokens;
|
||||
mod value;
|
||||
pub mod vm;
|
||||
mod builtin_functions;
|
||||
mod builtins;
|
||||
|
||||
pub fn compile_sourcedir(source_dir: &str) -> Result<HashMap<String, Chunk>, CrudLangError> {
|
||||
let mut registry = HashMap::new();
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use arc_swap::Guard;
|
|||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tracing::debug;
|
||||
use crate::builtin_functions::call_builtin;
|
||||
use crate::builtins::builtin_functions::call_builtin;
|
||||
|
||||
pub struct Vm {
|
||||
ip: usize,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue