Skip to content

Commit 5fb91d7

Browse files
committed
lexer - tokenize negative numbers
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent b305ae3 commit 5fb91d7

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

src/tim/engine/lexer.nim

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,53 @@ proc nextToken*(lex: var Lexer): TokenTuple =
192192
lex.advance()
193193
result = initToken(lex, tkPlus, line, col, pos, wsno)
194194
of '-':
195-
lex.advance()
196-
result = initToken(lex, tkMinus, line, col, pos, wsno)
195+
# Check if this is a negative number literal
196+
var peekPos = lex.pos + 1
197+
# Skip whitespace between '-' and potential digit
198+
while peekPos < lex.input.len and lex.input[peekPos] in {' ', '\t', '\r'}:
199+
inc peekPos
200+
if peekPos < lex.input.len and lex.input[peekPos] in {'0'..'9'}:
201+
# Tokenize as negative number
202+
lex.advance() # consume '-'
203+
# Skip whitespace
204+
while lex.current in {' ', '\t', '\r'}:
205+
lex.advance()
206+
lex.strbuf.setLen(0)
207+
lex.strbuf.add('-')
208+
var isFloat = false
209+
# Integer part
210+
while lex.current in {'0'..'9', '_'}:
211+
if lex.current != '_':
212+
lex.strbuf.add(lex.current)
213+
lex.advance()
214+
# Fractional part
215+
if lex.current == '.' and lex.peek().isDigit():
216+
isFloat = true
217+
lex.strbuf.add('.')
218+
lex.advance()
219+
while lex.current in {'0'..'9', '_'}:
220+
if lex.current != '_':
221+
lex.strbuf.add(lex.current)
222+
lex.advance()
223+
# Exponent part
224+
if isFloat and (lex.current == 'e' or lex.current == 'E'):
225+
lex.strbuf.add(lex.current)
226+
lex.advance()
227+
if lex.current == '+' or lex.current == '-':
228+
lex.strbuf.add(lex.current)
229+
lex.advance()
230+
while lex.current in {'0'..'9', '_'}:
231+
if lex.current != '_':
232+
lex.strbuf.add(lex.current)
233+
lex.advance()
234+
result = initToken(lex, tkFloat, move lex.strbuf, line, col, pos, wsno)
235+
elif isFloat:
236+
result = initToken(lex, tkFloat, move lex.strbuf, line, col, pos, wsno)
237+
else:
238+
result = initToken(lex, tkInteger, move lex.strbuf, line, col, pos, wsno)
239+
else:
240+
lex.advance()
241+
result = initToken(lex, tkMinus, line, col, pos, wsno)
197242
of '*':
198243
lex.advance()
199244
result = initToken(lex, tkAsterisk, line, col, pos, wsno)

0 commit comments

Comments
 (0)