Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/echo.z
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn main(argc: int, argv: [string]) -> int {
let i = 1;

while i < argc {
if i > 1 {
printf(" %s", argv[i]);
} else {
printf("%s", argv[i]);
}
i = i + 1;
}

printf("\n");
return 0;
}
9 changes: 9 additions & 0 deletions examples/struct.z
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct MyStruct {
pub name: string;
pub age: int;
internalField: int;

pub doSomething(times: int) -> void {
printf("I'm doing something %i times\n", times);
}
}
23 changes: 23 additions & 0 deletions src/Language/Zen/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ newtype Program =
data TopLevel
= Statement Statement
| Fn FunctionDef
| Struct StructDef
deriving (Show)

data StructDef
= StructDef
{ location :: Location
, name :: Text
, fields :: [StructField]
}
deriving (Show)

data StructField
= StructMember
{ location :: Location
, public :: Bool
, name :: Text
, fieldType :: Text
}
| StructFn
{ location :: Location
, public :: Bool
, fn :: FunctionDef
}
deriving (Show)

data FunctionArg
Expand Down
26 changes: 23 additions & 3 deletions src/Language/Zen/Combinator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Language.Zen.Combinator
) where

import Control.Monad.Combinators.Expr as E
import Data.Maybe (isJust)
import qualified Data.Text as T
import Text.Megaparsec

Expand All @@ -13,7 +14,8 @@ programParser :: Parser Program
programParser = between sp eof $ Program <$> many topLevelParser

topLevelParser :: Parser TopLevel
topLevelParser = (Statement <$> statementP) <|> (Fn <$> functionP)
topLevelParser =
(Statement <$> statementP) <|> (Fn <$> functionP) <|> (Struct <$> structP)

termP :: Parser Expr
termP =
Expand Down Expand Up @@ -87,9 +89,12 @@ whileStatementP = do
pure $ While loc predicate body

functionP :: Parser FunctionDef
functionP = do
functionP = rword "fn" *> functionP'

functionP' :: Parser FunctionDef
functionP' = do
loc <- getLocation
name <- rword "fn" *> identifier
name <- identifier
args <- parens (sepBy argP comma)
_ <- symbol "->"
rloc <- getLocation
Expand All @@ -108,6 +113,21 @@ argP = do
identifier
pure $ FunctionArg loc name ty

structP :: Parser StructDef
structP = do
_ <- rword "struct"
loc <- getLocation
name <- identifier
fields <- braces $ many structFieldP
pure $ StructDef loc name fields

structFieldP :: Parser StructField
structFieldP = do
loc <- getLocation
pub <- isJust <$> optional (rword "pub")
try (StructFn loc pub <$> functionP') <|>
(StructMember loc pub <$> identifier <*> (symbol ":" *> identifier <* semi))

opTable :: [[E.Operator Parser Expr]]
opTable =
[ [infixL Mul "*", infixL Div "/"]
Expand Down
13 changes: 12 additions & 1 deletion src/Language/Zen/Lexemes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ rword :: Text -> Parser ()
rword w = (lexeme . try) (string w *> notFollowedBy alphaNumChar)

reservedWords :: [Text]
reservedWords = ["let", "true", "false", "if", "else", "while", "fn", "return"]
reservedWords =
[ "let"
, "true"
, "false"
, "if"
, "else"
, "while"
, "fn"
, "return"
, "struct"
, "pub"
]

stringLiteral :: Parser Text
stringLiteral = do
Expand Down