Skip to content

Commit 64acb8e

Browse files
committed
Make main() optional (#388)
1 parent 33973e5 commit 64acb8e

File tree

5 files changed

+9
-15
lines changed

5 files changed

+9
-15
lines changed

doc/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Compiles the Umka program into bytecode. Here, `umka` is the interpreter instanc
3838
UMKA_API int umkaRun(void *umka);
3939
```
4040

41-
Runs the Umka program previously compiled to bytecode, i. e., calls its `main()` function. Here, `umka` is the interpreter instance handle. Returns `0` if the program execution finishes successfully and no run-time errors were detected, otherwise return the error code.
41+
Runs the Umka program previously compiled to bytecode, i.e., calls its `main()` function, if it exists, and gracefully deallocates heap memory referenced by global variables. Here, `umka` is the interpreter instance handle. Returns `0` if the program execution finishes successfully and no run-time errors were detected, otherwise return the error code.
4242

4343
```
4444
UMKA_API void umkaFree(void *umka);

doc/lang.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ fn p(a: int): (int, bool) {
14221422

14231423
## Modules
14241424

1425-
An Umka program consists of one or more source files, or modules. The main module should have the `main()` function with no parameters and no return values, which is the entry point of the program.
1425+
An Umka program consists of one or more source files, or modules. If the main module has a `main()` function with no parameters and no return values, this function becomes the entry point of the program.
14261426

14271427
Modules can be imported from other modules using the `import` clause in the beginning of the importing module. In this case, all the identifiers declared as exported in the module scope of the imported module become valid in the importing module, if qualified with the imported module name.
14281428

examples/3dcam/3dcam.um

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,3 @@ fn drawBodies() {
4545
drawCube(cube.position, 2, cube.height, 2, cube.color)
4646
}
4747
}
48-
49-
fn main() {}
50-
51-

playground/umka.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/umka_decl.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -921,14 +921,12 @@ void parseProgram(Compiler *comp)
921921
// Entry point
922922
genEntryPoint(&comp->gen, 0);
923923

924-
Ident *mainFn = identAssertFind(&comp->idents, &comp->modules, &comp->blocks, mainModule, "main", NULL);
925-
if (!identIsMain(mainFn))
926-
comp->error.handler(comp->error.context, "Illegal main() function");
927-
928-
// Dummy upvalue
929-
genPushZero(&comp->gen, sizeof(Interface) / sizeof(Slot));
930-
931-
genCall(&comp->gen, mainFn->offset);
924+
Ident *mainFn = identFind(&comp->idents, &comp->modules, &comp->blocks, mainModule, "main", NULL, false);
925+
if (mainFn && identIsMain(mainFn))
926+
{
927+
genPushZero(&comp->gen, sizeof(Interface) / sizeof(Slot)); // Dummy upvalue
928+
genCall(&comp->gen, mainFn->offset);
929+
}
932930

933931
doGarbageCollection(comp, 0);
934932
genHalt(&comp->gen);

0 commit comments

Comments
 (0)