This commit is contained in:
sunwoo1524 2024-02-11 00:34:18 +09:00
commit f606d5c1ea

169
main.is Normal file
View file

@ -0,0 +1,169 @@
// run a brainfuck program
@runProgram(code, input, mem_size) {
var pointer = 0
var input_index = 0
var memory = []
var front_brackets = []
var back_brackets = []
var code_arr = code.to_arr()
var input_arr = input.to_arr()
// initialize memory
for mem_size {
memory.push(0)
}
// compress the code (remove all plain texts)
var compressed = []
for let i, code.len {
if "+-><.,[]".index_of(code_arr[i]) != -1 {
compressed.push(code_arr[i])
}
}
code_arr = compressed
// find positions of brackets
var brackets_stack = []
for let i, code_arr.len {
var command = code_arr[i]
if command == "[" {
brackets_stack.push(i)
} elif command == "]" {
if brackets_stack.len == 0 {
Mk:dialog("Syntex error", "Brackets not matched.")
return null
}
front_brackets.push(brackets_stack[brackets_stack.len - 1])
back_brackets.push(i)
brackets_stack.pop()
}
}
if brackets_stack.len != 0 {
Mk:dialog("Syntex error", "Brackets not matched.")
return null
}
// run the program
var i = 0
var output = ""
loop {
// var continue = false
var command = code_arr[i]
if command == "+" {
if memory[pointer] == 255 {
memory[pointer] = 0
} else {
memory[pointer] = memory[pointer] + 1
}
} elif command == "-" {
if memory[pointer] == 0 {
memory[pointer] = 255
} else {
memory[pointer] = memory[pointer] - 1
}
} elif command == ">" {
if pointer == mem_size - 1 {
pointer = 0
} else {
pointer = pointer + 1
}
} elif command == "<" {
if pointer == 0 {
pointer = mem_size - 1
} else {
pointer = pointer - 1
}
} elif command == "." {
// print(Str:from_utf8_bytes([memory[pointer]]))
output = `{output}{Str:from_utf8_bytes([memory[pointer]])}`
} elif command == "," {
if input_index < input_arr.len {
memory[pointer] = input_arr[input_index].charcode_at(0)
input_index = input_index + 1
}
} elif command == "[" {
if memory[pointer] == 0 {
var tmp = 0
for let j, front_brackets.len {
if front_brackets[j] == i {
tmp = j
break
}
}
i = back_brackets[tmp]
continue
}
} elif command == "]" {
if memory[pointer] != 0 {
var tmp = 0
for let j, back_brackets.len {
if back_brackets[j] == i {
tmp = j
break
}
}
i = front_brackets[tmp]
continue
}
}
i = i + 1
if i == code_arr.len {
break
}
}
Mk:dialog("Output", output)
}
var code = "-[------->+<]>.-[->++++<]>.++++.[++++>---<]>.[->++++<]>--.-----.+++++++..-[--->+<]>.-[---->+<]>++.++[--->++<]>.+++.+++++++.+[->+++<]>.--------.[--->+<]>+.+++.-[->++<]>-.+[-->+++<]>++.---.+++++++++++++..+++++++.-[--->++<]>.--[--->++++<]>+.------.++++++++."
var input = ""
var mem_size = 30000
@loading() {
Ui:render([
Ui:C:mfm({ text: "<center>Loading... :bunhd_hop:</center>" })
])
}
@editor() {
Ui:render([
Ui:C:textarea({
label: "Your BF Code"
default: code
onInput: @(v) { code = v }
})
Ui:C:textInput({
label: "Input"
default: input
onInput: @(v) { input = v }
})
Ui:C:textInput({
label: "Memory Size"
default: `{mem_size}`
onInput: @(v) { mem_size = v.to_num() }
})
Ui:C:button({
text: "Execute"
onClick: @() {
loading()
runProgram(code, input, mem_size)
editor()
}
})
])
}
editor()