Compiler/lexical analyzer: Difference between revisions

Content added Content deleted
(Added another Nim example, using only system and a bit of strutils)
(Fixed my Nim example)
Line 5,145:
 
===Using nothing but system and strutils===
<lang nim>import strutils
import strutils
 
type
Line 5,329 ⟶ 5,328:
 
when isMainModule:
constlet Codecode = """readAll(stdin)
/*
Hello world
*/
print("Hello, World!\n");
"""
var
lexer = initLexer(Codecode)
token: Token
while true:
token = lexer.next()
stdout.write(token.ln, ' ', token.col, ' ', token.kind)
echocase token.kind
of tokInt: stdout.write(' ', token.intVal)
of tokChar: stdout.write(' ', token.charVal.ord)
of tokString: stdout.write(" \"", token.stringVal
.replace("\\", "\\\\")
.replace("\n", "\\n"), '"')
of tokIdent: stdout.write(' ', token.ident)
else: discard
stdout.write('\n')
if token.kind == tokEnd:
break</lang>
</lang>
 
=={{header|Perl}}==