We need to implement JavaScript parser in C, and we target very memory-constrained platforms; the most constrained part is the stack.
Currently, we have descent recursive parser, and C stack is easily overflown by reasonably complicated expressions. So, our goal is to make C stack consumption not to grow as the expressions nesting grows.
The only way that I can think of is to maintain our own stack, and our parser will be just a single huge function with lots of labels and gotos. It is rather messy and not very maintainable; we can hide some mess by macros, but anyway, it will be much more messy than usual function calls.
But then:
- We can implement stack in a more efficient way (embedded compilers are often, ... not very good)
- We can even use segmented stack (since huge contiguous pieces of memory are very expensive here)
Am I right that there are no other ways to implement that? I mean, other than the single function with lots of labels and gotos, and our own hand-made stack.
If I'm wrong, I'd be happy to listen for your suggestions.