r/ProgrammingLanguages • • Jan 10 '26

Help Adding Overloadable Functions to an Interpreted Language

I followed the lovely Crafting Interpreters book by Robert Nystrom to make a basic interpreted language for a game I am developing. I added typing, so I had to change some of the grammar and so on. I implement typing as the pass right before the interpreter execution, and right after the resolution pass.

I am currently trying to add functionality for function name overloading, but I am encountering difficulty in the resolution pass. Because the resolution pass does not touch types, nor do I want it to, it raises a compilation error when two functions of the same name are declared. Since the function called in a function call should depend on the types (e.g., a function signature in a closer scope may not match the arguments being passed), I am unsure how to proceed.

The only way I can conceive of modifying the resolver to allow for function overloading is to combine the resolution and typing passes, but this violates the single responsibility principle and I am trying to avoid it.

Any thoughts or ideas?

Thanks.

4 Upvotes

15 comments sorted by

3

u/srvhfvakc Jan 10 '26

Seems like the only way to do this would be to defer the compilation error when two same-name functions are declared to the typing pass. Since the only way to distinguish the two functions would be (by definition) their type.

1

u/TheUltimateAsh Jan 10 '26

This seems like the simplest solution. Thank you.

1

u/Germisstuck CrabStar Jan 10 '26

What my language will do, is something kinda different, but for function (and by extension, operator) overloading is handled with behavior being completely separate from data. Then by using control flow, determine which logic set is the currently used one (this sounds like hell to infer, so in my language you have to explicitly say something like "let x = new Counter() with CustomCounterLogic". With this, you can just change the logic in different contexts easily, allowing for using existing names.

I'm not sure how useful this would be to your language, but that's just what I'm doing.

1

u/TheUltimateAsh Jan 10 '26

This is very interesting. It almost seems like the strategy design pattern.

So is the CustomCounterLogic defined right there or is it defined elsewhere?

→ More replies (1)

1

u/AustinVelonaut Admiran Jan 10 '26

Another way to handle this would be to mark overloaded names distinct from non-overloaded names, with e.g. a declaration like generic print;, then when you come to name resolution, defer any resolution for names that are marked as generic until the typecheck phase.

1

u/TheUltimateAsh Jan 10 '26

I am trying to keep this language as basic as possible because I don’t want to scare off potential players, but this is still a fantastic idea, thank you. I may use something like this.

1

u/[deleted] Jan 10 '26

[removed] — view removed comment

1

u/TheUltimateAsh Jan 10 '26

This is smart. So I would store the name of the function as

“[returnType]functionName[param types…]”

The only problem with this is that I store Tokens, not strings, as names in my parser. Tokens have additional data such as line number and so on. But I think this is a really good solution.