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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt