Halfway done with fqsn lookup pass initial work

This commit is contained in:
greg 2019-09-03 03:20:17 -07:00
parent cefaeb1180
commit a5c9aca4d7
4 changed files with 56 additions and 24 deletions

View File

@ -12,7 +12,7 @@ pub struct Meta<T> {
n: T, n: T,
source_map: SourceMap, source_map: SourceMap,
type_data: TypeData, type_data: TypeData,
fqsn: Option<FullyQualifiedSymbolName> pub fqsn: Option<FullyQualifiedSymbolName>
} }
impl<T> Meta<T> { impl<T> Meta<T> {

View File

@ -4,13 +4,17 @@ use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use crate::symbol_table::SymbolTable; use crate::symbol_table::SymbolTable;
use crate::scope_resolution::ScopeResolver;
use crate::eval::State; use crate::eval::State;
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> { fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
let symbol_table = Rc::new(RefCell::new(SymbolTable::new())); let symbol_table = Rc::new(RefCell::new(SymbolTable::new()));
let mut state = State::new(symbol_table); 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(); 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 reduced = ast.reduce(&state.symbol_table_handle.borrow());
let all_output = state.evaluate(reduced, true); let all_output = state.evaluate(reduced, true);
all_output all_output

View File

@ -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<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)
}
impl Meta<Expression> { impl Meta<Expression> {
fn reduce(&self, symbol_table: &SymbolTable) -> Expr { fn reduce(&self, symbol_table: &SymbolTable) -> Expr {
@ -170,7 +156,10 @@ impl Meta<Expression> {
BinExp(binop, lhs, rhs) => binop.reduce(symbol_table, lhs, rhs), BinExp(binop, lhs, rhs) => binop.reduce(symbol_table, lhs, rhs),
PrefixExp(op, arg) => op.reduce(symbol_table, arg), PrefixExp(op, arg) => op.reduce(symbol_table, arg),
Value(qualified_name) => { 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 FullyQualifiedSymbolName(ref v) = sym_name;
let name = v.last().unwrap().name.clone(); let name = v.last().unwrap().name.clone();
match symbol_table.lookup_by_fqsn(&sym_name) { match symbol_table.lookup_by_fqsn(&sym_name) {
@ -187,7 +176,7 @@ impl Meta<Expression> {
TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce(symbol_table)).collect()), TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce(symbol_table)).collect()),
IfExpression { discriminator, body } => reduce_if_expression(discriminator, body, symbol_table), IfExpression { discriminator, body } => reduce_if_expression(discriminator, body, symbol_table),
Lambda { params, body, .. } => reduce_lambda(params, 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, Index { .. } => Expr::UnimplementedSigilValue,
WhileExpression { .. } => Expr::UnimplementedSigilValue, WhileExpression { .. } => Expr::UnimplementedSigilValue,
ForExpression { .. } => Expr::UnimplementedSigilValue, ForExpression { .. } => Expr::UnimplementedSigilValue,
@ -204,9 +193,11 @@ fn reduce_lambda(params: &Vec<FormalParam>, body: &Block, symbol_table: &SymbolT
}) })
} }
fn reduce_named_struct(name: &QualifiedName, fields: &Vec<(Rc<String>, Meta<Expression>)>, symbol_table: &SymbolTable) -> Expr { fn reduce_named_struct(fqsn: Option<&FullyQualifiedSymbolName>, name: &QualifiedName, fields: &Vec<(Rc<String>, Meta<Expression>)>, symbol_table: &SymbolTable) -> Expr {
let sym_name = match fqsn {
let sym_name = lookup_name_in_scope(name); Some(fqsn) => fqsn,
None => return Expr::ReductionError(format!("FQSN lookup for value failed")),
};
let FullyQualifiedSymbolName(ref v) = sym_name; let FullyQualifiedSymbolName(ref v) = sym_name;
let ref name = v.last().unwrap().name; let ref name = v.last().unwrap().name;
let (type_name, index, members_from_table) = match symbol_table.lookup_by_fqsn(&sym_name) { let (type_name, index, members_from_table) = match symbol_table.lookup_by_fqsn(&sym_name) {

View File

@ -1,7 +1,9 @@
use std::rc::Rc;
use crate::symbol_table::{ScopeSegment, ScopeSegmentKind, FullyQualifiedSymbolName};
use crate::ast::*; use crate::ast::*;
pub struct ScopeResolver { pub struct ScopeResolver {
} }
impl ScopeResolver { impl ScopeResolver {
@ -20,11 +22,46 @@ impl ScopeResolver {
} }
fn decl(&mut self, decl: &mut Declaration) -> Result<(), String> { 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<Expression>) -> Result<(), String> { fn expr(&mut self, expr: &mut Meta<Expression>) -> 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(()) 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<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)
}
#[cfg(test)]
mod tests {
#[test]
fn basic_scope() {
} }
}