Compiler crashes with this program

20 views
Skip to first unread message

Ryan Gonzalez

unread,
Apr 7, 2015, 1:29:01 PM4/7/15
to ooc-...@googlegroups.com
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.

Amos Wenger

unread,
Apr 7, 2015, 7:01:47 PM4/7/15
to Ryan Gonzalez, ooc-...@googlegroups.com
*sigh* I wish that compiler was more robust.. but in the meantime, I fixed your code a little:


This version compiles and executes a simple hello world.


What stopped the compiler from crashing was parenthesis around the condition expression of the if statements in the execution loop.

Sorry the compiler breaks in funny ways sometimes :|

Cheers,
- A


--
You received this message because you are subscribed to the Google Groups "ooc-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ooc-lang+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Gonzalez

unread,
Apr 7, 2015, 7:16:32 PM4/7/15
to amosw...@gmail.com, ooc-...@googlegroups.com
On Tue, Apr 7, 2015 at 6:01 PM, Amos Wenger <amosw...@gmail.com> wrote:
*sigh* I wish that compiler was more robust.. but in the meantime, I fixed your code a little:


This version compiles and executes a simple hello world.



Thanks! I incorporated most of the changes, except for the change of += to = ... + (I'm a brevity addict) and the addition of error handling in the main function (it's for a language benchmark, and none of the other entries have that).
 
What stopped the compiler from crashing was parenthesis around the condition expression of the if statements in the execution loop.

Sorry the compiler breaks in funny ways sometimes :|


Hehehe...

Small question: why is it that leaving line 7 as in the original causes:

/media/ryan/stuff/benchmarks/brainfuck/brainfuck.ooc: In function ‘brainfuck__Tape___defaults___impl’:
/media/ryan/stuff/benchmarks/brainfuck/brainfuck.ooc:24:112: error: ‘__brainfuck_arrLitOp4’ undeclared (first use in this function)
     bracketMap := HashMap<Int, Int> new()

Kind of cryptic...

Amos Wenger

unread,
Apr 7, 2015, 7:22:33 PM4/7/15
to Ryan Gonzalez, ooc-...@googlegroups.com
re += vs = ... +, I'm right there with you as a brevity addict, but I wasn't sure wether ArrayList operator overloads would do the right thing. If it works, great! I just wanted to get it working as quickly as possible.

re line 7:

honestly, I don't know. it's a C error, which means rock generates invalid C code.. ideally the bug would be reproduced using the smallest possible example & reported at https://github.com/fasterthanlime/rock/issues

david.h...@gmail.com

unread,
Apr 8, 2015, 3:53:41 AM4/8/15
to ooc-...@googlegroups.com, rym...@gmail.com, amosw...@gmail.com
RE += vs = ... +, this *might* be related, but it might also not be.

https://github.com/fasterthanlime/rock/issues/869
Reply all
Reply to author
Forward
0 new messages