mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
fix codegen formatting
This commit is contained in:
parent
ffd922759e
commit
d8fbc17a33
|
@ -83,7 +83,7 @@ impl Codegen {
|
||||||
|
|
||||||
Expr::Define { name, typehint, value } => {
|
Expr::Define { name, typehint, value } => {
|
||||||
format!(
|
format!(
|
||||||
"let {} : {} = {}{}",
|
"let _{} : {} = {}{}",
|
||||||
name.0,
|
name.0,
|
||||||
self.gen_typehint(&typehint.0),
|
self.gen_typehint(&typehint.0),
|
||||||
self.gen_expr(&value.0, false),
|
self.gen_expr(&value.0, false),
|
||||||
|
@ -91,7 +91,7 @@ impl Codegen {
|
||||||
},
|
},
|
||||||
Expr::Redefine { name, value } => {
|
Expr::Redefine { name, value } => {
|
||||||
format!(
|
format!(
|
||||||
"{} = {}{}",
|
"_{} = {}{}",
|
||||||
name.0,
|
name.0,
|
||||||
self.gen_expr(&value.0, false),
|
self.gen_expr(&value.0, false),
|
||||||
semicolon!())
|
semicolon!())
|
||||||
|
@ -117,7 +117,7 @@ impl Codegen {
|
||||||
|
|
||||||
Expr::If { cond, t, f } => {
|
Expr::If { cond, t, f } => {
|
||||||
format!(
|
format!(
|
||||||
"if {} {{{}}} else {{{}}}",
|
"if ({}) {{{}}} else {{{}}}",
|
||||||
self.gen_expr(&cond.0, false),
|
self.gen_expr(&cond.0, false),
|
||||||
self.gen_expr(&t.0, false),
|
self.gen_expr(&t.0, false),
|
||||||
self.gen_expr(&f.0, false))
|
self.gen_expr(&f.0, false))
|
||||||
|
|
|
@ -4,13 +4,11 @@ use syntax::{lex::lex, parse::parse};
|
||||||
use codegen::Codegen;
|
use codegen::Codegen;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let path = std::env::args().nth(1).expect("No file specified");
|
||||||
|
let input = std::fs::read_to_string(path).expect("Failed to read file");
|
||||||
|
|
||||||
let time = std::time::Instant::now();
|
let time = std::time::Instant::now();
|
||||||
|
|
||||||
let input = "
|
|
||||||
fun len T (vec : [T]) : int = return ;vec.length
|
|
||||||
|
|
||||||
@write(len([1, 2, 3]))
|
|
||||||
";
|
|
||||||
//
|
//
|
||||||
// Lex
|
// Lex
|
||||||
//
|
//
|
||||||
|
@ -37,6 +35,6 @@ fun len T (vec : [T]) : int = return ;vec.length
|
||||||
let mut codegen = Codegen::new();
|
let mut codegen = Codegen::new();
|
||||||
codegen.gen(ast.unwrap());
|
codegen.gen(ast.unwrap());
|
||||||
|
|
||||||
let mut file = File::create("out.rs").unwrap();
|
let mut file = File::create("out.ts").unwrap();
|
||||||
file.write_all(codegen.emitted.join("\n").as_bytes()).unwrap();
|
file.write_all(codegen.emitted.join("\n").as_bytes()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
7
examples/factorial.hz
Normal file
7
examples/factorial.hz
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
fun factorial (n : int) : int =
|
||||||
|
if n == 0
|
||||||
|
| return 1
|
||||||
|
| return n * factorial(n - 1)
|
||||||
|
|
||||||
|
result : int = factorial(5)
|
||||||
|
@write(result)
|
|
@ -1,11 +0,0 @@
|
||||||
fun foo (a : int) (b : int) : int = do
|
|
||||||
c : int = a + b
|
|
||||||
return c
|
|
||||||
end
|
|
||||||
|
|
||||||
bar = foo(34, 35)
|
|
||||||
@println(bar)
|
|
||||||
|
|
||||||
if bar == 69
|
|
||||||
| @println("That's 69")
|
|
||||||
| @println("Ok")
|
|
|
@ -1,15 +0,0 @@
|
||||||
type Left E = { tag: "Left", value: E }
|
|
||||||
type Right T = { tag: "Right", value: T }
|
|
||||||
|
|
||||||
type Either T E = Left E
|
|
||||||
| Right T
|
|
||||||
|
|
||||||
it : Either<Int, String> = { tag: "Left", value: "Uninit" }
|
|
||||||
|
|
||||||
fun rand_it : void = if @random() > 0.5
|
|
||||||
| set it = { tag: "Left", value: "Error" }
|
|
||||||
| set it = { tag: "Right", value: 42 }
|
|
||||||
|
|
||||||
rand()
|
|
||||||
@println(it.tag, it.value)
|
|
||||||
|
|
Loading…
Reference in a new issue