11-- 2020-05-17, AA
22
33-- HACK
4- --
5- -- This module add a number of standard ambient methods such as `print` to the beginning of the
6- -- file. This provides some backward compatibility with prior test cases as well as minimizes some
7- -- clutter.
8- --
9- -- If these methods are unused they are eliminated by the optimization passes in the further passes.
10-
11- -- TODO
12- --
13- -- Move this into a '.trp' file of the form
14- --
15- -- ```
16- -- let fun print x = fwrite (stdout authority, (toString x) ^"\n")
17- -- ...
18- -- in () end
19- -- ```
20- --
21- -- Which, similar to below, after parsing has the `dummy` value replaced by the actual program. This
22- -- preamble can then be specified at compile-time.
23-
24- module AddAmbientMethods (addAmbientMethods ) where
4+ -- This module add a number of standard
5+ -- ambient methods such as `print` to the
6+ -- beginning of the file. This provides some
7+ -- backward compatibility with prior test cases
8+ -- as well as minimizes some clutter
9+
10+ -- If these methods are unused they are
11+ -- eliminated by the optimization passes in
12+ -- the further passes.
13+
14+ module AddAmbientMethods (addAmbientMethods ) where
2515
2616import Basics
27- import Direct
17+ import Direct
2818import TroupePositionInfo
2919
30- printStringDecl :: FunDecl
31- printStringDecl = FunDecl " printString "
20+ printDecl :: FunDecl
21+ printDecl = FunDecl " print "
3222 [Lambda [VarPattern " x" ] $
33- Let [ ValDecl (VarPattern " fd" ) (App (Var " stdout" ) [Var " authority" ]) NoPos
34- , ValDecl (VarPattern " x'" ) (Bin Concat (Var " x" ) (Lit $ LString " \\ n" )) NoPos
35- ]
36- (App (Var " fwrite" ) [Tuple [Var " fd" , Var " x'" ]])
23+ Let [ValDecl (VarPattern " out" ) (App (Var " getStdout" ) [Var " authority" ]) NoPos ]
24+ (App (Var " fprintln" ) [Tuple [Var " out" , Var " x" ]])
3725 ] NoPos
3826
39- printDecl :: FunDecl
40- printDecl = FunDecl " print"
41- [Lambda [ VarPattern " x" ] $
42- (App (Var " printString" ) [App (Var " toString" ) [Var " x" ]])
27+ printWithLabelsDecl :: FunDecl
28+ printWithLabelsDecl = FunDecl " printWithLabels"
29+ [Lambda [VarPattern " x" ] $
30+ Let [ValDecl (VarPattern " out" ) (App (Var " getStdout" ) [Var " authority" ]) NoPos ]
31+ (App (Var " fprintlnWithLabels" ) [Tuple [Var " out" , Var " x" ]])
4332 ] NoPos
4433
45- printLDecl :: FunDecl
46- printLDecl = FunDecl " printL"
47- [Lambda [ VarPattern " x" ] $
48- (App (Var " printString" ) [App (Var " toStringL" ) [Var " x" ]])
49- ] NoPos
5034
51- inputLineDecl :: FunDecl
52- inputLineDecl = FunDecl " inputLine "
53- [Lambda [ VarPattern " _ " ] $
54- Let [ValDecl (VarPattern " fd " ) (App (Var " stdin " ) [Var " authority" ]) NoPos ]
55- (App (Var " freadln " ) [App (Var " stdin " ) [ Var " authority " ]])
35+ printStringDecl :: FunDecl
36+ printStringDecl = FunDecl " printString "
37+ [Lambda [VarPattern " x " ] $
38+ Let [ValDecl (VarPattern " out " ) (App (Var " getStdout " ) [Var " authority" ]) NoPos ]
39+ (App (Var " fwrite " ) [Tuple [ Var " out " , Bin Concat (Var " x " ) ( Lit ( LString " \\ n " )) ]])
5640 ] NoPos
5741
58- addAmbientMethods :: Prog -> Prog
59- addAmbientMethods (Prog imports atoms t) =
60- let t' = Let [FunDecs [printStringDecl,printDecl,printLDecl,inputLineDecl]] t
61- in Prog imports atoms t'
42+
43+
44+ addAmbientMethods :: Prog -> Prog
45+ addAmbientMethods (Prog imports atoms t) =
46+ let t' = Let [FunDecs [printDecl,printWithLabelsDecl,printStringDecl]] t
47+ in Prog imports atoms t'
0 commit comments