1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

c++ library, intrinsic pipable

This commit is contained in:
Natapat Samutpong 2022-03-13 06:26:59 +07:00
parent 1f5eaab23a
commit 2259141647
6 changed files with 46 additions and 20 deletions

4
Makefile Normal file
View file

@ -0,0 +1,4 @@
build-lib:
@echo "Building lib..."
cp ./lib/. /usr/include/hazure/ -r
@echo "Building lib... done"

View file

@ -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)

View file

@ -3,7 +3,7 @@ use std::fmt::Display;
use hir::{IR, IRKind, Value};
const MODULE_INCLUDES: [&str; 3] = [
"<iostream>", // stdin `@read()` and stdout `@write()`
"\"hazure/io.hpp\"", // `read()` and `write()`
"<stdbool.h>", // bool type
"<string>", // 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
}
},

View file

@ -105,7 +105,8 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
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<IRKind>, Option<LoweringError>) {
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(),

View file

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

22
lib/io.hpp Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <iostream>
template<typename T>
/**
* @brief Read the value from stdin and return it.
*/
T hazure_read() {
T x;
std::cin >> x;
return x;
}
template<typename T>
/**
* @brief Prints the value of the variable to the stdout.
*
* @param value The value to print.
*/
void hazure_write(T x) {
std::cout << x;
}