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
+1
View File
@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2024"
[dependencies]
parser = { path = "../parser" }
+7 -15
View File
@@ -1,21 +1,13 @@
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use std::sync::Arc;
use crate::top_level;
use crate::top_level::TopLevelScope;
pub enum Symbol {
Function(top_level::FunctionSymbol),
TypeSymbol(top_level::TypeSymbol),
StructSymbol(top_level::StructSymbol),
}
pub struct Scope {
pub parent: Option<Arc<Scope>>,
pub variables: Mutex<HashMap<String, Symbol>>,
pub enum Scope {
TopLevel(TopLevelScope),
}
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;
pub struct TypeSymbol {
pub export: bool,
use parser::ast::TopLevel;
use parser::ast::{ParamList, TypeExpr};
pub struct TypeSymbol(pub String);
pub struct VarSymbol {
pub var_type: TypeSymbol,
pub name: String,
}
pub struct FunctionSymbol {
pub export: bool,
pub name: String,
pub params: HashMap<String, TypeSymbol>,
pub params: HashMap<String, VarSymbol>,
pub return_type: Option<TypeSymbol>,
}
pub struct StructSymbol {
pub export: bool,
pub name: String,
pub fields: HashMap<String, TypeSymbol>,
pub fields: HashMap<String, VarSymbol>,
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
}
}