I recently made a toy programming language using C, Bison, Flex, and this post as a starting point. It looks a lot like Python except there’s no colons or whitespace rules.
The code is here, but it’s less important than the concept/algorithm I’m stuck on.
I designed my abstract syntax tree just like Rudi did in the post linked above.
The trick is, I can’t think of a great way to return from user-defined functions, or break out of loops. If I require only a single return statement at the end of a user-defined function, it’s doable (actually this is what currently works for user-defined functions).
Example:
i = 0
while 1 do
if i > 15 then
break
end
done
Example 2:
def mean(somelist)
if len(list) == 0 then
return 0 # throw error
else
return sum(somelist) / len(somelist)
end
end
Some popular stack-based languages push a value onto the stack which is then popped by the calling function. That might work for you.
Of course, this relies on having a function with a known return type. Python returns PyNone if the function is ‘void’ (i.e. returns nothing). A Python function returns only one value (which could be None, or an object or a tuple or whatever).
C-type languages also return only one value from a function.
I think the point is that in a strong-typed language you always have a return value and you must always return one. In a weak typed language where you might or might return a value, you have one nonetheless.