main.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // main.c
  3. // GravityObjC
  4. //
  5. // Created by Marco Bambini on 07/02/21.
  6. //
  7. #include "gravity_compiler.h"
  8. #include "gravity_macros.h"
  9. #include "gravity_core.h"
  10. #include "gravity_vm.h"
  11. #include "gravity_vmmacros.h"
  12. #include "gravity_objc.h"
  13. #include "console.h"
  14. int main (void) {
  15. // setup delegate
  16. gravity_delegate_t delegate = {
  17. .error_callback = report_error,
  18. };
  19. // read source file from the shared top level directory
  20. size_t size = 0;
  21. const char *path = current_filepath(__FILE__, "main.gravity");
  22. const char *source_code = file_read(path, &size);
  23. if (!source_code) return -1;
  24. // compile source code into bytecode
  25. gravity_compiler_t *compiler = gravity_compiler_create(&delegate);
  26. gravity_closure_t *closure = gravity_compiler_run(compiler, source_code, size, 0, false, true);
  27. // transfer bytecode into a newly created VM
  28. gravity_vm *vm = gravity_vm_new(&delegate);
  29. gravity_compiler_transfer(compiler, vm);
  30. gravity_compiler_free(compiler);
  31. if (!closure) goto cleanup;
  32. // register ObjC bridge into VM
  33. objc_register(vm);
  34. // run VM and print result
  35. if (gravity_vm_runmain(vm, closure)) {
  36. gravity_value_t result = gravity_vm_result(vm);
  37. double t = gravity_vm_time(vm);
  38. // print result as string
  39. gravity_value_t s = convert_value2string(vm, result);
  40. printf("RESULT: %s (%.4f ms)\n\n", VALUE_AS_CSTRING(s), t);
  41. }
  42. cleanup:
  43. gravity_vm_free(vm);
  44. gravity_core_free();
  45. return 0;
  46. }