-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter_Kotlin_version.kt
78 lines (72 loc) · 2.04 KB
/
Interpreter_Kotlin_version.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.company.brainf
import java.io.File
import java.util.*
fun main() {
val file=File("brainf.txt")
val reader=Scanner(file)
var code=""
while (reader.hasNextLine())
code+=reader.nextLine()
val legalCharacters=ArrayList(listOf('+', '-', '<', '>', '.', ',', '[', ']', '='))
val tokens=ArrayList<String>()
var looper=0
for (i in code.indices){
if (legalCharacters.contains(code[i]))
tokens.add(""+code[i])
when(code[i]) {
'[' -> looper++
']' -> looper--
}
}
if (looper!=0)
throw IllegalStateException("The number of '[' is not equal to the number of ']'")
interpret(tokens)
}
fun interpret(brainfTokens: ArrayList<String>) {
val memory=Array(30000){0}
var pointer=0
var isLooping=false
val loopStack=Stack<Int>()
var innerLoops=0
val scanner=Scanner(System.`in`)
var i=0
while(i<brainfTokens.size){
val brainfToken= brainfTokens[i]
if (isLooping){
if (brainfToken == "[")
innerLoops++
if (brainfToken == "]"){
if (innerLoops==0)
isLooping=false
else
innerLoops--
}
continue
}
when(brainfToken){
"+" -> memory[pointer]++
"-" -> memory[pointer]--
">" -> pointer++
"<" ->{
pointer--
if (pointer<0)
throw IllegalStateException("Pointer value less than 0 is not allowed")
}
"," -> memory[pointer]= scanner.next()[0].code
"." -> print(memory[pointer].toChar())
"[" ->{
if (memory[pointer]==0)
isLooping=true
else
loopStack.push(i)
}
"]" ->{
if (memory[pointer]!=0)
i=loopStack.peek()
else
loopStack.pop()
}
}
i++
}
}