diff --git a/compile.c b/compile.c index 4e094df..3307378 100644 --- a/compile.c +++ b/compile.c @@ -2727,9 +2727,9 @@ CORD compile(env_t *env, ast_t *ast) auto call = Match(ast, MethodCall); type_t *self_t = get_type(env, call->self); - if (streq(call->name, "serialize")) { + if (streq(call->name, "serialized")) { if (call->args) - code_err(ast, ":serialize() doesn't take any arguments"); + code_err(ast, ":serialized() doesn't take any arguments"); return CORD_all("generic_serialize((", compile_declaration(self_t, "[1]"), "){", compile(env, call->self), "}, ", compile_type_info(env, self_t), ")"); } diff --git a/docs/serialization.md b/docs/serialization.md index 6f0749c..9ffb605 100644 --- a/docs/serialization.md +++ b/docs/serialization.md @@ -10,12 +10,12 @@ original value. ## Serializing -To serialize data, simply call the method `:serialize()` on any value and it +To serialize data, simply call the method `:serialized()` on any value and it will return an array of bytes that encode the value's data: ```tomo value := Int64(5) ->> serialized := value:serialize() +>> serialized := value:serialized() = [0x0A] : [Byte] ``` @@ -30,7 +30,7 @@ is a placeholder, but it looks like this: ```tomo i := 123 -bytes := i:serialize() +bytes := i:serialized() roundtripped := DESERIALIZE(bytes):Int >> roundtripped @@ -58,7 +58,7 @@ c := @Cycle("A") c.next = @Cycle("B", next=c) >> c = @Cycle(name="A", next=@Cycle(name="B", next=@~1)) ->> serialized := c:serialize() +>> serialized := c:serialized() = [0x02, 0x02, 0x41, 0x01, 0x04, 0x02, 0x42, 0x01, 0x02] : [Byte] >> roundtrip := DESERIALIZE(serialized):@Cycle = @Cycle(name="A", next=@Cycle(name="B", next=@~1)) : @Cycle diff --git a/test/serialization.tm b/test/serialization.tm index 51b5b2a..33dd2a7 100644 --- a/test/serialization.tm +++ b/test/serialization.tm @@ -6,50 +6,50 @@ enum MyEnum(Zero, One(x:Int), Two(x:Num, y:Text)) func main(): do: >> obj := now() - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):Moment == obj = yes do: >> obj := Int64(123) - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):Int64 == obj = yes do: >> obj := 5 - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):Int == obj = yes do: >> obj := 9999999999999999999999999999999999999999999999999999 - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):Int == obj = yes do: >> obj := "Héllo" - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):Text >> DESERIALIZE(bytes):Text == obj = yes do: >> obj := [Int64(10), Int64(20), Int64(30)]:reversed() - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):[Int64] == obj = yes do: >> obj := yes - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):Bool == obj = yes do: >> obj := @[10, 20] - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> roundtrip := DESERIALIZE(bytes):@[Int] >> roundtrip == obj = no @@ -58,44 +58,44 @@ func main(): do: >> obj := {"A":10, "B":20; fallback={"C":30}} - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):{Text:Int} == obj = yes do: >> obj := @Foo("root") >> obj.next = @Foo("abcdef", next=obj) - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):@Foo = @Foo(name="root", next=@Foo(name="abcdef", next=@~1)) do: >> obj := MyEnum.Two(123, "OKAY") - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):MyEnum == obj = yes do: >> obj := "Hello"? - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):Text? == obj = yes do: >> obj := {10, 20, 30} - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):{Int} == obj = yes do: >> obj := NONE:Num - >> bytes := obj:serialize() + >> bytes := obj:serialized() >> DESERIALIZE(bytes):Num? == obj = yes do: cases := [0, -1, 1, 10, 100000, 999999999999999999999999999] for i in cases: - >> bytes := i:serialize() + >> bytes := i:serialized() >> DESERIALIZE(bytes):Int == i = yes diff --git a/typecheck.c b/typecheck.c index ba22830..e602c77 100644 --- a/typecheck.c +++ b/typecheck.c @@ -722,10 +722,6 @@ type_t *get_type(env_t *env, ast_t *ast) case FunctionCall: { auto call = Match(ast, FunctionCall); - // HACK: - if (call->fn->tag == Var && streq(Match(call->fn, Var)->name, "serialize")) - return Type(ArrayType, Type(ByteType)); - type_t *fn_type_t = get_type(env, call->fn); if (!fn_type_t) code_err(call->fn, "I couldn't find this function"); @@ -747,7 +743,7 @@ type_t *get_type(env_t *env, ast_t *ast) case MethodCall: { auto call = Match(ast, MethodCall); - if (streq(call->name, "serialize")) + if (streq(call->name, "serialized")) // Data serialization return Type(ArrayType, Type(ByteType)); type_t *self_value_t = value_type(get_type(env, call->self));