From 842383ba6e4bb6bbbc353af0f01e08ac6e1e36cb Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 22 Apr 2026 08:56:44 +0200 Subject: [PATCH] Testing scope importing --- Cargo.lock | 2 ++ semantic/Cargo.toml | 2 ++ semantic/src/scope.rs | 7 +++++-- semantic/src/top_level.rs | 34 ++++++++++++++++++++++++++++------ src/compiler.rs | 2 +- src/main.rs | 4 ++-- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e65460..57bae6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -168,6 +168,8 @@ name = "semantic" version = "0.1.0" dependencies = [ "parser", + "serde", + "serde_json", ] [[package]] diff --git a/semantic/Cargo.toml b/semantic/Cargo.toml index 69caf32..cbebdd1 100644 --- a/semantic/Cargo.toml +++ b/semantic/Cargo.toml @@ -5,3 +5,5 @@ edition = "2024" [dependencies] parser = { path = "../parser" } +serde = { version = "1.0.228", features = ["serde_derive"] } +serde_json = "1.0.149" diff --git a/semantic/src/scope.rs b/semantic/src/scope.rs index 9c1fd22..64a727f 100644 --- a/semantic/src/scope.rs +++ b/semantic/src/scope.rs @@ -17,8 +17,11 @@ pub enum Scope { } impl Scope { - pub fn from_top(top_level: &Vec) -> Arc { - let tl = TopLevelSymbolScope::from(top_level); + pub fn from_top( + path: &std::path::PathBuf, + top_level: &Vec, + ) -> Arc { + let tl = TopLevelSymbolScope::from(path, top_level); Arc::new(Self::TopLevel(TopLevelHirScope::from_tlss(&tl))) } diff --git a/semantic/src/top_level.rs b/semantic/src/top_level.rs index b1e85cb..c467fe3 100644 --- a/semantic/src/top_level.rs +++ b/semantic/src/top_level.rs @@ -1,20 +1,23 @@ use std::collections::HashMap; +use std::fs; +use std::path::PathBuf; use parser::ast::TopLevel; use parser::ast::{ParamList, TypeExpr}; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TypeSymbol(pub String); -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct VarSymbol { pub export: bool, pub var_type: TypeSymbol, pub name: String, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct FunctionSymbol { pub export: bool, pub name: String, @@ -22,7 +25,7 @@ pub struct FunctionSymbol { pub return_type: Option, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct StructSymbol { pub export: bool, pub name: String, @@ -96,22 +99,41 @@ impl StructSymbol { } } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct JSONScope { + pub structs: HashMap, + pub functions: HashMap, + pub package_name: String, +} + +type JsonScopeMap = HashMap; + #[derive(Clone, Debug)] pub struct TopLevelSymbolScope { + pub imports: HashMap, pub structs: HashMap, pub functions: HashMap, } impl TopLevelSymbolScope { - pub fn from(top_level: &Vec) -> Self { + pub fn from(path: &PathBuf, top_level: &Vec) -> Self { let mut scope = TopLevelSymbolScope { + imports: HashMap::new(), structs: HashMap::new(), functions: HashMap::new(), }; for top in top_level { match top { - TopLevel::Import(_) => unimplemented!(), + TopLevel::Import(package) => { + let json = fs::read(path.join("mist.map.json")).unwrap(); + let json_scope_map: JsonScopeMap = serde_json::from_slice(&json).unwrap(); + let json_scope = json_scope_map.get(package).unwrap().clone(); + + scope + .imports + .insert(json_scope.package_name.clone(), json_scope); + } TopLevel::Package(_) => {} TopLevel::FunctionDecl { diff --git a/src/compiler.rs b/src/compiler.rs index 385b950..c7cbbe9 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -47,7 +47,7 @@ pub fn build() { println!(" → type checking..."); - semantic::walk_ast(semantic::scope::Scope::from_top(&ast), &mut ast); + semantic::walk_ast(semantic::scope::Scope::from_top(&root, &ast), &mut ast); println!(" → generating Go code..."); diff --git a/src/main.rs b/src/main.rs index e2db46a..6331f9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,7 +56,7 @@ fn cmd_check(path: &str) { Ok(mut ast) => { println!("parse: ok"); - walk_ast(semantic::scope::Scope::from_top(&ast), &mut ast); + // walk_ast(semantic::scope::Scope::from_top(&ast), &mut ast); println!("{:#?}", ast) } @@ -71,7 +71,7 @@ fn cmd_parse(path: &str) { let source = read_ms_file(path); match parser::parse(&source) { Ok(mut ast) => { - walk_ast(semantic::scope::Scope::from_top(&ast), &mut ast); + // walk_ast(semantic::scope::Scope::from_top(&ast), &mut ast); fs::write("output.json", serde_json::to_string_pretty(&ast).unwrap()).unwrap_or_else( |e| {