diff --git a/crates/codegen/src/cpp.rs b/crates/codegen/src/cpp.rs index f3fe831..ec8529f 100644 --- a/crates/codegen/src/cpp.rs +++ b/crates/codegen/src/cpp.rs @@ -64,7 +64,9 @@ impl Codegen { IRKind::Intrinsic { name, args } => { match name.as_str() { "write" => { format!("hazure_write({}){}\n", self.gen_ir(&args[0], false), semicolon!()) }, - "read" => { format!("hazure_read(){}\n", semicolon!()) }, + "read" => { format!("hazure_read(){}\n", semicolon!()) }, + "write_file" => { format!("hazure_write({}){}\n", self.gen_ir(&args[0], false), semicolon!()) }, + "read_file" => { format!("hazure_read_file({}){}\n", self.gen_ir(&args[0], false), semicolon!()) }, "time" => { format!("hazure_get_time(){}\n", semicolon!()) }, _ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering } diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index cc57b41..1f35542 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1,9 +1,11 @@ use std::ops::Range; use parser::Expr; -const INTRINSICS: [&str; 3] = [ +const INTRINSICS: [&str; 5] = [ "write", "read", + "write_file", + "read_file", "time", ]; diff --git a/example/quine.hz b/example/quine.hz new file mode 100644 index 0000000..62451d2 --- /dev/null +++ b/example/quine.hz @@ -0,0 +1,4 @@ +fun main: int = do + @read_file("./example/quine.hz") -- Run this from root folder + |> @write(); +end; \ No newline at end of file diff --git a/lib/io.hpp b/lib/io.hpp index 592c926..d6b9b85 100644 --- a/lib/io.hpp +++ b/lib/io.hpp @@ -1,5 +1,7 @@ #pragma once #include +#include +#include template /** @@ -19,4 +21,33 @@ template */ void hazure_write(T x) { std::cout << x; +} + +/* + * @brief Read the value from the file and return it. + * + * @param file_name The name of the file to read from. + * @return std::string The value read from the file. + */ +std::string hazure_read_file(std::string filename) { + std::ifstream file(filename); + std::string content((std::istreambuf_iterator(file)), + (std::istreambuf_iterator())); + return content; +} + +/* + * @brief Write string to file. + * + * @param filename The file name to write to. + * @param content The content to write. + */ +void hazure_write_file(std::string filename, std::string content) { + std::ofstream file(filename); + if (file.is_open()) { + file << content; + file.close(); + } else { + std::cerr << "Unable to open " << filename << std::endl; + } } \ No newline at end of file