This commit is contained in:
janis 2025-11-08 15:43:22 +01:00
parent 32b8e84891
commit ae2cf5f9d3
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8
5 changed files with 59 additions and 6 deletions

3
Cargo.lock generated
View file

@ -34,3 +34,6 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]] [[package]]
name = "typeck" name = "typeck"
version = "0.1.0" version = "0.1.0"
dependencies = [
"libcompiler",
]

View file

@ -587,4 +587,26 @@ mod display {
} }
} }
} }
#[repr(transparent)]
pub struct Displayed<T: core::fmt::Display>(pub T);
impl<T: core::fmt::Display> core::fmt::Debug for Displayed<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
pub trait DisplayedSliceExt {
type Displayed: core::fmt::Debug;
fn displayed(self) -> Self::Displayed;
}
impl<'a, T: core::fmt::Display> DisplayedSliceExt for &'a [T] {
type Displayed = &'a [Displayed<T>];
fn displayed(self) -> Self::Displayed {
unsafe { core::mem::transmute(self) }
}
}
} }
pub use display::{Displayed, DisplayedSliceExt};

View file

@ -62,6 +62,7 @@ section .text
;; rdi: pointer to buffer ;; rdi: pointer to buffer
;; rsi: length of buffer ;; rsi: length of buffer
;; define-fn: fn tokeniser_init_buf(buf: *const u8, buf_len: usize)
tokeniser_init_buf: tokeniser_init_buf:
push rbp push rbp
mov rbp, rsp mov rbp, rsp

View file

@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
libcompiler = { path = "../libcompiler" }

View file

@ -1,14 +1,40 @@
pub fn add(left: u64, right: u64) -> u64 { fn init() {
left + right unsafe {
libcompiler::ffi::bump_init();
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use libcompiler::ffi::Ast;
use crate::init;
#[test] #[test]
fn it_works() { fn type_graph() {
let result = add(2, 2); init();
assert_eq!(result, 4);
let src = b"fn my-fun(a: u32, b: u32) -> void {
let x: i64 = a as i64 + b as i64;
}";
unsafe {
libcompiler::ffi::tokeniser_init_buf(src.as_ptr(), src.len());
let mut ast = Ast {
nodes: libcompiler::vec::Vec::default(),
};
let func_id = libcompiler::ffi::parse_func(&mut ast);
let mut symtable = core::mem::MaybeUninit::<libcompiler::ffi::SymbolTable>::uninit();
libcompiler::ffi::ast_build_symtable(&mut ast, func_id, &mut symtable);
let mut symtable = symtable.assume_init();
use libcompiler::DisplayedSliceExt;
println!(
"Symbol Table: {:#?}",
symtable.symtable.as_slice().displayed()
);
libcompiler::ffi::ast_resolve_var_refs(&mut ast, &mut symtable, func_id);
}
} }
} }