2019-09-24 03:28:59 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2019-09-09 17:45:34 -07:00
|
|
|
use crate::symbol_table::{SymbolTable, ScopeSegment, ScopeSegmentKind, FullyQualifiedSymbolName};
|
2019-09-03 01:42:28 -07:00
|
|
|
use crate::ast::*;
|
2019-09-24 03:28:59 -07:00
|
|
|
use crate::util::ScopeStack;
|
|
|
|
|
|
|
|
type FQSNPrefix = Vec<ScopeSegment>;
|
2019-09-25 03:26:31 -07:00
|
|
|
type NameScopeStack<'t> = ScopeStack<'t, Rc<String>, FQSNPrefix>;
|
2019-09-03 01:42:28 -07:00
|
|
|
|
2019-09-09 17:45:34 -07:00
|
|
|
pub struct ScopeResolver<'a> {
|
2019-09-24 03:28:59 -07:00
|
|
|
symbol_table: &'a mut SymbolTable,
|
|
|
|
name_scope_stack: ScopeStack<'a, Rc<String>, FQSNPrefix>,
|
2019-09-03 02:19:37 -07:00
|
|
|
}
|
|
|
|
|
2019-10-15 19:06:07 -07:00
|
|
|
impl<'a> ASTVisitor for ScopeResolver<'a> {
|
|
|
|
fn ast(&mut self, _ast: &AST) {
|
|
|
|
println!("VISITING AST!!!");
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 19:02:48 -07:00
|
|
|
|
2019-09-09 17:45:34 -07:00
|
|
|
impl<'a> ScopeResolver<'a> {
|
2019-09-19 03:34:09 -07:00
|
|
|
pub fn new(symbol_table: &'a mut SymbolTable) -> ScopeResolver {
|
2019-09-24 03:28:59 -07:00
|
|
|
let name_scope_stack = ScopeStack::new(None);
|
|
|
|
ScopeResolver { symbol_table, name_scope_stack }
|
2019-09-03 02:59:19 -07:00
|
|
|
}
|
|
|
|
pub fn resolve(&mut self, ast: &mut AST) -> Result<(), String> {
|
2019-10-15 19:06:07 -07:00
|
|
|
walk_ast(self, ast);
|
2019-09-25 01:45:02 -07:00
|
|
|
self.block(&mut ast.statements)?;
|
2019-09-03 02:59:19 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-25 03:18:54 -07:00
|
|
|
|
|
|
|
//TODO this is incomplete
|
|
|
|
fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> FullyQualifiedSymbolName {
|
|
|
|
let QualifiedName { components: vec, .. } = sym_name;
|
|
|
|
let len = vec.len();
|
|
|
|
let new_vec: Vec<ScopeSegment> = vec.iter().enumerate().map(|(i, name)| {
|
|
|
|
let kind = if i == (len - 1) {
|
|
|
|
ScopeSegmentKind::Terminal
|
|
|
|
} else {
|
|
|
|
ScopeSegmentKind::Type
|
|
|
|
};
|
|
|
|
ScopeSegment { name: name.clone(), kind }
|
|
|
|
}).collect();
|
|
|
|
FullyQualifiedSymbolName(new_vec)
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:42:01 -07:00
|
|
|
fn import(&mut self, import_spec: &ImportSpecifier) -> Result<(), String> {
|
|
|
|
let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec;
|
|
|
|
match imported_names {
|
|
|
|
ImportedNames::All => unimplemented!(),
|
|
|
|
ImportedNames::LastOfPath => {
|
|
|
|
let name = path_components.last().unwrap(); //TODO handle better
|
|
|
|
let fqsn_prefix = path_components.iter().map(|c| ScopeSegment {
|
|
|
|
name: c.clone(), kind: ScopeSegmentKind::Type
|
|
|
|
}).collect();
|
|
|
|
self.name_scope_stack.insert(name.clone(), fqsn_prefix);
|
|
|
|
()
|
|
|
|
}
|
2019-09-24 19:24:07 -07:00
|
|
|
ImportedNames::List(ref names) => {
|
|
|
|
let fqsn_prefix: FQSNPrefix = path_components.iter().map(|c| ScopeSegment {
|
|
|
|
name: c.clone(), kind: ScopeSegmentKind::Type
|
|
|
|
}).collect();
|
|
|
|
for name in names.iter() {
|
|
|
|
self.name_scope_stack.insert(name.clone(), fqsn_prefix.clone());
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 18:42:01 -07:00
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-20 02:21:39 -07:00
|
|
|
fn decl(&mut self, decl: &Declaration) -> Result<(), String> {
|
2019-09-09 01:04:46 -07:00
|
|
|
use Declaration::*;
|
2019-09-03 03:20:17 -07:00
|
|
|
match decl {
|
2019-09-09 01:04:46 -07:00
|
|
|
Binding { expr, .. } => self.expr(expr),
|
|
|
|
FuncDecl(_, block) => self.block(block),
|
2019-09-03 03:20:17 -07:00
|
|
|
_ => Ok(()),
|
|
|
|
}
|
2019-09-03 02:59:19 -07:00
|
|
|
}
|
2019-09-20 02:21:39 -07:00
|
|
|
fn block(&mut self, block: &Block) -> Result<(), String> {
|
|
|
|
for statement in block.iter() {
|
|
|
|
match statement.kind {
|
|
|
|
StatementKind::Declaration(ref decl) => self.decl(decl),
|
|
|
|
StatementKind::Expression(ref expr) => self.expr(expr),
|
2019-09-25 01:45:02 -07:00
|
|
|
StatementKind::Import(ref spec) => self.import(spec),
|
2019-09-09 01:04:46 -07:00
|
|
|
}?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-03 02:59:19 -07:00
|
|
|
|
2019-09-20 02:21:39 -07:00
|
|
|
fn expr(&mut self, expr: &Expression) -> Result<(), String> {
|
2019-09-03 10:23:38 -07:00
|
|
|
use ExpressionKind::*;
|
2019-09-20 02:21:39 -07:00
|
|
|
match &expr.kind {
|
2019-09-03 03:20:17 -07:00
|
|
|
ExpressionKind::Value(qualified_name) => {
|
2019-09-25 03:18:54 -07:00
|
|
|
let fqsn = self.lookup_name_in_scope(&qualified_name);
|
2019-09-20 01:57:48 -07:00
|
|
|
let ref id = qualified_name.id;
|
2019-09-19 03:34:09 -07:00
|
|
|
self.symbol_table.map_id_to_fqsn(id, fqsn);
|
2019-09-03 03:20:17 -07:00
|
|
|
},
|
2019-09-03 10:23:38 -07:00
|
|
|
NamedStruct { name, .. } => {
|
2019-09-20 02:21:39 -07:00
|
|
|
let ref id = name.id;
|
2019-09-25 03:18:54 -07:00
|
|
|
let fqsn = self.lookup_name_in_scope(&name);
|
2019-09-20 01:36:58 -07:00
|
|
|
self.symbol_table.map_id_to_fqsn(id, fqsn);
|
2019-09-03 10:23:38 -07:00
|
|
|
},
|
2019-09-20 02:21:39 -07:00
|
|
|
BinExp(_, ref lhs, ref rhs) => {
|
2019-09-03 10:23:38 -07:00
|
|
|
self.expr(lhs)?;
|
|
|
|
self.expr(rhs)?;
|
|
|
|
},
|
2019-09-20 02:21:39 -07:00
|
|
|
PrefixExp(_, ref arg) => {
|
2019-09-03 10:23:38 -07:00
|
|
|
self.expr(arg)?;
|
|
|
|
},
|
|
|
|
TupleLiteral(exprs) => {
|
2019-09-20 02:21:39 -07:00
|
|
|
for expr in exprs.iter() {
|
2019-09-03 10:23:38 -07:00
|
|
|
self.expr(expr)?;
|
|
|
|
}
|
|
|
|
},
|
2019-09-20 02:21:39 -07:00
|
|
|
Call { f, arguments } => {
|
|
|
|
self.expr(&f)?;
|
|
|
|
for arg in arguments.iter() {
|
2019-09-03 10:23:38 -07:00
|
|
|
self.invoc(arg)?;
|
|
|
|
}
|
2019-09-08 02:11:15 -07:00
|
|
|
},
|
2019-10-10 18:33:34 -07:00
|
|
|
IfExpression { discriminator, body } => {
|
|
|
|
match discriminator {
|
|
|
|
None => (),
|
|
|
|
Some(ref expr) => self.expr(expr)?
|
|
|
|
};
|
|
|
|
self.if_expr_body(body)?;
|
|
|
|
},
|
|
|
|
Lambda { params, body, .. } => {
|
2019-09-20 02:21:39 -07:00
|
|
|
self.block(&body)?;
|
|
|
|
for param in params.iter() {
|
|
|
|
if let Some(ref expr) = param.default {
|
2019-09-09 01:04:46 -07:00
|
|
|
self.expr(expr)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-09-03 03:20:17 -07:00
|
|
|
_ => ()
|
|
|
|
};
|
2019-09-03 02:59:19 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-03 10:23:38 -07:00
|
|
|
|
2019-09-20 02:21:39 -07:00
|
|
|
fn invoc(&mut self, invoc: &InvocationArgument) -> Result<(), String> {
|
2019-09-03 10:23:38 -07:00
|
|
|
use InvocationArgument::*;
|
|
|
|
match invoc {
|
|
|
|
Positional(expr) => self.expr(expr),
|
|
|
|
Keyword { expr, .. } => self.expr(expr),
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-09-08 02:11:15 -07:00
|
|
|
|
2019-10-10 18:33:34 -07:00
|
|
|
fn if_expr_body(&mut self, body: &IfExpressionBody) -> Result<(), String> {
|
|
|
|
use IfExpressionBody::*;
|
|
|
|
match body {
|
|
|
|
SimpleConditional { then_case, else_case } => {
|
|
|
|
self.block(then_case)?;
|
|
|
|
if let Some(block) = else_case {
|
|
|
|
self.block(block)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
SimplePatternMatch { pattern, then_case, else_case } => {
|
|
|
|
self.pattern(pattern)?;
|
|
|
|
self.block(then_case)?;
|
|
|
|
if let Some(block) = else_case {
|
|
|
|
self.block(block)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CondList(conditions) => {
|
|
|
|
for arm in conditions.iter() {
|
|
|
|
if let Condition::Pattern(ref pat) = arm.condition {
|
|
|
|
self.pattern(pat)?;
|
|
|
|
}
|
|
|
|
self.block(&arm.body)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-20 02:21:39 -07:00
|
|
|
fn pattern(&mut self, pat: &Pattern) -> Result<(), String> {
|
2019-09-08 02:11:15 -07:00
|
|
|
use Pattern::*;
|
|
|
|
match pat {
|
|
|
|
Ignored => (),
|
|
|
|
TuplePattern(patterns) => {
|
|
|
|
for pat in patterns {
|
|
|
|
self.pattern(pat)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Literal(_) => (),
|
|
|
|
TupleStruct(name, patterns) => {
|
2019-09-08 04:27:04 -07:00
|
|
|
self.qualified_name_in_pattern(name);
|
2019-09-08 02:11:15 -07:00
|
|
|
for pat in patterns {
|
|
|
|
self.pattern(pat)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Record(name, key_patterns) => {
|
2019-09-08 04:27:04 -07:00
|
|
|
self.qualified_name_in_pattern(name);
|
2019-09-08 02:11:15 -07:00
|
|
|
for (_, pat) in key_patterns {
|
|
|
|
self.pattern(pat)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
VarOrName(name) => {
|
2019-09-08 04:27:04 -07:00
|
|
|
self.qualified_name_in_pattern(name);
|
2019-09-08 02:11:15 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-08 04:27:04 -07:00
|
|
|
/// this might be a variable or a pattern. if a variable, set to none
|
2019-09-20 02:21:39 -07:00
|
|
|
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) {
|
2019-09-20 02:03:10 -07:00
|
|
|
let ref id = qualified_name.id;
|
2019-09-25 03:18:54 -07:00
|
|
|
let fqsn = self.lookup_name_in_scope(qualified_name);
|
2019-09-20 01:44:20 -07:00
|
|
|
if self.symbol_table.lookup_by_fqsn(&fqsn).is_some() {
|
|
|
|
self.symbol_table.map_id_to_fqsn(&id, fqsn);
|
|
|
|
}
|
2019-09-08 02:11:15 -07:00
|
|
|
}
|
2019-09-03 03:20:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn basic_scope() {
|
|
|
|
|
|
|
|
}
|
2019-09-03 01:42:28 -07:00
|
|
|
}
|