Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import stdio as io
- import File
- enum Direction { Right, Left, Up, Down }
- class Befunge {
- List<Int> values
- Int x_cur
- Int y_cur
- Direction dir
- String code
- List<String> lines
- public new() {
- dir = RIGHT
- values = new Array<Int>()
- x_cur = 0
- y_cur = 0
- args = io.args()
- if (args.length != 1)
- io.exit(1)
- code = File.getContent(args[0])
- lines = ~/\r\n|\n|\r/g.split(code)
- for (i in 0 ... lines.length) {
- for (j in lines[i].length ... 80)
- lines[i] += ' '
- }
- while (true) {
- match lines[y_cur].charAt(x_cur) {
- '+' { add() }
- '-' { sub() }
- '*' { mul() }
- '/' { div() }
- '%' { mod() }
- '!' { not() }
- '`' { greater() }
- '>' { move_right() }
- '<' { move_left() }
- '^' { move_up() }
- 'v' { move_down() }
- '?' { move_random() }
- '_' { pop_horizontal() }
- '|' { pop_vertical() }
- '\"' { string() }
- ':' { dup() }
- '\\' { swap() }
- '$' { jmp() }
- '.' { display_int() }
- ',' { display_char() }
- '#' { trampoline() }
- 'p' { set_code() }
- 'g' { get_code() }
- '&' { ask_int() }
- '~' { ask_char() }
- '@' { exit() }
- ' ' { null }
- nb if (~/[0-9]/.match(nb)) {
- push_number((Int) nb)
- }
- _ {
- io.println("Erreur : opcode invalide '${lines[y_cur].charAt(x_cur)}' a la ligne ${y_cur}, caractere ${x_cur}")
- io.exit(1)
- }
- }
- move_pt()
- if ((x_cur + 1) * (y_cur + 1) > lines.length * 80)
- break
- }
- io.println("\n")
- io.print("Pile finale : ")
- io.println(values)
- io.print("Position du pointeur : ")
- io.println("ligne ${y_cur}, caractere ${x_cur}")
- }
- move_pt() {
- match dir {
- Direction.Right { x_cur++}
- Direction.Left { x_cur-- }
- Direction.Up { y_cur-- }
- Direction.Down { y_cur++ }
- }
- if (x_cur < 0)
- x_cur = 79
- if (y_cur < 0)
- y_cur = 24
- if (x_cur >= 80)
- x_cur = 0
- if (y_cur >= 25)
- y_cur = 0
- }
- Int pop() {
- if (values.length == 0)
- return 0
- return values.pop()
- }
- push_number(Int nb) {
- values.push(nb)
- }
- # +
- add() {
- b = pop()
- a = pop()
- values.push(a + b)
- }
- # -
- sub() {
- b = pop()
- a = pop()
- values.push(a - b)
- }
- # *
- mul() {
- b = pop()
- a = pop()
- values.push(a * b)
- }
- # /
- div() {
- b = pop()
- a = pop()
- values.push(Math.floor(a / b))
- }
- # %
- mod() {
- b = pop()
- a = pop()
- values.push(a % b)
- }
- # !
- not() {
- a = pop()
- values.push(a == 0 ? 1 : 0)
- }
- # `
- greater() {
- b = pop()
- a = pop()
- values.push(b > a ? 1 : 0)
- }
- # >
- move_right() {
- dir = Direction.Right
- }
- # <
- move_left() {
- dir = Direction.Left
- }
- # ^
- move_up() {
- dir = Direction.Up
- }
- # v
- move_down() {
- dir = Direction.Down
- }
- # ?
- move_random() {
- dir = Direction.random()
- }
- # _
- pop_horizontal() {
- a = pop()
- dir = if a == 0 { Direction.Right } else { Direction.Left }
- }
- # |
- pop_vertical() {
- a = pop()
- dir = if a == 0 { Direction.Down } else { Direction.Up }
- }
- # "..."
- string() {
- while (move_pt() && lines[y_cur].charAt(x_cur) != '\"')
- values.push(lines[y_cur].charCodeAt(x_cur))
- }
- # :
- dup() {
- a = pop()
- values.push(a)
- values.push(a)
- }
- # \
- swap() {
- b = pop()
- a = pop()
- values.push(b)
- values.push(a)
- }
- # $
- jmp() {
- pop()
- }
- # .
- display_int() {
- a = pop()
- io.print(a)
- }
- # ,
- display_char() {
- a = pop().toChar()
- io.print(a)
- }
- # #
- trampoline() {
- move_pt()
- }
- # p
- set_code() {
- io.println(values)
- y = pop()
- x = pop()
- v = pop()
- before = lines[y_cur].substr(0, x_cur)
- after = lines[y_cur].substr(x_cur + 1)
- lines[y_cur] = before + lines[y].charAt(x) + after
- }
- # g
- get_code() {
- y = pop()
- x = pop()
- values.push(lines[y].charCodeAt(x))
- }
- # &
- ask_int() {
- io.println(values)
- a = (Int) io.stdin().readLine()
- values.push(a)
- }
- # ~
- ask_char() {
- a = io.stdin().readByte()
- values.push(a)
- }
- # @
- exit() {
- io.exit(0)
- }
- public static main() {
- new Befunge()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement