From 225914164709a943faf67216b2dd2c7d9664b25c Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sun, 13 Mar 2022 06:26:59 +0700 Subject: [PATCH] c++ library, intrinsic pipable --- Makefile | 4 ++++ README.md | 11 ++--------- crates/codegen/src/cpp.rs | 6 +++--- crates/hir/src/lib.rs | 11 +++++++++-- example/pipe.hz | 12 ++++++------ lib/io.hpp | 22 ++++++++++++++++++++++ 6 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 Makefile create mode 100644 lib/io.hpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7687d67 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +build-lib: + @echo "Building lib..." + cp ./lib/. /usr/include/hazure/ -r + @echo "Building lib... done" \ No newline at end of file diff --git a/README.md b/README.md index 43296e4..2c9b5e2 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Note: Everything in this project can be changed at anytime! (I'm still finding o # Prerequistie - `clang++`(preferred, default) or any C++ compiler +- `make` for Makefile - Rust (if you're going to build from source) # Configuration @@ -21,13 +22,5 @@ You can also configurate Hades compiler (currently you can only change the C++ c compiler = "clang++" ``` -# TODO -> This is only contains important TODOs, smaller TODOs isn't listed here and instead scattered around among sources code. -- More compiler configuration (e.x. complier options) -- Optimization (IR) -- Standard library & Runtime stuff - # License -Hades is licensed under both [MIT license](https://github.com/azur1s/hades/blob/master/LICENSE-MIT) and [Apache License](https://github.com/azur1s/hades/blob/master/LICENSE-APACHE) - -Anything helps! :D +Hades is licensed under both [MIT license](https://github.com/azur1s/hades/blob/master/LICENSE-MIT) and [Apache License](https://github.com/azur1s/hades/blob/master/LICENSE-APACHE) \ No newline at end of file diff --git a/crates/codegen/src/cpp.rs b/crates/codegen/src/cpp.rs index e42795e..cf57030 100644 --- a/crates/codegen/src/cpp.rs +++ b/crates/codegen/src/cpp.rs @@ -3,7 +3,7 @@ use std::fmt::Display; use hir::{IR, IRKind, Value}; const MODULE_INCLUDES: [&str; 3] = [ - "", // stdin `@read()` and stdout `@write()` + "\"hazure/io.hpp\"", // `read()` and `write()` "", // bool type "", // string type ]; @@ -62,8 +62,8 @@ impl Codegen { IRKind::Intrinsic { name, args } => { match name.as_str() { - "write" => { format!("std::cout << {};\n", self.gen_ir(&args[0], false)) }, - "read" => { format!("std::cin >> {};\n", self.gen_ir(&args[0], false)) }, + "write" => { format!("hazure_write({}){}\n", self.gen_ir(&args[0], false), semicolon!()) }, + "read" => { format!("hazure_read({}){}\n", self.gen_ir(&args[0], false), 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 4993bf9..8c2a6b6 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -105,7 +105,8 @@ pub fn expr_to_ir(expr: &Expr) -> (Option, Option) { if_err_return!(lhs_ir.1); match &rhs.0 { - call @ Expr::Call { name, args } => { + call @ Expr::Call { name, args } + | call @ Expr::Intrinsic { name, args } => { let name = match &name.0 { Expr::Identifier(s) => s.clone(), // Should never happen because the parser should have caught this @@ -125,7 +126,13 @@ pub fn expr_to_ir(expr: &Expr) -> (Option, Option) { let mut args = vec![lhs_ir.0.unwrap()]; args.append(&mut largs); - return (Some(IRKind::Call { name, args }), None); + let ir_kind = match call.0.unwrap() { + IRKind::Call { .. } => IRKind::Call { name, args }, + IRKind::Intrinsic { .. } => IRKind::Intrinsic { name, args }, + _ => unreachable!() + }; + + return (Some(ir_kind), None); }, _ => return (None, Some(LoweringError { span: rhs.1.clone(), diff --git a/example/pipe.hz b/example/pipe.hz index 371d42c..5a63b35 100644 --- a/example/pipe.hz +++ b/example/pipe.hz @@ -2,13 +2,13 @@ fun foo (xs: int): int = return xs + 1; fun bar (xs: int) (x: int): int = return xs - x; fun main: int = do - let res: int = foo(69) - |> bar(1); - @write(res); + foo(69) + |> bar(1) + |> @write(); @write("\n"); - let y: int = 68 - |> foo(); - @write(y); + 68 + |> foo() + |> @write(); end; \ No newline at end of file diff --git a/lib/io.hpp b/lib/io.hpp new file mode 100644 index 0000000..592c926 --- /dev/null +++ b/lib/io.hpp @@ -0,0 +1,22 @@ +#pragma once +#include + +template +/** + * @brief Read the value from stdin and return it. + */ +T hazure_read() { + T x; + std::cin >> x; + return x; +} + +template +/** + * @brief Prints the value of the variable to the stdout. + * + * @param value The value to print. + */ +void hazure_write(T x) { + std::cout << x; +} \ No newline at end of file