aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2018-12-25 00:00:45 +0100
committerCamil Staps2018-12-25 00:00:45 +0100
commit8afc5e6b37b4b212f7ffb85e7bca6b0dad6c12a0 (patch)
tree281cbde504097b12505e9d9793705f6189e3a4c8
parentAdd interactive shell (diff)
Add booleans, encoded as 1 and 0
-rw-r--r--Sjit/Compile.icl1
-rw-r--r--Sjit/Syntax.dcl1
-rw-r--r--Sjit/Syntax.icl16
-rw-r--r--test/basic.result2
-rw-r--r--test/basic.test2
5 files changed, 21 insertions, 1 deletions
diff --git a/Sjit/Compile.icl b/Sjit/Compile.icl
index 5e4cef5..96db7f2 100644
--- a/Sjit/Compile.icl
+++ b/Sjit/Compile.icl
@@ -102,6 +102,7 @@ compile f cs
where
expr :: !Expr !CompileState -> Either String (![Instr], !CompileState)
expr (Int i) cs = Right ([PushI i], {cs & sp=cs.sp+1, pc=cs.pc+1})
+ expr (Bool b) cs = Right ([PushI (if b 1 0)], {cs & sp=cs.sp+1, pc=cs.pc+1})
expr (Var v) cs = case get v cs.vars of
Just i -> Right ([PushRef (i-cs.sp)], {cs & sp=cs.sp+1, pc=cs.pc+1})
Nothing -> Left ("undefined variable '" +++ v +++ "'")
diff --git a/Sjit/Syntax.dcl b/Sjit/Syntax.dcl
index 44adbfa..6a9f056 100644
--- a/Sjit/Syntax.dcl
+++ b/Sjit/Syntax.dcl
@@ -4,6 +4,7 @@ from Data.Either import :: Either
:: Expr
= Int !Int
+ | Bool !Bool
| Var !String
| App !String ![Expr]
diff --git a/Sjit/Syntax.icl b/Sjit/Syntax.icl
index ae75087..7412d7c 100644
--- a/Sjit/Syntax.icl
+++ b/Sjit/Syntax.icl
@@ -13,6 +13,8 @@ import Text.Parsers.Simple.Core
:: Token
= TIdent !String
| TInt !Int
+ | TTrue
+ | TFalse
| TEq
| TComma
@@ -28,6 +30,8 @@ where
toString t = case t of
TIdent s -> "'" +++ s +++ "'"
TInt n -> toString n
+ TTrue -> "True"
+ TFalse -> "False"
TEq -> "="
TComma -> ","
TParenOpen -> "("
@@ -44,7 +48,11 @@ where
n | isIdent n
# (i,n) = readIdent isIdent [] i e s
- -> lex [TIdent n:tks] i e s
+ # tk = case n of
+ "True" -> TTrue
+ "False" -> TFalse
+ n -> TIdent n
+ -> lex [tk:tks] i e s
n | isFunnyIdent n
# (i,n) = readIdent isFunnyIdent [] i e s
@@ -107,6 +115,7 @@ where
liftM2 App ident (pToken TParenOpen *> pSepBy expr (pToken TComma) <* pToken TParenClose)
<|> Var <$> ident
<|> Int <$> int
+ <|> Bool <$> bool
<|> (pToken TParenOpen *> expr <* pToken TParenClose)
ident :: Parser Token String
@@ -118,6 +127,11 @@ arg =: ident
int :: Parser Token Int
int =: (\(TInt n) -> n) <$> pSatisfy (\t->t=:TInt _)
+bool :: Parser Token Bool
+bool =:
+ pToken TTrue $> True
+ <|> pToken TFalse $> False
+
parse_function :: !String -> Either String Function
parse_function s = lex s >>= \tks -> case parse function tks of
Right f -> Right f
diff --git a/test/basic.result b/test/basic.result
index b4de394..e435210 100644
--- a/test/basic.result
+++ b/test/basic.result
@@ -1 +1,3 @@
11
+1
+0
diff --git a/test/basic.test b/test/basic.test
index b31be57..4492ffc 100644
--- a/test/basic.test
+++ b/test/basic.test
@@ -1,3 +1,5 @@
id x = x
const x y = x
const(3,5) * id(7) - const(10,2)
+const(True,False)
+const(False,37)