Rename typename
to just ty
.
This commit is contained in:
parent
b7d15a2c3c
commit
59b28f7ce9
4 changed files with 36 additions and 35 deletions
|
@ -0,0 +1 @@
|
||||||
|
mod ir;
|
|
@ -7,7 +7,7 @@ mod integer;
|
||||||
mod local;
|
mod local;
|
||||||
mod string;
|
mod string;
|
||||||
mod term;
|
mod term;
|
||||||
mod typename;
|
mod ty;
|
||||||
mod unary;
|
mod unary;
|
||||||
|
|
||||||
pub use atom::Atom;
|
pub use atom::Atom;
|
||||||
|
@ -19,7 +19,7 @@ pub use integer::Integer;
|
||||||
pub use local::Local;
|
pub use local::Local;
|
||||||
pub use string::String;
|
pub use string::String;
|
||||||
pub use term::{NodeType, Term};
|
pub use term::{NodeType, Term};
|
||||||
pub use typename::Type;
|
pub use ty::Ty;
|
||||||
pub use unary::Unary;
|
pub use unary::Unary;
|
||||||
|
|
||||||
use crate::input_location::InputLocation;
|
use crate::input_location::InputLocation;
|
||||||
|
|
|
@ -23,7 +23,7 @@ pub enum NodeType {
|
||||||
Binary,
|
Binary,
|
||||||
Boolean,
|
Boolean,
|
||||||
Call,
|
Call,
|
||||||
Type,
|
Ty,
|
||||||
Constructor,
|
Constructor,
|
||||||
Declaration,
|
Declaration,
|
||||||
Definition,
|
Definition,
|
||||||
|
@ -43,8 +43,8 @@ enum Inner {
|
||||||
Binary(Binary, Box<Term>, Box<Term>),
|
Binary(Binary, Box<Term>, Box<Term>),
|
||||||
Boolean(Boolean),
|
Boolean(Boolean),
|
||||||
Call(Identifier, Vec<Term>),
|
Call(Identifier, Vec<Term>),
|
||||||
Type(Type),
|
Ty(Ty),
|
||||||
Constructor(Type, Vec<(Identifier, Term)>),
|
Constructor(Ty, Vec<(Identifier, Term)>),
|
||||||
Declaration(Identifier, Box<Term>),
|
Declaration(Identifier, Box<Term>),
|
||||||
Definition(
|
Definition(
|
||||||
Identifier,
|
Identifier,
|
||||||
|
@ -91,7 +91,7 @@ impl Term {
|
||||||
Inner::Boolean(_) => NodeType::Boolean,
|
Inner::Boolean(_) => NodeType::Boolean,
|
||||||
Inner::Binary(_, _, _) => NodeType::Binary,
|
Inner::Binary(_, _, _) => NodeType::Binary,
|
||||||
Inner::Call(_, _) => NodeType::Call,
|
Inner::Call(_, _) => NodeType::Call,
|
||||||
Inner::Type(_) => NodeType::Type,
|
Inner::Ty(_) => NodeType::Ty,
|
||||||
Inner::Constructor(_, _) => NodeType::Constructor,
|
Inner::Constructor(_, _) => NodeType::Constructor,
|
||||||
Inner::Definition(_, _, _, _, _) => NodeType::Definition,
|
Inner::Definition(_, _, _, _, _) => NodeType::Definition,
|
||||||
Inner::Declaration(_, _) => NodeType::Declaration,
|
Inner::Declaration(_, _) => NodeType::Declaration,
|
||||||
|
@ -140,16 +140,16 @@ impl Term {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn typename(&self) -> Option<&Type> {
|
pub fn ty(&self) -> Option<&Ty> {
|
||||||
match self.inner {
|
match self.inner {
|
||||||
Inner::Type(ref node) => Some(node),
|
Inner::Ty(ref node) => Some(node),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn constructor(&self) -> Option<(&Type, &Vec<(Identifier, Term)>)> {
|
pub fn constructor(&self) -> Option<(&Ty, &Vec<(Identifier, Term)>)> {
|
||||||
match self.inner {
|
match self.inner {
|
||||||
Inner::Constructor(ref typename, ref properties) => Some((typename, properties)),
|
Inner::Constructor(ref ty, ref properties) => Some((ty, properties)),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -282,11 +282,11 @@ impl<'a> From<Pair<'a, Rule>> for Term {
|
||||||
}
|
}
|
||||||
Rule::typename => Term {
|
Rule::typename => Term {
|
||||||
location: InputLocation::from(pair.clone()),
|
location: InputLocation::from(pair.clone()),
|
||||||
inner: Inner::Type(Type::from(pair)),
|
inner: Inner::Ty(Ty::from(pair)),
|
||||||
},
|
},
|
||||||
Rule::constructor => {
|
Rule::constructor => {
|
||||||
let mut inner = pair.clone().into_inner();
|
let mut inner = pair.clone().into_inner();
|
||||||
let typename = Type::from(inner.next().unwrap());
|
let ty = Ty::from(inner.next().unwrap());
|
||||||
let mut properties = Vec::new();
|
let mut properties = Vec::new();
|
||||||
|
|
||||||
for property in inner {
|
for property in inner {
|
||||||
|
@ -303,7 +303,7 @@ impl<'a> From<Pair<'a, Rule>> for Term {
|
||||||
|
|
||||||
Term {
|
Term {
|
||||||
location: InputLocation::from(pair),
|
location: InputLocation::from(pair),
|
||||||
inner: Inner::Constructor(typename, properties),
|
inner: Inner::Constructor(ty, properties),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Rule::declaration => {
|
Rule::declaration => {
|
||||||
|
@ -570,8 +570,8 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_typename() {
|
fn test_typename() {
|
||||||
let terms = Term::input("Marty").unwrap();
|
let terms = Term::input("Marty").unwrap();
|
||||||
assert_eq!(terms[0].node_type(), NodeType::Type);
|
assert_eq!(terms[0].node_type(), NodeType::Ty);
|
||||||
let term = terms[0].typename().unwrap().clone();
|
let term = terms[0].ty().unwrap().clone();
|
||||||
assert_eq!(term.value(), "Marty");
|
assert_eq!(term.value(), "Marty");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -579,16 +579,16 @@ mod test {
|
||||||
fn test_constructor_empty() {
|
fn test_constructor_empty() {
|
||||||
let terms = Term::input("Character {}").unwrap();
|
let terms = Term::input("Character {}").unwrap();
|
||||||
assert_eq!(terms[0].node_type(), NodeType::Constructor);
|
assert_eq!(terms[0].node_type(), NodeType::Constructor);
|
||||||
let (typename, properties) = terms[0].constructor().unwrap();
|
let (ty, properties) = terms[0].constructor().unwrap();
|
||||||
assert_eq!(typename.value_ref(), "Character");
|
assert_eq!(ty.value_ref(), "Character");
|
||||||
assert_eq!(properties.len(), 0);
|
assert_eq!(properties.len(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_constructor() {
|
fn test_constructor() {
|
||||||
let terms = Term::input("Character { name: \"Marty McFly\" }").unwrap();
|
let terms = Term::input("Character { name: \"Marty McFly\" }").unwrap();
|
||||||
let (typename, properties) = terms[0].constructor().unwrap();
|
let (ty, properties) = terms[0].constructor().unwrap();
|
||||||
assert_eq!(typename.value_ref(), "Character");
|
assert_eq!(ty.value_ref(), "Character");
|
||||||
assert_eq!(properties.len(), 1);
|
assert_eq!(properties.len(), 1);
|
||||||
let (key, value) = &properties[0];
|
let (key, value) = &properties[0];
|
||||||
assert_eq!(key.value_ref(), "name");
|
assert_eq!(key.value_ref(), "name");
|
||||||
|
@ -602,12 +602,12 @@ mod test {
|
||||||
|
|
||||||
let (deftype, defname, args, body, _reqs) = terms[0].definition().unwrap();
|
let (deftype, defname, args, body, _reqs) = terms[0].definition().unwrap();
|
||||||
assert_eq!(deftype.value_ref(), "type");
|
assert_eq!(deftype.value_ref(), "type");
|
||||||
assert_eq!(defname.typename().unwrap().value_ref(), "Delorean");
|
assert_eq!(defname.ty().unwrap().value_ref(), "Delorean");
|
||||||
|
|
||||||
let (argname, argvalue) = &args[0];
|
let (argname, argvalue) = &args[0];
|
||||||
assert_eq!(argname.value_ref(), "speed");
|
assert_eq!(argname.value_ref(), "speed");
|
||||||
|
|
||||||
assert_eq!(argvalue.typename().unwrap().value_ref(), "Integer");
|
assert_eq!(argvalue.ty().unwrap().value_ref(), "Integer");
|
||||||
|
|
||||||
let (argname, argvalues) = &body[0];
|
let (argname, argvalues) = &body[0];
|
||||||
assert_eq!(argname.value_ref(), "do");
|
assert_eq!(argname.value_ref(), "do");
|
||||||
|
@ -621,7 +621,7 @@ mod test {
|
||||||
let terms = Term::input("type Delorean").unwrap();
|
let terms = Term::input("type Delorean").unwrap();
|
||||||
let (deftype, defname, args, body, _reqs) = terms[0].definition().unwrap();
|
let (deftype, defname, args, body, _reqs) = terms[0].definition().unwrap();
|
||||||
assert_eq!(deftype.value_ref(), "type");
|
assert_eq!(deftype.value_ref(), "type");
|
||||||
assert_eq!(defname.typename().unwrap().value_ref(), "Delorean");
|
assert_eq!(defname.ty().unwrap().value_ref(), "Delorean");
|
||||||
assert_eq!(args.len(), 0);
|
assert_eq!(args.len(), 0);
|
||||||
assert_eq!(body.len(), 0);
|
assert_eq!(body.len(), 0);
|
||||||
}
|
}
|
||||||
|
@ -654,7 +654,7 @@ mod test {
|
||||||
assert_eq!(defname.atom().unwrap().value_ref(), "example");
|
assert_eq!(defname.atom().unwrap().value_ref(), "example");
|
||||||
assert_eq!(args.len(), 2);
|
assert_eq!(args.len(), 2);
|
||||||
assert_eq!(body.len(), 1);
|
assert_eq!(body.len(), 1);
|
||||||
assert_eq!(reqs.unwrap().typename().unwrap().value_ref(), "Integer");
|
assert_eq!(reqs.unwrap().ty().unwrap().value_ref(), "Integer");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -662,15 +662,15 @@ mod test {
|
||||||
let terms = Term::input("trait Integer: Add + Subtract").unwrap();
|
let terms = Term::input("trait Integer: Add + Subtract").unwrap();
|
||||||
let (deftype, defname, args, body, reqs_option) = terms[0].definition().unwrap();
|
let (deftype, defname, args, body, reqs_option) = terms[0].definition().unwrap();
|
||||||
assert_eq!(deftype.value_ref(), "trait");
|
assert_eq!(deftype.value_ref(), "trait");
|
||||||
assert_eq!(defname.typename().unwrap().value_ref(), "Integer");
|
assert_eq!(defname.ty().unwrap().value_ref(), "Integer");
|
||||||
assert_eq!(args.len(), 0);
|
assert_eq!(args.len(), 0);
|
||||||
assert_eq!(body.len(), 0);
|
assert_eq!(body.len(), 0);
|
||||||
|
|
||||||
let reqs = reqs_option.unwrap();
|
let reqs = reqs_option.unwrap();
|
||||||
let (op, lhs, rhs) = reqs.binary().unwrap();
|
let (op, lhs, rhs) = reqs.binary().unwrap();
|
||||||
assert_eq!(op.value_ref(), &binary::Operator::Plus);
|
assert_eq!(op.value_ref(), &binary::Operator::Plus);
|
||||||
assert_eq!(lhs.typename().unwrap().value_ref(), "Add");
|
assert_eq!(lhs.ty().unwrap().value_ref(), "Add");
|
||||||
assert_eq!(rhs.typename().unwrap().value_ref(), "Subtract");
|
assert_eq!(rhs.ty().unwrap().value_ref(), "Subtract");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -791,7 +791,7 @@ mod test {
|
||||||
let terms = Term::input("My.Greeter.hello()").unwrap();
|
let terms = Term::input("My.Greeter.hello()").unwrap();
|
||||||
let (method_name, receiver, arguments) = terms[0].method_call().unwrap();
|
let (method_name, receiver, arguments) = terms[0].method_call().unwrap();
|
||||||
assert_eq!(method_name.value_ref(), "hello");
|
assert_eq!(method_name.value_ref(), "hello");
|
||||||
assert_eq!(receiver.typename().unwrap().value_ref(), "My.Greeter");
|
assert_eq!(receiver.ty().unwrap().value_ref(), "My.Greeter");
|
||||||
assert_eq!(arguments.len(), 0);
|
assert_eq!(arguments.len(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,18 +4,18 @@ use crate::input_location::InputLocation;
|
||||||
use pest::iterators::Pair;
|
use pest::iterators::Pair;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Type {
|
pub struct Ty {
|
||||||
value: String,
|
value: String,
|
||||||
location: InputLocation,
|
location: InputLocation,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Type {
|
impl Ty {
|
||||||
pub fn value(&self) -> &str {
|
pub fn value(&self) -> &str {
|
||||||
&self.value
|
&self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value for Type {
|
impl Value for Ty {
|
||||||
type Item = String;
|
type Item = String;
|
||||||
|
|
||||||
fn value(self) -> Self::Item {
|
fn value(self) -> Self::Item {
|
||||||
|
@ -27,23 +27,23 @@ impl Value for Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Location for Type {
|
impl Location for Ty {
|
||||||
fn location(&self) -> &InputLocation {
|
fn location(&self) -> &InputLocation {
|
||||||
&self.location
|
&self.location
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<Pair<'a, Rule>> for Type {
|
impl<'a> From<Pair<'a, Rule>> for Ty {
|
||||||
fn from(pair: Pair<'a, Rule>) -> Self {
|
fn from(pair: Pair<'a, Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
match pair.as_rule() {
|
||||||
Rule::typename => {
|
Rule::typename => {
|
||||||
let value = pair.clone().into_span().as_str().to_string();
|
let value = pair.clone().into_span().as_str().to_string();
|
||||||
Type {
|
Ty {
|
||||||
value: value,
|
value: value,
|
||||||
location: InputLocation::from(pair.into_span()),
|
location: InputLocation::from(pair.into_span()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!("Expected pair to be a Type"),
|
_ => unreachable!("Expected pair to be a Ty"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ mod test {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.next()
|
.next()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let r#type = Type::from(pair);
|
let r#type = Ty::from(pair);
|
||||||
assert_eq!(r#type.value(), "Marty.McFly");
|
assert_eq!(r#type.value(), "Marty.McFly");
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue