docs: codec

This commit is contained in:
2026-02-09 19:45:58 -05:00
parent c6186d9c80
commit 08bf248745
2 changed files with 10 additions and 7 deletions

View File

@@ -20,7 +20,7 @@ func GetRegistry() *registry.Registry {
// Marshalers // Marshalers
(registry.RegisterCodec(r, lambda.Marshaler{}, "lambda")) (registry.RegisterCodec(r, lambda.Marshaler{}, "lambda"))
(registry.RegisterCodec(r, saccharine.Marshaler{}, "saccharine")) (registry.RegisterCodec(r, saccharine.Codec{}, "saccharine"))
return r return r
} }

View File

@@ -1,14 +1,15 @@
// Package "saccharine" provides a simple language built on top of λ-calculus,
// to facilitate productive coding using it.
package saccharine package saccharine
import ( import (
"git.maximhutz.com/max/lambda/pkg/codec" "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) tokens, err := scan(s)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -17,8 +18,10 @@ func (m Marshaler) Decode(s string) (Expression, error) {
return parse(tokens) 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 return stringifyExpression(e), nil
} }
var _ codec.Codec[Expression] = (*Marshaler)(nil) var _ codec.Codec[Expression] = (*Codec)(nil)