From 498ea72da43a139ad9882d57af7b0aadbc32370c Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 4 Jun 2019 18:07:46 -0300 Subject: [PATCH] add support for ++ and += --- src/scanner.zig | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/scanner.zig b/src/scanner.zig index 0c74a70..48fd2f9 100644 --- a/src/scanner.zig +++ b/src/scanner.zig @@ -229,6 +229,23 @@ pub const Scanner = struct { ); } + fn makeTripleMatchToken( + self: *Scanner, + char1: u8, + ttype1: TokenType, + char2, + ttype2: TokenType, + fallback: TokenType, + ) Token { + if (self.match(char1)) { + return self.makeToken(ttype1); + } else if (self.match(char2)) { + return self.makeToken(ttype2); + } else { + return self.makeToken(fallback); + } + } + pub fn nextToken(self: *Scanner) !?Token { self.skipWhitespace(); self.start = self.current; @@ -255,26 +272,14 @@ pub const Scanner = struct { '$' => self.makeToken(.DollarSign), '-' => self.makeToken(.Minus), - '+' => self.makeToken(.Plus), '*' => self.makeToken(.Star), + '%' => self.makeToken(.Modulo), '!' => self.makeMatchToken('=', .BangEqual, .Bang), '=' => self.makeMatchToken('=', .EqualEqual, .Equal), - - // there can be three tokens from a < - // - <, which is LessThan - // - <=, which is LessEqual - // - <<, which is LeftDoubleChevron - '<' => blk: { - if (self.match('=')) { - break :blk self.makeToken(.LessEqual); - } else if (self.match('<')) { - break :blk self.makeToken(.LeftDoubleChevron); - } else { - break :blk self.makeToken(.Less); - } - }, '>' => self.makeMatchToken('=', .GreaterEqual, .Greater), + '+' => self.makeTripleMatchToken('+', .PlusPlus, '=', .PlusEqual, .Plus), + '<' => self.makeTripleMatchToken('=', .LessEqual, '<', .LeftDoubleChevron, .Less), '/' => blk: { var next = self.peekNext();