docs: document remaining packages and simplify AST types #45
@@ -55,7 +55,7 @@ func reduceLet(s *saccharine.LetStatement, e lambda.Expression) lambda.Expressio
|
||||
if len(s.Parameters) == 0 {
|
||||
value = encodeExpression(s.Body)
|
||||
} else {
|
||||
value = encodeAbstraction(saccharine.NewAbstraction(s.Parameters, s.Body))
|
||||
value = encodeAbstraction(&saccharine.Abstraction{Parameters: s.Parameters, Body: s.Body})
|
||||
}
|
||||
|
||||
return lambda.NewApplication(
|
||||
@@ -112,15 +112,15 @@ func encodeExpression(s saccharine.Expression) lambda.Expression {
|
||||
func decodeExression(l lambda.Expression) saccharine.Expression {
|
||||
switch l := l.(type) {
|
||||
case lambda.Variable:
|
||||
return saccharine.NewAtom(l.Name())
|
||||
return &saccharine.Atom{Name: l.Name()}
|
||||
case lambda.Abstraction:
|
||||
return saccharine.NewAbstraction(
|
||||
[]string{l.Parameter()},
|
||||
decodeExression(l.Body()))
|
||||
return &saccharine.Abstraction{
|
||||
Parameters: []string{l.Parameter()},
|
||||
Body: decodeExression(l.Body())}
|
||||
case lambda.Application:
|
||||
return saccharine.NewApplication(
|
||||
decodeExression(l.Abstraction()),
|
||||
[]saccharine.Expression{decodeExression(l.Argument())})
|
||||
return &saccharine.Application{
|
||||
Abstraction: decodeExression(l.Abstraction()),
|
||||
Arguments: []saccharine.Expression{decodeExression(l.Argument())}}
|
||||
default:
|
||||
panic(fmt.Errorf("unknown expression type: %T", l))
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package saccharine
|
||||
|
||||
type Expression interface {
|
||||
isExpression()
|
||||
expression()
|
||||
}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
@@ -25,25 +25,7 @@ type Clause struct {
|
||||
Returns Expression
|
||||
}
|
||||
|
||||
func (Abstraction) isExpression() {}
|
||||
func (Application) isExpression() {}
|
||||
func (Atom) isExpression() {}
|
||||
func (Clause) isExpression() {}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
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}
|
||||
}
|
||||
func (Abstraction) expression() {}
|
||||
func (Application) expression() {}
|
||||
func (Atom) expression() {}
|
||||
func (Clause) expression() {}
|
||||
|
||||
@@ -83,7 +83,7 @@ func parseAbstraction(i *TokenIterator) (*Abstraction, error) {
|
||||
} else if body, err := parseExpression(i); err != nil {
|
||||
return nil, err
|
||||
} 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 {
|
||||
return nil, fmt.Errorf("no closing brackets (col %d): %w", i.MustGet().Column, err)
|
||||
} 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 {
|
||||
return nil, fmt.Errorf("no variable (col %d): %w", i.Index(), err)
|
||||
} 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) {
|
||||
@@ -186,7 +186,7 @@ func parseLet(i *TokenIterator) (*LetStatement, error) {
|
||||
} else if body, err := parseExpression(i); err != nil {
|
||||
return nil, err
|
||||
} 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 {
|
||||
return nil, err
|
||||
} else {
|
||||
return NewDeclare(value), nil
|
||||
return &DeclareStatement{Value: value}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package saccharine
|
||||
|
||||
type Statement interface {
|
||||
IsStatement()
|
||||
statement()
|
||||
}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
@@ -16,15 +16,5 @@ type DeclareStatement struct {
|
||||
Value Expression
|
||||
}
|
||||
|
||||
func (LetStatement) IsStatement() {}
|
||||
func (DeclareStatement) IsStatement() {}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
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}
|
||||
}
|
||||
func (LetStatement) statement() {}
|
||||
func (DeclareStatement) statement() {}
|
||||
|
||||
Reference in New Issue
Block a user