X-Git-Url: http://git.lukelau.me/?p=timetravel.git;a=blobdiff_plain;f=Programs.hs;fp=Programs.hs;h=14f066bb17c4c8a74d4f0b57bf75f9ff54177c66;hp=22cc493266ed76f1658de6330343412202fcaab6;hb=00b953eca8f8cdb1e39cf37c14c3705af3fc1afe;hpb=9a09a1be54afb5e15dfea8a8676bd6e1941b37ec diff --git a/Programs.hs b/Programs.hs index 22cc493..14f066b 100644 --- a/Programs.hs +++ b/Programs.hs @@ -2,11 +2,25 @@ module Programs where import AST -testProg :: Statement -testProg = Seq (Assign "x" (Const (I 0))) loop +-- | Infinitely increments and prints x (can't be written to a file sadly) +increment :: Statement +increment = Seq (Assign "x" (Const (I 0))) loop where loop = Seq (Print (Var "x")) (Seq (Assign "x" (Add (Var "x") (Const (I 1)))) loop) -testTry :: Statement -testTry = Try (Print (Add (Const (I 3)) (Const (B True)))) + +-- | Catches a type error and prints 0 +tryCatch :: Statement +tryCatch = Try (Print (Add (Const (I 3)) (Const (B True)))) (Print (Const (I 0))) + +-- | Calculates nth fibonacci (5 by default) and stores result in 'x' +fibonacci :: Statement +fibonacci = Seq (Assign "n" (Const (I 5))) + (Seq (Assign "x" (Const (I 1))) + (Seq (Assign "y" (Const (I 0))) + (While (Gt (Var "n") (Const (I 0))) + (Seq (Assign "tmp" (Var "x")) + (Seq (Assign "x" (Add (Var "x") (Var "y"))) + (Seq (Assign "y" (Var "x")) + (Assign "n" (Sub (Var "n") (Const (I 1))))))))))