From 5754ecf6419457d349611293b56dd448579377fa Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sun, 13 Mar 2022 07:01:39 +0700 Subject: [PATCH] @time() --- crates/codegen/src/cpp.rs | 12 +++++++----- crates/hir/src/lib.rs | 3 ++- example/time.hz | 4 ++++ lib/time.hpp | 9 +++++++++ 4 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 example/time.hz create mode 100644 lib/time.hpp diff --git a/crates/codegen/src/cpp.rs b/crates/codegen/src/cpp.rs index cf57030..f3fe831 100644 --- a/crates/codegen/src/cpp.rs +++ b/crates/codegen/src/cpp.rs @@ -2,10 +2,11 @@ use std::fmt::Display; use hir::{IR, IRKind, Value}; -const MODULE_INCLUDES: [&str; 3] = [ - "\"hazure/io.hpp\"", // `read()` and `write()` - "", // bool type - "", // string type +const MODULE_INCLUDES: [&str; 4] = [ + "\"hazure/io.hpp\"", + "\"hazure/time.hpp\"", + "", + "", ]; pub struct Codegen { @@ -63,7 +64,8 @@ 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", self.gen_ir(&args[0], false), semicolon!()) }, + "read" => { format!("hazure_read(){}\n", 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 8c2a6b6..cc57b41 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1,9 +1,10 @@ use std::ops::Range; use parser::Expr; -const INTRINSICS: [&str; 2] = [ +const INTRINSICS: [&str; 3] = [ "write", "read", + "time", ]; #[derive(Debug)] diff --git a/example/time.hz b/example/time.hz new file mode 100644 index 0000000..e1a8ed9 --- /dev/null +++ b/example/time.hz @@ -0,0 +1,4 @@ +fun main: int = do + @time() + |> @write(); +end; \ No newline at end of file diff --git a/lib/time.hpp b/lib/time.hpp new file mode 100644 index 0000000..3533196 --- /dev/null +++ b/lib/time.hpp @@ -0,0 +1,9 @@ +#pragma once +#include + +/* + * @brief Get time in seconds since the Epoch. + */ +int hazure_get_time() { + return std::time(0); +} \ No newline at end of file