Top level scope
This commit is contained in:
+7
-15
@@ -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
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user