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 /* 18 * Example to compile a C string into an ucode program. 19 * Build with gcc -o execute-string -lucode execute-string.c 20 */ 21 22 #include <stdio.h> 23 24 #include <ucode/compiler.h> 25 #include <ucode/lib.h> 26 #include <ucode/vm.h> 27 28 29 #define MULTILINE_STRING(...) #__VA_ARGS__ 30 31 static const char *program_code = MULTILINE_STRING( 32 {% 33 function add(a, b) { 34 c = a + b; 35 36 return c; 37 } 38 39 result = add(x, y); 40 41 printf('%d + %d is %d\n', x, y, result); 42 43 return result; 44 %} 45 ); 46 47 static uc_parse_config_t config = { 48 .strict_declarations = false, 49 .lstrip_blocks = true, 50 .trim_blocks = true 51 }; 52 53 int main(int argc, char **argv) 54 { 55 int exit_code = 0; 56 57 /* create a source buffer containing the program code */ 58 uc_source_t *src = uc_source_new_buffer("my program", strdup(program_code), strlen(program_code)); 59 60 /* compile source buffer into function */ 61 char *syntax_error = NULL; 62 uc_program_t *program = uc_compile(&config, src, &syntax_error); 63 64 /* release source buffer */ 65 uc_source_put(src); 66 67 /* check if compilation failed */ 68 if (!program) { 69 fprintf(stderr, "Failed to compile program: %s\n", syntax_error); 70 free(syntax_error); 71 72 return 1; 73 } 74 75 /* initialize default module search path */ 76 uc_search_path_init(&config.module_search_path); 77 78 /* initialize VM context */ 79 uc_vm_t vm = { 0 }; 80 uc_vm_init(&vm, &config); 81 82 /* load standard library into global VM scope */ 83 uc_stdlib_load(uc_vm_scope_get(&vm)); 84 85 /* add global variables x and y to VM scope */ 86 ucv_object_add(uc_vm_scope_get(&vm), "x", ucv_int64_new(123)); 87 ucv_object_add(uc_vm_scope_get(&vm), "y", ucv_int64_new(456)); 88 89 /* execute compiled program function */ 90 uc_value_t *last_expression_result = NULL; 91 int return_code = uc_vm_execute(&vm, program, &last_expression_result); 92 93 /* release program */ 94 uc_program_put(program); 95 96 /* handle return status */ 97 switch (return_code) { 98 case STATUS_OK: 99 exit_code = 0; 100 101 char *s = ucv_to_string(&vm, last_expression_result); 102 103 printf("Program finished successfully.\n"); 104 printf("Function return value is %s\n", s); 105 free(s); 106 break; 107 108 case STATUS_EXIT: 109 exit_code = (int)ucv_int64_get(last_expression_result); 110 111 printf("The invoked program called exit().\n"); 112 printf("Exit code is %d\n", exit_code); 113 break; 114 115 case ERROR_COMPILE: 116 exit_code = 1; 117 118 printf("A compilation error occurred while running the program\n"); 119 break; 120 121 case ERROR_RUNTIME: 122 exit_code = 2; 123 124 printf("A runtime error occurred while running the program\n"); 125 break; 126 } 127 128 /* free last expression result */ 129 ucv_put(last_expression_result); 130 131 /* free VM context */ 132 uc_vm_free(&vm); 133 134 /* free search module path vector */ 135 uc_search_path_free(&config.module_search_path); 136 137 return exit_code; 138 } 139
This page was automatically generated by LXR 0.3.1. • OpenWrt