mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
some more intrinsic, refactor map example
This commit is contained in:
parent
1f9a9cdf71
commit
1a517da5a1
|
@ -73,6 +73,8 @@ impl Codegen {
|
||||||
|
|
||||||
"get" => { format!("{}[{}]", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false)) },
|
"get" => { format!("{}[{}]", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false)) },
|
||||||
"len" => { format!("{}.length", self.gen_ir(&args[0], false)) },
|
"len" => { format!("{}.length", self.gen_ir(&args[0], false)) },
|
||||||
|
"insert" => { format!("{}.push({})", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false)) },
|
||||||
|
"concat" => { format!("{}.concat({})", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false)) },
|
||||||
|
|
||||||
"throw" => { format!("throw new Error({}){}", self.gen_ir(&args[0], false), semicolon!()) },
|
"throw" => { format!("throw new Error({}){}", self.gen_ir(&args[0], false), semicolon!()) },
|
||||||
_ => unreachable!("{}", format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering
|
_ => unreachable!("{}", format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use parser::types::{Expr, Typehint};
|
use parser::types::{Expr, Typehint};
|
||||||
|
|
||||||
const INTRINSICS: [&str; 8] = [
|
const INTRINSICS: [&str; 10] = [
|
||||||
"write",
|
"write",
|
||||||
"read",
|
"read",
|
||||||
"write_file",
|
"write_file",
|
||||||
|
@ -9,6 +9,8 @@ const INTRINSICS: [&str; 8] = [
|
||||||
"emit",
|
"emit",
|
||||||
"get",
|
"get",
|
||||||
"len",
|
"len",
|
||||||
|
"insert",
|
||||||
|
"concat",
|
||||||
"throw",
|
"throw",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
fun map_ (vec: [int]) (fn: |int| -> int) (current: int): void = do
|
fun map_ (vec: [int]) (fn: |int| -> int) (current: int): [int] = do
|
||||||
if current == @len(vec) then
|
if current == @len(vec) then
|
||||||
do end
|
return []
|
||||||
else
|
else
|
||||||
do
|
do
|
||||||
@get(vec, current)
|
let new : [int] = []
|
||||||
|> fn(_)
|
let x : int = @get(vec, current) |> fn(_)
|
||||||
|> @write(_)
|
@insert(new, x)
|
||||||
@write("\n")
|
|
||||||
|
|
||||||
map_(vec, fn, current + 1)
|
let y : [int] = map_(vec, fn, current + 1)
|
||||||
|
return @concat(new, y)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
fun map (vec: [int]) (fn: |int| -> int): void = map_(vec, fn, 0)
|
fun map (vec: [int]) (fn: |int| -> int): [int] = return map_(vec, fn, 0)
|
||||||
|
|
||||||
fun mul10 (x: int): int = x * 10
|
fun mul10 (x: int): int = return x * 10
|
||||||
|
|
||||||
fun main: void = do
|
fun main: void = do
|
||||||
let foo: [int] = [69, 420, 727, 1337, 42069, 69420]
|
let foo: [int] = [69, 420, 727, 1337, 42069, 69420]
|
||||||
map(foo, mul10)
|
map(foo, mul10) |> @write(_)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue