From bcd81677bb1db078a114ad7792c0265299e1ba6e Mon Sep 17 00:00:00 2001 From: Shautvast Date: Wed, 12 Nov 2025 13:00:41 +0100 Subject: [PATCH] all clippys done --- Cargo.lock | 52 ++++++++++++++++++++-------------------- Cargo.toml | 2 +- source/hello/web.crud | 5 ++-- src/builtins/mod.rs | 3 +++ src/builtins/string.rs | 30 ++++++++++++++++------- src/bytecode_compiler.rs | 16 ++++++------- src/main.rs | 14 +++++------ src/value.rs | 14 ++++++++++- src/vm.rs | 47 ++++++++++++++---------------------- 9 files changed, 99 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2bd431f..f535543 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -293,32 +293,6 @@ dependencies = [ "libc", ] -[[package]] -name = "crudlang" -version = "0.1.0" -dependencies = [ - "arc-swap", - "axum", - "chrono", - "clap", - "dotenv", - "log", - "log4rs", - "notify", - "reqwest", - "serde", - "thiserror", - "tokio", - "tokio-postgres", - "tower", - "tower-http", - "tower-livereload", - "tracing", - "tracing-subscriber", - "url", - "walkdir", -] - [[package]] name = "crypto-common" version = "0.1.6" @@ -1852,6 +1826,32 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "tipi-lang" +version = "0.1.0" +dependencies = [ + "arc-swap", + "axum", + "chrono", + "clap", + "dotenv", + "log", + "log4rs", + "notify", + "reqwest", + "serde", + "thiserror", + "tokio", + "tokio-postgres", + "tower", + "tower-http", + "tower-livereload", + "tracing", + "tracing-subscriber", + "url", + "walkdir", +] + [[package]] name = "tokio" version = "1.48.0" diff --git a/Cargo.toml b/Cargo.toml index 95acecd..adfb9a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "crudlang" +name = "tipi-lang" version = "0.1.0" edition = "2024" diff --git a/source/hello/web.crud b/source/hello/web.crud index 100f0ad..ecea46d 100644 --- a/source/hello/web.crud +++ b/source/hello/web.crud @@ -1,6 +1,5 @@ object Person: name: string -// fn get(path: string) -> string: -// let p = Person(name: path) -// service.add("hello", p.name) +fn get(path: string) -> string: + "hello" + path \ No newline at end of file diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index 7a80f53..18dd8bc 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -33,3 +33,6 @@ pub fn call( (self_val, args) } +pub(crate) fn expected(expected_type: &str) -> RuntimeError { + RuntimeError::ExpectedType(expected_type.to_string()) +} \ No newline at end of file diff --git a/src/builtins/string.rs b/src/builtins/string.rs index 0914f08..f9e12e1 100644 --- a/src/builtins/string.rs +++ b/src/builtins/string.rs @@ -1,13 +1,14 @@ use std::collections::HashMap; -use crate::builtins::{insert, MethodMap}; +use crate::builtins::{expected, insert, MethodMap}; use crate::errors::RuntimeError; -use crate::value::Value; +use crate::value::{bool, i64, string, Value}; pub(crate) fn string_methods() -> MethodMap { let mut string_methods: MethodMap = HashMap::new(); let m = &mut string_methods; insert(m, "len", string_len); insert(m, "to_uppercase", string_to_uppercase); + insert(m, "to_lowercase", string_to_lowercase); insert(m, "contains", string_contains); insert(m, "reverse", string_reverse); string_methods @@ -15,24 +16,31 @@ pub(crate) fn string_methods() -> MethodMap { fn string_len(self_val: Value, _args: Vec) -> Result { match self_val { - Value::String(s) => Ok(Value::I64(s.len() as i64)), - _ => Err(RuntimeError::ExpectedType("string".to_string())), + Value::String(s) => Ok(i64(s.len() as i64)), + _ => Err(expected_a_string()), } } fn string_to_uppercase(self_val: Value, _args: Vec) -> Result { match self_val { - Value::String(s) => Ok(Value::String(s.to_uppercase())), - _ => Err(RuntimeError::ExpectedType("string".to_string())), + Value::String(s) => Ok(string(s.to_uppercase())), + _ => Err(expected_a_string()), + } +} + +fn string_to_lowercase(self_val: Value, _args: Vec) -> Result { + match self_val { + Value::String(s) => Ok(string(s.to_lowercase())), + _ => Err(expected_a_string()), } } fn string_contains(self_val: Value, args: Vec) -> Result { match (self_val, args.first()) { (Value::String(s), Some(Value::String(pat))) => { - Ok(Value::Bool(s.contains(pat.as_str()))) + Ok(bool(s.contains(pat.as_str()))) } - _ => Err(RuntimeError::ExpectedType("string".to_string())), + _ => Err(expected_a_string()), } } @@ -41,6 +49,10 @@ fn string_reverse(self_val: Value, _: Vec) -> Result Value::String(s) => { Ok(s.chars().rev().collect::().into()) } - _ => Err(RuntimeError::ExpectedType("string".to_string())), + _ => Err(expected_a_string()), } +} + +fn expected_a_string() -> RuntimeError { + expected("string") } \ No newline at end of file diff --git a/src/bytecode_compiler.rs b/src/bytecode_compiler.rs index 577f9d9..d98b008 100644 --- a/src/bytecode_compiler.rs +++ b/src/bytecode_compiler.rs @@ -39,7 +39,7 @@ pub(crate) fn compile_function( compiler.vars.insert(name, var_index); } - let mut chunk = compiler.compile(&function.body, symbols, registry, namespace)?; + let mut chunk = compiler.compile(&function.body, symbols, registry, namespace)?.chunk; chunk.function_parameters = function.parameters.to_vec(); Ok(chunk) } @@ -52,7 +52,7 @@ pub(crate) fn compile_in_namespace( ) -> Result<(), CompilerErrorAtLine> { let name = namespace.unwrap_or("main"); let compiler = Compiler::new(name); - let chunk = compiler.compile(ast, symbols, registry, name)?; + let chunk = compiler.compile(ast, symbols, registry, name)?.chunk; let qname = if let Some(namespace) = namespace { format!("{}/{}", namespace, "main") } else { @@ -62,7 +62,7 @@ pub(crate) fn compile_in_namespace( Ok(()) } -struct Compiler { +pub(crate) struct Compiler { chunk: Chunk, _had_error: bool, current_line: usize, @@ -70,7 +70,7 @@ struct Compiler { } impl Compiler { - fn new(name: &str) -> Self { + pub(crate) fn new(name: &str) -> Self { Self { chunk: Chunk::new(name), _had_error: false, @@ -79,19 +79,19 @@ impl Compiler { } } - fn compile( + pub(crate) fn compile( mut self, ast: &Vec, symbols: &SymbolTable, registry: &mut Registry, namespace: &str, - ) -> Result { + ) -> Result { for statement in ast { self.compile_statement(statement, symbols, registry, namespace)?; } self.emit_byte(OP_RETURN); - Ok(self.chunk) + Ok(self) } fn raise(&self, error: CompilerError) -> CompilerErrorAtLine { @@ -223,7 +223,7 @@ impl Compiler { if let Some(name_index) = name_index { self.emit_bytes(OP_GET, *name_index as u16); } else { - return Err(self.raise(CompilerError::UndeclaredVariable(name.to_string()))); + return Err(self.raise(UndeclaredVariable(name.to_string()))); } } Expression::Literal { value, .. } => { diff --git a/src/main.rs b/src/main.rs index e457c1a..92b97a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,10 @@ use axum::http::StatusCode; use axum::routing::any; use axum::{Json, Router}; use clap::Parser; -use crudlang::chunk::Chunk; -use crudlang::errors::CrudLangError; -use crudlang::vm::interpret_async; -use crudlang::{compile_sourcedir, map_underlying}; +use tipi_lang::chunk::Chunk; +use tipi_lang::errors::CrudLangError; +use tipi_lang::vm::interpret_async; +use tipi_lang::{compile_sourcedir, map_underlying}; use std::collections::HashMap; use std::sync::Arc; use arc_swap::ArcSwap; @@ -37,7 +37,7 @@ async fn main() -> Result<(), CrudLangError> { let swap = Arc::new(ArcSwap::from(Arc::new(registry))); if !empty { if args.watch { - crudlang::file_watch::start_watch_daemon(&source, swap.clone()); + tipi_lang::file_watch::start_watch_daemon(&source, swap.clone()); } println!("-- Compilation successful --"); let state =AppState { @@ -57,14 +57,14 @@ async fn main() -> Result<(), CrudLangError> { ); if args.repl { - std::thread::spawn(move || crudlang::repl::start(swap.clone()).unwrap()); + std::thread::spawn(move || tipi_lang::repl::start(swap.clone()).unwrap()); } axum::serve(listener, app).await.map_err(map_underlying())?; } else { println!("No source files found or compilation error"); if args.repl { - crudlang::repl::start(swap.clone())?; + tipi_lang::repl::start(swap.clone())?; } } Ok(()) diff --git a/src/value.rs b/src/value.rs index 888ac7e..272f4e5 100644 --- a/src/value.rs +++ b/src/value.rs @@ -9,7 +9,7 @@ use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Shl, Shr, Sub}; #[derive(Debug, Clone)] pub struct Object { pub(crate) definition: String, - pub(crate) fields: Vec<(String,Value)>, + pub(crate) fields: Vec<(String, Value)>, } #[derive(Debug, Clone)] @@ -32,6 +32,18 @@ pub enum Value { Void, } +pub(crate) fn string(v: impl Into) -> Value { + Value::String(v.into()) +} + +pub(crate) fn i64(v: impl Into) -> Value { + Value::I64(v.into()) +} + +pub(crate) fn bool(v: impl Into) -> Value { + Value::Bool(v.into()) +} + impl Value { pub fn cast_u32(self) -> Result { match self { diff --git a/src/vm.rs b/src/vm.rs index 78b315a..b76bfb4 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -7,31 +7,23 @@ use arc_swap::Guard; use std::collections::HashMap; use std::sync::Arc; use tracing::debug; +use crate::Registry; pub struct Vm { ip: usize, stack: Vec, local_vars: HashMap, error_occurred: bool, - registry: Arc>, + pub(crate) registry: Arc>, } + pub fn interpret( registry: Guard>>, function: &str, ) -> Result { let chunk = registry.get(function).unwrap().clone(); - // for (key,value) in registry.iter() { - // println!("{}", key); - // value.disassemble(); - // } - let mut vm = Vm { - ip: 0, - stack: vec![], - local_vars: HashMap::new(), - error_occurred: false, - registry: registry.clone(), - }; + let mut vm = Vm::new(®istry); vm.run(&get_context(function), &chunk) } @@ -44,13 +36,7 @@ pub async fn interpret_async( ) -> Result { let chunk = registry.get(function); if let Some(chunk) = chunk { - let mut vm = Vm { - ip: 0, - stack: vec![], - local_vars: HashMap::new(), - error_occurred: false, - registry: registry.clone(), - }; + let mut vm = Vm::new(®istry); vm.local_vars .insert("path".to_string(), Value::String(uri.into())); vm.local_vars @@ -71,18 +57,21 @@ fn value_map(strings: HashMap) -> HashMap { } pub fn interpret_function(chunk: &Chunk, args: Vec) -> Result { - let mut vm = Vm { - ip: 0, - stack: vec![], - local_vars: HashMap::new(), - error_occurred: false, - registry: Arc::new(HashMap::new()), - }; - + let mut vm = Vm::new(& Arc::new(HashMap::new())); vm.run_function(chunk, args) } impl Vm { + pub(crate) fn new(registry: &Arc) -> Self { + Self { + ip: 0, + stack: vec![], + local_vars: HashMap::new(), + error_occurred: false, + registry: registry.clone(), + } + } + fn run_function(&mut self, chunk: &Chunk, mut args: Vec) -> Result { // arguments -> locals for (_, name) in chunk.vars.iter() { @@ -91,7 +80,7 @@ impl Vm { self.run("", chunk) } - fn run(&mut self, context: &str, chunk: &Chunk) -> Result { + pub(crate) fn run(&mut self, context: &str, chunk: &Chunk) -> Result { loop { if self.error_occurred { return Err(Something); @@ -313,7 +302,7 @@ fn unary_op(stack: &mut Vm, op: impl Fn(&Value) -> Result + C } } -fn get_context(path: &str) -> String { +pub(crate) fn get_context(path: &str) -> String { let mut parts: Vec<&str> = path.split('/').collect(); if parts.len() >= 2 { parts.truncate(parts.len() - 2);