From a5c9aca4d786333a4899395ab070b9000df59637 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 3 Sep 2019 03:20:17 -0700 Subject: [PATCH] Halfway done with fqsn lookup pass initial work --- schala-lang/language/src/ast.rs | 2 +- schala-lang/language/src/eval/test.rs | 6 ++- schala-lang/language/src/reduced_ast.rs | 29 +++++-------- schala-lang/language/src/scope_resolution.rs | 43 ++++++++++++++++++-- 4 files changed, 56 insertions(+), 24 deletions(-) diff --git a/schala-lang/language/src/ast.rs b/schala-lang/language/src/ast.rs index 875e155..ce59dfe 100644 --- a/schala-lang/language/src/ast.rs +++ b/schala-lang/language/src/ast.rs @@ -12,7 +12,7 @@ pub struct Meta { n: T, source_map: SourceMap, type_data: TypeData, - fqsn: Option + pub fqsn: Option } impl Meta { diff --git a/schala-lang/language/src/eval/test.rs b/schala-lang/language/src/eval/test.rs index 76da84a..b72f7fa 100644 --- a/schala-lang/language/src/eval/test.rs +++ b/schala-lang/language/src/eval/test.rs @@ -4,13 +4,17 @@ use std::cell::RefCell; use std::rc::Rc; use crate::symbol_table::SymbolTable; +use crate::scope_resolution::ScopeResolver; use crate::eval::State; fn evaluate_all_outputs(input: &str) -> Vec> { let symbol_table = Rc::new(RefCell::new(SymbolTable::new())); let mut state = State::new(symbol_table); - let ast = crate::util::quick_ast(input); + let mut ast = crate::util::quick_ast(input); state.symbol_table_handle.borrow_mut().add_top_level_symbols(&ast).unwrap(); + let mut scope_resolver = crate::scope_resolution::ScopeResolver::new(); + let _ = scope_resolver.resolve(&mut ast); + let reduced = ast.reduce(&state.symbol_table_handle.borrow()); let all_output = state.evaluate(reduced, true); all_output diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index 8fc1c05..0d3685b 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -142,20 +142,6 @@ impl InvocationArgument { } } -//TODO this is incomplete -fn lookup_name_in_scope(sym_name: &QualifiedName) -> FullyQualifiedSymbolName { - let QualifiedName(vec) = sym_name; - let len = vec.len(); - let new_vec: Vec = 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) -} impl Meta { fn reduce(&self, symbol_table: &SymbolTable) -> Expr { @@ -170,7 +156,10 @@ impl Meta { BinExp(binop, lhs, rhs) => binop.reduce(symbol_table, lhs, rhs), PrefixExp(op, arg) => op.reduce(symbol_table, arg), Value(qualified_name) => { - let sym_name = lookup_name_in_scope(&qualified_name); + let ref sym_name = match self.fqsn { + Some(ref fqsn) => fqsn, + None => return Expr::ReductionError(format!("FQSN lookup for value failed")), + }; let FullyQualifiedSymbolName(ref v) = sym_name; let name = v.last().unwrap().name.clone(); match symbol_table.lookup_by_fqsn(&sym_name) { @@ -187,7 +176,7 @@ impl Meta { TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce(symbol_table)).collect()), IfExpression { discriminator, body } => reduce_if_expression(discriminator, body, symbol_table), Lambda { params, body, .. } => reduce_lambda(params, body, symbol_table), - NamedStruct { name, fields } => reduce_named_struct(name, fields, symbol_table), + NamedStruct { name, fields } => reduce_named_struct(self.fqsn.as_ref(), name, fields, symbol_table), Index { .. } => Expr::UnimplementedSigilValue, WhileExpression { .. } => Expr::UnimplementedSigilValue, ForExpression { .. } => Expr::UnimplementedSigilValue, @@ -204,9 +193,11 @@ fn reduce_lambda(params: &Vec, body: &Block, symbol_table: &SymbolT }) } -fn reduce_named_struct(name: &QualifiedName, fields: &Vec<(Rc, Meta)>, symbol_table: &SymbolTable) -> Expr { - - let sym_name = lookup_name_in_scope(name); +fn reduce_named_struct(fqsn: Option<&FullyQualifiedSymbolName>, name: &QualifiedName, fields: &Vec<(Rc, Meta)>, symbol_table: &SymbolTable) -> Expr { + let sym_name = match fqsn { + Some(fqsn) => fqsn, + None => return Expr::ReductionError(format!("FQSN lookup for value failed")), + }; let FullyQualifiedSymbolName(ref v) = sym_name; let ref name = v.last().unwrap().name; let (type_name, index, members_from_table) = match symbol_table.lookup_by_fqsn(&sym_name) { diff --git a/schala-lang/language/src/scope_resolution.rs b/schala-lang/language/src/scope_resolution.rs index c396b86..63f7fcd 100644 --- a/schala-lang/language/src/scope_resolution.rs +++ b/schala-lang/language/src/scope_resolution.rs @@ -1,7 +1,9 @@ +use std::rc::Rc; + +use crate::symbol_table::{ScopeSegment, ScopeSegmentKind, FullyQualifiedSymbolName}; use crate::ast::*; pub struct ScopeResolver { - } impl ScopeResolver { @@ -20,11 +22,46 @@ impl ScopeResolver { } fn decl(&mut self, decl: &mut Declaration) -> Result<(), String> { - Ok(()) + match decl { + Declaration::Binding { expr, .. } => self.expr(expr), + _ => Ok(()), + } } fn expr(&mut self, expr: &mut Meta) -> Result<(), String> { + match &expr.node().kind { + + //TODO this needs to fully recurse + ExpressionKind::Value(qualified_name) => { + //TODO fill this out + let fqsn = lookup_name_in_scope(&qualified_name); + expr.fqsn = Some(fqsn); + }, + _ => () + }; Ok(()) } - +} + +//TODO this is incomplete +fn lookup_name_in_scope(sym_name: &QualifiedName) -> FullyQualifiedSymbolName { + let QualifiedName(vec) = sym_name; + let len = vec.len(); + let new_vec: Vec = 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) +} + +#[cfg(test)] +mod tests { + #[test] + fn basic_scope() { + + } }