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-09 17:45:34 -07:00
|
|
|
pub struct ScopeResolver<'a> {
|
|
|
|
symbol_table: &'a SymbolTable
|
2019-09-03 02:19:37 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:45:34 -07:00
|
|
|
impl<'a> ScopeResolver<'a> {
|
|
|
|
pub fn new(symbol_table: &'a SymbolTable) -> ScopeResolver {
|
|
|
|
ScopeResolver { symbol_table }
|
2019-09-03 02:59:19 -07:00
|
|
|
}
|
|
|
|
pub fn resolve(&mut self, ast: &mut AST) -> Result<(), String> {
|
2019-09-11 19:06:00 -07:00
|
|
|
for statement in ast.statements.iter_mut() {
|
2019-09-17 02:25:11 -07:00
|
|
|
match statement.mut_node().kind {
|
|
|
|
StatementKind::Declaration(ref mut decl) => self.decl(decl),
|
|
|
|
StatementKind::Expression(ref mut expr) => self.expr(expr),
|
2019-09-03 02:59:19 -07:00
|
|
|
}?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decl(&mut self, decl: &mut 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-09 01:04:46 -07:00
|
|
|
fn block(&mut self, block: &mut Block) -> Result<(), String> {
|
|
|
|
for statement in block.iter_mut() {
|
2019-09-17 02:25:11 -07:00
|
|
|
match statement.mut_node().kind {
|
|
|
|
StatementKind::Declaration(ref mut decl) => self.decl(decl),
|
|
|
|
StatementKind::Expression(ref mut expr) => self.expr(expr),
|
2019-09-09 01:04:46 -07:00
|
|
|
}?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-03 02:59:19 -07:00
|
|
|
|
|
|
|
fn expr(&mut self, expr: &mut Meta<Expression>) -> Result<(), String> {
|
2019-09-03 10:23:38 -07:00
|
|
|
use ExpressionKind::*;
|
|
|
|
let inner_expr = expr.mut_node();
|
|
|
|
match &mut inner_expr.kind {
|
2019-09-03 03:20:17 -07:00
|
|
|
ExpressionKind::Value(qualified_name) => {
|
2019-09-06 10:03:50 -07:00
|
|
|
let fqsn = lookup_name_in_scope(&qualified_name.node());
|
2019-09-03 03:20:17 -07:00
|
|
|
expr.fqsn = Some(fqsn);
|
|
|
|
},
|
2019-09-03 10:23:38 -07:00
|
|
|
NamedStruct { name, .. } => {
|
2019-09-06 17:19:41 -07:00
|
|
|
let fqsn = lookup_name_in_scope(&name.node());
|
2019-09-03 10:23:38 -07:00
|
|
|
expr.fqsn = Some(fqsn);
|
|
|
|
},
|
|
|
|
BinExp(_, ref mut lhs, ref mut rhs) => {
|
|
|
|
self.expr(lhs)?;
|
|
|
|
self.expr(rhs)?;
|
|
|
|
},
|
|
|
|
PrefixExp(_, ref mut arg) => {
|
|
|
|
self.expr(arg)?;
|
|
|
|
},
|
|
|
|
TupleLiteral(exprs) => {
|
|
|
|
for expr in exprs.iter_mut() {
|
|
|
|
self.expr(expr)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Call { ref mut f, arguments } => {
|
|
|
|
self.expr(f)?;
|
|
|
|
for arg in arguments.iter_mut() {
|
|
|
|
self.invoc(arg)?;
|
|
|
|
}
|
2019-09-08 02:11:15 -07:00
|
|
|
},
|
2019-09-09 01:04:46 -07:00
|
|
|
Lambda { params, body, .. } => {
|
|
|
|
self.block(body)?;
|
|
|
|
for param in params.iter_mut() {
|
|
|
|
if let Some(ref mut expr) = param.default {
|
|
|
|
self.expr(expr)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
IfExpression { ref mut body, ref mut discriminator } => {
|
|
|
|
match &mut **discriminator {
|
|
|
|
Discriminator::Simple(expr) | Discriminator::BinOp(expr, _) => self.expr(expr)?
|
|
|
|
};
|
|
|
|
|
|
|
|
match &mut **body {
|
2019-09-10 03:31:23 -07:00
|
|
|
IfExpressionBody::SimplePatternMatch(ref mut pat, ref mut alt1, ref mut alt2) => {
|
2019-09-09 01:04:46 -07:00
|
|
|
self.pattern(pat)?;
|
2019-09-10 03:31:23 -07:00
|
|
|
self.block(alt1)?;
|
|
|
|
if let Some(alt) = alt2.as_mut() {
|
|
|
|
self.block(alt)?;
|
|
|
|
}
|
2019-09-09 01:04:46 -07:00
|
|
|
},
|
|
|
|
IfExpressionBody::GuardList(guardarms) => {
|
|
|
|
for arm in guardarms.iter_mut() {
|
|
|
|
if let Guard::Pat(ref mut pat) = arm.guard {
|
|
|
|
self.pattern(pat)?;
|
|
|
|
}
|
2019-09-10 03:31:23 -07:00
|
|
|
self.block(&mut arm.body)?;
|
2019-09-08 02:11:15 -07:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 01:04:46 -07:00
|
|
|
_ => ()
|
2019-09-08 02:11:15 -07:00
|
|
|
}
|
|
|
|
},
|
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
|
|
|
|
|
|
|
fn invoc(&mut self, invoc: &mut InvocationArgument) -> Result<(), String> {
|
|
|
|
use InvocationArgument::*;
|
|
|
|
match invoc {
|
|
|
|
Positional(expr) => self.expr(expr),
|
|
|
|
Keyword { expr, .. } => self.expr(expr),
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-09-08 02:11:15 -07:00
|
|
|
|
|
|
|
fn pattern(&mut self, pat: &mut Pattern) -> Result<(), String> {
|
|
|
|
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
|
|
|
|
fn qualified_name_in_pattern(&mut self, meta_qualified_name: &mut Meta<QualifiedName>, ) {
|
2019-09-08 02:11:15 -07:00
|
|
|
let inner_name = meta_qualified_name.node();
|
|
|
|
let fqsn = lookup_name_in_scope(inner_name);
|
2019-09-10 03:31:23 -07:00
|
|
|
meta_qualified_name.fqsn = if self.symbol_table.lookup_by_fqsn(&fqsn).is_some() { Some(fqsn) } else { None };
|
2019-09-08 02:11:15 -07:00
|
|
|
}
|
2019-09-03 03:20:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO this is incomplete
|
|
|
|
fn lookup_name_in_scope(sym_name: &QualifiedName) -> FullyQualifiedSymbolName {
|
2019-09-19 01:34:21 -07:00
|
|
|
let QualifiedName { components: vec, .. } = sym_name;
|
2019-09-03 03:20:17 -07:00
|
|
|
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-03 02:59:19 -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
|
|
|
}
|