From 8b87945bee0bb2d9638c7b5d49484758a15aa250 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 2 Sep 2019 14:13:53 -0700 Subject: [PATCH] Wrap remaining Expressions in Meta --- schala-lang/language/src/ast.rs | 20 +++++++++++------- schala-lang/language/src/parsing.rs | 12 +++++------ schala-lang/language/src/parsing/test.rs | 26 ++++++++++++------------ schala-lang/language/src/reduced_ast.rs | 4 ++-- schala-lang/language/src/typechecking.rs | 4 ++-- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/schala-lang/language/src/ast.rs b/schala-lang/language/src/ast.rs index cdd9175..5a4466b 100644 --- a/schala-lang/language/src/ast.rs +++ b/schala-lang/language/src/ast.rs @@ -2,6 +2,7 @@ use std::rc::Rc; use std::convert::From; use crate::typechecking::TypeData; +use crate::symbol_table::FullyQualifiedSymbolName; mod operators; pub use operators::*; @@ -11,11 +12,16 @@ pub struct Meta { n: T, source_map: SourceMap, type_data: TypeData, + fqsn: Option } impl Meta { pub fn new(n: T) -> Meta { - Meta { n, source_map: SourceMap::default(), type_data: TypeData::new() } + Meta { n, + source_map: SourceMap::default(), + type_data: TypeData::new(), + fqsn: None, + } } pub fn node(&self) -> &T { @@ -30,7 +36,7 @@ struct SourceMap { impl From for Meta { fn from(expr: Expression) -> Meta { - Meta { n: expr, source_map: SourceMap::default(), type_data: TypeData::new() } + Meta::new(expr) } } @@ -52,7 +58,7 @@ pub struct QualifiedName(pub Vec>); #[derive(Debug, PartialEq, Clone)] pub struct FormalParam { pub name: ParamName, - pub default: Option, + pub default: Option>, pub anno: Option } @@ -176,18 +182,18 @@ pub enum ExpressionKind { #[derive(Debug, PartialEq, Clone)] pub enum InvocationArgument { - Positional(Expression), + Positional(Meta), Keyword { name: Rc, - expr: Expression, + expr: Meta, }, Ignored } #[derive(Debug, PartialEq, Clone)] pub enum Discriminator { - Simple(Expression), - BinOp(Expression, BinOp) + Simple(Meta), + BinOp(Meta, BinOp) } #[derive(Debug, PartialEq, Clone)] diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 7e68045..a0993ce 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -477,7 +477,7 @@ impl Parser { let default = match self.token_handler.peek_kind() { Equals => { self.token_handler.next(); - Some(self.expression()?) + Some(self.expression()?.into()) }, _ => None }; @@ -659,16 +659,16 @@ impl Parser { Equals => { self.token_handler.next(); self.token_handler.next(); - let expr = self.expression()?; + let expr = self.expression()?.into(); InvocationArgument::Keyword { name: s.clone(), expr } }, _ => { let expr = self.expression()?; - InvocationArgument::Positional(expr) + InvocationArgument::Positional(expr.into()) } } }, - _ => InvocationArgument::Positional(self.expression()?) + _ => InvocationArgument::Positional(self.expression()?.into()) }) } @@ -824,9 +824,9 @@ impl Parser { let lhs = self.prefix_expr()?; let ref next = self.token_handler.peek_kind(); Ok(if let Some(op) = BinOp::from_sigil_token(next) { - Discriminator::BinOp(lhs, op) + Discriminator::BinOp(lhs.into(), op) } else { - Discriminator::Simple(lhs) + Discriminator::Simple(lhs.into()) }) } diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index 4f2b64c..3ccabb3 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -209,7 +209,7 @@ fn parsing_functions() { parse_test!("oi()", AST(vec![exst!(Call { f: bx!(ex!(m val!("oi"))), arguments: vec![] })])); parse_test!("oi(a, 2 + 2)", AST(vec![exst!(Call { f: bx!(ex!(m val!("oi"))), - arguments: vec![inv!(ex!(val!("a"))).into(), inv!(ex!(binexp!("+", NatLiteral(2), NatLiteral(2)))).into()] + arguments: vec![inv!(ex!(m val!("a"))).into(), inv!(ex!(m binexp!("+", NatLiteral(2), NatLiteral(2)))).into()] })])); parse_error!("a(b,,c)"); @@ -255,7 +255,7 @@ fn functions_with_default_args() { Meta::new(Declaration( FuncDecl(Signature { name: rc!(func), operator: false, type_anno: None, params: vec![ FormalParam { name: rc!(x), default: None, anno: Some(ty!("Int")) }, - FormalParam { name: rc!(y), default: Some(ex!(s "4")), anno: Some(ty!("Int")) } + FormalParam { name: rc!(y), default: Some(Meta::new(ex!(s "4"))), anno: Some(ty!("Int")) } ]}, vec![]) )) ]) @@ -323,7 +323,7 @@ fn parsing_block_expressions() { "if a() then { b(); c() }", AST(vec![exst!( IfExpression { discriminator: bx! { - Discriminator::Simple(ex!(Call { f: bx!(ex!(m val!("a"))), arguments: vec![]})) + Discriminator::Simple(ex!(m Call { f: bx!(ex!(m val!("a"))), arguments: vec![]})) }, body: bx! { IfExpressionBody::SimpleConditional( @@ -339,7 +339,7 @@ fn parsing_block_expressions() { "if a() then { b(); c() } else { q }", AST(vec![exst!( IfExpression { discriminator: bx! { - Discriminator::Simple(ex!(Call { f: bx!(ex!(m val!("a"))), arguments: vec![]})) + Discriminator::Simple(ex!(m Call { f: bx!(ex!(m val!("a"))), arguments: vec![]})) }, body: bx! { IfExpressionBody::SimpleConditional( @@ -500,7 +500,7 @@ fn parsing_lambdas() { type_anno: None, body: vec![exst!(s "y")] } )), - arguments: vec![inv!(ex!(NatLiteral(1))).into()] })])); + arguments: vec![inv!(ex!(m NatLiteral(1))).into()] })])); parse_test_wrap_ast! { r#"\(x: Int): String { "q" }"#, @@ -544,7 +544,7 @@ fn more_advanced_lambdas() { exst! { Call { f: bx!(ex!(m Call { f: bx!(ex!(m val!("wahoo"))), arguments: vec![] })), - arguments: vec![inv!(ex!(NatLiteral(3))).into()], + arguments: vec![inv!(ex!(m NatLiteral(3))).into()], } } ]) @@ -595,7 +595,7 @@ fn patterns() { parse_test_wrap_ast! { "if x is Some(a) then { 4 } else { 9 }", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) } ) } @@ -603,7 +603,7 @@ fn patterns() { parse_test_wrap_ast! { "if x is Some(a) then 4 else 9", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) } ) } @@ -611,7 +611,7 @@ fn patterns() { parse_test_wrap_ast! { "if x is Something { a, b: x } then { 4 } else { 9 }", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), body: bx!(IfExpressionBody::SimplePatternMatch( Pattern::Record(rc!(Something), vec![ (rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))), @@ -629,7 +629,7 @@ fn pattern_literals() { "if x is -1 then 1 else 2", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), body: bx!(IfExpressionBody::SimplePatternMatch( Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: NatLiteral(1) }), vec![exst!(NatLiteral(1))], @@ -643,7 +643,7 @@ fn pattern_literals() { "if x is 1 then 1 else 2", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), body: bx!(IfExpressionBody::SimplePatternMatch( Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1) }), vec![exst!(s "1")], @@ -657,7 +657,7 @@ fn pattern_literals() { "if x is true then 1 else 2", AST(vec![ exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), body: bx!(IfExpressionBody::SimplePatternMatch( Pattern::Literal(PatternLiteral::BoolPattern(true)), vec![exst!(NatLiteral(1))], @@ -672,7 +672,7 @@ fn pattern_literals() { "if x is \"gnosticism\" then 1 else 2", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), body: bx!(IfExpressionBody::SimplePatternMatch( Pattern::Literal(PatternLiteral::StringPattern(rc!(gnosticism))), vec![exst!(s "1")], diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index bf1153d..79b7212 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -135,7 +135,7 @@ impl InvocationArgument { fn reduce(&self, symbol_table: &SymbolTable) -> Expr { use crate::ast::InvocationArgument::*; match self { - Positional(ex) => ex.reduce(symbol_table), + Positional(ex) => ex.node().reduce(symbol_table), Keyword { .. } => Expr::UnimplementedSigilValue, Ignored => Expr::UnimplementedSigilValue, } @@ -237,7 +237,7 @@ fn reduce_call_expression(func: &Meta, arguments: &Vec Expr { let cond = Box::new(match *discriminator { - Discriminator::Simple(ref expr) => expr.reduce(symbol_table), + Discriminator::Simple(ref expr) => expr.node().reduce(symbol_table), Discriminator::BinOp(ref _expr, ref _binop) => panic!("Can't yet handle binop discriminators") }); match *body { diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index 88b8fa4..fb45c8f 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -291,7 +291,7 @@ impl<'a> TypeContext<'a> { fn invoc(&mut self, invoc: &InvocationArgument) -> InferResult { use InvocationArgument::*; match invoc { - Positional(expr) => self.expr(expr), + Positional(expr) => self.expr(expr.node()), _ => Ok(ty!(Nat)) //TODO this is wrong } } @@ -349,7 +349,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, then_clause, else_clause), + (Simple(expr), SimpleConditional(then_clause, else_clause)) => self.handle_simple_if(expr.node(), then_clause, else_clause), _ => TypeError::new(format!("Complex conditionals not supported")) } }