|
3 | 3 | import com.kcl.api.Spec.*; |
4 | 4 |
|
5 | 5 | public class API implements Service { |
6 | | - static { |
7 | | - // Load the dynamic library (the .dll, .so, or .dylib file) |
8 | | - System.loadLibrary("kcl_lib_jni"); |
9 | | - } |
| 6 | + static { |
| 7 | + // Load the dynamic library (the .dll, .so, or .dylib file) |
| 8 | + System.loadLibrary("kcl_lib_jni"); |
| 9 | + } |
| 10 | + |
| 11 | + private native byte[] callNative(byte[] call, byte[] args); |
10 | 12 |
|
11 | | - private native byte[] callNative(byte[] call, byte[] args); |
| 13 | + public API() { |
| 14 | + } |
12 | 15 |
|
13 | | - // Example method that calls a native function |
14 | | - public ExecProgram_Result execProgram(ExecProgram_Args args) throws Exception { |
15 | | - return ExecProgram_Result.parseFrom(call("KclvmService.ExecProgram", args.toByteArray())); |
16 | | - } |
| 16 | + /** |
| 17 | + * Parses a program given a set of file paths and returns the result. |
| 18 | + * * |
| 19 | + * <p> |
| 20 | + * Example usage: |
| 21 | + * |
| 22 | + * <pre>{@code |
| 23 | + * |
| 24 | + * import com.kcl.api.*; |
| 25 | + * import com.kcl.ast.*; |
| 26 | + * import com.kcl.util.JsonUtil; |
| 27 | + * |
| 28 | + * // Create an instance of the API class |
| 29 | + * API api = new API(); |
| 30 | + * // Parse the program by providing the file paths to the API |
| 31 | + * ParseProgram_Result result = api.parseProgram( |
| 32 | + * ParseProgram_Args.newBuilder().addPaths("a.k").build() |
| 33 | + * ); |
| 34 | + * // Print the JSON representation of the AST (Abstract Syntax Tree) |
| 35 | + * System.out.println(result.getAstJson()); |
| 36 | + * // Deserialize the JSON string into a Program object |
| 37 | + * Program program = JsonUtil.deserializeProgram(result.getAstJson()); |
| 38 | + * }</pre> |
| 39 | + * |
| 40 | + * @param args the arguments specifying the file paths to be parsed. |
| 41 | + * @return the result of parsing the program and parse errors, including the AST in JSON format. |
| 42 | + * @throws Exception if an error occurs during the remote procedure call. |
| 43 | + */ |
| 44 | + @Override |
| 45 | + public ParseProgram_Result parseProgram(ParseProgram_Args args) throws Exception { |
| 46 | + return ParseProgram_Result.parseFrom(call("KclvmService.ParseProgram", args.toByteArray())); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public ParseFile_Result parseFile(ParseFile_Args args) throws Exception { |
| 51 | + return ParseFile_Result.parseFrom(call("KclvmService.ParseFile", args.toByteArray())); |
| 52 | + } |
| 53 | + |
| 54 | + public ExecProgram_Result execProgram(ExecProgram_Args args) throws Exception { |
| 55 | + return ExecProgram_Result.parseFrom(call("KclvmService.ExecProgram", args.toByteArray())); |
| 56 | + } |
17 | 57 |
|
18 | 58 | @Override |
19 | 59 | public OverrideFile_Result overrideFile(OverrideFile_Args args) throws Exception { |
@@ -65,26 +105,26 @@ public Test_Result test(Test_Args args) throws Exception { |
65 | 105 | return Test_Result.parseFrom(call("KclvmService.Test", args.toByteArray())); |
66 | 106 | } |
67 | 107 |
|
68 | | - private byte[] call(String name, byte[] args) throws Exception { |
69 | | - byte[] result = callNative(name.getBytes(), args); |
70 | | - if (result != null && startsWith(result, "ERROR")) { |
71 | | - throw new Error(result.toString()); |
72 | | - } |
73 | | - return result; |
74 | | - } |
75 | | - |
76 | | - static boolean startsWith(byte[] array, String prefix) { |
77 | | - byte[] prefixBytes = prefix.getBytes(); |
78 | | - if (array.length < prefixBytes.length) { |
79 | | - return false; |
80 | | - } |
81 | | - |
82 | | - for (int i = 0; i < prefixBytes.length; i++) { |
83 | | - if (array[i] != prefixBytes[i]) { |
84 | | - return false; |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - return true; |
89 | | - } |
| 108 | + private byte[] call(String name, byte[] args) throws Exception { |
| 109 | + byte[] result = callNative(name.getBytes(), args); |
| 110 | + if (result != null && startsWith(result, "ERROR")) { |
| 111 | + throw new java.lang.Error(result.toString()); |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | + |
| 116 | + static boolean startsWith(byte[] array, String prefix) { |
| 117 | + byte[] prefixBytes = prefix.getBytes(); |
| 118 | + if (array.length < prefixBytes.length) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + for (int i = 0; i < prefixBytes.length; i++) { |
| 123 | + if (array[i] != prefixBytes[i]) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return true; |
| 129 | + } |
90 | 130 | } |
0 commit comments