I have this simple BF interpreter:
import structs/ArrayList
import structs/HashMap
import structs/Stack
import io/File
Tape: class {
tape := [0] as ArrayList<Int>
pos := 0
init: func {}
get: func -> Int { return tape[pos] }
inc: func { tape[pos] += 1 }
dec: func { tape[pos] -= 1 }
advance: func {
pos += 1
if (pos > tape size) {
tape add(0)
}
}
devance: func { pos -= 1 }
}
Program: class {
code: String
bracketMap: HashMap<Int, Int>
init: func (text: String) {
leftstack := Stack<Int> new()
pc := 0
valid := ['[', ']', '<', '>', '+', '-', ',', '.'] as ArrayList<Char>
for (chr in text) {
if (valid contains?(chr)) {
code append(chr)
if (chr == '[') {
leftstack push(pc)
} else if (chr == ']') {
left := leftstack pop()
right := pc
bracketMap[left] = right
bracketMap[right] = left
}
}
}
}
run: func {
pc := 0
tape := Tape new()
while (pc < code length()) {
chr := code[pc]
match chr {
case '+' => tape inc()
case '-' => tape dec()
case '>' => tape advance()
case '<' => tape devance()
//case '[' => if tape get() == 0 { pc = bracketMap[pc] }
case '[' => (tape get() == 0 && pc = bracketMap[pc] )
//case ']' => if tape get() != 0 { pc = bracketMap[pc] }
case ']' => (tape get() != 0 && pc = bracketMap[pc])
case '.' => tape get() as Char print()
}
pc += 1
}
}
}
main: func(args: ArrayList<String>) {
Program new(File new(args get(0)) read()) run()
}
It seems to crash the compiler:
ryan@DevPC-LX:~/stuff/benchmarks/brainfuck$ rock -v *.ooc
Parsing...
Resolving...
Generating C sources...
[OutOfBoundsException in ArrayList]: Trying to access an element at offset 1, but size is only 1!
(SIGABRT) abort program
[fancy backtrace]
0 Backtrace class() in lang/Backtrace (at /media/ryan/stuff/rock/sdk/lang/Backtrace.ooc:285)
1 Exception getCurrentBacktrace() in lang/Exception (at /media/ryan/stuff/rock/sdk/lang/Exception.ooc:209)
2 _signalHandler() in lang/Exception (at /media/ryan/stuff/rock/sdk/lang/Exception.ooc:269)
3 killpg() in (at (null):0)
4 GI raise() in (at /build/buildd/eglibc-2.19/signal/../nptl/sysdeps/unix/sysv/linux/raise.c:56)
5 GI abort() in (at /build/buildd/eglibc-2.19/stdlib/abort.c:91)
6 Exception throw_impl() in lang/Exception (at /media/ryan/stuff/rock/sdk/lang/Exception.ooc:194)
7 ArrayList get_impl() in structs/ArrayList (at /media/ryan/stuff/rock/sdk/structs/ArrayList.ooc:81)
8 FunctionCallWriter write_functionCall() in rock/backend/cnaughty/FunctionCallWriter (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/FunctionCallWriter.ooc:95)
9 CGenerator visitCommaSequence_impl() in rock/backend/cnaughty/CGenerator (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/CGenerator.ooc:470)
10 CGenerator visitParenthesis_impl() in rock/backend/cnaughty/CGenerator (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/CGenerator.ooc:399)
11 Skeleton writeLine_impl() in rock/backend/cnaughty/Skeleton (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/Skeleton.ooc:22)
12 CGenerator visitBlock_impl() in rock/backend/cnaughty/CGenerator (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/CGenerator.ooc:368)
13 Skeleton writeLine_impl() in rock/backend/cnaughty/Skeleton (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/Skeleton.ooc:22)
14 __rock_backend_cnaughty_ControlStatementWriter_rock_backend_cnaughty_ControlStatementWriter_closure33() in rock/backend/cnaughty/ControlStatementWriter (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/ControlStatementWriter.ooc:67)
15 Skeleton writeLine_impl() in rock/backend/cnaughty/Skeleton (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/Skeleton.ooc:22)
16 ControlStatementWriter writeConditional() in rock/backend/cnaughty/ControlStatementWriter (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/ControlStatementWriter.ooc:14)
17 Skeleton writeLine_impl() in rock/backend/cnaughty/Skeleton (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/Skeleton.ooc:22)
18 ClassDeclWriter writeInstanceImplFuncs() in rock/backend/cnaughty/ClassDeclWriter (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/ClassDeclWriter.ooc:242)
19 ClassDeclWriter write__class() in rock/backend/cnaughty/ClassDeclWriter (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/ClassDeclWriter.ooc:30)
20 ModuleWriter write() in rock/backend/cnaughty/ModuleWriter (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/ModuleWriter.ooc:106)
21 CGenerator write_impl() in rock/backend/cnaughty/CGenerator (at /media/ryan/stuff/rock/source/rock/backend/cnaughty/CGenerator.ooc:68)
22 SequenceDriver generateSources_impl() in rock/frontend/drivers/SequenceDriver (at /media/ryan/stuff/rock/source/rock/frontend/drivers/SequenceDriver.ooc:184)
23 SequenceDriver compile_impl() in rock/frontend/drivers/SequenceDriver (at /media/ryan/stuff/rock/source/rock/frontend/drivers/SequenceDriver.ooc:47)
24 __rock_frontend_CommandLine_rock_frontend_CommandLine_closure317() in rock/frontend/CommandLine (at /media/ryan/stuff/rock/source/rock/frontend/CommandLine.ooc:678)
25 Time runTime() in os/Time (at /media/ryan/stuff/rock/sdk/os/Time.ooc:134)
26 CommandLine postParsing_impl() in rock/frontend/CommandLine (at /media/ryan/stuff/rock/source/rock/frontend/CommandLine.ooc:677)
27 CommandLine parse_impl() in rock/frontend/CommandLine (at /media/ryan/stuff/rock/source/rock/frontend/CommandLine.ooc:634)
28 CommandLine init() in rock/frontend/CommandLine (at /media/ryan/stuff/rock/source/rock/frontend/CommandLine.ooc:536)
29 CommandLine new() in rock/frontend/CommandLine (at /media/ryan/stuff/rock/source/rock/frontend/CommandLine.ooc:30)
30 main() in (at /media/ryan/stuff/rock/source/rock/rock.ooc:2)
31 libc_start_main() in (at /build/buildd/eglibc-2.19/csu/libc-start.c:321)
32 _start() in (at (null):0)
ryan@DevPC-LX:~/stuff/benchmarks/brainfuck$
???
--
Ryan
[ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong.