Rename Node -> Meta

This commit is contained in:
greg 2019-02-21 01:49:15 -08:00
parent 9bb3a2be88
commit b35262c444
3 changed files with 49 additions and 49 deletions

View File

@ -5,15 +5,15 @@ use crate::builtin::{BinOp, PrefixOp};
use crate::typechecking::TypeData; use crate::typechecking::TypeData;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Node<T> { pub struct Meta<T> {
n: T, n: T,
source_map: SourceMap, source_map: SourceMap,
type_data: TypeData, type_data: TypeData,
} }
impl<T> Node<T> { impl<T> Meta<T> {
pub fn new(n: T) -> Node<T> { pub fn new(n: T) -> Meta<T> {
Node { n, source_map: SourceMap::default(), type_data: TypeData::new() } Meta { n, source_map: SourceMap::default(), type_data: TypeData::new() }
} }
pub fn node(&self) -> &T { pub fn node(&self) -> &T {
@ -26,22 +26,22 @@ impl<T> Node<T> {
struct SourceMap { struct SourceMap {
} }
impl From<Expression> for Node<Expression> { impl From<Expression> for Meta<Expression> {
fn from(expr: Expression) -> Node<Expression> { fn from(expr: Expression) -> Meta<Expression> {
Node { n: expr, source_map: SourceMap::default(), type_data: TypeData::new() } Meta { n: expr, source_map: SourceMap::default(), type_data: TypeData::new() }
} }
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct AST(pub Vec<Node<Statement>>); pub struct AST(pub Vec<Meta<Statement>>);
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum Statement { pub enum Statement {
ExpressionStatement(Node<Expression>), ExpressionStatement(Meta<Expression>),
Declaration(Declaration), Declaration(Declaration),
} }
pub type Block = Vec<Node<Statement>>; pub type Block = Vec<Meta<Statement>>;
pub type ParamName = Rc<String>; pub type ParamName = Rc<String>;
pub type FormalParam = (ParamName, Option<TypeIdentifier>); pub type FormalParam = (ParamName, Option<TypeIdentifier>);
@ -115,9 +115,9 @@ pub enum ExpressionKind {
FloatLiteral(f64), FloatLiteral(f64),
StringLiteral(Rc<String>), StringLiteral(Rc<String>),
BoolLiteral(bool), BoolLiteral(bool),
BinExp(BinOp, Box<Node<Expression>>, Box<Node<Expression>>), BinExp(BinOp, Box<Meta<Expression>>, Box<Meta<Expression>>),
PrefixExp(PrefixOp, Box<Node<Expression>>), PrefixExp(PrefixOp, Box<Meta<Expression>>),
TupleLiteral(Vec<Node<Expression>>), TupleLiteral(Vec<Meta<Expression>>),
Value(Rc<String>), Value(Rc<String>),
NamedStruct { NamedStruct {
name: Rc<String>, name: Rc<String>,
@ -125,7 +125,7 @@ pub enum ExpressionKind {
}, },
Call { Call {
f: Box<Expression>, f: Box<Expression>,
arguments: Vec<Node<Expression>>, arguments: Vec<Meta<Expression>>,
}, },
Index { Index {
indexee: Box<Expression>, indexee: Box<Expression>,

View File

@ -288,7 +288,7 @@ impl Parser {
continue; continue;
}, },
_ => statements.push( _ => statements.push(
Node::new(self.statement()?) Meta::new(self.statement()?)
), ),
} }
} }
@ -413,9 +413,9 @@ impl Parser {
} }
#[recursive_descent_method] #[recursive_descent_method]
fn nonempty_func_body(&mut self) -> ParseResult<Vec<Node<Statement>>> { fn nonempty_func_body(&mut self) -> ParseResult<Vec<Meta<Statement>>> {
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict); let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
Ok(statements.into_iter().map(|s| Node::new(s)).collect()) Ok(statements.into_iter().map(|s| Meta::new(s)).collect())
} }
#[recursive_descent_method] #[recursive_descent_method]
@ -589,7 +589,7 @@ impl Parser {
let mut expr = self.index_expr()?; let mut expr = self.index_expr()?;
while let LParen = self.token_handler.peek_kind() { while let LParen = self.token_handler.peek_kind() {
let arguments = delimited!(self, LParen, expression, Comma, RParen); let arguments = delimited!(self, LParen, expression, Comma, RParen);
let arguments = arguments.into_iter().map(|s| Node::new(s)).collect(); let arguments = arguments.into_iter().map(|s| Meta::new(s)).collect();
expr = Expression(ExpressionKind::Call { f: bx!(expr), arguments }, None); //TODO none is incorrect expr = Expression(ExpressionKind::Call { f: bx!(expr), arguments }, None); //TODO none is incorrect
} }
@ -669,7 +669,7 @@ impl Parser {
0 => Ok(Expression(TupleLiteral(vec![]), None)), 0 => Ok(Expression(TupleLiteral(vec![]), None)),
1 => Ok(inner.pop().unwrap()), 1 => Ok(inner.pop().unwrap()),
_ => { _ => {
let inner: Vec<Node<Expression>> = inner.into_iter().map(|ex| ex.into()).collect(); let inner: Vec<Meta<Expression>> = inner.into_iter().map(|ex| ex.into()).collect();
Ok(Expression(TupleLiteral(inner), None)) Ok(Expression(TupleLiteral(inner), None))
} }
} }
@ -904,7 +904,7 @@ impl Parser {
#[recursive_descent_method] #[recursive_descent_method]
fn block(&mut self) -> ParseResult<Block> { fn block(&mut self) -> ParseResult<Block> {
let block = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict); let block = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
Ok(block.into_iter().map(|s| { Node::new(s) }).collect()) Ok(block.into_iter().map(|s| { Meta::new(s) }).collect())
} }
#[recursive_descent_method] #[recursive_descent_method]
@ -913,7 +913,7 @@ impl Parser {
LCurlyBrace => self.block(), LCurlyBrace => self.block(),
_ => { _ => {
let expr = self.expression()?; let expr = self.expression()?;
Ok(vec![Node::new(Statement::ExpressionStatement(expr.into()))]) Ok(vec![Meta::new(Statement::ExpressionStatement(expr.into()))])
} }
} }
} }
@ -973,7 +973,7 @@ impl Parser {
Ok(match tok.get_kind() { Ok(match tok.get_kind() {
LCurlyBrace => { LCurlyBrace => {
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict); let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
StatementBlock(statements.into_iter().map(|s| Node::new(s)).collect()) StatementBlock(statements.into_iter().map(|s| Meta::new(s)).collect())
}, },
Keyword(Kw::Return) => { Keyword(Kw::Return) => {
self.token_handler.next(); self.token_handler.next();
@ -1117,7 +1117,7 @@ mod parse_tests {
use super::tokenize; use super::tokenize;
use super::ParseResult; use super::ParseResult;
use crate::builtin::{PrefixOp, BinOp}; use crate::builtin::{PrefixOp, BinOp};
use crate::ast::{AST, Node, Expression, Statement, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody}; use crate::ast::{AST, Meta, Expression, Statement, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody};
use super::Statement::*; use super::Statement::*;
use super::Declaration::*; use super::Declaration::*;
use super::Signature; use super::Signature;
@ -1173,14 +1173,14 @@ mod parse_tests {
($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression($lhs, None).into())) } ($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression($lhs, None).into())) }
} }
macro_rules! exst { macro_rules! exst {
($expr_type:expr) => { Node::new(Statement::ExpressionStatement(Expression($expr_type, None).into())) }; ($expr_type:expr) => { Meta::new(Statement::ExpressionStatement(Expression($expr_type, None).into())) };
($expr_type:expr, $type_anno:expr) => { Node::new(Statement::ExpressionStatement(Expression($expr_type, Some($type_anno)).into())) }; ($expr_type:expr, $type_anno:expr) => { Meta::new(Statement::ExpressionStatement(Expression($expr_type, Some($type_anno)).into())) };
($op:expr, $lhs:expr, $rhs:expr) => { Node::new(Statement::ExpressionStatement(ex!(binexp!($op, $lhs, $rhs)))) }; ($op:expr, $lhs:expr, $rhs:expr) => { Meta::new(Statement::ExpressionStatement(ex!(binexp!($op, $lhs, $rhs)))) };
(s $statement_text:expr) => { (s $statement_text:expr) => {
{ {
let tokens: Vec<crate::tokenizing::Token> = tokenize($statement_text); let tokens: Vec<crate::tokenizing::Token> = tokenize($statement_text);
let mut parser = super::Parser::new(tokens); let mut parser = super::Parser::new(tokens);
Node::new(parser.statement().unwrap()) Meta::new(parser.statement().unwrap())
} }
} }
} }
@ -1286,7 +1286,7 @@ mod parse_tests {
#[test] #[test]
fn parsing_functions() { fn parsing_functions() {
parse_test!("fn oi()", AST(vec![Node::new(Declaration(FuncSig(Signature { name: rc!(oi), operator: false, params: vec![], type_anno: None })))])); parse_test!("fn oi()", AST(vec![Meta::new(Declaration(FuncSig(Signature { name: rc!(oi), operator: false, params: vec![], type_anno: None })))]));
parse_test!("oi()", AST(vec![exst!(Call { f: bx!(ex!(val!("oi"))), arguments: vec![] })])); parse_test!("oi()", AST(vec![exst!(Call { f: bx!(ex!(val!("oi"))), arguments: vec![] })]));
parse_test!("oi(a, 2 + 2)", AST(vec![exst!(Call parse_test!("oi(a, 2 + 2)", AST(vec![exst!(Call
{ f: bx!(ex!(val!("oi"))), { f: bx!(ex!(val!("oi"))),
@ -1294,16 +1294,16 @@ mod parse_tests {
})])); })]));
parse_error!("a(b,,c)"); parse_error!("a(b,,c)");
parse_test!("fn a(b, c: Int): Int", AST(vec![Node::new(Declaration( parse_test!("fn a(b, c: Int): Int", AST(vec![Meta::new(Declaration(
FuncSig(Signature { name: rc!(a), operator: false, params: vec![ FuncSig(Signature { name: rc!(a), operator: false, params: vec![
(rc!(b), None), (rc!(c), Some(ty!("Int"))) (rc!(b), None), (rc!(c), Some(ty!("Int")))
], type_anno: Some(ty!("Int")) })))])); ], type_anno: Some(ty!("Int")) })))]));
parse_test!("fn a(x) { x() }", AST(vec![Node::new(Declaration( parse_test!("fn a(x) { x() }", AST(vec![Meta::new(Declaration(
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None }, FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])))])); vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])))]));
parse_test!("fn a(x) {\n x() }", AST(vec![Node::new(Declaration( parse_test!("fn a(x) {\n x() }", AST(vec![Meta::new(Declaration(
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None }, FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])))])); vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])))]));
@ -1312,7 +1312,7 @@ fn a(x) {
x() x()
} }
"#; "#;
parse_test!(multiline, AST(vec![Node::new(Declaration( parse_test!(multiline, AST(vec![Meta::new(Declaration(
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None }, FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])))])); vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])))]));
let multiline2 = r#" let multiline2 = r#"
@ -1322,7 +1322,7 @@ fn a(x) {
} }
"#; "#;
parse_test!(multiline2, AST(vec![Node::new(Declaration( parse_test!(multiline2, AST(vec![Meta::new(Declaration(
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None }, FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
vec![exst!(s "x()")])))])); vec![exst!(s "x()")])))]));
} }
@ -1340,11 +1340,11 @@ fn a(x) {
#[test] #[test]
fn parsing_types() { fn parsing_types() {
parse_test!("type Yolo = Yolo", AST(vec![Node::new(Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: false} ))])); parse_test!("type Yolo = Yolo", AST(vec![Meta::new(Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: false} ))]));
parse_test!("type mut Yolo = Yolo", AST(vec![Node::new(Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: true} ))])); parse_test!("type mut Yolo = Yolo", AST(vec![Meta::new(Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: true} ))]));
parse_test!("type alias Sex = Drugs", AST(vec![Node::new(Declaration(TypeAlias(rc!(Sex), rc!(Drugs))))])); parse_test!("type alias Sex = Drugs", AST(vec![Meta::new(Declaration(TypeAlias(rc!(Sex), rc!(Drugs))))]));
parse_test!("type Sanchez = Miguel | Alejandro(Int, Option<a>) | Esperanza { a: Int, b: String }", parse_test!("type Sanchez = Miguel | Alejandro(Int, Option<a>) | Esperanza { a: Int, b: String }",
AST(vec![Node::new(Declaration(TypeDecl{ AST(vec![Meta::new(Declaration(TypeDecl{
name: tys!("Sanchez"), name: tys!("Sanchez"),
body: TypeBody(vec![ body: TypeBody(vec![
UnitStruct(rc!(Miguel)), UnitStruct(rc!(Miguel)),
@ -1364,7 +1364,7 @@ fn a(x) {
}))])); }))]));
parse_test!("type Jorge<a> = Diego | Kike(a)", AST(vec![ parse_test!("type Jorge<a> = Diego | Kike(a)", AST(vec![
Node::new(Declaration(TypeDecl{ Meta::new(Declaration(TypeDecl{
name: TypeSingletonName { name: rc!(Jorge), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] }, name: TypeSingletonName { name: rc!(Jorge), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] },
body: TypeBody(vec![UnitStruct(rc!(Diego)), TupleStruct(rc!(Kike), vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })])]), body: TypeBody(vec![UnitStruct(rc!(Diego)), TupleStruct(rc!(Kike), vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })])]),
mutable: false mutable: false
@ -1374,9 +1374,9 @@ fn a(x) {
#[test] #[test]
fn parsing_bindings() { fn parsing_bindings() {
parse_test!("let mut a = 10", AST(vec![Node::new(Declaration(Binding { name: rc!(a), constant: false, type_anno: None, expr: ex!(NatLiteral(10)) } ))])); parse_test!("let mut a = 10", AST(vec![Meta::new(Declaration(Binding { name: rc!(a), constant: false, type_anno: None, expr: ex!(NatLiteral(10)) } ))]));
parse_test!("let a = 2 + 2", AST(vec![Node::new(Declaration(Binding { name: rc!(a), constant: true, type_anno: None, expr: ex!(binexp!("+", NatLiteral(2), NatLiteral(2))) }) )])); parse_test!("let a = 2 + 2", AST(vec![Meta::new(Declaration(Binding { name: rc!(a), constant: true, type_anno: None, expr: ex!(binexp!("+", NatLiteral(2), NatLiteral(2))) }) )]));
parse_test!("let a: Nat = 2 + 2", AST(vec![Node::new(Declaration( parse_test!("let a: Nat = 2 + 2", AST(vec![Meta::new(Declaration(
Binding { name: rc!(a), constant: true, type_anno: Some(Singleton(TypeSingletonName { name: rc!(Nat), params: vec![] })), Binding { name: rc!(a), constant: true, type_anno: Some(Singleton(TypeSingletonName { name: rc!(Nat), params: vec![] })),
expr: ex!(binexp!("+", NatLiteral(2), NatLiteral(2))) } expr: ex!(binexp!("+", NatLiteral(2), NatLiteral(2))) }
))])); ))]));
@ -1453,7 +1453,7 @@ fn a(x) {
#[test] #[test]
fn parsing_interfaces() { fn parsing_interfaces() {
parse_test!("interface Unglueable { fn unglue(a: Glue); fn mar(): Glue }", AST(vec![ parse_test!("interface Unglueable { fn unglue(a: Glue); fn mar(): Glue }", AST(vec![
Node::new(Declaration(Interface { Meta::new(Declaration(Interface {
name: rc!(Unglueable), name: rc!(Unglueable),
signatures: vec![ signatures: vec![
Signature { name: rc!(unglue), operator: false, params: vec![(rc!(a), Some(Singleton(TypeSingletonName { name: rc!(Glue), params: vec![] })))], type_anno: None }, Signature { name: rc!(unglue), operator: false, params: vec![(rc!(a), Some(Singleton(TypeSingletonName { name: rc!(Glue), params: vec![] })))], type_anno: None },
@ -1466,7 +1466,7 @@ fn a(x) {
#[test] #[test]
fn parsing_impls() { fn parsing_impls() {
parse_test!("impl Heh { fn yolo(); fn swagg(); }", AST(vec![ parse_test!("impl Heh { fn yolo(); fn swagg(); }", AST(vec![
Node::new( Meta::new(
Declaration(Impl { Declaration(Impl {
type_name: ty!("Heh"), type_name: ty!("Heh"),
interface_name: None, interface_name: None,
@ -1476,7 +1476,7 @@ fn a(x) {
] }))])); ] }))]));
parse_test!("impl Mondai for Lollerino { fn yolo(); fn swagg(); }", AST(vec![ parse_test!("impl Mondai for Lollerino { fn yolo(); fn swagg(); }", AST(vec![
Node::new(Declaration(Impl { Meta::new(Declaration(Impl {
type_name: ty!("Lollerino"), type_name: ty!("Lollerino"),
interface_name: Some(TypeSingletonName { name: rc!(Mondai), params: vec![] }), interface_name: Some(TypeSingletonName { name: rc!(Mondai), params: vec![] }),
block: vec![ block: vec![
@ -1485,7 +1485,7 @@ fn a(x) {
] }))])); ] }))]));
parse_test!("impl Hella<T> for (Alpha, Omega) { }", AST(vec![ parse_test!("impl Hella<T> for (Alpha, Omega) { }", AST(vec![
Node::new(Declaration(Impl { Meta::new(Declaration(Impl {
type_name: Tuple(vec![ty!("Alpha"), ty!("Omega")]), type_name: Tuple(vec![ty!("Alpha"), ty!("Omega")]),
interface_name: Some(TypeSingletonName { name: rc!(Hella), params: vec![ty!("T")] }), interface_name: Some(TypeSingletonName { name: rc!(Hella), params: vec![ty!("T")] }),
block: vec![] block: vec![]
@ -1493,7 +1493,7 @@ fn a(x) {
])); ]));
parse_test!("impl Option<WTFMate> { fn oi() }", AST(vec![ parse_test!("impl Option<WTFMate> { fn oi() }", AST(vec![
Node::new( Meta::new(
Declaration(Impl { Declaration(Impl {
type_name: Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("WTFMate")]}), type_name: Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("WTFMate")]}),
interface_name: None, interface_name: None,
@ -1506,7 +1506,7 @@ fn a(x) {
#[test] #[test]
fn parsing_type_annotations() { fn parsing_type_annotations() {
parse_test!("let a = b : Int", AST(vec![ parse_test!("let a = b : Int", AST(vec![
Node::new( Meta::new(
Declaration(Binding { name: rc!(a), constant: true, type_anno: None, expr: Declaration(Binding { name: rc!(a), constant: true, type_anno: None, expr:
Expression(val!("b"), Some(ty!("Int"))) }))])); Expression(val!("b"), Some(ty!("Int"))) }))]));

View File

@ -377,7 +377,7 @@ impl Declaration {
} }
impl BinOp { impl BinOp {
fn reduce(&self, symbol_table: &SymbolTable, lhs: &Box<Node<Expression>>, rhs: &Box<Node<Expression>>) -> Expr { fn reduce(&self, symbol_table: &SymbolTable, lhs: &Box<Meta<Expression>>, rhs: &Box<Meta<Expression>>) -> Expr {
if **self.sigil() == "=" { if **self.sigil() == "=" {
Expr::Assign { Expr::Assign {
val: Box::new(lhs.node().reduce(symbol_table)), val: Box::new(lhs.node().reduce(symbol_table)),
@ -391,7 +391,7 @@ impl BinOp {
} }
impl PrefixOp { impl PrefixOp {
fn reduce(&self, symbol_table: &SymbolTable, arg: &Box<Node<Expression>>) -> Expr { fn reduce(&self, symbol_table: &SymbolTable, arg: &Box<Meta<Expression>>) -> Expr {
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone()))); let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
Expr::Call { f, args: vec![arg.node().reduce(symbol_table)]} Expr::Call { f, args: vec![arg.node().reduce(symbol_table)]}
} }