diff --git a/Cargo.lock b/Cargo.lock index 847e7b6..e0fc3d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,7 @@ name = "entry" version = "0.1.0" dependencies = [ "compiler", + "lower", "parser", "vm", ] diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 8af7aa4..7ed71db 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -157,9 +157,9 @@ impl Compiler { } } - pub fn compile_program(&mut self, stmts: Vec<(Stmt, Span)>) -> Vec { + pub fn compile_program(&mut self, stmts: Vec) -> Vec { let mut instrs = vec![]; - for (stmt, _) in stmts { + for stmt in stmts { instrs.extend(self.compile_stmt(stmt)); } instrs diff --git a/entry/Cargo.toml b/entry/Cargo.toml index 1aeac1e..804e31e 100644 --- a/entry/Cargo.toml +++ b/entry/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] parser = { path = "../parser" } +lower = { path = "../lower" } compiler = { path = "../compiler" } vm = { path = "../vm" } diff --git a/entry/src/main.rs b/entry/src/main.rs index 147db60..0c8e96f 100644 --- a/entry/src/main.rs +++ b/entry/src/main.rs @@ -1,4 +1,5 @@ use compiler::Compiler; +use lower::Lower; use parser::{lex, parse, report}; use vm::exec::Executor; @@ -11,8 +12,10 @@ fn main() { let (ast, parse_errors) = parse(tokens, src.len()); if let Some(ast) = ast { + let mut lower = Lower::new(); + let last = lower.opt_stmts(ast.iter().map(|(s, _)| s.clone()).collect()); let mut compiler = Compiler::new(); - let instrs = compiler.compile_program(ast); + let instrs = compiler.compile_program(last); // instrs.iter().for_each(|i| println!("{:?}", i)); let mut executor = Executor::new(instrs); match executor.run() { diff --git a/examples/a.sial b/examples/a.hlm similarity index 59% rename from examples/a.sial rename to examples/a.hlm index 802db1d..ce3e42a 100644 --- a/examples/a.sial +++ b/examples/a.hlm @@ -8,6 +8,5 @@ end fun double x = x * 2 fun main = do - let add = \x y -> x + y in - print(add(double(17), succ(34))) + x |> \a -> println(a) end \ No newline at end of file diff --git a/examples/factorial.sial b/examples/factorial.hlm similarity index 100% rename from examples/factorial.sial rename to examples/factorial.hlm diff --git a/examples/ht.sial b/examples/ht.hlm similarity index 83% rename from examples/ht.sial rename to examples/ht.hlm index b582a2c..0ceedfd 100644 --- a/examples/ht.sial +++ b/examples/ht.hlm @@ -1,12 +1,15 @@ +// This is just proof of concept on what the language +// might look like in the future + import http from "http" --- Define a custom type to represent a user +// Define a custom type to represent a user type User = id: number, name: string, end --- Define a function to handle incoming HTTP requests +// Define a function to handle incoming HTTP requests fun handle_request req: http.IncomingMessage, res: http.ServerResponse, diff --git a/examples/sim.sial b/examples/sim.hlm similarity index 100% rename from examples/sim.sial rename to examples/sim.hlm diff --git a/lower/src/lib.rs b/lower/src/lib.rs index 75510ed..df53f8c 100644 --- a/lower/src/lib.rs +++ b/lower/src/lib.rs @@ -1,6 +1,8 @@ #![allow(clippy::new_without_default)] use parser::*; +type SExpr = (Expr, std::ops::Range); + pub struct Lower {} impl Lower { @@ -8,13 +10,39 @@ impl Lower { Self {} } - fn fold_pipe(&self, e: Expr) -> Vec { - if let Expr::Binary((BinaryOp::Pipe, _), left, right) = e { - vec![Expr::Call(right, vec![*left])] - } else { - unreachable!() + pub fn opt_stmts(&self, ss: Vec) -> Vec { + ss.into_iter() + .flat_map(|s| self.opt_stmt(s.clone()).unwrap_or_else(|| vec![s])) + .collect() + } + + pub fn opt_stmt(&self, s: Stmt) -> Option> { + match s { + // Stmt::Fun(name, args, body) => Some(vec![Stmt::Fun( + // name, + // args, + // self.opt_expr(body.0).unwrap_or_else(|| vec![body.0]), + // )]), + _ => None, } } + + pub fn opt_exprs(&self, es: Vec) -> Vec { + es.into_iter() + .flat_map(|e| self.opt_expr(e.clone()).unwrap_or_else(|| vec![e])) + .collect() + } + + pub fn opt_expr(&self, e: Expr) -> Option> { + match e { + Expr::Binary((BinaryOp::Pipe, _), left, right) => Some(self.fold_pipe(*left, *right)), + _ => None, + } + } + + fn fold_pipe(&self, left: SExpr, right: SExpr) -> Vec { + vec![Expr::Call(Box::new(right), vec![left])] + } } #[cfg(test)] @@ -35,7 +63,7 @@ mod test { let ex = ex.unwrap(); let l = Lower::new(); - let ex = l.fold_pipe(ex.0); + let ex = l.opt_expr(ex.0).unwrap(); println!("{:?}", ex); } }