Testing scope importing

This commit is contained in:
2026-04-22 08:56:44 +02:00
parent 664e8aba74
commit 842383ba6e
6 changed files with 40 additions and 11 deletions
Generated
+2
View File
@@ -168,6 +168,8 @@ name = "semantic"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"parser", "parser",
"serde",
"serde_json",
] ]
[[package]] [[package]]
+2
View File
@@ -5,3 +5,5 @@ edition = "2024"
[dependencies] [dependencies]
parser = { path = "../parser" } parser = { path = "../parser" }
serde = { version = "1.0.228", features = ["serde_derive"] }
serde_json = "1.0.149"
+5 -2
View File
@@ -17,8 +17,11 @@ pub enum Scope {
} }
impl Scope { impl Scope {
pub fn from_top(top_level: &Vec<parser::ast::TopLevel>) -> Arc<Self> { pub fn from_top(
let tl = TopLevelSymbolScope::from(top_level); path: &std::path::PathBuf,
top_level: &Vec<parser::ast::TopLevel>,
) -> Arc<Self> {
let tl = TopLevelSymbolScope::from(path, top_level);
Arc::new(Self::TopLevel(TopLevelHirScope::from_tlss(&tl))) Arc::new(Self::TopLevel(TopLevelHirScope::from_tlss(&tl)))
} }
+28 -6
View File
@@ -1,20 +1,23 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use parser::ast::TopLevel; use parser::ast::TopLevel;
use parser::ast::{ParamList, TypeExpr}; use parser::ast::{ParamList, TypeExpr};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TypeSymbol(pub String); pub struct TypeSymbol(pub String);
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VarSymbol { pub struct VarSymbol {
pub export: bool, pub export: bool,
pub var_type: TypeSymbol, pub var_type: TypeSymbol,
pub name: String, pub name: String,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FunctionSymbol { pub struct FunctionSymbol {
pub export: bool, pub export: bool,
pub name: String, pub name: String,
@@ -22,7 +25,7 @@ pub struct FunctionSymbol {
pub return_type: Option<TypeSymbol>, pub return_type: Option<TypeSymbol>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StructSymbol { pub struct StructSymbol {
pub export: bool, pub export: bool,
pub name: String, pub name: String,
@@ -96,22 +99,41 @@ impl StructSymbol {
} }
} }
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JSONScope {
pub structs: HashMap<String, StructSymbol>,
pub functions: HashMap<String, FunctionSymbol>,
pub package_name: String,
}
type JsonScopeMap = HashMap<String, JSONScope>;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct TopLevelSymbolScope { pub struct TopLevelSymbolScope {
pub imports: HashMap<String, JSONScope>,
pub structs: HashMap<String, StructSymbol>, pub structs: HashMap<String, StructSymbol>,
pub functions: HashMap<String, FunctionSymbol>, pub functions: HashMap<String, FunctionSymbol>,
} }
impl TopLevelSymbolScope { impl TopLevelSymbolScope {
pub fn from(top_level: &Vec<TopLevel>) -> Self { pub fn from(path: &PathBuf, top_level: &Vec<TopLevel>) -> Self {
let mut scope = TopLevelSymbolScope { let mut scope = TopLevelSymbolScope {
imports: HashMap::new(),
structs: HashMap::new(), structs: HashMap::new(),
functions: HashMap::new(), functions: HashMap::new(),
}; };
for top in top_level { for top in top_level {
match top { 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::Package(_) => {}
TopLevel::FunctionDecl { TopLevel::FunctionDecl {
+1 -1
View File
@@ -47,7 +47,7 @@ pub fn build() {
println!(" → type checking..."); 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..."); println!(" → generating Go code...");
+2 -2
View File
@@ -56,7 +56,7 @@ fn cmd_check(path: &str) {
Ok(mut ast) => { Ok(mut ast) => {
println!("parse: ok"); 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) println!("{:#?}", ast)
} }
@@ -71,7 +71,7 @@ fn cmd_parse(path: &str) {
let source = read_ms_file(path); let source = read_ms_file(path);
match parser::parse(&source) { match parser::parse(&source) {
Ok(mut ast) => { 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( fs::write("output.json", serde_json::to_string_pretty(&ast).unwrap()).unwrap_or_else(
|e| { |e| {