Skip to content

Commit 728772e

Browse files
Added REPL
Started REPL addition. The parser does not handle multiple eof's yet, but lines of code are otherwise processing and the REPL can be exited with "quit".
1 parent 0d5a108 commit 728772e

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

swift/Sources/Lexer/Lexer.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ extension String {
2323
}
2424

2525
struct Lexer {
26-
let input: String
26+
var input: String = ""
2727
private var currentIndex: String.Index
2828
private var currentChar: Character { input[currentIndex] }
2929
private var keywords: [String : Token] = Token.defaultKeywords
3030

31+
init() {
32+
self.currentIndex = input.startIndex
33+
}
3134
init(input: String) {
3235
self.input = input
3336
self.currentIndex = input.startIndex

swift/Sources/Lexer/REPL.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import ArgumentParser
2+
3+
@main
4+
struct REPL: ParsableCommand {
5+
@Argument private var prompt = ">> "
6+
7+
mutating func run() {
8+
print("Welcome the Monkey language REPL")
9+
print("Enter 'quit' to exit")
10+
print(prompt, terminator: "")
11+
while let line = readLine(), line != "quit" {
12+
var lexer = Lexer(input: line)
13+
var tokens = [Token]()
14+
for _ in line {
15+
tokens += [lexer.nextToken()]
16+
}
17+
print(tokens)
18+
print(prompt, terminator: "")
19+
}
20+
}
21+
}

swift/Sources/Lexer/main.swift

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)