From 71b3365de22ed8c4ee8ad412f947cf266a83176d Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 20 Sep 2019 02:21:39 -0700 Subject: [PATCH] Remove all the rest of the instances of Meta from the AST Still need to do tests --- schala-lang/language/src/ast.rs | 38 +++++------ schala-lang/language/src/parsing.rs | 18 ++--- schala-lang/language/src/reduced_ast.rs | 19 +++--- schala-lang/language/src/scope_resolution.rs | 69 ++++++++++---------- schala-lang/language/src/symbol_table.rs | 7 +- schala-lang/language/src/typechecking.rs | 21 +++--- 6 files changed, 84 insertions(+), 88 deletions(-) diff --git a/schala-lang/language/src/ast.rs b/schala-lang/language/src/ast.rs index ffc933f..ae470f1 100644 --- a/schala-lang/language/src/ast.rs +++ b/schala-lang/language/src/ast.rs @@ -80,7 +80,7 @@ impl From for Meta { pub struct AST { #[derivative(PartialEq="ignore")] pub id: ItemId, - pub statements: Vec> + pub statements: Vec } #[derive(Derivative, Debug, Clone)] @@ -93,11 +93,11 @@ pub struct Statement { #[derive(Debug, PartialEq, Clone)] pub enum StatementKind { - Expression(Meta), + Expression(Expression), Declaration(Declaration), //TODO Declaration should also be Meta-wrapped; only Expression and Declaration are Meta-wrapped maybe? } -pub type Block = Vec>; +pub type Block = Vec; pub type ParamName = Rc; #[derive(Debug, Derivative, Clone)] @@ -111,7 +111,7 @@ pub struct QualifiedName { #[derive(Debug, PartialEq, Clone)] pub struct FormalParam { pub name: ParamName, - pub default: Option>, + pub default: Option, pub anno: Option } @@ -129,7 +129,7 @@ pub enum Declaration { name: Rc, constant: bool, type_anno: Option, - expr: Meta, + expr: Expression, }, Impl { type_name: TypeIdentifier, @@ -200,28 +200,28 @@ pub enum ExpressionKind { FloatLiteral(f64), StringLiteral(Rc), BoolLiteral(bool), - BinExp(BinOp, Box>, Box>), - PrefixExp(PrefixOp, Box>), - TupleLiteral(Vec>), + BinExp(BinOp, Box, Box), + PrefixExp(PrefixOp, Box), + TupleLiteral(Vec), Value(QualifiedName), NamedStruct { - name: Meta, - fields: Vec<(Rc, Meta)>, + name: QualifiedName, + fields: Vec<(Rc, Expression)>, }, Call { - f: Box>, + f: Box, arguments: Vec, }, Index { - indexee: Box>, - indexers: Vec>, + indexee: Box, + indexers: Vec, }, IfExpression { discriminator: Box, body: Box, }, WhileExpression { - condition: Option>>, + condition: Option>, body: Block, }, ForExpression { @@ -233,23 +233,23 @@ pub enum ExpressionKind { type_anno: Option, body: Block, }, - ListLiteral(Vec>), + ListLiteral(Vec), } #[derive(Debug, PartialEq, Clone)] pub enum InvocationArgument { - Positional(Meta), + Positional(Expression), Keyword { name: Rc, - expr: Meta, + expr: Expression, }, Ignored } #[derive(Debug, PartialEq, Clone)] pub enum Discriminator { - Simple(Meta), - BinOp(Meta, BinOp) + Simple(Expression), + BinOp(Expression, BinOp) } #[derive(Debug, PartialEq, Clone)] diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 09c0d5e..24fcc67 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -332,7 +332,7 @@ impl Parser { continue; }, _ => statements.push( - Meta::new(self.statement()?) + self.statement()? ), } } @@ -459,9 +459,9 @@ impl Parser { } #[recursive_descent_method] - fn nonempty_func_body(&mut self) -> ParseResult>> { + fn nonempty_func_body(&mut self) -> ParseResult> { let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict); - Ok(statements.into_iter().map(|s| Meta::new(s)).collect()) + Ok(statements) } #[recursive_descent_method] @@ -751,7 +751,7 @@ impl Parser { 0 => Ok(Expression::new(self.id_store.fresh(), TupleLiteral(vec![]))), 1 => Ok(inner.pop().unwrap()), _ => { - let inner: Vec> = inner.into_iter().map(|ex| ex.into()).collect(); + let inner: Vec = inner.into_iter().map(|ex| ex.into()).collect(); Ok(Expression::new(self.id_store.fresh(), TupleLiteral(inner))) } } @@ -767,7 +767,7 @@ impl Parser { Ok(match self.token_handler.peek_kind() { LCurlyBrace if !self.restrictions.no_struct_literal => { let fields = self.record_block()?; - Expression::new(self.id_store.fresh(), NamedStruct { name: Meta::new(qualified_identifier), fields }) + Expression::new(self.id_store.fresh(), NamedStruct { name: qualified_identifier, fields }) }, _ => Expression::new(self.id_store.fresh(), Value(qualified_identifier)) }) @@ -789,7 +789,7 @@ impl Parser { } #[recursive_descent_method] - fn record_block(&mut self) -> ParseResult, Meta)>> { + fn record_block(&mut self) -> ParseResult, Expression)>> { Ok( delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace) .into_iter().map(|(s, ex)| (s, ex.into())).collect() @@ -1055,7 +1055,7 @@ impl Parser { #[recursive_descent_method] fn block(&mut self) -> ParseResult { let block = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict); - Ok(block.into_iter().map(|s| { Meta::new(s) }).collect()) + Ok(block) } #[recursive_descent_method] @@ -1065,7 +1065,7 @@ impl Parser { _ => { let expr = self.expression()?; let s = Statement { id: self.id_store.fresh(), kind: StatementKind::Expression(expr.into()) }; - Ok(vec![Meta::new(s)]) + Ok(vec![s]) } } } @@ -1125,7 +1125,7 @@ impl Parser { Ok(match tok.get_kind() { LCurlyBrace => { let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict); - StatementBlock(statements.into_iter().map(|s| Meta::new(s)).collect()) + StatementBlock(statements) }, Keyword(Kw::Return) => { self.token_handler.next(); diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index 1a8b42c..49272d1 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -125,8 +125,8 @@ impl<'a> Reducer<'a> { ReducedAST(output) } - fn statement(&mut self, stmt: &Meta) -> Stmt { - match &stmt.node().kind { + fn statement(&mut self, stmt: &Statement) -> Stmt { + match &stmt.kind { StatementKind::Expression(expr) => Stmt::Expr(self.expression(&expr)), StatementKind::Declaration(decl) => self.declaration(&decl), } @@ -145,11 +145,10 @@ impl<'a> Reducer<'a> { } } - fn expression(&mut self, expr: &Meta) -> Expr { + fn expression(&mut self, expr: &Expression) -> Expr { use crate::ast::ExpressionKind::*; let symbol_table = self.symbol_table; - let ref node = expr.node(); - let ref input = node.kind; + let ref input = expr.kind; match input { NatLiteral(n) => Expr::Lit(Lit::Nat(*n)), FloatLiteral(f) => Expr::Lit(Lit::Float(*f)), @@ -180,7 +179,7 @@ impl<'a> Reducer<'a> { TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| self.expression(e)).collect()), IfExpression { discriminator, body } => self.reduce_if_expression(discriminator, body), Lambda { params, body, .. } => self.reduce_lambda(params, body), - NamedStruct { name, fields } => self.reduce_named_struct(name.node(), fields), + NamedStruct { name, fields } => self.reduce_named_struct(name, fields), Index { .. } => Expr::UnimplementedSigilValue, WhileExpression { .. } => Expr::UnimplementedSigilValue, ForExpression { .. } => Expr::UnimplementedSigilValue, @@ -196,7 +195,7 @@ impl<'a> Reducer<'a> { }) } - fn reduce_named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc, Meta)>) -> Expr { + fn reduce_named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc, Expression)>) -> Expr { let symbol_table = self.symbol_table; let ref sym_name = match symbol_table.get_fqsn_from_id(&name.id) { Some(fqsn) => fqsn, @@ -225,7 +224,7 @@ impl<'a> Reducer<'a> { Expr::Call { f, args } } - fn reduce_call_expression(&mut self, func: &Meta, arguments: &Vec) -> Expr { + fn reduce_call_expression(&mut self, func: &Expression, arguments: &Vec) -> Expr { Expr::Call { f: Box::new(self.expression(func)), args: arguments.iter().map(|arg| self.invocation_argument(arg)).collect(), @@ -291,7 +290,7 @@ impl<'a> Reducer<'a> { } } - fn binop(&mut self, binop: &BinOp, lhs: &Box>, rhs: &Box>) -> Expr { + fn binop(&mut self, binop: &BinOp, lhs: &Box, rhs: &Box) -> Expr { let operation = Builtin::from_str(binop.sigil()).ok(); match operation { Some(Builtin::Assignment) => Expr::Assign { @@ -309,7 +308,7 @@ impl<'a> Reducer<'a> { } } - fn prefix(&mut self, prefix: &PrefixOp, arg: &Box>) -> Expr { + fn prefix(&mut self, prefix: &PrefixOp, arg: &Box) -> Expr { match prefix.builtin { Some(op) => { let f = Box::new(Expr::Func(Func::BuiltIn(op))); diff --git a/schala-lang/language/src/scope_resolution.rs b/schala-lang/language/src/scope_resolution.rs index 2cc5a47..2f155b3 100644 --- a/schala-lang/language/src/scope_resolution.rs +++ b/schala-lang/language/src/scope_resolution.rs @@ -10,16 +10,16 @@ impl<'a> ScopeResolver<'a> { ScopeResolver { symbol_table } } pub fn resolve(&mut self, ast: &mut AST) -> Result<(), String> { - for statement in ast.statements.iter_mut() { - match statement.mut_node().kind { - StatementKind::Declaration(ref mut decl) => self.decl(decl), - StatementKind::Expression(ref mut expr) => self.expr(expr), + for statement in ast.statements.iter() { + match statement.kind { + StatementKind::Declaration(ref decl) => self.decl(decl), + StatementKind::Expression(ref expr) => self.expr(expr), }?; } Ok(()) } - fn decl(&mut self, decl: &mut Declaration) -> Result<(), String> { + fn decl(&mut self, decl: &Declaration) -> Result<(), String> { use Declaration::*; match decl { Binding { expr, .. } => self.expr(expr), @@ -27,75 +27,74 @@ impl<'a> ScopeResolver<'a> { _ => Ok(()), } } - fn block(&mut self, block: &mut Block) -> Result<(), String> { - for statement in block.iter_mut() { - match statement.mut_node().kind { - StatementKind::Declaration(ref mut decl) => self.decl(decl), - StatementKind::Expression(ref mut expr) => self.expr(expr), + 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), }?; } Ok(()) } - fn expr(&mut self, expr: &mut Meta) -> Result<(), String> { + fn expr(&mut self, expr: &Expression) -> Result<(), String> { use ExpressionKind::*; - let inner_expr = expr.mut_node(); - match &mut inner_expr.kind { + match &expr.kind { ExpressionKind::Value(qualified_name) => { let fqsn = lookup_name_in_scope(&qualified_name); let ref id = qualified_name.id; self.symbol_table.map_id_to_fqsn(id, fqsn); }, NamedStruct { name, .. } => { - let ref id = name.node().id; - let fqsn = lookup_name_in_scope(&name.node()); + let ref id = name.id; + let fqsn = lookup_name_in_scope(&name); self.symbol_table.map_id_to_fqsn(id, fqsn); }, - BinExp(_, ref mut lhs, ref mut rhs) => { + BinExp(_, ref lhs, ref rhs) => { self.expr(lhs)?; self.expr(rhs)?; }, - PrefixExp(_, ref mut arg) => { + PrefixExp(_, ref arg) => { self.expr(arg)?; }, TupleLiteral(exprs) => { - for expr in exprs.iter_mut() { + for expr in exprs.iter() { self.expr(expr)?; } }, - Call { ref mut f, arguments } => { - self.expr(f)?; - for arg in arguments.iter_mut() { + Call { f, arguments } => { + self.expr(&f)?; + for arg in arguments.iter() { self.invoc(arg)?; } }, Lambda { params, body, .. } => { - self.block(body)?; - for param in params.iter_mut() { - if let Some(ref mut expr) = param.default { + self.block(&body)?; + for param in params.iter() { + if let Some(ref expr) = param.default { self.expr(expr)?; } } }, - IfExpression { ref mut body, ref mut discriminator } => { - match &mut **discriminator { + IfExpression { ref body, ref discriminator } => { + match &**discriminator { Discriminator::Simple(expr) | Discriminator::BinOp(expr, _) => self.expr(expr)? }; - match &mut **body { - IfExpressionBody::SimplePatternMatch(ref mut pat, ref mut alt1, ref mut alt2) => { + match &**body { + IfExpressionBody::SimplePatternMatch(ref pat, ref alt1, ref alt2) => { self.pattern(pat)?; self.block(alt1)?; - if let Some(alt) = alt2.as_mut() { + if let Some(alt) = alt2 { self.block(alt)?; } }, IfExpressionBody::GuardList(guardarms) => { - for arm in guardarms.iter_mut() { - if let Guard::Pat(ref mut pat) = arm.guard { + for arm in guardarms.iter() { + if let Guard::Pat(ref pat) = arm.guard { self.pattern(pat)?; } - self.block(&mut arm.body)?; + self.block(&arm.body)?; } } _ => () @@ -106,7 +105,7 @@ impl<'a> ScopeResolver<'a> { Ok(()) } - fn invoc(&mut self, invoc: &mut InvocationArgument) -> Result<(), String> { + fn invoc(&mut self, invoc: &InvocationArgument) -> Result<(), String> { use InvocationArgument::*; match invoc { Positional(expr) => self.expr(expr), @@ -115,7 +114,7 @@ impl<'a> ScopeResolver<'a> { } } - fn pattern(&mut self, pat: &mut Pattern) -> Result<(), String> { + fn pattern(&mut self, pat: &Pattern) -> Result<(), String> { use Pattern::*; match pat { Ignored => (), @@ -145,7 +144,7 @@ impl<'a> ScopeResolver<'a> { } /// this might be a variable or a pattern. if a variable, set to none - fn qualified_name_in_pattern(&mut self, qualified_name: &mut QualifiedName) { + fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) { let ref id = qualified_name.id; let fqsn = lookup_name_in_scope(qualified_name); if self.symbol_table.lookup_by_fqsn(&fqsn).is_some() { diff --git a/schala-lang/language/src/symbol_table.rs b/schala-lang/language/src/symbol_table.rs index 1950e96..d2fee74 100644 --- a/schala-lang/language/src/symbol_table.rs +++ b/schala-lang/language/src/symbol_table.rs @@ -5,7 +5,7 @@ use std::fmt; use std::fmt::Write; use crate::ast; -use crate::ast::{ItemId, Meta, TypeBody, TypeSingletonName, Signature, Statement, StatementKind}; +use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, StatementKind}; use crate::typechecking::TypeName; type LineNumber = u32; @@ -164,7 +164,7 @@ impl SymbolTable { self.add_symbols_from_scope(&ast.statements, &mut scope_name_stack) } - fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec>, scope_name_stack: &mut Vec) -> Result<(), String> { + fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec, scope_name_stack: &mut Vec) -> Result<(), String> { use self::ast::Declaration::*; fn insert_and_check_duplicate_symbol(table: &mut SymbolTrackTable, name: &Rc) -> Result<(), String> { @@ -183,8 +183,7 @@ impl SymbolTable { let mut seen_identifiers: SymbolTrackTable = HashMap::new(); - for meta in statements.iter() { - let statement = meta.node(); + for statement in statements.iter() { if let Statement { kind: StatementKind::Declaration(decl), .. } = statement { match decl { FuncSig(ref signature) => { diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index bea3db3..e4fd486 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -265,14 +265,14 @@ impl<'a> TypeContext<'a> { pub fn typecheck(&mut self, ast: &AST) -> Result { let mut returned_type = Type::Const(TypeConst::Unit); for statement in ast.statements.iter() { - returned_type = self.statement(statement.node())?; + returned_type = self.statement(statement)?; } Ok(returned_type) } fn statement(&mut self, statement: &Statement) -> InferResult { match &statement.kind { - StatementKind::Expression(e) => self.expr(e.node()), + StatementKind::Expression(e) => self.expr(e), StatementKind::Declaration(decl) => self.decl(&decl), } } @@ -281,7 +281,7 @@ impl<'a> TypeContext<'a> { use self::Declaration::*; match decl { Binding { name, expr, .. } => { - let ty = self.expr(expr.node())?; + let ty = self.expr(expr)?; self.variable_map.insert(name.clone(), ty); }, _ => (), @@ -292,7 +292,7 @@ impl<'a> TypeContext<'a> { fn invoc(&mut self, invoc: &InvocationArgument) -> InferResult { use InvocationArgument::*; match invoc { - Positional(expr) => self.expr(expr.node()), + Positional(expr) => self.expr(expr), _ => Ok(ty!(Nat)) //TODO this is wrong } } @@ -315,8 +315,8 @@ impl<'a> TypeContext<'a> { BoolLiteral(_) => ty!(Bool), FloatLiteral(_) => ty!(Float), StringLiteral(_) => ty!(StringT), - PrefixExp(op, expr) => self.prefix(op, expr.node())?, - BinExp(op, lhs, rhs) => self.binexp(op, lhs.node(), rhs.node())?, + PrefixExp(op, expr) => self.prefix(op, expr)?, + BinExp(op, lhs, rhs) => self.binexp(op, lhs, rhs)?, IfExpression { discriminator, body } => self.if_expr(discriminator, body)?, Value(val) => self.handle_value(val)?, Call { box ref f, arguments } => self.call(f, arguments)?, @@ -350,7 +350,7 @@ impl<'a> TypeContext<'a> { fn if_expr(&mut self, discriminator: &Discriminator, body: &IfExpressionBody) -> InferResult { use self::Discriminator::*; use self::IfExpressionBody::*; match (discriminator, body) { - (Simple(expr), SimpleConditional(then_clause, else_clause)) => self.handle_simple_if(expr.node(), then_clause, else_clause), + (Simple(expr), SimpleConditional(then_clause, else_clause)) => self.handle_simple_if(expr, then_clause, else_clause), _ => TypeError::new(format!("Complex conditionals not supported")) } } @@ -384,8 +384,8 @@ impl<'a> TypeContext<'a> { Ok(ty!(argument_types, ret_type)) } - fn call(&mut self, f: &Meta, args: &Vec) -> InferResult { - let tf = self.expr(f.node())?; + fn call(&mut self, f: &Expression, args: &Vec) -> InferResult { + let tf = self.expr(f)?; let arg_types: InferResult> = args.iter().map(|ex| self.invoc(ex)).collect(); let arg_types = arg_types?; self.handle_apply(tf, arg_types) @@ -406,8 +406,7 @@ impl<'a> TypeContext<'a> { fn block(&mut self, block: &Block) -> InferResult { let mut output = ty!(Unit); - for s in block.iter() { - let statement = s.node(); + for statement in block.iter() { output = self.statement(statement)?; } Ok(output)