sandbox.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <kuroko/kuroko.h>
4 #include <kuroko/vm.h>
5 #include <kuroko/util.h>
6 
7 #include "simple-repl.h"
8 
9 int main(int argc, char * argv[]) {
10  /* Disable default modules */
11  krk_initVM(KRK_GLOBAL_NO_DEFAULT_MODULES);
12 
13  /* Set up our module context. */
14  krk_startModule("__main__");
15 
16  int retval = 0;
17  if (argc > 1) {
18  KrkValue result = krk_interpret(argv[1], "<stdin>");
19  if (!IS_NONE(result)) {
20  if (IS_INTEGER(result)) {
21  retval = AS_INTEGER(result);
22  }
23  KrkClass * type = krk_getType(result);
24  if (type->_reprer) {
25  krk_push(result);
26  result = krk_callDirect(type->_reprer, 1);
27  }
28  if (IS_STRING(result)) {
29  fprintf(stdout, " => %s\n", AS_CSTRING(result));
30  }
31  } else if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) {
32  retval = 1;
33  }
34  } else {
35  runSimpleRepl();
36  }
37 
38  krk_freeVM();
39  return retval;
40 }
41 
Top-level header with configuration macros.
Type object.
Definition: object.h:215
KrkObj * _reprer
__repr__ Called to create a reproducible string representation of an instance
Definition: object.h:229
int flags
Definition: vm.h:165
void krk_freeVM(void)
Release resources from the VM.
Definition: vm.c:953
void krk_initVM(int flags)
Initialize the VM at program startup.
Definition: vm.c:868
Stack reference or primative value.
KrkClass * krk_getType(KrkValue value)
Get the class representing a value.
Definition: vm.c:240
Utilities for creating native bindings.
Core API for the bytecode virtual machine.
krk_threadLocal KrkThreadState krk_currentThread
Thread-local VM state.
KrkValue krk_interpret(const char *src, const char *fromFile)
Compile and execute a source code input.
Definition: vm.c:3219
void krk_push(KrkValue value)
Push a stack value.
Definition: vm.c:118
KrkInstance * krk_startModule(const char *name)
Set up a new module object in the current thread.
Definition: vm.c:3209
KrkValue krk_callDirect(KrkObj *callable, int argCount)
Call a closure or native function with argCount arguments.
Definition: vm.c:740