|
| 1 | +package main |
| 2 | + |
| 3 | +// #include <stdlib.h> |
| 4 | +// #include <stdio.h> |
| 5 | +import "C" |
| 6 | +import ( |
| 7 | + "quill/internal/jsonapi" |
| 8 | + "sync" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + interpreters = make(map[int]*jsonapi.QuillInterpreter) |
| 14 | + nextID = 1 |
| 15 | + mu sync.Mutex |
| 16 | +) |
| 17 | + |
| 18 | +//export quill_new_interpreter |
| 19 | +func quill_new_interpreter(source *C.char) *C.char { |
| 20 | + goSource := C.GoString(source) |
| 21 | + interp, result := jsonapi.NewQuillInterpreter(goSource) |
| 22 | + |
| 23 | + if interp != nil { |
| 24 | + mu.Lock() |
| 25 | + interpreters[nextID] = interp |
| 26 | + nextID++ |
| 27 | + mu.Unlock() |
| 28 | + } |
| 29 | + |
| 30 | + cResult := C.CString(result) |
| 31 | + if cResult == nil { |
| 32 | + return C.CString(`{"success":false,"error":"Failed to allocate C string"}`) |
| 33 | + } |
| 34 | + return cResult |
| 35 | +} |
| 36 | + |
| 37 | +//export quill_step |
| 38 | +func quill_step(interpID C.int) *C.char { |
| 39 | + mu.Lock() |
| 40 | + interp, exists := interpreters[int(interpID)] |
| 41 | + mu.Unlock() |
| 42 | + |
| 43 | + if !exists { |
| 44 | + return C.CString(`{"success":false,"error":"Invalid interpreter ID"}`) |
| 45 | + } |
| 46 | + |
| 47 | + result := interp.Step() |
| 48 | + cResult := C.CString(result) |
| 49 | + if cResult == nil { |
| 50 | + return C.CString(`{"success":false,"error":"Failed to allocate C string"}`) |
| 51 | + } |
| 52 | + return cResult |
| 53 | +} |
| 54 | + |
| 55 | +//export quill_handle_choice |
| 56 | +func quill_handle_choice(interpID C.int, choiceIndex C.int) *C.char { |
| 57 | + mu.Lock() |
| 58 | + interp, exists := interpreters[int(interpID)] |
| 59 | + mu.Unlock() |
| 60 | + |
| 61 | + if !exists { |
| 62 | + return C.CString(`{"success":false,"error":"Invalid interpreter ID"}`) |
| 63 | + } |
| 64 | + |
| 65 | + result := interp.HandleChoiceInput(int(choiceIndex)) |
| 66 | + cResult := C.CString(result) |
| 67 | + if cResult == nil { |
| 68 | + return C.CString(`{"success":false,"error":"Failed to allocate C string"}`) |
| 69 | + } |
| 70 | + return cResult |
| 71 | +} |
| 72 | + |
| 73 | +//export quill_get_state |
| 74 | +func quill_get_state(interpID C.int) *C.char { |
| 75 | + mu.Lock() |
| 76 | + interp, exists := interpreters[int(interpID)] |
| 77 | + mu.Unlock() |
| 78 | + |
| 79 | + if !exists { |
| 80 | + return C.CString(`{"success":false,"error":"Invalid interpreter ID"}`) |
| 81 | + } |
| 82 | + |
| 83 | + result := interp.GetState() |
| 84 | + cResult := C.CString(result) |
| 85 | + if cResult == nil { |
| 86 | + return C.CString(`{"success":false,"error":"Failed to allocate C string"}`) |
| 87 | + } |
| 88 | + return cResult |
| 89 | +} |
| 90 | + |
| 91 | +//export quill_is_ended |
| 92 | +func quill_is_ended(interpID C.int) *C.char { |
| 93 | + mu.Lock() |
| 94 | + interp, exists := interpreters[int(interpID)] |
| 95 | + mu.Unlock() |
| 96 | + |
| 97 | + if !exists { |
| 98 | + return C.CString(`{"success":false,"error":"Invalid interpreter ID"}`) |
| 99 | + } |
| 100 | + |
| 101 | + result := interp.IsEnded() |
| 102 | + cResult := C.CString(result) |
| 103 | + if cResult == nil { |
| 104 | + return C.CString(`{"success":false,"error":"Failed to allocate C string"}`) |
| 105 | + } |
| 106 | + return cResult |
| 107 | +} |
| 108 | + |
| 109 | +//export quill_is_waiting_for_choice |
| 110 | +func quill_is_waiting_for_choice(interpID C.int) *C.char { |
| 111 | + mu.Lock() |
| 112 | + interp, exists := interpreters[int(interpID)] |
| 113 | + mu.Unlock() |
| 114 | + |
| 115 | + if !exists { |
| 116 | + return C.CString(`{"success":false,"error":"Invalid interpreter ID"}`) |
| 117 | + } |
| 118 | + |
| 119 | + result := interp.IsWaitingForChoice() |
| 120 | + cResult := C.CString(result) |
| 121 | + if cResult == nil { |
| 122 | + return C.CString(`{"success":false,"error":"Failed to allocate C string"}`) |
| 123 | + } |
| 124 | + return cResult |
| 125 | +} |
| 126 | + |
| 127 | +//export quill_parse_only |
| 128 | +func quill_parse_only(source *C.char) *C.char { |
| 129 | + goSource := C.GoString(source) |
| 130 | + result := jsonapi.ParseOnly(goSource) |
| 131 | + cResult := C.CString(result) |
| 132 | + if cResult == nil { |
| 133 | + return C.CString(`{"success":false,"error":"Failed to allocate C string"}`) |
| 134 | + } |
| 135 | + return cResult |
| 136 | +} |
| 137 | + |
| 138 | +//export quill_free_string |
| 139 | +func quill_free_string(str *C.char) { |
| 140 | + C.free(unsafe.Pointer(str)) |
| 141 | +} |
| 142 | + |
| 143 | +//export quill_free_interpreter |
| 144 | +func quill_free_interpreter(interpID C.int) { |
| 145 | + mu.Lock() |
| 146 | + delete(interpreters, int(interpID)) |
| 147 | + mu.Unlock() |
| 148 | +} |
| 149 | + |
| 150 | +func main() {} |
0 commit comments