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

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt