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

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt