goby-lang / goby

Goby - Yet another programming language written in Go
MIT License
3.49k stars 171 forks source link

Interpreter crashing on sliced Array access #403

Closed 64kramsystem closed 6 years ago

64kramsystem commented 7 years ago

When I access an array in sliced form Array[start, length], the interpreter crashes.

Sample command:

a = [0, 1, 2]
a[0, 3] # kaboom!

Exact run output (the interpreter overwrites the second line when crashing):

$ ./goby -i
Goby 0.1.2 😙 😇 🤔
» a = [0, 1, 2]
#» [0, 1, 2]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x7114c5]

goroutine 1 [running]:
github.com/goby-lang/goby/compiler/ast.(*BaseNode).MarkAsExp(0x0)
  /home/saverio/code/go/src/github.com/goby-lang/goby/compiler/ast/ast.go:36 +0x5
github.com/goby-lang/goby/compiler/parser.(*Parser).parseStatement(0xc420114240, 0xfdb598, 0x1)
  /home/saverio/code/go/src/github.com/goby-lang/goby/compiler/parser/statement_parsing.go:34 +0xbd
github.com/goby-lang/goby/compiler/parser.(*Parser).ParseProgram(0xc420114240, 0x7, 0xc420063e40)
  /home/saverio/code/go/src/github.com/goby-lang/goby/compiler/parser/parser.go:176 +0xdf
github.com/goby-lang/goby/igb.StartIgb(0xb1be56, 0x5)
  /home/saverio/code/go/src/github.com/goby-lang/goby/igb/repl.go:160 +0x4d1
main.main()
  /home/saverio/code/go/src/github.com/goby-lang/goby/goby.go:29 +0x954

===============

st0012 commented 6 years ago

@saveriomiroddi Can you help fixing this?

64kramsystem commented 6 years ago

@st0012 I need a couple of days to get acquainted with the lexer (or parser) :smile:

I will write back with a positive or negative answer in a couple of days :smile:

64kramsystem commented 6 years ago

@st0012 ok, I had a look at the parser, and definitely manual parsers are not my thing :smile: so I'll pass on this, sorry.

st0012 commented 6 years ago

@saveriomiroddi So, would you suggest we use a parser generator? (this is not relevant to this issue)

64kramsystem commented 6 years ago

@st0012 this is an interestring/complex subject; I'm not sure.

Wrapping up the pros/cons of the current implementation:

I'm unsure. Switching in the short term would be very hard, because it would be a lot of work, and the current implementation works. I think the best approach is to have a look at how large projects tackle parsing, and then decide. In case parser generators would be the common practice, then definitely a parser generator should be seriously taken into account.

It's crucial to know that a handcrafted parser will cause newcomers to the project to struggle and likely not touch the parsing code (see the Regexp (#390) and Float (#443)), overall making the project less attractive to actively contribute to (IMO). I spent over an hour yesterday on this (#403) bug, and I have a vague idea of what's causing it, but I have no idea at all about how a fix would be, as it's hard to follow parsing logic in general.

For clarity, I had, in the past, experience with parser generators, but it was for SQL, which is a relatively small language.

64kramsystem commented 6 years ago

Another point: since Ruby uses a parser generator, and Goby is very similar, the Ruby grammar definition could (not 100% sure, though) be recycled, making the cost of switching [significantly] lower than starting from scratch.

st0012 commented 6 years ago

But do you understand how to write those parser generator's grammar rules? I think understand the Go code's logic is still far more easier than learning how to write the grammar rules.

Using parser generator means contributors needs to learn both Go and how to define a grammar rule, which is like learning a new language. So I don't think switching to parser generator will lower the entry barrier.

st0012 commented 6 years ago

And I personally want to restrict some syntax usage in Goby, and we also won't support some usages like URI("http://example.com"). So copying Ruby's grammar definition won't be a good way for switching to parser generator, we still need to define our grammar rules.

64kramsystem commented 6 years ago

But do you understand how to write those parser generator's grammar rules? I think understand the Go code's logic is still far more easier than learning how to write the grammar rules.

Fair enough, I take the perception of the difficulty of each approach is personal :smile:

ishankhare07 commented 6 years ago

@st0012 is this issue still open? can you give me a heads-up for this- like where to begin debugging it. I can probably look into fixing this issue.

st0012 commented 6 years ago

@ishankhare07 I think this issue hasn’t been fixed, but I can’t tell you much now because I’m about to fly home. You should join our slack and ask this in discussion channel and someone would help you https://goby-lang-slackin.herokuapp.com/

Alexius-Huang commented 6 years ago

I think PR #559 solved this issue, so this issue can be closed now.