Last active
December 20, 2025 18:54
-
-
Save kinjalkishor/db9ecae0394f4ce7883009695decaef0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if _MSC_VER | |
| #pragma comment(linker, "/subsystem:console") | |
| #endif | |
| //-------------------------- | |
| #include <stdlib.h> | |
| #include <conio.h> | |
| #include <stdio.h> | |
| #pragma comment (lib ,"umkalib.lib") | |
| #include <iostream> | |
| // Umka embedded scripting example | |
| #include <stdio.h> | |
| #define UMKA_STATIC | |
| #include "umka_api.h" | |
| void print_msg_cpp(UmkaStackSlot *params, UmkaStackSlot *result) | |
| { | |
| printf("Hello World from C++\n"); | |
| } | |
| void mul_cpp(UmkaStackSlot *params, UmkaStackSlot *result) { | |
| //return a * b; | |
| int a = umkaGetParam(params, 0)->intVal; | |
| int b = umkaGetParam(params, 1)->intVal; | |
| printf("In mul_cpp: a = %d, b = %d\n", a, b); | |
| int sum = a * b; | |
| result->intVal = sum; | |
| } | |
| int main() | |
| { | |
| const char* script1 = R"( | |
| import ( | |
| "std.um" | |
| "rl.um" | |
| ) | |
| fn print_msg() { | |
| rl::print_msg_cpp() | |
| printf("Hello world from Umka print_msg\n"); | |
| } | |
| fn add(a: int, b: int): int { | |
| printf("In umka add: a = %d, b = %d\n", a, b); | |
| a = rl::mul_cpp(a, b); | |
| printf("After mul_cpp: a = %d, b = %d\n", a, b); | |
| return a + b; | |
| } | |
| fn main() { | |
| printf("Hello world from Umka main\n"); | |
| } | |
| )"; | |
| //---------------------------- | |
| // Umka initialization | |
| // for function from umka | |
| UmkaFuncContext umka_print_msg = {0}; | |
| UmkaFuncContext umka_add = {0}; | |
| UmkaFuncContext umka_mul = {0}; | |
| Umka *umka = umkaAlloc(); | |
| // relative file path not working | |
| //bool umkaOk = umkaInit(umka, "../../source/code/test3_fs/scripts/test.um", NULL, 1024 * 1024, NULL, 0, NULL, false, false, NULL); | |
| //bool umkaOk = umkaInit(umka, "FULL_SCRIPT_PATH/test.um", NULL, 1024 * 1024, NULL, 0, NULL, false, false, NULL); | |
| bool umkaOk = umkaInit(umka, NULL, script1, 1024 * 1024, NULL, 0, NULL, false, false, NULL); | |
| // Add cpp functions to Umka script | |
| if (umkaOk) | |
| { | |
| umkaAddFunc(umka, "print_msg_cpp", &print_msg_cpp); | |
| umkaAddFunc(umka, "mul_cpp", &mul_cpp); | |
| umkaAddModule(umka, "rl.um", | |
| "fn print_msg_cpp*()\n" | |
| "fn mul_cpp*(a: int, b: int): int\n" | |
| ); | |
| umkaOk = umkaCompile(umka); | |
| } | |
| // If Umka script compiled call functions from Umka sript | |
| if (umkaOk) { | |
| printf("Umka initialized\n"); | |
| umkaGetFunc(umka, NULL, "print_msg", &umka_print_msg); | |
| umkaGetFunc(umka, NULL, "add", &umka_add); | |
| umkaGetParam(umka_add.params, 0)->intVal = 4; | |
| umkaGetParam(umka_add.params, 1)->intVal = 5; | |
| umkaCall(umka, &umka_add) == 0; | |
| int result = umka_add.result->intVal; | |
| printf("Result of add from umka: %d\n", result); | |
| } else { | |
| UmkaError *error = umkaGetError(umka); | |
| printf("Umka error %s (%d, %d): %s\n", error->fileName, error->line, error->pos, error->msg); | |
| } | |
| // Checking for error if some umka function call fails | |
| umkaOk = umkaCall(umka, &umka_print_msg) == 0; | |
| if (!umkaOk) { | |
| UmkaError *error = umkaGetError(umka); | |
| printf("Umka runtime error %s (%d): %s\n", error->fileName, error->line, error->msg); | |
| } | |
| // Run Umka script. Executes main function from Umka script. | |
| umkaRun(umka); | |
| //--------------------- | |
| // Umka de-initialization | |
| umkaFree(umka); | |
| //------------------------ | |
| printf("\n"); | |
| _getch(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment