diff --git a/README.md b/README.md index 21509dd..f9f6cc3 100644 --- a/README.md +++ b/README.md @@ -235,7 +235,3 @@ ISSUES * improve indenting WIP guards -* | /$uuid -> service.get(uuid)? - | /&first_name -> service.get_by_firstname(first_name)? - | /&last_name -> service.get_by_lastname(last_name)? - | 404 \ No newline at end of file diff --git a/examples/web/README.md b/examples/web/README.md index 5dd73a9..177eddd 100644 --- a/examples/web/README.md +++ b/examples/web/README.md @@ -1,18 +1,7 @@ * currently needs postgres on the default port * user 'postgres' * password 'boompje' -* create a table -``` -create table customers( - id bigint primary key, - first_name varchar(50), - last_name varchar(50) -) -``` -* insert data -``` -insert into customers (first_name,last_name) values ('first', 'last') -``` * run ```cargo run -- --source examples/web --watch``` +* see ```main.tp``` for the database queries * and head to http://localhost:3000/api/customer * optionally, adjust the database ddl, update the query in db.tp, and recheck the outpout \ No newline at end of file diff --git a/examples/web/main.tp b/examples/web/main.tp new file mode 100644 index 0000000..9e55304 --- /dev/null +++ b/examples/web/main.tp @@ -0,0 +1,3 @@ +println("Creating the customers table") +sql("create table if not exists customers(id serial primary key, first_name varchar(50), last_name varchar(50))") +sql("insert into customers (first_name,last_name) values ('first', 'last')") \ No newline at end of file diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index bbf31f7..56f4a43 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::fs; +use log::info; use walkdir::WalkDir; use crate::{symbol_builder, AsmRegistry, TIPI_EXT}; use crate::compiler::assembly_pass::AsmChunk; @@ -20,7 +21,7 @@ pub fn compile_sourcedir(source_dir: &str) -> Result, for entry in WalkDir::new(source_dir).into_iter().filter_map(|e| e.ok()) { let path = entry.path().to_str().unwrap(); if path.ends_with(TIPI_EXT) { - println!("-- Compiling {} -- ", path); + info!("-- Compiling {} -- ", path); let source = fs::read_to_string(path).map_err(map_underlying())?; let tokens = scan_pass::scan(&source)?; match ast_pass::compile(Some(path), tokens, &mut symbol_table) { @@ -64,5 +65,5 @@ pub fn run(src: &str) -> Result { let mut asm_registry = HashMap::new(); assembly_pass::compile(None, &ast, &symbol_table, &mut asm_registry)?; let registry = arc_swap::ArcSwap::from(std::sync::Arc::new(asm_registry)); - crate::vm::interpret(registry.load(), "main").map_err(TipiLangError::from) + crate::vm::run(registry.load(), "main").map_err(TipiLangError::from) } diff --git a/src/file_watch.rs b/src/file_watch.rs index a788a38..8f1c84e 100644 --- a/src/file_watch.rs +++ b/src/file_watch.rs @@ -17,12 +17,13 @@ pub fn start_watch_daemon(source: &str, registry: Arc Result<(), TipiLangError> { //TODO make configurable - let manager = - PostgresConnectionManager::new_from_stringlike("host=localhost user=postgres password=boompje", NoTls) - .unwrap(); + let manager = PostgresConnectionManager::new_from_stringlike( + "host=localhost user=postgres password=boompje", + NoTls, + ) + .unwrap(); let pool = bb8::Pool::builder() .build(manager) .await @@ -51,17 +53,22 @@ async fn main() -> Result<(), TipiLangError> { let source = fs::read_to_string(file).expect("Unable to read file"); run(&source)?; } else { - println!("-- Tipilang --"); + info!("-- Tipilang --"); let source = args.source.unwrap_or("./source".to_string()); let registry = compile_sourcedir(&source)?; let empty = registry.is_empty(); let swap = Arc::new(ArcSwap::from(Arc::new(registry))); if !empty { + let registry = swap.clone().load(); + if let Some(main) = registry.get("/main") { + vm::run(registry, "/main")?; + } + if args.watch { tipi_lang::file_watch::start_watch_daemon(&source, swap.clone()); } - println!("-- Compilation successful --"); + info!("-- Compilation successful --"); let state = AppState { registry: swap.clone(), }; @@ -73,7 +80,7 @@ async fn main() -> Result<(), TipiLangError> { .await .map_err(map_underlying())?; - println!( + info!( "-- Listening on {} --\n", listener.local_addr().map_err(map_underlying())? ); @@ -84,7 +91,7 @@ async fn main() -> Result<(), TipiLangError> { axum::serve(listener, app).await.map_err(map_underlying())?; } else { - println!("No source files found or compilation error"); + error!("No source files found or compilation error"); if args.repl { tipi_lang::repl::start(swap.clone())?; } @@ -123,7 +130,7 @@ async fn handle_any( } let path = &req.uri().to_string(); info!("invoked {:?} => {}", req, function_qname); - match interpret_async( + match run_async( state.registry.load(), &function_qname, path, diff --git a/src/vm.rs b/src/vm.rs index 9460bc0..0c2df6b 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -10,7 +10,7 @@ use std::collections::HashMap; use std::rc::Rc; use std::sync::Arc; -pub async fn interpret_async( +pub async fn run_async( registry: Guard>>, function: &str, uri: &str, @@ -38,13 +38,13 @@ pub async fn interpret_async( } } -pub fn interpret(registry: Guard>, function: &str) -> Result { +pub fn run(registry: Guard>, function: &str) -> Result { let chunk = registry.get(function).unwrap().clone(); let mut vm = Vm::new(®istry); vm.run(&get_context(function), &chunk) } -pub fn interpret_function(chunk: &AsmChunk, args: Vec, registry: Arc) -> Result { +pub fn run_function(chunk: &AsmChunk, args: Vec, registry: Arc) -> Result { let mut vm = Vm::new(®istry); vm.run_function(chunk, args) } @@ -241,7 +241,7 @@ impl Vm { return Err(RuntimeError::FunctionNotFound(function_name)); } } else { - let result = interpret_function(function_chunk.unwrap(), args, self.registry.clone())?; + let result = run_function(function_chunk.unwrap(), args, self.registry.clone())?; self.push(Rc::new(RefCell::new(result))); } }