aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdvard Thörnros <edvard.thornros@gmail.com>2021-03-08 23:05:00 +0100
committerEdvard Thörnros <edvard.thornros@gmail.com>2021-03-08 23:05:00 +0100
commit8a5202b02d909265fc16ddaf4f74112f32dc0b35 (patch)
tree44c7f66b0c190d70ea4c99d1e7c69deb9f28d386 /src
parent4e6ef21576d9ec6a8861246464b1905819b68efe (diff)
downloadsylt-8a5202b02d909265fc16ddaf4f74112f32dc0b35.tar.gz
syntax for union types
Diffstat (limited to 'src')
-rw-r--r--src/compiler.rs27
-rw-r--r--src/tokenizer.rs2
2 files changed, 24 insertions, 5 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index 4fdd13b..22256e4 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -1403,12 +1403,29 @@ impl Compiler {
}
fn parse_type(&mut self) -> Result<Type, ()> {
- let ty = self.parse_simple_type();
- if self.peek() == Token::Questionmark {
- self.eat();
- ty.map(|x| Type::Union(vec![x, Type::Void]))
+ let mut tys = vec![self.parse_simple_type()?];
+ loop {
+ match self.peek() {
+ Token::Questionmark => {
+ self.eat();
+ tys.push(Type::Void);
+ return Ok(Type::Union(tys));
+ },
+
+ Token::Pipe => {
+ self.eat();
+ tys.push(self.parse_simple_type()?);
+ },
+
+ _ => {
+ break;
+ },
+ }
+ }
+ if tys.len() == 1 {
+ Ok(tys[0].clone())
} else {
- ty
+ Ok(Type::Union(tys))
}
}
diff --git a/src/tokenizer.rs b/src/tokenizer.rs
index 664532a..4d1280d 100644
--- a/src/tokenizer.rs
+++ b/src/tokenizer.rs
@@ -122,6 +122,8 @@ pub enum Token {
Bang,
#[token("?")]
Questionmark,
+ #[token("|")]
+ Pipe,
#[token(",")]
Comma,