Rename :serialize() -> :serialized()

This commit is contained in:
Bruce Hill 2024-11-29 19:39:17 -05:00
parent 0d6ef67a01
commit e2fa11b7fe
4 changed files with 22 additions and 26 deletions

View File

@ -2727,9 +2727,9 @@ CORD compile(env_t *env, ast_t *ast)
auto call = Match(ast, MethodCall); auto call = Match(ast, MethodCall);
type_t *self_t = get_type(env, call->self); type_t *self_t = get_type(env, call->self);
if (streq(call->name, "serialize")) { if (streq(call->name, "serialized")) {
if (call->args) 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]"), "){", return CORD_all("generic_serialize((", compile_declaration(self_t, "[1]"), "){",
compile(env, call->self), "}, ", compile_type_info(env, self_t), ")"); compile(env, call->self), "}, ", compile_type_info(env, self_t), ")");
} }

View File

@ -10,12 +10,12 @@ original value.
## Serializing ## 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: will return an array of bytes that encode the value's data:
```tomo ```tomo
value := Int64(5) value := Int64(5)
>> serialized := value:serialize() >> serialized := value:serialized()
= [0x0A] : [Byte] = [0x0A] : [Byte]
``` ```
@ -30,7 +30,7 @@ is a placeholder, but it looks like this:
```tomo ```tomo
i := 123 i := 123
bytes := i:serialize() bytes := i:serialized()
roundtripped := DESERIALIZE(bytes):Int roundtripped := DESERIALIZE(bytes):Int
>> roundtripped >> roundtripped
@ -58,7 +58,7 @@ c := @Cycle("A")
c.next = @Cycle("B", next=c) c.next = @Cycle("B", next=c)
>> c >> c
= @Cycle(name="A", next=@Cycle(name="B", next=@~1)) = @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] = [0x02, 0x02, 0x41, 0x01, 0x04, 0x02, 0x42, 0x01, 0x02] : [Byte]
>> roundtrip := DESERIALIZE(serialized):@Cycle >> roundtrip := DESERIALIZE(serialized):@Cycle
= @Cycle(name="A", next=@Cycle(name="B", next=@~1)) : @Cycle = @Cycle(name="A", next=@Cycle(name="B", next=@~1)) : @Cycle

View File

@ -6,50 +6,50 @@ enum MyEnum(Zero, One(x:Int), Two(x:Num, y:Text))
func main(): func main():
do: do:
>> obj := now() >> obj := now()
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):Moment == obj >> DESERIALIZE(bytes):Moment == obj
= yes = yes
do: do:
>> obj := Int64(123) >> obj := Int64(123)
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):Int64 == obj >> DESERIALIZE(bytes):Int64 == obj
= yes = yes
do: do:
>> obj := 5 >> obj := 5
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):Int == obj >> DESERIALIZE(bytes):Int == obj
= yes = yes
do: do:
>> obj := 9999999999999999999999999999999999999999999999999999 >> obj := 9999999999999999999999999999999999999999999999999999
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):Int == obj >> DESERIALIZE(bytes):Int == obj
= yes = yes
do: do:
>> obj := "Héllo" >> obj := "Héllo"
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):Text >> DESERIALIZE(bytes):Text
>> DESERIALIZE(bytes):Text == obj >> DESERIALIZE(bytes):Text == obj
= yes = yes
do: do:
>> obj := [Int64(10), Int64(20), Int64(30)]:reversed() >> obj := [Int64(10), Int64(20), Int64(30)]:reversed()
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):[Int64] == obj >> DESERIALIZE(bytes):[Int64] == obj
= yes = yes
do: do:
>> obj := yes >> obj := yes
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):Bool == obj >> DESERIALIZE(bytes):Bool == obj
= yes = yes
do: do:
>> obj := @[10, 20] >> obj := @[10, 20]
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> roundtrip := DESERIALIZE(bytes):@[Int] >> roundtrip := DESERIALIZE(bytes):@[Int]
>> roundtrip == obj >> roundtrip == obj
= no = no
@ -58,44 +58,44 @@ func main():
do: do:
>> obj := {"A":10, "B":20; fallback={"C":30}} >> obj := {"A":10, "B":20; fallback={"C":30}}
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):{Text:Int} == obj >> DESERIALIZE(bytes):{Text:Int} == obj
= yes = yes
do: do:
>> obj := @Foo("root") >> obj := @Foo("root")
>> obj.next = @Foo("abcdef", next=obj) >> obj.next = @Foo("abcdef", next=obj)
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):@Foo >> DESERIALIZE(bytes):@Foo
= @Foo(name="root", next=@Foo(name="abcdef", next=@~1)) = @Foo(name="root", next=@Foo(name="abcdef", next=@~1))
do: do:
>> obj := MyEnum.Two(123, "OKAY") >> obj := MyEnum.Two(123, "OKAY")
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):MyEnum == obj >> DESERIALIZE(bytes):MyEnum == obj
= yes = yes
do: do:
>> obj := "Hello"? >> obj := "Hello"?
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):Text? == obj >> DESERIALIZE(bytes):Text? == obj
= yes = yes
do: do:
>> obj := {10, 20, 30} >> obj := {10, 20, 30}
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):{Int} == obj >> DESERIALIZE(bytes):{Int} == obj
= yes = yes
do: do:
>> obj := NONE:Num >> obj := NONE:Num
>> bytes := obj:serialize() >> bytes := obj:serialized()
>> DESERIALIZE(bytes):Num? == obj >> DESERIALIZE(bytes):Num? == obj
= yes = yes
do: do:
cases := [0, -1, 1, 10, 100000, 999999999999999999999999999] cases := [0, -1, 1, 10, 100000, 999999999999999999999999999]
for i in cases: for i in cases:
>> bytes := i:serialize() >> bytes := i:serialized()
>> DESERIALIZE(bytes):Int == i >> DESERIALIZE(bytes):Int == i
= yes = yes

View File

@ -722,10 +722,6 @@ type_t *get_type(env_t *env, ast_t *ast)
case FunctionCall: { case FunctionCall: {
auto call = Match(ast, 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); type_t *fn_type_t = get_type(env, call->fn);
if (!fn_type_t) if (!fn_type_t)
code_err(call->fn, "I couldn't find this function"); 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: { case MethodCall: {
auto call = Match(ast, MethodCall); auto call = Match(ast, MethodCall);
if (streq(call->name, "serialize")) if (streq(call->name, "serialized")) // Data serialization
return Type(ArrayType, Type(ByteType)); return Type(ArrayType, Type(ByteType));
type_t *self_value_t = value_type(get_type(env, call->self)); type_t *self_value_t = value_type(get_type(env, call->self));