28 lines
622 B
Go
28 lines
622 B
Go
package saccharine
|
|
|
|
import (
|
|
"git.maximhutz.com/max/lambda/pkg/codec"
|
|
)
|
|
|
|
// A Codec is a [codec.Codec] that serializes Saccharine expressions.
|
|
type Codec struct{}
|
|
|
|
// 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
|
|
}
|
|
|
|
return parse(tokens)
|
|
}
|
|
|
|
// 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] = (*Codec)(nil)
|