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 71 return 1; 72 } 73 74 /* initialize default module search path */ 75 uc_search_path_init(&config.module_search_path); 76 77 /* initialize VM context */ 78 uc_vm_t vm = { 0 }; 79 uc_vm_init(&vm, &config); 80 81 /* load standard library into global VM scope */ 82 uc_stdlib_load(uc_vm_scope_get(&vm)); 83 84 /* add global variables x and y to VM scope */ 85 ucv_object_add(uc_vm_scope_get(&vm), "x", ucv_int64_new(123)); 86 ucv_object_add(uc_vm_scope_get(&vm), "y", ucv_int64_new(456)); 87 88 /* execute compiled program function */ 89 uc_value_t *last_expression_result = NULL; 90 int return_code = uc_vm_execute(&vm, program, &last_expression_result); 91 92 /* release program */ 93 uc_program_put(program); 94 95 /* handle return status */ 96 switch (return_code) { 97 case STATUS_OK: 98 exit_code = 0; 99 100 char *s = ucv_to_string(&vm, last_expression_result); 101 102 printf("Program finished successfully.\n"); 103 printf("Function return value is %s\n", s); 104 free(s); 105 break; 106 107 case STATUS_EXIT: 108 exit_code = (int)ucv_int64_get(last_expression_result); 109 110 printf("The invoked program called exit().\n"); 111 printf("Exit code is %d\n", exit_code); 112 break; 113 114 case ERROR_COMPILE: 115 exit_code = 1; 116 117 printf("A compilation error occurred while running the program\n"); 118 break; 119 120 case ERROR_RUNTIME: 121 exit_code = 2; 122 123 printf("A runtime error occurred while running the program\n"); 124 break; 125 } 126 127 /* free last expression result */ 128 ucv_put(last_expression_result); 129 130 /* free VM context */ 131 uc_vm_free(&vm); 132 133 /* free search module path vector */ 134 uc_search_path_free(&config.module_search_path); 135 136 return exit_code; 137 } 138
This page was automatically generated by LXR 0.3.1. • OpenWrt