translate source code from a high-level programming language to a lower level language
- parse the source code and perform its behavior directly;
- translate source code into some efficient intermediate representation and immediately execute this;
- explicitly execute stored precompiled code made by a compiler which is part of the interpreter system.
- ex. 1: Lisp, Brainfuck, ..
- ex. 2: Python, Ruby, ..
- ex. 3: Java, Basic, ..
[
LET,
IDENT("five"),
ASSIGN,
INT(5),
LET,
IDENT("ten"),
ASSIGN,
INT(10),
]
(
,)
{
,}
<
,>
proc
if
proc nextToken*(self: Lexer): token.Token =
var tok: token.Token
self.skipWhiteSpace()
case self.ch
of ':':
tok = newToken(COLON, self.ch)
of '(':
tok = newToken(LPAREN, self.ch)
of ')':
tok = newToken(RPAREN, self.ch)
of ',':
tok = newToken(COMMA, self.ch)
of '+':
tok = newToken(PLUS, self.ch)
else:
if isLetter(self.ch):
let l = self.readIdentifier()
let t = LookUpIdent(l)
return Token(Type: t, Literal: l)
elif isDigit(self.ch):
let t = token.INT
let l = self.readNumber()
return Token(Type: t, Literal: l)
else:
tok = newToken(token.ILLEGAL, self.ch)
self.readNextChar()
tok
=
and ==
!
and !=
case self.ch
of '=':
if self.peekChar() == '=':
let ch = self.ch
self.readNextChar()
let l = $ch & $self.ch # make `==`
tok = Token(Type: EQ, Literal: l)
else:
tok = newToken(ASSIGN, self.ch) # `=`