From ae2cf5f9d3de3c0c056ba614d74eb039b2a23821 Mon Sep 17 00:00:00 2001 From: janis Date: Sat, 8 Nov 2025 15:43:22 +0100 Subject: [PATCH] idk? --- Cargo.lock | 3 +++ lang/libcompiler/src/lib.rs | 22 +++++++++++++++++++++ lang/src/tokeniser.asm | 1 + lang/typeck/Cargo.toml | 1 + lang/typeck/src/lib.rs | 38 +++++++++++++++++++++++++++++++------ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 03c5ac2..6275808 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,3 +34,6 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "typeck" version = "0.1.0" +dependencies = [ + "libcompiler", +] diff --git a/lang/libcompiler/src/lib.rs b/lang/libcompiler/src/lib.rs index 490dcd9..f8e5568 100644 --- a/lang/libcompiler/src/lib.rs +++ b/lang/libcompiler/src/lib.rs @@ -587,4 +587,26 @@ mod display { } } } + + #[repr(transparent)] + pub struct Displayed(pub T); + impl core::fmt::Debug for Displayed { + 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]; + fn displayed(self) -> Self::Displayed { + unsafe { core::mem::transmute(self) } + } + } } + +pub use display::{Displayed, DisplayedSliceExt}; diff --git a/lang/src/tokeniser.asm b/lang/src/tokeniser.asm index 6202fff..ad6273a 100644 --- a/lang/src/tokeniser.asm +++ b/lang/src/tokeniser.asm @@ -62,6 +62,7 @@ section .text ;; rdi: pointer to buffer ;; rsi: length of buffer +;; define-fn: fn tokeniser_init_buf(buf: *const u8, buf_len: usize) tokeniser_init_buf: push rbp mov rbp, rsp diff --git a/lang/typeck/Cargo.toml b/lang/typeck/Cargo.toml index 16719f0..4523ddf 100644 --- a/lang/typeck/Cargo.toml +++ b/lang/typeck/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +libcompiler = { path = "../libcompiler" } \ No newline at end of file diff --git a/lang/typeck/src/lib.rs b/lang/typeck/src/lib.rs index b93cf3f..4a5f0d5 100644 --- a/lang/typeck/src/lib.rs +++ b/lang/typeck/src/lib.rs @@ -1,14 +1,40 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +fn init() { + unsafe { + libcompiler::ffi::bump_init(); + } } #[cfg(test)] mod tests { - use super::*; + use libcompiler::ffi::Ast; + + use crate::init; #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + fn type_graph() { + init(); + + 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::::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); + } } }