feat: no constructors for expressions and statements for saccharine

This commit is contained in:
2026-02-09 19:37:49 -05:00
parent d24cbd9d86
commit c4ba80924a
4 changed files with 22 additions and 50 deletions

View File

@@ -55,7 +55,7 @@ func reduceLet(s *saccharine.LetStatement, e lambda.Expression) lambda.Expressio
if len(s.Parameters) == 0 { if len(s.Parameters) == 0 {
value = encodeExpression(s.Body) value = encodeExpression(s.Body)
} else { } else {
value = encodeAbstraction(saccharine.NewAbstraction(s.Parameters, s.Body)) value = encodeAbstraction(&saccharine.Abstraction{Parameters: s.Parameters, Body: s.Body})
} }
return lambda.NewApplication( return lambda.NewApplication(
@@ -112,15 +112,15 @@ func encodeExpression(s saccharine.Expression) lambda.Expression {
func decodeExression(l lambda.Expression) saccharine.Expression { func decodeExression(l lambda.Expression) saccharine.Expression {
switch l := l.(type) { switch l := l.(type) {
case lambda.Variable: case lambda.Variable:
return saccharine.NewAtom(l.Name()) return &saccharine.Atom{Name: l.Name()}
case lambda.Abstraction: case lambda.Abstraction:
return saccharine.NewAbstraction( return &saccharine.Abstraction{
[]string{l.Parameter()}, Parameters: []string{l.Parameter()},
decodeExression(l.Body())) Body: decodeExression(l.Body())}
case lambda.Application: case lambda.Application:
return saccharine.NewApplication( return &saccharine.Application{
decodeExression(l.Abstraction()), Abstraction: decodeExression(l.Abstraction()),
[]saccharine.Expression{decodeExression(l.Argument())}) Arguments: []saccharine.Expression{decodeExression(l.Argument())}}
default: default:
panic(fmt.Errorf("unknown expression type: %T", l)) panic(fmt.Errorf("unknown expression type: %T", l))
} }

View File

@@ -1,7 +1,7 @@
package saccharine package saccharine
type Expression interface { type Expression interface {
isExpression() expression()
} }
/** ------------------------------------------------------------------------- */ /** ------------------------------------------------------------------------- */
@@ -25,25 +25,7 @@ type Clause struct {
Returns Expression Returns Expression
} }
func (Abstraction) isExpression() {} func (Abstraction) expression() {}
func (Application) isExpression() {} func (Application) expression() {}
func (Atom) isExpression() {} func (Atom) expression() {}
func (Clause) isExpression() {} func (Clause) expression() {}
/** ------------------------------------------------------------------------- */
func NewAbstraction(parameter []string, body Expression) *Abstraction {
return &Abstraction{Parameters: parameter, Body: body}
}
func NewApplication(abstraction Expression, arguments []Expression) *Application {
return &Application{Abstraction: abstraction, Arguments: arguments}
}
func NewAtom(name string) *Atom {
return &Atom{Name: name}
}
func NewClause(statements []Statement, returns Expression) *Clause {
return &Clause{Statements: statements, Returns: returns}
}

View File

@@ -83,7 +83,7 @@ func parseAbstraction(i *TokenIterator) (*Abstraction, error) {
} else if body, err := parseExpression(i); err != nil { } else if body, err := parseExpression(i); err != nil {
return nil, err return nil, err
} else { } else {
return NewAbstraction(parameters, body), nil return &Abstraction{Parameters: parameters, Body: body}, nil
} }
}) })
} }
@@ -97,7 +97,7 @@ func parseApplication(i *TokenIterator) (*Application, error) {
} else if _, err := parseToken(i, TokenCloseParen, true); err != nil { } else if _, err := parseToken(i, TokenCloseParen, true); err != nil {
return nil, fmt.Errorf("no closing brackets (col %d): %w", i.MustGet().Column, err) return nil, fmt.Errorf("no closing brackets (col %d): %w", i.MustGet().Column, err)
} else { } else {
return NewApplication(expressions[0], expressions[1:]), nil return &Application{Abstraction: expressions[0], Arguments: expressions[1:]}, nil
} }
}) })
} }
@@ -106,7 +106,7 @@ func parseAtom(i *TokenIterator) (*Atom, error) {
if tok, err := parseToken(i, TokenAtom, true); err != nil { if tok, err := parseToken(i, TokenAtom, true); err != nil {
return nil, fmt.Errorf("no variable (col %d): %w", i.Index(), err) return nil, fmt.Errorf("no variable (col %d): %w", i.Index(), err)
} else { } else {
return NewAtom(tok.Value), nil return &Atom{Name: tok.Value}, nil
} }
} }
@@ -155,7 +155,7 @@ func parseClause(i *TokenIterator, braces bool) (*Clause, error) {
} }
} }
return NewClause(stmts[:len(stmts)-1], last.Value), nil return &Clause{Statements: stmts[:len(stmts)-1], Returns: last.Value}, nil
} }
func parseExpression(i *TokenIterator) (Expression, error) { func parseExpression(i *TokenIterator) (Expression, error) {
@@ -186,7 +186,7 @@ func parseLet(i *TokenIterator) (*LetStatement, error) {
} else if body, err := parseExpression(i); err != nil { } else if body, err := parseExpression(i); err != nil {
return nil, err return nil, err
} else { } else {
return NewLet(parameters[0], parameters[1:], body), nil return &LetStatement{Name: parameters[0], Parameters: parameters[1:], Body: body}, nil
} }
}) })
} }
@@ -195,7 +195,7 @@ func parseDeclare(i *TokenIterator) (*DeclareStatement, error) {
if value, err := parseExpression(i); err != nil { if value, err := parseExpression(i); err != nil {
return nil, err return nil, err
} else { } else {
return NewDeclare(value), nil return &DeclareStatement{Value: value}, nil
} }
} }

View File

@@ -1,7 +1,7 @@
package saccharine package saccharine
type Statement interface { type Statement interface {
IsStatement() statement()
} }
/** ------------------------------------------------------------------------- */ /** ------------------------------------------------------------------------- */
@@ -16,15 +16,5 @@ type DeclareStatement struct {
Value Expression Value Expression
} }
func (LetStatement) IsStatement() {} func (LetStatement) statement() {}
func (DeclareStatement) IsStatement() {} func (DeclareStatement) statement() {}
/** ------------------------------------------------------------------------- */
func NewLet(name string, parameters []string, body Expression) *LetStatement {
return &LetStatement{Name: name, Parameters: parameters, Body: body}
}
func NewDeclare(value Expression) *DeclareStatement {
return &DeclareStatement{Value: value}
}