1
1
Fork 0
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:
Natapat Samutpong 2022-03-20 05:18:27 +07:00
parent 0288428090
commit f9a88c14c5
3 changed files with 26 additions and 3 deletions

View file

@ -58,9 +58,10 @@ impl Codegen {
IRKind::Intrinsic { name, args } => { IRKind::Intrinsic { name, args } => {
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
} }

View file

@ -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",
]; ];

View file

@ -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;
}