mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
implements read_fle
, std -> runtime
This commit is contained in:
parent
0288428090
commit
f9a88c14c5
|
@ -60,7 +60,8 @@ impl Codegen {
|
||||||
match name.as_str() {
|
match name.as_str() {
|
||||||
"write" => { format!("write({}){}\n" , self.gen_ir(&args[0], false), semicolon!()) },
|
"write" => { format!("write({}){}\n" , self.gen_ir(&args[0], false), semicolon!()) },
|
||||||
"write_file" => { format!("writeFile({}, {}){}\n", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false), semicolon!()) },
|
"write_file" => { format!("writeFile({}, {}){}\n", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false), semicolon!()) },
|
||||||
"read" => { todo!() },
|
"read" => { format!("read({}){}\n" , self.gen_ir(&args[0], false), semicolon!()) },
|
||||||
|
"read_file" => { format!("readFile({}){}\n" , self.gen_ir(&args[0], false), semicolon!()) }
|
||||||
"emit" => { format!("{}", self.gen_ir(&args[0], false).trim_start_matches('"').trim_end_matches('"')) },
|
"emit" => { format!("{}", self.gen_ir(&args[0], false).trim_start_matches('"').trim_end_matches('"')) },
|
||||||
_ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering
|
_ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use parser::Expr;
|
use parser::Expr;
|
||||||
|
|
||||||
const INTRINSICS: [&str; 4] = [
|
const INTRINSICS: [&str; 5] = [
|
||||||
"write",
|
"write",
|
||||||
"read",
|
"read",
|
||||||
"write_file",
|
"write_file",
|
||||||
|
"read_file",
|
||||||
"emit",
|
"emit",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,16 @@ export function write(text: any): void {
|
||||||
writeAllSync(Deno.stdout, bytes);
|
writeAllSync(Deno.stdout, bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read text from the stdin stream.
|
||||||
|
* @param prompt_str The prompt string to display.
|
||||||
|
* @returns The text read from the stdin stream.
|
||||||
|
*/
|
||||||
|
export function read(prompt_str: string): string {
|
||||||
|
const input = prompt(prompt_str, "");
|
||||||
|
return input ? input : "";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes text to the file.
|
* Writes text to the file.
|
||||||
* @param text The text to write. Can be any type.
|
* @param text The text to write. Can be any type.
|
||||||
|
@ -18,3 +28,14 @@ export function writeFile(text: any, path: string): void {
|
||||||
const bytes: Uint8Array = new TextEncoder().encode(text);
|
const bytes: Uint8Array = new TextEncoder().encode(text);
|
||||||
Deno.writeFileSync(path, bytes);
|
Deno.writeFileSync(path, bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read text from the file.
|
||||||
|
* @param path The path to the file.
|
||||||
|
* @returns The text read from the file.
|
||||||
|
*/
|
||||||
|
export function readFile(path: string): string {
|
||||||
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
const text = decoder.decode(Deno.readFileSync(path));
|
||||||
|
return text;
|
||||||
|
}
|
Loading…
Reference in a new issue