From 92f18d8f4278a6e6322c4162f78494762ba7cbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 13 Jan 2021 14:46:10 +0100 Subject: Change the assignment syntax --- src/compiler.rs | 17 +++++++++++++---- src/tokenizer.rs | 3 +++ tests/if.tdy | 4 ++-- tests/scoping.tdy | 6 +++--- tests/variables.tdy | 15 +++++++++++---- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index b7e946c..c59de4d 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -446,6 +446,15 @@ impl Compiler { self.stack.truncate(h); } + fn type_ident(&mut self) -> Result { + if let Token::Identifier(typ) = self.peek() { + self.eat(); + Type::try_from(typ.as_ref()) + } else { + Err(()) + } + } + fn statement(&mut self, block: &mut Block) { self.clear_panic(); @@ -462,14 +471,14 @@ impl Compiler { block.add(Op::Print, self.line()); }, - tokens!(Token::Identifier(name), Token::Identifier(typ), Token::ColonEqual) => { - self.eat(); + tokens!(Token::Identifier(name), Token::Colon) => { self.eat(); self.eat(); - if let Ok(typ) = Type::try_from(typ.as_ref()) { + if let Ok(typ) = self.type_ident() { + expect!(self, Token::Equal, "Expected assignment."); self.define_variable(&name, typ, block); } else { - error!(self, format!("Failed to parse type '{}'.", typ)); + error!(self, format!("Expected type found '{:?}'.", self.peek())); } } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 62397c5..2e28cd2 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -96,6 +96,9 @@ pub enum Token { #[token("<=")] LessEqual, + #[token("|")] + Pipe, + #[token("&&")] And, #[token("||")] diff --git a/tests/if.tdy b/tests/if.tdy index 196e221..be3e3ca 100644 --- a/tests/if.tdy +++ b/tests/if.tdy @@ -1,5 +1,5 @@ -a int := 0 -res int := 0 +a : int = 0 +res : int = 0 if 1 == 2 { diff --git a/tests/scoping.tdy b/tests/scoping.tdy index 6424985..7679948 100644 --- a/tests/scoping.tdy +++ b/tests/scoping.tdy @@ -1,13 +1,13 @@ -a int := 1 +a : int = 1 { a <=> 1 - a int := a + a + a : int = a + a a <=> 2 } a <=> 1 { a = 2 - a int := 1 + a : int = 1 } a <=> 2 diff --git a/tests/variables.tdy b/tests/variables.tdy index 62b30d2..74d8cdd 100644 --- a/tests/variables.tdy +++ b/tests/variables.tdy @@ -1,9 +1,9 @@ // a variable -a int := 1 +a : int = 1 a <=> 1 // another variable -b int := 2 +b : int = 2 b <=> 2 // assignment @@ -11,7 +11,14 @@ a = b a <=> 2 // ordering -c int := 3 -d int := 4 +c : int = 3 +d : int = 4 c <=> 3 d <=> 4 + +// No types + +e := 1 +f := e + 1 +e <=> 1 +f <=> 2 -- cgit v1.2.1