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

Sources/ucode/examples/state-reuse.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 #define MULTILINE_STRING(...) #__VA_ARGS__
 25 
 26 static const char *program_code = MULTILINE_STRING(
 27         {%
 28                 let n = global.value || 1;
 29 
 30                 print("Current value is " + n + "\n");
 31 
 32                 global.value = n * 2;
 33         %}
 34 );
 35 
 36 static uc_parse_config_t config = {
 37         .strict_declarations = false,
 38         .lstrip_blocks = true,
 39         .trim_blocks = true
 40 };
 41 
 42 int main(int argc, char **argv)
 43 {
 44         int exit_code = 0;
 45 
 46         /* create a source buffer containing the program code */
 47         uc_source_t *src = uc_source_new_buffer("my program", strdup(program_code), strlen(program_code));
 48 
 49         /* compile source buffer into function */
 50         char *syntax_error = NULL;
 51         uc_program_t *program = uc_compile(&config, src, &syntax_error);
 52 
 53         /* release source buffer */
 54         uc_source_put(src);
 55 
 56         /* check if compilation failed */
 57         if (!program) {
 58                 fprintf(stderr, "Failed to compile program: %s\n", syntax_error);
 59 
 60                 return 1;
 61         }
 62 
 63         /* initialize VM context */
 64         uc_vm_t vm = { 0 };
 65         uc_vm_init(&vm, &config);
 66 
 67         /* load standard library into global VM scope */
 68         uc_stdlib_load(uc_vm_scope_get(&vm));
 69 
 70         /* execute compiled program function five times */
 71         for (int i = 0; i < 5; i++) {
 72                 printf("Iteration %d: ", i + 1);
 73 
 74                 /* execute program function */
 75                 int return_code = uc_vm_execute(&vm, program, NULL);
 76 
 77                 /* handle return status */
 78                 if (return_code == ERROR_COMPILE || return_code == ERROR_RUNTIME) {
 79                         printf("An error occurred while running the program\n");
 80                         exit_code = 1;
 81                         break;
 82                 }
 83 
 84                 /* perform GC step */
 85                 ucv_gc(&vm);
 86         }
 87 
 88         /* release program function */
 89         uc_program_put(program);
 90 
 91         /* free VM context */
 92         uc_vm_free(&vm);
 93 
 94         return exit_code;
 95 }
 96 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt