docs: codec
This commit is contained in:
@@ -20,7 +20,7 @@ func GetRegistry() *registry.Registry {
|
||||
|
||||
// Marshalers
|
||||
(registry.RegisterCodec(r, lambda.Marshaler{}, "lambda"))
|
||||
(registry.RegisterCodec(r, saccharine.Marshaler{}, "saccharine"))
|
||||
(registry.RegisterCodec(r, saccharine.Codec{}, "saccharine"))
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
// Package "saccharine" provides a simple language built on top of λ-calculus,
|
||||
// to facilitate productive coding using it.
|
||||
package saccharine
|
||||
|
||||
import (
|
||||
"git.maximhutz.com/max/lambda/pkg/codec"
|
||||
)
|
||||
|
||||
type Marshaler struct{}
|
||||
// A Codec is a [codec.Codec] that serializes Saccharine expressions.
|
||||
type Codec struct{}
|
||||
|
||||
func (m Marshaler) Decode(s string) (Expression, error) {
|
||||
// Decode parses a string as Saccharine source code. Returns an error
|
||||
// if it cannot.
|
||||
func (c Codec) Decode(s string) (Expression, error) {
|
||||
tokens, err := scan(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -17,8 +18,10 @@ func (m Marshaler) Decode(s string) (Expression, error) {
|
||||
return parse(tokens)
|
||||
}
|
||||
|
||||
func (m Marshaler) Encode(e Expression) (string, error) {
|
||||
// Encode turns a Saccharine expression into a string. Returns an error if it
|
||||
// cannot.
|
||||
func (c Codec) Encode(e Expression) (string, error) {
|
||||
return stringifyExpression(e), nil
|
||||
}
|
||||
|
||||
var _ codec.Codec[Expression] = (*Marshaler)(nil)
|
||||
var _ codec.Codec[Expression] = (*Codec)(nil)
|
||||
|
||||
Reference in New Issue
Block a user