X-Git-Url: https://git.lukelau.me/?p=timetravel.git;a=blobdiff_plain;f=Programs.hs;h=3f05aaa24da2b35ef94b1eeea4a62d60b9127bd1;hp=22cc493266ed76f1658de6330343412202fcaab6;hb=HEAD;hpb=9a09a1be54afb5e15dfea8a8676bd6e1941b37ec diff --git a/Programs.hs b/Programs.hs index 22cc493..3f05aaa 100644 --- a/Programs.hs +++ b/Programs.hs @@ -2,11 +2,28 @@ 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)))))))))) + +boolNot :: Statement +boolNot = Print (Not (Const (B False)))