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