From 40aa6de8a706ac88a7451dad0058c59f26bd9a7f Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 8 Jan 2015 18:18:33 +0100 Subject: Replace runes with named tokens --- lexer.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'lexer.go') diff --git a/lexer.go b/lexer.go index 9049cbd..b059625 100644 --- a/lexer.go +++ b/lexer.go @@ -15,8 +15,23 @@ const ( upper quoted char + sum + sub + mul + div ) +var charmap = map[rune]int { + '+': SUM, + '-': SUB, + '*': MUL, + '/': DIV, + '=': EQ, + '(': LBR, + ')': RBR, + ',': COM, +} + type item struct { typ int val string @@ -54,7 +69,12 @@ func (y *yyLex) Lex(lval *yySymType) int { lval.sval = item.val[1:len(item.val)-1] return STRING case char: - return int(item.val[0]) + c := rune(item.val[0]) + if ch, ok := charmap[c]; ok { + return ch + } else { + return int(c) + } } return eof } -- cgit v1.2.3