feat: stuff
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/cli"
|
||||
"git.maximhutz.com/max/lambda/internal/config"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
||||
)
|
||||
|
||||
@@ -31,24 +30,24 @@ func main() {
|
||||
// Turn tokens into syntax tree.
|
||||
expression, err := saccharine.GetTree(tokens)
|
||||
cli.HandleError(err)
|
||||
logger.Info("Parsed syntax tree.", "tree", lambda.Stringify(expression))
|
||||
logger.Info("Parsed syntax tree.", "tree", saccharine.Stringify(expression))
|
||||
|
||||
// Reduce expression.
|
||||
start := time.Now()
|
||||
|
||||
if options.Explanation {
|
||||
fmt.Println(lambda.Stringify(expression))
|
||||
fmt.Println(saccharine.Stringify(expression))
|
||||
}
|
||||
|
||||
for lambda.ReduceOnce(&expression) {
|
||||
logger.Info("Reduction.", "tree", lambda.Stringify(expression))
|
||||
if options.Explanation {
|
||||
fmt.Println(" =", lambda.Stringify(expression))
|
||||
}
|
||||
}
|
||||
// for lambda.ReduceOnce(&expression) {
|
||||
// logger.Info("Reduction.", "tree", saccharine.Stringify(expression))
|
||||
// if options.Explanation {
|
||||
// fmt.Println(" =", saccharine.Stringify(expression))
|
||||
// }
|
||||
// }
|
||||
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
|
||||
fmt.Println(lambda.Stringify(expression))
|
||||
fmt.Println(saccharine.Stringify(expression))
|
||||
fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms")
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package saccharine
|
||||
|
||||
type Node interface {
|
||||
IsNode()
|
||||
Accept(Visitor)
|
||||
}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
@@ -36,6 +36,14 @@ func NewVariable(name string) *Variable {
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
func (_ Abstraction) IsNode() {}
|
||||
func (_ Application) IsNode() {}
|
||||
func (_ Variable) IsNode() {}
|
||||
func (a *Abstraction) Accept(x Visitor) { x.VisitAbstraction(a) }
|
||||
func (a *Application) Accept(x Visitor) { x.VisitApplication(a) }
|
||||
func (v *Variable) Accept(x Visitor) { x.VisitVariable(v) }
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
type Visitor interface {
|
||||
VisitAbstraction(*Abstraction)
|
||||
VisitApplication(*Application)
|
||||
VisitVariable(*Variable)
|
||||
}
|
||||
|
||||
36
pkg/saccharine/stringify.go
Normal file
36
pkg/saccharine/stringify.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package saccharine
|
||||
|
||||
import "strings"
|
||||
|
||||
type stringifyVisitor struct {
|
||||
builder strings.Builder
|
||||
}
|
||||
|
||||
func (v *stringifyVisitor) VisitVariable(a *Variable) {
|
||||
v.builder.WriteString(a.Name)
|
||||
}
|
||||
|
||||
func (v *stringifyVisitor) VisitAbstraction(f *Abstraction) {
|
||||
v.builder.WriteRune('\\')
|
||||
v.builder.WriteString(strings.Join(f.Parameters, " "))
|
||||
v.builder.WriteRune('.')
|
||||
f.Body.Accept(v)
|
||||
}
|
||||
|
||||
func (v *stringifyVisitor) VisitApplication(c *Application) {
|
||||
v.builder.WriteRune('(')
|
||||
c.Abstraction.Accept(v)
|
||||
|
||||
for _, argument := range c.Arguments {
|
||||
v.builder.WriteRune(' ')
|
||||
argument.Accept(v)
|
||||
}
|
||||
|
||||
v.builder.WriteRune(')')
|
||||
}
|
||||
|
||||
func Stringify(n Node) string {
|
||||
b := &stringifyVisitor{}
|
||||
n.Accept(b)
|
||||
return b.builder.String()
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
0 := \f x.x
|
||||
inc n := \f x.(f (n f x))
|
||||
inc n := \f x.f (n f x)
|
||||
add n m := m inc n
|
||||
mult n m := m (n f)
|
||||
exp m n := m n
|
||||
|
||||
@@ -12,5 +12,5 @@
|
||||
)
|
||||
\n f x.(f (n f x))
|
||||
)
|
||||
\f x.x
|
||||
\f x.((((((x))))))
|
||||
)
|
||||
Reference in New Issue
Block a user