about summary refs log tree commit diff
path: root/src/parser/type_.rs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2021-03-14T20·43-0400
committerGriffin Smith <root@gws.fyi>2021-03-14T20·43-0400
commitecb4c0f803e9b408e4fd21c475769eb4dc649d14 (patch)
tree80390b00a6009cea21fbb68cbf56e6a193b478a2 /src/parser/type_.rs
parent7960c3270e1a338f4da40d044a6896df96d82c79 (diff)
Universally quantified type variables
Implement universally quantified type variables, both explicitly given
by the user and inferred by the type inference algorithm.
Diffstat (limited to 'src/parser/type_.rs')
-rw-r--r--src/parser/type_.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/parser/type_.rs b/src/parser/type_.rs
index 66b4f9f72c23..c90ceda4d72e 100644
--- a/src/parser/type_.rs
+++ b/src/parser/type_.rs
@@ -1,6 +1,7 @@
 use nom::character::complete::{multispace0, multispace1};
 use nom::{alt, delimited, do_parse, map, named, opt, separated_list0, tag, terminated, tuple};
 
+use super::ident;
 use crate::ast::{FunctionType, Type};
 
 named!(function_type(&str) -> Type, do_parse!(
@@ -29,6 +30,7 @@ named!(pub type_(&str) -> Type, alt!(
     tag!("bool") => { |_| Type::Bool } |
     tag!("cstring") => { |_| Type::CString } |
     function_type |
+    ident => { |id| Type::Var(id) } |
     delimited!(
         tuple!(tag!("("), multispace0),
         type_,
@@ -38,7 +40,10 @@ named!(pub type_(&str) -> Type, alt!(
 
 #[cfg(test)]
 mod tests {
+    use std::convert::TryFrom;
+
     use super::*;
+    use crate::ast::Ident;
 
     #[test]
     fn simple_types() {
@@ -103,4 +108,18 @@ mod tests {
             })
         )
     }
+
+    #[test]
+    fn type_vars() {
+        assert_eq!(
+            test_parse!(type_, "fn x, y -> x"),
+            Type::Function(FunctionType {
+                args: vec![
+                    Type::Var(Ident::try_from("x").unwrap()),
+                    Type::Var(Ident::try_from("y").unwrap()),
+                ],
+                ret: Box::new(Type::Var(Ident::try_from("x").unwrap())),
+            })
+        )
+    }
 }