summaryrefslogtreecommitdiff
path: root/assignment-13/uFPL
diff options
context:
space:
mode:
Diffstat (limited to 'assignment-13/uFPL')
-rw-r--r--assignment-13/uFPL/Arduino.dcl10
-rw-r--r--assignment-13/uFPL/Arduino.icl16
-rw-r--r--assignment-13/uFPL/Bootstrap.icl5
-rw-r--r--assignment-13/uFPL/C.dcl13
-rw-r--r--assignment-13/uFPL/C.icl14
-rw-r--r--assignment-13/uFPL/Examples.dcl7
-rw-r--r--assignment-13/uFPL/Examples.icl70
-rw-r--r--assignment-13/uFPL/Sim.dcl4
-rw-r--r--assignment-13/uFPL/Sim.icl2
9 files changed, 90 insertions, 51 deletions
diff --git a/assignment-13/uFPL/Arduino.dcl b/assignment-13/uFPL/Arduino.dcl
deleted file mode 100644
index efed603..0000000
--- a/assignment-13/uFPL/Arduino.dcl
+++ /dev/null
@@ -1,10 +0,0 @@
-definition module uFPL.Arduino
-
-from StdOverloaded import class toString
-
-from uFPL.Util import class print
-
-:: Button = B0 | B1 | B2 | B3 | B4 | B5
-
-instance toString Button
-instance print Button
diff --git a/assignment-13/uFPL/Arduino.icl b/assignment-13/uFPL/Arduino.icl
deleted file mode 100644
index 2fbab30..0000000
--- a/assignment-13/uFPL/Arduino.icl
+++ /dev/null
@@ -1,16 +0,0 @@
-implementation module uFPL.Arduino
-
-import StdOverloaded
-
-import uFPL.Util
-
-instance toString Button
-where
- toString B0 = "B0"
- toString B1 = "B1"
- toString B2 = "B2"
- toString B3 = "B3"
- toString B4 = "B4"
- toString B5 = "B5"
-
-instance print Button where print b = print (toString b)
diff --git a/assignment-13/uFPL/Bootstrap.icl b/assignment-13/uFPL/Bootstrap.icl
index 1ca75ac..4429cf5 100644
--- a/assignment-13/uFPL/Bootstrap.icl
+++ b/assignment-13/uFPL/Bootstrap.icl
@@ -7,7 +7,6 @@ import StdString
from Data.Func import $
-import uFPL.Arduino
import uFPL.C
import uFPL
@@ -127,9 +126,9 @@ where
"bool " +++ short +++ "dirty(struct " +++ short +++ "share *share) {" +:
" if (share->dirty) {" +:
" share->dirty--;" +:
- " return 1;" +:
+ " return true;" +:
" }" +:
- " return 0;" +:
+ " return false;" +:
"}"
instance zero CProg where zero = {bootstrap=rts, globals=sharesMap gen predefShares, funs=[]}
diff --git a/assignment-13/uFPL/C.dcl b/assignment-13/uFPL/C.dcl
index 3b25183..938707b 100644
--- a/assignment-13/uFPL/C.dcl
+++ b/assignment-13/uFPL/C.dcl
@@ -2,7 +2,6 @@ definition module uFPL.C
from Data.Maybe import :: Maybe
-from uFPL.Arduino import :: Button
from uFPL.Util import class print
:: Signedness = Sig | Unsig
@@ -13,28 +12,23 @@ from uFPL.Util import class print
| CTInt Signedness
| CTLong Signedness
| CTVoid
- | CTArray CType
| CTStruct String
:: CExpr
- = CEButton Button
- | CEGlobal String
+ = CEGlobal String
| CEInfix String CExpr CExpr
| CEApp String [CExpr]
| CEBool Bool
| CEInt Int
| CEChar Char
- | CEBArray Int [Bool]
- | CEIArray Int [Int]
+ | CEString String
| CEIf CExpr CExpr CExpr
| CERef CExpr
- | CEDeref CExpr
| CEStruct [(String, CExpr)]
:: CBody
= CBReturn (Maybe CExpr)
| CBIf CExpr CBody [(CExpr, CBody)] (Maybe CBody)
- | CBWhile CExpr CBody
| CBAssign String CExpr
| CBSeq CBody CBody
| CBEmpty
@@ -49,8 +43,7 @@ from uFPL.Util import class print
}
:: CFun =
- { params :: [(Int, CType)]
- , body :: CBody
+ { body :: CBody
, type :: CType
, name :: String
}
diff --git a/assignment-13/uFPL/C.icl b/assignment-13/uFPL/C.icl
index 87f8573..61e572e 100644
--- a/assignment-13/uFPL/C.icl
+++ b/assignment-13/uFPL/C.icl
@@ -12,7 +12,6 @@ import Data.List
import Data.Maybe
import Data.Tuple
-import uFPL.Arduino
import uFPL.Util
(`seq`) infixr 0 :: CBody CBody -> CBody
@@ -26,7 +25,7 @@ gDefault{|Bool|} = False
gDefault{|Char|} = '\x00'
gDefault{|Maybe|} _ = Nothing
-derive gDefault Button, Signedness, CType, CExpr, CBody, CFun
+derive gDefault Signedness, CType, CExpr, CBody, CFun
instance print Signedness
where
@@ -40,25 +39,19 @@ where
print (CTInt s) = print s o print " int"
print (CTLong s) = print s o print " long"
print CTVoid = print "void"
- print (CTArray t) = print "*" o print t
print (CTStruct s) = print "struct " o print s
-instance print (Int,CType) // Variable
-where
- print (i,t) = print t o print " v" o print i
-
instance print CExpr
where
- print (CEButton b) = print b
print (CEGlobal g) = print g
print (CEInfix op a b) = print "(" o print a o print ") " o print op o print " (" o print b o print ")"
print (CEApp f ps) = print f o print "(" o prsperse ", " ps o print ")"
print (CEBool b) = print (if b "true" "false")
print (CEInt i) = print i
print (CEChar c) = print "'" o print (toString c) o print "'"
+ print (CEString s) = print "\"" o print s o print "\""
print (CEIf b t e) = print "(" o print b o print " ? " o print t o print " : " o print e o print ")"
print (CERef e) = print "&(" o print e o print ")"
- print (CEDeref e) = print "*(" o print e o print ")"
print (CEStruct m) = print "{" o indent o nl o prsperse (print "," o nl) (map pr m) o unindent o nl o print "}"
where
pr :: (String, CExpr) -> String
@@ -76,7 +69,6 @@ where
o print e o unindent o nl o print "}"
Nothing ->
print "}"
- print (CBWhile e b) = print "while (" o print e o print ") {" o nl o print b o nl o print "}"
print (CBAssign v e) = print v o print " = " o print e o print ";"
print (CBSeq a b) = print a o nl o print b
print CBEmpty = id
@@ -89,7 +81,7 @@ where
instance print CFun
where
print cf = print cf.CFun.type o print " " o print cf.CFun.name
- o print "(" o prsperse ", " cf.params o print ") {" o indent o nl
+ o print "() {" o indent o nl
o print cf.body o unindent o nl o print "}"
instance print CProg
diff --git a/assignment-13/uFPL/Examples.dcl b/assignment-13/uFPL/Examples.dcl
new file mode 100644
index 0000000..de3512e
--- /dev/null
+++ b/assignment-13/uFPL/Examples.dcl
@@ -0,0 +1,7 @@
+definition module uFPL.Examples
+
+import uFPL
+
+example_empty :: (String, [NamedRule])
+example_score :: (String, [NamedRule])
+example_countdown :: (String, [NamedRule])
diff --git a/assignment-13/uFPL/Examples.icl b/assignment-13/uFPL/Examples.icl
new file mode 100644
index 0000000..1e870e6
--- /dev/null
+++ b/assignment-13/uFPL/Examples.icl
@@ -0,0 +1,70 @@
+implementation module uFPL.Examples
+
+import StdChar
+import StdInt
+import StdString
+
+import uFPL
+import uFPL.Bootstrap
+
+example_empty :: (String, [NamedRule])
+example_empty = ("my_program", [])
+
+example_score :: (String, [NamedRule])
+example_score = ("score",
+ "a" :=: pressed b0 >>> [scorea <# scorea +. lit 1]
+ ||| "b" :=: pressed b1 >>> [scoreb <# scoreb +. lit 1]
+ ||| "r" :=: pressed b2 >>> (
+ scorea <# lit 0 :.
+ scoreb <# lit 0 :.
+ SetCursor (lit 0, lit 0) :. // Reset screen
+ PrintS " "
+ )
+ ||| "print" :=: (Change scorea ?| Change scoreb) >>> (
+ SetCursor (lit 0, lit 0) :.
+ PrintS "A: " :.
+ Print scorea :.
+ PrintS " - B: " :.
+ Print scoreb
+ ))
+where
+ scorea = rwInt "scorea" 0
+ scoreb = rwInt "scoreb" 0
+
+example_countdown :: (String, [NamedRule])
+example_countdown = ("countdown",
+ "time" :=:
+ (Change millis >>> [When (millis -. DELAY >. counter) (
+ counter <# counter +. DELAY :.
+ seconds <# running ? (seconds -. lit 1, seconds)
+ )]) :.
+ seconds ?= lit -1 >>> (
+ minutes <# minutes -. lit 1 :.
+ seconds <# lit 59
+ ) :.
+ minutes ?= lit -1 >>> (
+ running <# false :.
+ minutes <# lit 0 :.
+ seconds <# lit 0
+ ) :.
+ Change seconds >>> (
+ SetCursor (lit 0, lit 0) :.
+ When (minutes <. lit 10) [Print (lit 0)] :.
+ Print minutes :.
+ Print (lit ':') :.
+ When (seconds <. lit 10) [Print (lit 0)] :.
+ Print seconds
+ )
+ ||| "setsec" :=: pressed b0 >>> [seconds <# seconds +. lit 1]
+ ||| "setmin" :=: pressed b1 >>> [minutes <# minutes +. lit 1]
+ ||| "on_off" :=: pressed b2 >>> [running <# running ? (false, true)]
+ ||| "reset" :=: pressed b3 >>> (
+ seconds <# lit 0 :.
+ minutes <# lit 0
+ ))
+where
+ running = rwBool "running" False
+ minutes = rwInt "minutes" 0
+ seconds = rwInt "seconds" 15
+ counter = rwULong "counter" 0 // If set to 0, this will overflow on first iteration
+ DELAY = lit 1000
diff --git a/assignment-13/uFPL/Sim.dcl b/assignment-13/uFPL/Sim.dcl
index 96527db..6904033 100644
--- a/assignment-13/uFPL/Sim.dcl
+++ b/assignment-13/uFPL/Sim.dcl
@@ -53,8 +53,10 @@ instance toString UFPLException
| ITrigger ITrigger [IRule]
| ISetCursor (IExpr, IExpr)
| IPrint IExpr
+ | IPrintS String
-:: INamedRule = Rule String [IRule]
+:: INamedRule
+ = Rule String [IRule]
:: IShareState t =
{ isval :: t
diff --git a/assignment-13/uFPL/Sim.icl b/assignment-13/uFPL/Sim.icl
index 892708c..07f3103 100644
--- a/assignment-13/uFPL/Sim.icl
+++ b/assignment-13/uFPL/Sim.icl
@@ -249,6 +249,7 @@ where
(e :: Expr Char rw) -> return (dynamic Print e)
(e :: Expr Bool rw) -> return (dynamic Print e)
_ -> throw (LiftException "IPrint")
+ lift (IPrintS s) = return (dynamic PrintS s)
instance unlift IRule Rule
where
@@ -257,6 +258,7 @@ where
unlift (t >>> rs) = ITrigger (unlift t) (unlift rs)
unlift (SetCursor (c,r)) = ISetCursor (unlift c, unlift r)
unlift (Print e) = IPrint (unlift e)
+ unlift (PrintS s) = IPrintS s
instance lift INamedRule
where