about summary refs log tree commit diff
path: root/users/glittershark/achilles/src/ast
diff options
context:
space:
mode:
authorGriffin Smith <grfn@gws.fyi>2021-03-28T17·28-0400
committerglittershark <grfn@gws.fyi>2021-03-28T17·33+0000
commit8e13b1303a0d152c2f3b68f2421163e94fdf226c (patch)
tree0cac0fee2cd52f912bb118cff78fdca9d28ac895 /users/glittershark/achilles/src/ast
parentdb62866d820cf524b67cebe34033d3928804cf3c (diff)
feat(achilles): Implement a Unit type r/2356
Add support for a zero-sized Unit type. This requires some special at
the codegen level because LLVM (unsurprisingly) only allows Void types
in function return position - to make that a little easier to handle
there's a new pass that strips any unit-only expressions and pulls
unit-only function arguments up to new `let` bindings, so we never have
to actually pass around unit values.

Change-Id: I0fc18a516821f2d69172c42a6a5d246b23471e38
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2695
Reviewed-by: glittershark <grfn@gws.fyi>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/glittershark/achilles/src/ast')
-rw-r--r--users/glittershark/achilles/src/ast/hir.rs2
-rw-r--r--users/glittershark/achilles/src/ast/mod.rs7
2 files changed, 8 insertions, 1 deletions
diff --git a/users/glittershark/achilles/src/ast/hir.rs b/users/glittershark/achilles/src/ast/hir.rs
index 8726af509388..0d145d620bef 100644
--- a/users/glittershark/achilles/src/ast/hir.rs
+++ b/users/glittershark/achilles/src/ast/hir.rs
@@ -246,7 +246,7 @@ impl<'a, T> Expr<'a, T> {
     }
 }
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, PartialEq, Eq)]
 pub enum Decl<'a, T> {
     Fun {
         name: Ident<'a>,
diff --git a/users/glittershark/achilles/src/ast/mod.rs b/users/glittershark/achilles/src/ast/mod.rs
index 53f222a6a11a..7dc2de895709 100644
--- a/users/glittershark/achilles/src/ast/mod.rs
+++ b/users/glittershark/achilles/src/ast/mod.rs
@@ -30,6 +30,7 @@ impl<'a> Ident<'a> {
         Ident(Cow::Owned(self.0.clone().into_owned()))
     }
 
+    /// Construct an identifier from a &str without checking that it's a valid identifier
     pub fn from_str_unchecked(s: &'a str) -> Self {
         debug_assert!(is_valid_identifier(s));
         Self(Cow::Borrowed(s))
@@ -109,6 +110,7 @@ pub enum UnaryOperator {
 
 #[derive(Debug, PartialEq, Eq, Clone)]
 pub enum Literal<'a> {
+    Unit,
     Int(u64),
     Bool(bool),
     String(Cow<'a, str>),
@@ -120,6 +122,7 @@ impl<'a> Literal<'a> {
             Literal::Int(i) => Literal::Int(*i),
             Literal::Bool(b) => Literal::Bool(*b),
             Literal::String(s) => Literal::String(Cow::Owned(s.clone().into_owned())),
+            Literal::Unit => Literal::Unit,
         }
     }
 }
@@ -308,6 +311,7 @@ pub enum Type<'a> {
     Float,
     Bool,
     CString,
+    Unit,
     Var(Ident<'a>),
     Function(FunctionType<'a>),
 }
@@ -319,6 +323,7 @@ impl<'a> Type<'a> {
             Type::Float => Type::Float,
             Type::Bool => Type::Bool,
             Type::CString => Type::CString,
+            Type::Unit => Type::Unit,
             Type::Var(v) => Type::Var(v.to_owned()),
             Type::Function(f) => Type::Function(f.to_owned()),
         }
@@ -374,6 +379,7 @@ impl<'a> Type<'a> {
             Type::Float => Type::Float,
             Type::Bool => Type::Bool,
             Type::CString => Type::CString,
+            Type::Unit => Type::Unit,
         }
     }
 }
@@ -385,6 +391,7 @@ impl<'a> Display for Type<'a> {
             Type::Float => f.write_str("float"),
             Type::Bool => f.write_str("bool"),
             Type::CString => f.write_str("cstring"),
+            Type::Unit => f.write_str("()"),
             Type::Var(v) => v.fmt(f),
             Type::Function(ft) => ft.fmt(f),
         }