Top level scope

This commit is contained in:
2026-04-15 09:16:26 +02:00
parent 19629363b0
commit 6717be038f
5 changed files with 126 additions and 19 deletions
Generated
+3
View File
@@ -158,6 +158,9 @@ dependencies = [
[[package]] [[package]]
name = "semantic" name = "semantic"
version = "0.1.0" version = "0.1.0"
dependencies = [
"parser",
]
[[package]] [[package]]
name = "serde" name = "serde"
+8
View File
@@ -1,3 +1,5 @@
use std::collections::HashMap;
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
@@ -123,3 +125,9 @@ pub enum VarKind {
Const, Const,
Var, Var,
} }
impl ParamList {
pub fn to_hashmap<T>(&self, f: impl Fn(&String, &TypeExpr) -> T) -> HashMap<String, T> {
self.0.iter().map(|(k, v)| (k.clone(), f(k, v))).collect()
}
}
+1
View File
@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
parser = { path = "../parser" }
+7 -15
View File
@@ -1,21 +1,13 @@
use std::{ use std::sync::Arc;
collections::HashMap,
sync::{Arc, Mutex},
};
use crate::top_level; use crate::top_level::TopLevelScope;
pub enum Symbol { pub enum Scope {
Function(top_level::FunctionSymbol), TopLevel(TopLevelScope),
TypeSymbol(top_level::TypeSymbol),
StructSymbol(top_level::StructSymbol),
}
pub struct Scope {
pub parent: Option<Arc<Scope>>,
pub variables: Mutex<HashMap<String, Symbol>>,
} }
impl Scope { impl Scope {
pub fn from_top_level() {} pub fn from_top(top_level: &Vec<parser::ast::TopLevel>) -> Arc<Self> {
Arc::new(Self::TopLevel(TopLevelScope::from(top_level)))
}
} }
+107 -4
View File
@@ -1,20 +1,123 @@
use std::collections::HashMap; use std::collections::HashMap;
pub struct TypeSymbol { use parser::ast::TopLevel;
pub export: bool,
use parser::ast::{ParamList, TypeExpr};
pub struct TypeSymbol(pub String);
pub struct VarSymbol {
pub var_type: TypeSymbol,
pub name: String, pub name: String,
} }
pub struct FunctionSymbol { pub struct FunctionSymbol {
pub export: bool, pub export: bool,
pub name: String, pub name: String,
pub params: HashMap<String, TypeSymbol>, pub params: HashMap<String, VarSymbol>,
pub return_type: Option<TypeSymbol>,
} }
pub struct StructSymbol { pub struct StructSymbol {
pub export: bool, pub export: bool,
pub name: String, pub name: String,
pub fields: HashMap<String, TypeSymbol>, pub fields: HashMap<String, VarSymbol>,
pub methods: HashMap<String, FunctionSymbol>, pub methods: HashMap<String, FunctionSymbol>,
} }
impl TypeSymbol {
pub fn from_ast(expr: TypeExpr) -> Self {
TypeSymbol(match expr {
TypeExpr::Identifier(i) => i.to_string(),
})
}
}
impl FunctionSymbol {
pub fn from_ast(
export: bool,
name: String,
params: ParamList,
return_type: Option<TypeExpr>,
) -> Self {
Self {
export: export,
name: name.clone(),
params: params.to_hashmap(|name, v| VarSymbol {
name: name.clone(),
var_type: TypeSymbol(match v {
TypeExpr::Identifier(i) => i.to_string(),
}),
}),
return_type: return_type.map(TypeSymbol::from_ast),
}
}
}
impl StructSymbol {
pub fn from_ast(export: bool, name: String, fields: ParamList) -> Self {
Self {
export,
name,
fields: fields.to_hashmap(|name, v| VarSymbol {
name: name.clone(),
var_type: TypeSymbol(match v {
TypeExpr::Identifier(i) => i.to_string(),
}),
}),
// TODO - parse struct methods
methods: HashMap::new(),
}
}
}
pub struct TopLevelScope {
pub structs: HashMap<String, StructSymbol>,
pub functions: HashMap<String, FunctionSymbol>,
}
impl TopLevelScope {
pub fn from(top_level: &Vec<TopLevel>) -> Self {
let mut scope = TopLevelScope {
structs: HashMap::new(),
functions: HashMap::new(),
};
for top in top_level {
match top {
TopLevel::Import(_) => unimplemented!(),
TopLevel::FunctionDecl {
export,
name,
params,
return_type,
..
} => {
scope.functions.insert(
name.clone(),
FunctionSymbol::from_ast(
*export,
name.clone(),
params.clone(),
return_type.clone(),
),
);
}
TopLevel::StructDecl {
export,
name,
fields,
} => {
scope.structs.insert(
name.clone(),
StructSymbol::from_ast(*export, name.clone(), fields.clone()),
);
}
}
}
scope
}
}