about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPaweł Dybiec <pawel.to.malpa@gmail.com>2020-01-08 20:22:14 +0100
committerPaweł Dybiec <pawel.to.malpa@gmail.com>2020-01-08 20:22:14 +0100
commitea41a7529715987941257a0330c1eb1ae08cc12c (patch)
tree825c5a1507d253dac04ef43c136449b117f931f0 /src
Initial commit
Initial version of project. What is done:
* Syntax of expressions,commands,programs and types
* Crude typechecking
* Some typechecking tests
Diffstat (limited to 'src')
-rw-r--r--src/Lib.hs6
-rw-r--r--src/Syntax.hs34
-rw-r--r--src/Typecheck.hs43
3 files changed, 83 insertions, 0 deletions
diff --git a/src/Lib.hs b/src/Lib.hs
new file mode 100644
index 0000000..d36ff27
--- /dev/null
+++ b/src/Lib.hs
@@ -0,0 +1,6 @@
+module Lib
+    ( someFunc
+    ) where
+
+someFunc :: IO ()
+someFunc = putStrLn "someFunc"
diff --git a/src/Syntax.hs b/src/Syntax.hs
new file mode 100644
index 0000000..1c888b5
--- /dev/null
+++ b/src/Syntax.hs
@@ -0,0 +1,34 @@
+module Syntax
+  where
+
+type Idnt = String
+data Typ = TInt
+         | TRecord  [(Idnt, Typ)]
+         | TVariant [(Idnt, Typ)]
+         | TPtr Typ
+           deriving(Eq,Show)
+
+data Expr = Var Idnt
+          | IntLit Int
+          | Record [(Idnt, Expr)]
+          | Variant Typ [(Idnt, Expr)]
+          | Add Expr Expr
+          | Mult Expr Expr
+          | Neg Expr
+          | Deref Expr
+           deriving(Eq,Show)
+data Com = Skip
+         | Seq Com Com
+         | If Expr Com Com
+         | While Expr Com
+         | Asgn Idnt Expr
+         | Decl Idnt Expr Com
+         | Alloc Idnt Expr
+         | Save Idnt Expr
+           deriving(Eq,Show)
+         
+data FuncDecl = Func Idnt [Idnt] [(Idnt,Expr)] Com Expr
+           deriving(Eq,Show)
+data Program = P [FuncDecl] [(Idnt, Int)] Com
+           deriving(Eq,Show)
+
diff --git a/src/Typecheck.hs b/src/Typecheck.hs
new file mode 100644
index 0000000..d8b8128
--- /dev/null
+++ b/src/Typecheck.hs
@@ -0,0 +1,43 @@
+module Typecheck
+(inferExpr,
+ checkExpr)
+  where
+import Syntax
+import Control.Monad
+type Environment = [(Idnt,Typ)]
+
+assertInt TInt = Just ()
+assertInt _ = Nothing
+assertRec (Record _) = Just ()
+assertRec _ = Nothing
+assertVar (Variant _ _) = Just ()
+assertVar _ = Nothing
+assertPtr (TPtr _) = Just ()
+assertPtr _ = Nothing
+
+inferExpr :: Expr -> Environment -> Maybe Typ
+inferExpr (Var name) env = lookup name env
+inferExpr (IntLit _) _ = Just TInt
+inferExpr (Record _) _ = undefined
+inferExpr (Variant _ _) _ = undefined
+inferExpr (Add e1 e2) env = do
+  checkExpr e1 env TInt
+  checkExpr e1 env TInt
+  return TInt
+inferExpr (Mult e1 e2) env = do
+  checkExpr e1 env TInt
+  checkExpr e1 env TInt
+  return TInt
+inferExpr (Neg e) env = do
+  checkExpr e env TInt
+  return TInt
+inferExpr (Deref e1) env = do
+  t <- inferExpr e1 env
+  assertPtr t
+  return t
+
+checkExpr :: Expr -> Environment -> Typ -> Maybe ()
+checkExpr e env t = do
+  t' <- inferExpr e env
+  guard (t==t')
+  return ()