I am writing a compiler for my toy language for learning and curiosity purposes only and I already have accomplished some things, like math operations and stuff, I am having some trouble trying to parse a variable "within" a variable though. Take these examples in pseudo-code:
numbers = [1..20]
My parser already understand this, and stuff like:
ten = numbers[9]
However, when I have a variable that is composed of more than one "element", like a 2D array or an object inside an object, I cannot parse, the parser blocks itself inside an infinite loop. Example:
things = [['one'], ['two'], ['three']]
person = {
name = 'john'
car = {
model = 'ninja'
price = 23000.0
}
}
things[1][0] // This is causing the infinite loop
person.car.price // And this
This is what my parser looks like (in pseudo-code):
parseVarReference() {
variable = expect(TOKEN_IDENTIFIER);
if (current() == '[') {
// try to parse array or object or fallback to regular variables
}
}
Now, in my head, I would parse a nested variable like this:
parseVarReference() {
variable = parseVarReference();
if (current() == '[') {
// try to parse array or object or fallback to regular variables
}
}
Obviously this is causing an infinite loop and I cannot think in a way of solving this without breaking my parser.