all clippys done

This commit is contained in:
Shautvast 2025-11-12 13:00:41 +01:00
parent 0e919983bd
commit bcd81677bb
9 changed files with 99 additions and 84 deletions

52
Cargo.lock generated
View file

@ -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"

View file

@ -1,5 +1,5 @@
[package]
name = "crudlang"
name = "tipi-lang"
version = "0.1.0"
edition = "2024"

View file

@ -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

View file

@ -33,3 +33,6 @@ pub fn call(
(self_val, args)
}
pub(crate) fn expected(expected_type: &str) -> RuntimeError {
RuntimeError::ExpectedType(expected_type.to_string())
}

View file

@ -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<Value>) -> Result<Value, RuntimeError> {
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<Value>) -> Result<Value, RuntimeError> {
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<Value>) -> Result<Value, RuntimeError> {
match self_val {
Value::String(s) => Ok(string(s.to_lowercase())),
_ => Err(expected_a_string()),
}
}
fn string_contains(self_val: Value, args: Vec<Value>) -> Result<Value, RuntimeError> {
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<Value>) -> Result<Value, RuntimeError>
Value::String(s) => {
Ok(s.chars().rev().collect::<String>().into())
}
_ => Err(RuntimeError::ExpectedType("string".to_string())),
_ => Err(expected_a_string()),
}
}
fn expected_a_string() -> RuntimeError {
expected("string")
}

View file

@ -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<Statement>,
symbols: &SymbolTable,
registry: &mut Registry,
namespace: &str,
) -> Result<Chunk, CompilerErrorAtLine> {
) -> Result<Self, CompilerErrorAtLine> {
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, .. } => {

View file

@ -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(())

View file

@ -32,6 +32,18 @@ pub enum Value {
Void,
}
pub(crate) fn string(v: impl Into<String>) -> Value {
Value::String(v.into())
}
pub(crate) fn i64(v: impl Into<i64>) -> Value {
Value::I64(v.into())
}
pub(crate) fn bool(v: impl Into<bool>) -> Value {
Value::Bool(v.into())
}
impl Value {
pub fn cast_u32(self) -> Result<Self, ValueError> {
match self {

View file

@ -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<Value>,
local_vars: HashMap<String, Value>,
error_occurred: bool,
registry: Arc<HashMap<String, Chunk>>,
pub(crate) registry: Arc<HashMap<String, Chunk>>,
}
pub fn interpret(
registry: Guard<Arc<HashMap<String, Chunk>>>,
function: &str,
) -> Result<Value, RuntimeError> {
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(&registry);
vm.run(&get_context(function), &chunk)
}
@ -44,13 +36,7 @@ pub async fn interpret_async(
) -> Result<Value, RuntimeError> {
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(&registry);
vm.local_vars
.insert("path".to_string(), Value::String(uri.into()));
vm.local_vars
@ -71,18 +57,21 @@ fn value_map(strings: HashMap<String, String>) -> HashMap<Value, Value> {
}
pub fn interpret_function(chunk: &Chunk, args: Vec<Value>) -> Result<Value, RuntimeError> {
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<Registry>) -> 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<Value>) -> Result<Value, RuntimeError> {
// 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<Value, RuntimeError> {
pub(crate) fn run(&mut self, context: &str, chunk: &Chunk) -> Result<Value, RuntimeError> {
loop {
if self.error_occurred {
return Err(Something);
@ -313,7 +302,7 @@ fn unary_op(stack: &mut Vm, op: impl Fn(&Value) -> Result<Value, ValueError> + 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);