r/ProgrammingLanguages • u/Tasty_Replacement_29 Bau • Jun 24 '26
How to combine REPL, main, modules, and initalization
I'm designing a systems programming language called Bau.
Goals are: Simple, concise, fast (transpiled to C), and memory safe (ref counting + ownership/borrowing).
One design question I'm currently exploring is how to combine:
- REPL-style top-level execution
- Global variables
- An optional main() function
No main() is required:
for i := range(0, 20)
println(factorial(i))
fun factorial(x int) int
if x <= 1
return 1
return x * factorial(x - 1)One question is: should imported modules be allowed to contain arbitrary top-level executable code at all? I allow it, because I think sometimes it is needed to initialize e.g. a random number generator, or pre-calculate a table (even thought my language allows compile-time execution). It could be limited to method calls for global variable / constant initialization, but then I'm pretty sure people would use that mechanism for initialization, which then just makes live harder.
Example:
unicodeTable := buildUnicodeTable()Current rules:
- Import statements need to be at the top of a module, but otherwise the order of function declarations etc. is arbitrary.
- Each module (file) may contain global variables.
- Each module may contain at most one contiguous block of top-level statements (internally, an initialization function generated from its top-level statements).
- Top-level variables are globals.
- Top-level statements are executed automatically.
- If both top-level statements and main() exist, top-level statements run first, then main().
- Cyclic imports are disallowed (like in Go), to simplify and speed up compilation and init code.
- Imported modules are initialized in dependency order, before the importing module is initialized.
Example:
println('init')
fun main()
println('hello')My questions:
- Are these rules coherent, or do they create surprising behavior?
- Is allowing arbitrary top-level execution in imported modules a mistake for a systems language?
- How should initialization order work across imported modules?
- Are there languages that successfully combine optional main(), module init code, and globals without causing confusion?
- What are the disadvantages of disallowing cyclic imports?
4
u/Inconstant_Moo 🧿 Pipefish Jun 24 '26
I don't like the idea of top-level things just hanging about loose.
In Go and Pipefish there's a distinction between
init, which is optional can be put in any module and is run immediately it's compiled; andmainwhich is also optional and is only run if it's in the root module.If initialization is to be a contiguous block of code (as of course it should be) then making it a named function makes things easier for everyone.
3
u/phovos Jun 24 '26 edited Jun 24 '26
This sounds suspiciously like 'dynamic linking'. In which case, yes all the systems languages do it and there are a few ways to do it (<include> and .dll, etc; it's a middle-step of compilation [GCC adheres to the System V AMD64 ABI on my system (Application Binary Interface), the pipeline where DLL is relevant]).
edit: I can't post images which almost made me despair but then I remember my new Obisian workflow that saves the source of all my images as meta data so boom I have the youtube video where the img came from: https://www.youtube.com/watch?v=E2q0SfBTZ-Q[image shows the tower from (Source Code)-> (Preprocessor) -> (Compiler)... (Linker)..] (unfortunately it is long and I didn't save the timestamp; I am going to take a look at my workflow and see about timestamps, now).
2
u/oscarryz Yz Jun 25 '26 edited Jun 25 '26
The global could be scoped to the module or file, and require FQN to access them.
I assume the code runs when imported? You could simplify all these rules and make it easier to understand if you don't execute anything, but let the functions access the scoped variables. When executed (eg foo.init()) the module will load and execute all the free floating code, similar to free floating blocks do in Java.
Yet another option, is to allow the module itself be callable (that is what I do, but in my case there is no distinction between modules and functions); that way the floating code doesn't execute until explicitly invoked. In the case of a program (main-less source code) specifying the file is the invocation. But again this might not apply for you.
2
u/tobega Jun 30 '26
Very interesting questions. I think your choices seem consistent.
I do something similar, but made different choices:
- Top-level code is considered a main and not run for modules. Only definitions can be exported in a module.
- Code using a module does not choose the implementation it imports. It documents the required interfaces and the main program injects implementations of dependencies. That means the main can execute any required initializations.