summaryrefslogtreecommitdiff
path: root/assignment-13/ufpl.icl
blob: 73d1ff07da19f11442374f814e45ea3c2e4b9c2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
implementation module ufpl

import StdArray
import StdBool
import StdClass
from StdFunc import const, flip, id, o
from StdGeneric import :: Bimap{..}, bimapId; bm :== bimapId
import StdInt
import StdList
import StdMisc
import StdOverloaded
import StdString
import StdTuple

from Data.Func import $
import Data.List

import Arduino
import Bootstrap
import C
import Util

typedfun :: CType String -> String
typedfun t f = flip (+++) f case t of
	CTBool   -> "b"
	CTInt  s -> sig s +++ "i"
	CTChar s -> sig s +++ "c"
	CTLong s -> sig s +++ "l"
where
	sig :: Signedness -> String
	sig Sig = ""
	sig Unsig = "u"

append :: Shares Shares -> Shares
append NoShares ss = ss
append (Shares s ss) sss = Shares s (append ss sss)

removeDupShares :: Shares -> Shares
removeDupShares NoShares = NoShares
removeDupShares (Shares s ss) = if exists id (Shares s) (removeDupShares ss)
where
	exists = any (\s` -> s.sname == s`) (sharesMap (\s -> s.sname) ss)

sharesMap :: (A.t rw: (Shared t rw) -> a) Shares -> [a]
sharesMap _ NoShares = []
sharesMap f (Shares s ss) = [f s:sharesMap f ss]

instance allShares [t] | allShares t
where
	allShares` [] = NoShares
	allShares` [x:xs] = append (allShares` x) (allShares` xs)

instance allShares (Expr t rw)
where
	allShares` (ELit _) = NoShares
	allShares` (EShared s) = Shares s NoShares
	allShares` (a +. b) = append (allShares` a) (allShares` b)
	allShares` (a -. b) = append (allShares` a) (allShares` b)
	allShares` (a *. b) = append (allShares` a) (allShares` b)
	allShares` (a /. b) = append (allShares` a) (allShares` b)
	allShares` (EEq  _ a b) = append (allShares` a) (allShares` b)
	allShares` (ELt  _ a b) = append (allShares` a) (allShares` b)
	allShares` (EAnd _ a b) = append (allShares` a) (allShares` b)
	allShares` (EOr  _ a b) = append (allShares` a) (allShares` b)
	allShares` (EIf  b t e) = append (allShares` b) (append (allShares` t) (allShares` e))

instance allShares Trigger
where
	allShares` (Change t) = allShares` t
	allShares` (s ?= t) = append (allShares` s) (allShares` t)
	allShares` (a ?& b) = append (allShares` a) (allShares` b)
	allShares` (a ?| b) = append (allShares` a) (allShares` b)

instance allShares Rule
where
	allShares` (s <# e) = append (allShares` s) (allShares` e)
	allShares` (When t rs) = foldr1 append [allShares` t:map allShares` rs]
	allShares` (t >>> rs) = foldr1 append [allShares` t:map allShares` rs]
	allShares` (SetCursor (c,r)) = append (allShares` c) (allShares` r)
	allShares` (Print v) = allShares` v

instance allShares NamedRule
where
	allShares` (_ :=: r) = allShares` r

instance Expr Int where litExpr i = CEInt i
instance Expr Bool where litExpr b = CEBool b
instance Expr Char where litExpr c = CEChar c

lit :: (t -> Expr t RO)
lit = ELit

(?) infix 4 :: (Expr Bool rwa) (Expr t rwb, Expr t rwc) -> Expr t RO
(?) b (t,e) = EIf b t e

(==.) infix 4 :: (Expr t rwa) (Expr t rwb) -> Expr Bool RO | Expr, == t
(==.) a b = EEq bm a b

(<.) infix 4 :: (Expr t rwa) (Expr t rwb) -> Expr Bool RO | Expr, < t
(<.) a b = ELt bm a b

(>.) infix 4 :: (Expr t rwa) (Expr t rwb) -> Expr Bool RO | Expr, < t
(>.) a b = ELt bm b a

(&&.) infixr 3 :: (Expr Bool rwa) (Expr Bool rwb) -> Expr Bool RO
(&&.) a b = EAnd bm a b

(||.) infixr 4 :: (Expr Bool rwa) (Expr Bool rwb) -> Expr Bool RO
(||.) a b = EOr bm a b

instance :. Rule   where (:.) a b = [a,b]
instance :. [Rule] where (:.) r rs = [r:rs]

instance ||| NamedRule   where (|||) a b = [a,b]
instance ||| [NamedRule] where (|||) r rs = [r:rs]

instance gen (Expr t rw) CExpr | Expr t
where
	gen (ELit v) = litExpr v
	gen (EShared s) = CEGlobal ("s" +++ s.sname +++ ".val")
	gen (a +. b) = CEInfix "+" (gen a) (gen b)
	gen (a -. b) = CEInfix "-" (gen a) (gen b)
	gen (a *. b) = CEInfix "*" (gen a) (gen b)
	gen (a /. b) = CEInfix "/" (gen a) (gen b)
	gen (EEq  _ a b) = CEInfix "==" (gen a) (gen b)
	gen (ELt  _ a b) = CEInfix "<"  (gen a) (gen b)
	gen (EAnd _ a b) = CEInfix "&&" (gen a) (gen b)
	gen (EOr  _ a b) = CEInfix "||" (gen a) (gen b)
	gen (EIf b t e) = CEIf (gen b) (gen t) (gen e)

instance gen Trigger CExpr
where
	gen (Change (EShared shr)) = CEApp (typedfun shr.stype "dirty") [CERef (CEGlobal ("s" +++ shr.sname))]
	gen (EShared shr ?= e) = CEInfix "&&" (gen (Change (EShared shr))) (CEInfix "==" (CEGlobal ("s" +++ shr.sname +++ ".val")) (gen e))
	gen (a ?& b) = CEInfix "&&" (gen a) (gen b)
	gen (a ?| b) = CEInfix "||" (gen a) (gen b)

instance gen Rule CBody
where
	gen (EShared shr <# val) = CBExpr (CEApp (typedfun shr.stype "set") [CERef (CEGlobal ("s" +++ shr.sname)), gen val])
	gen (When b rs) = CBIf (gen b) (gen rs) CBEmpty
	gen (t >>> rs) = CBIf (gen t) (gen rs) CBEmpty
	gen (SetCursor (c,r)) = CBExpr (CEApp "lcd.setCursor" [gen c, gen r])
	gen (Print e) = CBExpr (CEApp "lcd.print" [gen e])

instance gen [r] CBody | gen r CBody
where
	gen [] = CBEmpty
	gen rs = foldr1 CBSeq (map gen rs)

instance gen NamedRule CFun
where
	gen (name :=: rs) =
		{ params = []
		, body   = gen rs
		, fresh  = 0
		, type   = CTVoid
		, name   = "t" +++ name
		}

instance gen (Shared t rw) CVar
where
	gen shr =
		{ name  = "s" +++ shr.sname
		, type  = CTStruct (typedfun shr.stype "share")
		, value = shr.srepr.map_to shr.sinit
		}

instance gen [NamedRule] String
where
	gen rs = foldl1 (+++) $
		[rts] ++
		["\n" +++ printToString var \\ var <- sharesMap genv (allShares rs)] ++
		["\n" +++ printToString (genf rule) \\ rule <- rs]
	where
		genf :: (NamedRule -> CFun)
		genf = gen

		genv :: ((Shared t rw) -> CVar)
		genv = gen

Start :: String
Start = gen example_score

example_score :: [NamedRule]
example_score =
		"a" :=: b0 ?= true >>> [scorea <# scorea +. lit 1]
	||| "b" :=: b1 ?= true >>> [scoreb <# scoreb +. lit 1]
	||| "r" :=: b2 ?= true >>> [scorea <# lit 0, scoreb <# lit 0]
	||| "print" :=: Change scorea ?| Change scoreb >>>
		SetCursor (lit 0, lit 0) :.
		Print scorea :.
		Print (lit '-') :.
		Print scoreb
where
	scorea = rwInt "scorea" 0
	scoreb = rwInt "scoreb" 0

example_countdown :: [NamedRule]
example_countdown =
		"time" :=:
			When (millis -. DELAY >. counter)
				(counter <# counter +. DELAY :.
				seconds <# seconds -. lit 1 :.
				SetCursor (lit 0, lit 0) :.
				Print minutes :.
				Print (lit ':') :.
				Print seconds)
		:.  When (seconds ==. lit 0) (seconds <# lit 60 :. minutes <# minutes -. lit 1)
	||| "status" :=:
			When (seconds ==. lit 60 &&. minutes ==. lit 0) [running <# false]
where
	running = rwBool "running" False
	minutes = rwUInt "minutes" 0
	seconds = rwUInt "seconds" 0
	counter = rwULong "counter" 1000 // If set to 0, this will overflow on first iteration
	DELAY = lit 1000