aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-09-01 22:34:28 +0100
committerJuan J. Martinez <jjm@usebox.net>2022-09-01 22:34:38 +0100
commit3d2b80cf454e682ba1fcd094465b7ee1a94297dd (patch)
tree68b6731494756df20fc4cc0cea3b556f92f0ed12 /src
parent476b0d2d6c27b9ec326b465480795582a3b22f4c (diff)
downloadmicro-lang-hs-3d2b80cf454e682ba1fcd094465b7ee1a94297dd.tar.gz
micro-lang-hs-3d2b80cf454e682ba1fcd094465b7ee1a94297dd.zip
Variable declaration
Diffstat (limited to 'src')
-rw-r--r--src/Ast.hs3
-rw-r--r--src/Compiler.hs15
-rw-r--r--src/Lexer.hs4
-rw-r--r--src/Parser.hs14
4 files changed, 29 insertions, 7 deletions
diff --git a/src/Ast.hs b/src/Ast.hs
index 3d351c6..af87a8b 100644
--- a/src/Ast.hs
+++ b/src/Ast.hs
@@ -23,7 +23,8 @@ data Expr
= Num Integer SourcePos
| Bool' Bool SourcePos
| BinOp Op Expr Expr
- | Var Ident SourcePos
+ | Variable Ident SourcePos
+ | Var Ident Type Expr SourcePos
| -- fn [params] return body private anomyous pos
Func Ident [FuncParam] (Maybe Type) [Expr] Bool Bool SourcePos
| Call Expr [Expr] SourcePos
diff --git a/src/Compiler.hs b/src/Compiler.hs
index a8cca5d..a708f96 100644
--- a/src/Compiler.hs
+++ b/src/Compiler.hs
@@ -92,7 +92,7 @@ verifyFuncType ident params ret pos = do
map
( \(id, t, pos) ->
if not (definedType t)
- then Just $ Error UndefinedType ("undefined type in \"" ++ id ++ "\"") pos
+ then Just $ Error UndefinedType ("undefined type in function declaration \"" ++ id ++ "\"") pos
else Nothing
)
params
@@ -145,6 +145,17 @@ compile x = do
Nothing -> return $ Right rtyp
Right _ -> addError $ Error NonCallable "non callable value in function call" pos
_ -> return $ Right Nothing
+ (A.Var ident typ val pos) -> do
+ (ev, errs) <- get
+ (ev, errs) <- return $ foldlEither addSymUniq (ev, errs) [(ident, typ, pos)]
+ errs <-
+ return $
+ if not (definedType typ)
+ then Error UndefinedType ("undefined type in variable declaration \"" ++ ident ++ "\"") pos : errs
+ else errs
+ -- TODO: typecheck value
+ put (ev, errs)
+ return $ Right $ Just typ
(A.Return value pos) -> do
(ev, errs) <- get
case getSyml ev "$fn$" of
@@ -154,7 +165,7 @@ compile x = do
case r of
Just err -> addError $ Error TypeError err pos
Nothing -> return $ Right rtyp
- (A.Var ident pos) -> do
+ (A.Variable ident pos) -> do
(ev, errs) <- get
case getSym ev ident of
Just (_, t, _) -> return $ Right $ Just t
diff --git a/src/Lexer.hs b/src/Lexer.hs
index 1778b21..7d09752 100644
--- a/src/Lexer.hs
+++ b/src/Lexer.hs
@@ -9,8 +9,8 @@ import qualified Text.Parsec.Token as T
scanner :: T.TokenParser ()
scanner = T.makeTokenParser style
where
- ops = ["+", "*", "-", ";"]
- names = ["module", "private", "def", "return", "->", "true", "false"]
+ ops = ["+", "*", "-", ";", "="]
+ names = ["module", "private", "var", "def", "return", "->", "true", "false"]
style =
emptyDef
{ T.commentLine = "#",
diff --git a/src/Parser.hs b/src/Parser.hs
index ea698c8..f341bc2 100644
--- a/src/Parser.hs
+++ b/src/Parser.hs
@@ -43,7 +43,7 @@ variable :: Parser Expr
variable = do
pos <- getPosition
var <- identifier
- return $ Var var pos
+ return $ Variable var pos
typ :: Parser Type
typ = do
@@ -116,6 +116,16 @@ call = do
args <- parens $ commaSep expr
return $ Call ident args pos
+var :: Parser Expr
+var = do
+ pos <- getPosition
+ reserved "var"
+ (ident, typ, _) <- arg
+ reservedOp "=" <?> "assignation"
+ value <- expr
+ reservedOp ";"
+ return $ Var ident typ value pos
+
factor :: Parser Expr
factor =
number
@@ -150,7 +160,7 @@ program = do
n <-
many $ do
function
- -- TODO: variable decl
+ <|> var
<|> statement <?> "statement"
return $ [m] ++ n