mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
Prepare optimizer
This commit is contained in:
parent
6ebe1bc7ae
commit
cfca9828f6
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -76,6 +76,7 @@ name = "entry"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"compiler",
|
"compiler",
|
||||||
|
"lower",
|
||||||
"parser",
|
"parser",
|
||||||
"vm",
|
"vm",
|
||||||
]
|
]
|
||||||
|
|
|
@ -157,9 +157,9 @@ impl Compiler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile_program(&mut self, stmts: Vec<(Stmt, Span)>) -> Vec<Instr> {
|
pub fn compile_program(&mut self, stmts: Vec<Stmt>) -> Vec<Instr> {
|
||||||
let mut instrs = vec![];
|
let mut instrs = vec![];
|
||||||
for (stmt, _) in stmts {
|
for stmt in stmts {
|
||||||
instrs.extend(self.compile_stmt(stmt));
|
instrs.extend(self.compile_stmt(stmt));
|
||||||
}
|
}
|
||||||
instrs
|
instrs
|
||||||
|
|
|
@ -5,6 +5,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
parser = { path = "../parser" }
|
parser = { path = "../parser" }
|
||||||
|
lower = { path = "../lower" }
|
||||||
compiler = { path = "../compiler" }
|
compiler = { path = "../compiler" }
|
||||||
vm = { path = "../vm" }
|
vm = { path = "../vm" }
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use compiler::Compiler;
|
use compiler::Compiler;
|
||||||
|
use lower::Lower;
|
||||||
use parser::{lex, parse, report};
|
use parser::{lex, parse, report};
|
||||||
use vm::exec::Executor;
|
use vm::exec::Executor;
|
||||||
|
|
||||||
|
@ -11,8 +12,10 @@ fn main() {
|
||||||
let (ast, parse_errors) = parse(tokens, src.len());
|
let (ast, parse_errors) = parse(tokens, src.len());
|
||||||
|
|
||||||
if let Some(ast) = ast {
|
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 mut compiler = Compiler::new();
|
||||||
let instrs = compiler.compile_program(ast);
|
let instrs = compiler.compile_program(last);
|
||||||
// instrs.iter().for_each(|i| println!("{:?}", i));
|
// instrs.iter().for_each(|i| println!("{:?}", i));
|
||||||
let mut executor = Executor::new(instrs);
|
let mut executor = Executor::new(instrs);
|
||||||
match executor.run() {
|
match executor.run() {
|
||||||
|
|
|
@ -8,6 +8,5 @@ end
|
||||||
fun double x = x * 2
|
fun double x = x * 2
|
||||||
|
|
||||||
fun main = do
|
fun main = do
|
||||||
let add = \x y -> x + y in
|
x |> \a -> println(a)
|
||||||
print(add(double(17), succ(34)))
|
|
||||||
end
|
end
|
|
@ -1,12 +1,15 @@
|
||||||
|
// This is just proof of concept on what the language
|
||||||
|
// might look like in the future
|
||||||
|
|
||||||
import http from "http"
|
import http from "http"
|
||||||
|
|
||||||
-- Define a custom type to represent a user
|
// Define a custom type to represent a user
|
||||||
type User =
|
type User =
|
||||||
id: number,
|
id: number,
|
||||||
name: string,
|
name: string,
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Define a function to handle incoming HTTP requests
|
// Define a function to handle incoming HTTP requests
|
||||||
fun handle_request
|
fun handle_request
|
||||||
req: http.IncomingMessage,
|
req: http.IncomingMessage,
|
||||||
res: http.ServerResponse,
|
res: http.ServerResponse,
|
|
@ -1,6 +1,8 @@
|
||||||
#![allow(clippy::new_without_default)]
|
#![allow(clippy::new_without_default)]
|
||||||
use parser::*;
|
use parser::*;
|
||||||
|
|
||||||
|
type SExpr = (Expr, std::ops::Range<usize>);
|
||||||
|
|
||||||
pub struct Lower {}
|
pub struct Lower {}
|
||||||
|
|
||||||
impl Lower {
|
impl Lower {
|
||||||
|
@ -8,13 +10,39 @@ impl Lower {
|
||||||
Self {}
|
Self {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fold_pipe(&self, e: Expr) -> Vec<Expr> {
|
pub fn opt_stmts(&self, ss: Vec<Stmt>) -> Vec<Stmt> {
|
||||||
if let Expr::Binary((BinaryOp::Pipe, _), left, right) = e {
|
ss.into_iter()
|
||||||
vec![Expr::Call(right, vec![*left])]
|
.flat_map(|s| self.opt_stmt(s.clone()).unwrap_or_else(|| vec![s]))
|
||||||
} else {
|
.collect()
|
||||||
unreachable!()
|
}
|
||||||
|
|
||||||
|
pub fn opt_stmt(&self, s: Stmt) -> Option<Vec<Stmt>> {
|
||||||
|
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<Expr>) -> Vec<Expr> {
|
||||||
|
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<Vec<Expr>> {
|
||||||
|
match e {
|
||||||
|
Expr::Binary((BinaryOp::Pipe, _), left, right) => Some(self.fold_pipe(*left, *right)),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fold_pipe(&self, left: SExpr, right: SExpr) -> Vec<Expr> {
|
||||||
|
vec![Expr::Call(Box::new(right), vec![left])]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -35,7 +63,7 @@ mod test {
|
||||||
|
|
||||||
let ex = ex.unwrap();
|
let ex = ex.unwrap();
|
||||||
let l = Lower::new();
|
let l = Lower::new();
|
||||||
let ex = l.fold_pipe(ex.0);
|
let ex = l.opt_expr(ex.0).unwrap();
|
||||||
println!("{:?}", ex);
|
println!("{:?}", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue