From 840d820d0951dc38e266bd4d184be2cf8bf142e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 12 Jan 2021 18:47:49 +0100 Subject: peek tokens helper macro --- src/compiler.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/compiler.rs b/src/compiler.rs index 15d92fd..50e8ee4 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -378,14 +378,20 @@ impl Compiler { fn statement(&mut self, block: &mut Block) { self.clear_panic(); + macro_rules! tokens { + ($( $token:pat ),*) => { + ($( $token , )* ..) + }; + } + match self.peek_four() { - (Token::Print, _, _, _) => { + tokens!(Token::Print) => { self.eat(); self.expression(block); block.add(Op::Print, self.line()); }, - (Token::Identifier(name), Token::Identifier(typ), Token::ColonEqual, _) => { + tokens!(Token::Identifier(name), Token::Identifier(typ), Token::ColonEqual) => { self.eat(); self.eat(); self.eat(); @@ -396,32 +402,32 @@ impl Compiler { } } - (Token::Identifier(name), Token::ColonEqual, _, _) => { + tokens!(Token::Identifier(name), Token::ColonEqual) => { self.eat(); self.eat(); self.define_variable(&name, Type::UnkownType, block); } - (Token::Identifier(name), Token::Equal, _, _) => { + tokens!(Token::Identifier(name), Token::Equal) => { self.eat(); self.eat(); self.assign(&name, block); } - (Token::If, _, _, _) => { + tokens!(Token::If) => { self.if_statment(block); } - (Token::Unreachable, _, _, _) => { + tokens!(Token::Unreachable) => { self.eat(); block.add(Op::Unreachable, self.line()); } - (Token::LeftBrace, _, _, _) => { + tokens!(Token::LeftBrace) => { self.scope(block); } - (Token::Newline, _, _, _) => {} + tokens!(Token::Newline) => {} _ => { self.expression(block); -- cgit v1.2.1