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
|
@ -58,9 +58,10 @@ impl Codegen {
|
|||
|
||||
IRKind::Intrinsic { name, args } => {
|
||||
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!()) },
|
||||
"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('"')) },
|
||||
_ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
use std::ops::Range;
|
||||
use parser::Expr;
|
||||
|
||||
const INTRINSICS: [&str; 4] = [
|
||||
const INTRINSICS: [&str; 5] = [
|
||||
"write",
|
||||
"read",
|
||||
"write_file",
|
||||
"read_file",
|
||||
"emit",
|
||||
];
|
||||
|
||||
|
|
|
@ -9,6 +9,16 @@ export function write(text: any): void {
|
|||
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.
|
||||
* @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);
|
||||
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