Skip to content

Commit dcc02bc

Browse files
committed
Add JSON Odin
1 parent 4e0e8a6 commit dcc02bc

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

common/commands.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ MCS_BUILD = mcs -debug- -optimize+ -out:$@ $^
2727
MLTON_BUILD = mlton -output $@ $^
2828
NIM_CLANG_BUILD = nim c -o:$@ --cc:clang $(NIM_FLAGS) $^
2929
NIM_GCC_BUILD = nim c -o:$@ --cc:gcc $(NIM_FLAGS) $^
30+
ODIN_BUILD = odin build $^ -file -out:$@ -o:speed -no-bounds-check
3031
RUSTC_BUILD = rustc $(RUSTC_FLAGS) -C opt-level=3 -C lto -C codegen-units=1 -C panic=abort -C strip=symbols -o $@ $^
3132
RUST_CLIPPY = clippy-driver -o $@.clippy $^
3233
SWIFTC_BUILD = swiftc -parse-as-library -O -lto=llvm-full -cross-module-optimization $(LIBNOTIFY_FLAGS) -import-objc-header ../common/libnotify/libnotify.h -o $@ $^

json/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ executables := \
6464
json-core/target/Release/net8.0/json-core \
6565
target/json_zig \
6666
target/json_yajl_gcc \
67-
target/json_yajl_clang
67+
target/json_yajl_clang \
68+
target/json_odin
6869

6970
# TODO: Enable Julia test after https://github.com/quinnj/JSON3.jl/issues/276 fixed
7071
artifacts := $(executables) \
@@ -331,6 +332,9 @@ target/json_yajl_gcc: test_yajl.c | target libnotify
331332
target/json_yajl_clang: test_yajl.c | target libnotify
332333
$(CLANG_BUILD) -lyajl
333334

335+
target/json_odin: test.odin
336+
$(ODIN_BUILD)
337+
334338
# Run
335339

336340
.PHONY: run

json/test.odin

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import "core:encoding/json"
4+
import "core:fmt"
5+
import "core:net"
6+
import "core:os"
7+
import "core:os/os2"
8+
9+
Coordinate :: struct {
10+
x, y, z: f64,
11+
}
12+
13+
TestStruct :: struct {
14+
coordinates: []Coordinate,
15+
}
16+
17+
notify :: proc(msg: string) {
18+
sock, _ := net.dial_tcp("127.0.0.1:9001")
19+
defer net.close(sock)
20+
net.send(sock, transmute([]byte)msg)
21+
}
22+
23+
calc :: proc(s: string) -> Coordinate {
24+
j: TestStruct
25+
if err := json.unmarshal_string(s, &j); err != nil {
26+
panic(fmt.tprintf("%v", err))
27+
}
28+
29+
coord := Coordinate{}
30+
for c in j.coordinates {
31+
coord.x += c.x
32+
coord.y += c.y
33+
coord.z += c.z
34+
}
35+
36+
return coord
37+
}
38+
39+
main :: proc() {
40+
context.allocator = context.temp_allocator
41+
defer free_all(context.temp_allocator)
42+
43+
right := Coordinate{2.0, 0.5, 0.25}
44+
for v in ([]string {
45+
`{"coordinates":[{"x":2.0,"y":0.5,"z":0.25}]}`,
46+
`{"coordinates":[{"y":0.5,"x":2.0,"z":0.25}]}`,
47+
}) {
48+
left := calc(v)
49+
assert(left == right)
50+
}
51+
52+
s, ok := os.read_entire_file_from_filename("/tmp/1.json")
53+
if !ok {
54+
panic("Failed to read 1.json")
55+
}
56+
57+
notify(fmt.tprintf("Odin\t%d", os2.get_pid()))
58+
results := calc(string(s))
59+
notify("stop")
60+
61+
fmt.println(results)
62+
}

0 commit comments

Comments
 (0)