feat: fun little program
This commit is contained in:
5
Makefile
5
Makefile
@@ -11,4 +11,7 @@ thunk: it
|
||||
@ ./lambda.exe - < ./samples/thunk.txt
|
||||
|
||||
saccharine: it
|
||||
@ ./lambda.exe -x - < ./samples/saccharine.txt
|
||||
@ ./lambda.exe - < ./samples/saccharine.txt
|
||||
|
||||
saccharineX: it
|
||||
@ ./lambda.exe -x - < ./samples/saccharine.txt > saccharine.out
|
||||
@@ -44,15 +44,20 @@ func main() {
|
||||
fmt.Println(lambda.Stringify(compiled))
|
||||
}
|
||||
|
||||
steps := 0
|
||||
|
||||
for lambda.ReduceOnce(&compiled) {
|
||||
logger.Info("reduction", "tree", lambda.Stringify(compiled))
|
||||
if options.Explanation {
|
||||
fmt.Println(" =", lambda.Stringify(compiled))
|
||||
}
|
||||
steps++
|
||||
}
|
||||
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
|
||||
fmt.Println(lambda.Stringify(compiled))
|
||||
fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms")
|
||||
fmt.Fprintln(os.Stderr, "Steps:", steps)
|
||||
fmt.Fprintln(os.Stderr, "Speed:", float32(steps)/(float32(elapsed)/1000), "ops")
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ func parseClause(i *TokenIterator, braces bool) (*ast.Clause, error) {
|
||||
} else if len(stmts) == 0 {
|
||||
return nil, fmt.Errorf("no statements in clause")
|
||||
} else if last, ok = stmts[len(stmts)-1].(*ast.DeclareStatement); !ok {
|
||||
return nil, fmt.Errorf("this clause contains no final return value")
|
||||
return nil, fmt.Errorf("this clause contains no final return value (col %d)", i.MustGet().Column)
|
||||
}
|
||||
|
||||
if braces {
|
||||
|
||||
@@ -1,19 +1,52 @@
|
||||
true x y := x
|
||||
false x y := y
|
||||
T x y := x
|
||||
F x y := y
|
||||
if b t e := (b t e)
|
||||
|
||||
pair a b := \c.(c a b)
|
||||
left p := (p true)
|
||||
false p := (p false)
|
||||
left p := (p T)
|
||||
right p := (p F)
|
||||
|
||||
zero 0 1 x := x
|
||||
print n := (n 0 1 end)
|
||||
|
||||
inc n := \0 1 x.{
|
||||
initial := (pair true x)
|
||||
onZero p := (pair false ((left p 1 0) (right p)))
|
||||
onOne p := (pair (left p) (1 (right p)))
|
||||
|
||||
(n onZero onOne initial)
|
||||
}
|
||||
fix f := (\x.(f (x x)) \x.(f (x x)))
|
||||
|
||||
(print zero)
|
||||
some x := (pair T x)
|
||||
none := \.F
|
||||
isfull := left
|
||||
unwrap := right
|
||||
|
||||
nil := none
|
||||
push i l := (some (pair i l))
|
||||
peek l := (left (unwrap l))
|
||||
pop l := (right (unwrap l))
|
||||
|
||||
inc := (fix \self l.{
|
||||
(if (isfull l)
|
||||
(if (peek l)
|
||||
(push F (self (pop l)))
|
||||
(push T (pop l))
|
||||
)
|
||||
(push T nil)
|
||||
)
|
||||
})
|
||||
|
||||
print := (fix \self l.{
|
||||
(if (isfull l)
|
||||
((if (peek l) 1 0) (self (pop l)))
|
||||
END
|
||||
)
|
||||
})
|
||||
|
||||
one := (push T nil)
|
||||
double N := (push F N)
|
||||
|
||||
N :=
|
||||
(double (double (double (double (double
|
||||
(double (double (double (double (double
|
||||
(double (double (double (double (double
|
||||
(double (double (double (double (double
|
||||
(double (double (double (double (double
|
||||
(double (double (double (double (double
|
||||
one))))))))))))))))))))))))))))))
|
||||
|
||||
(print N)
|
||||
|
||||
Reference in New Issue
Block a user