• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/examples/native-function.c

  1 /*
  2  * Copyright (C) 2021 Jo-Philipp Wich <jo@mein.io>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 #include <stdio.h>
 18 
 19 #include <ucode/compiler.h>
 20 #include <ucode/lib.h>
 21 #include <ucode/vm.h>
 22 
 23 
 24 #define MULTILINE_STRING(...) #__VA_ARGS__
 25 
 26 static const char *program_code = MULTILINE_STRING(
 27         {%
 28                 print("add() = " + add(5, 3.1, 2) + "\n");
 29                 print("multiply() = " + multiply(7.3, 5) + "\n");
 30         %}
 31 );
 32 
 33 static uc_parse_config_t config = {
 34         .strict_declarations = false,
 35         .lstrip_blocks = true,
 36         .trim_blocks = true
 37 };
 38 
 39 static uc_value_t *
 40 multiply_two_numbers(uc_vm_t *vm, size_t nargs)
 41 {
 42         uc_value_t *x = uc_fn_arg(0);
 43         uc_value_t *y = uc_fn_arg(1);
 44 
 45         return ucv_double_new(ucv_to_double(x) * ucv_to_double(y));
 46 }
 47 
 48 static uc_value_t *
 49 add_all_numbers(uc_vm_t *vm, size_t nargs)
 50 {
 51         double res = 0.0;
 52 
 53         for (size_t n = 0; n < nargs; n++)
 54                 res += ucv_to_double(uc_fn_arg(n));
 55 
 56         return ucv_double_new(res);
 57 }
 58 
 59 int main(int argc, char **argv)
 60 {
 61         int exit_code = 0;
 62 
 63         /* create a source buffer containing the program code */
 64         uc_source_t *src = uc_source_new_buffer("my program", strdup(program_code), strlen(program_code));
 65 
 66         /* compile source buffer into function */
 67         char *syntax_error = NULL;
 68         uc_program_t *program = uc_compile(&config, src, &syntax_error);
 69 
 70         /* release source buffer */
 71         uc_source_put(src);
 72 
 73         /* check if compilation failed */
 74         if (!program) {
 75                 fprintf(stderr, "Failed to compile program: %s\n", syntax_error);
 76 
 77                 return 1;
 78         }
 79 
 80         /* initialize default module search path */
 81         uc_search_path_init(&config.module_search_path);
 82 
 83         /* initialize VM context */
 84         uc_vm_t vm = { 0 };
 85         uc_vm_init(&vm, &config);
 86 
 87         /* load standard library into global VM scope */
 88         uc_stdlib_load(uc_vm_scope_get(&vm));
 89 
 90         /* register our native functions as "add" and "multiply" */
 91         uc_function_register(uc_vm_scope_get(&vm), "add", add_all_numbers);
 92         uc_function_register(uc_vm_scope_get(&vm), "multiply", multiply_two_numbers);
 93 
 94         /* execute program function */
 95         int return_code = uc_vm_execute(&vm, program, NULL);
 96 
 97         /* release program */
 98         uc_program_put(program);
 99 
100         /* handle return status */
101         if (return_code == ERROR_COMPILE || return_code == ERROR_RUNTIME) {
102                 printf("An error occurred while running the program\n");
103                 exit_code = 1;
104         }
105 
106         /* free VM context */
107         uc_vm_free(&vm);
108 
109         /* free search module path vector */
110         uc_search_path_free(&config.module_search_path);
111 
112         return exit_code;
113 }
114 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt