aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Fun/Generator.hs6
-rw-r--r--src/Fun/Grammar.hs60
-rw-r--r--src/Fun/Tree.hs8
-rw-r--r--test.fun7
4 files changed, 49 insertions, 32 deletions
diff --git a/src/Fun/Generator.hs b/src/Fun/Generator.hs
index 17f47e3..a3c2728 100644
--- a/src/Fun/Generator.hs
+++ b/src/Fun/Generator.hs
@@ -8,6 +8,6 @@ import Fun.Typer
data GenerateError = GenerateError String
generateAsm :: Tree -> Either GenerateError String
--- generateAsm (Tree t) = Right $ show t
-generateAsm t =
- Left $ GenerateError $ show $ getFunction t "_start" [InternalType "_void"]
+generateAsm (Tree t) = Right $ show t
+-- generateAsm t =
+-- Left $ GenerateError $ show $ getFunction t "_start" [InternalType "_void"]
diff --git a/src/Fun/Grammar.hs b/src/Fun/Grammar.hs
index 47191de..c9cd034 100644
--- a/src/Fun/Grammar.hs
+++ b/src/Fun/Grammar.hs
@@ -4,10 +4,6 @@ import Fun.Parser
import Fun.Tree
import Fun.Typer
--- TODO: Multiple programs (= files) in tree
-tree :: Parser Tree
-tree = program >>> build >>> Tree <?> "tree" where build p = [p]
-
program :: Parser Program
program = iterFull block >>> Program <?> "program"
@@ -68,12 +64,25 @@ internalFunctions = ["_start", "_asm"]
functionName :: Parser String
functionName =
+ letter
+ <+> iter (special <|> alphanum)
+ >>> build
+ <|> internalWord
+ <=> (`elem` internalFunctions)
+ <|> literal '('
+ <-+> functionInfixName
+ <+-> literal ')'
+ <?> "function name"
+ where build (first, rest) = first : rest
+
+functionInfixName :: Parser String
+functionInfixName =
(special <|> letter)
<+> iter (special <|> alphanum)
>>> build
<|> internalWord
<=> (`elem` internalFunctions)
- <?> "function name"
+ <?> "infix function name"
where build (first, rest) = first : rest
functionTypes :: Parser [Type]
@@ -140,7 +149,6 @@ functionPatternElement :: Parser FunctionPatternElement
functionPatternElement =
(functionParameter >>> FunctionPatternParameter)
<|> (literal '_' >>> const FunctionPatternWildcard)
- <|> (literal '_' <+-> literal '*' >>> const FunctionPatternSuperWildcard)
<|> (number >>> FunctionPatternNumber)
<|> (string >>> FunctionPatternString)
<?> "function pattern element"
@@ -150,21 +158,31 @@ functionParameter =
letters <|> (letter <+> oneOrMore alphanum) >>> build <?> "function parameter"
where build (a, b) = a : b
+functionArgument :: Parser FunctionArgument
+functionArgument =
+ (functionName >>> FunctionName)
+ <|> (functionParameter >>> FunctionParameter)
+ <|> (string >>> FunctionString)
+ <|> (number >>> FunctionNumber)
+ <?> "function argument"
+
+functionBodyExpression :: Parser FunctionBody
+functionBodyExpression =
+ (functionName <+> iter (space <-+> functionArgument) >>> buildCall)
+ <|> ( functionArgument -- TODO: Fixity precedence in parser? Hmmm
+ <+-> space
+ <+> functionInfixName
+ <+-> space
+ <+> functionArgument
+ >>> buildInfixCall
+ )
+ <|> (literal '(' <-+> functionBody <+-> literal ')' >>> FunctionBodySub)
+ where
+ buildCall (name, args) = FunctionBodyCall name args
+ buildInfixCall ((arg1, name), arg2) = FunctionBodyInfixCall name [arg1, arg2]
+
functionBody :: Parser FunctionBody
functionBody =
- iter (space <-+> functionBodyElement)
+ space
+ <-+> (functionBodyExpression <|> functionArgument >>> FunctionBodyValue)
<+-> newline
- >>> FunctionBody
- <?> "function body"
-
-functionBodyElement :: Parser FunctionBodyElement
-functionBodyElement =
- statement
- <|> (functionName >>> FunctionBodyIdentifier)
- <|> (functionParameter >>> FunctionBodyIdentifier)
- <|> (string >>> FunctionBodyString)
- <|> (number >>> FunctionBodyNumber)
- <?> "function body element"
-
-statement :: Parser FunctionBodyElement -- TODO
-statement = acceptSpecial "if" >>> Statement
diff --git a/src/Fun/Tree.hs b/src/Fun/Tree.hs
index 817a077..7b87853 100644
--- a/src/Fun/Tree.hs
+++ b/src/Fun/Tree.hs
@@ -47,15 +47,13 @@ data FunctionPattern = FunctionPattern
}
deriving Show
-data FunctionPatternElement = FunctionPatternParameter String | FunctionPatternString String | FunctionPatternNumber Integer | FunctionPatternWildcard | FunctionPatternSuperWildcard
+data FunctionPatternElement = FunctionPatternParameter String | FunctionPatternString String | FunctionPatternNumber Integer | FunctionPatternWildcard
deriving (Show, Eq, Ord)
-data FunctionBody = FunctionBody
- { bElements :: [FunctionBodyElement]
- }
+data FunctionBody = FunctionBodyCall { cName :: String, cArgs :: [FunctionArgument] } | FunctionBodyInfixCall { iName :: String, iArgs :: [FunctionArgument] } | FunctionBodyValue FunctionArgument | FunctionBodySub FunctionBody
deriving Show
-data FunctionBodyElement = Statement String | FunctionBodyIdentifier String | FunctionBodyParameter String | FunctionBodyString String | FunctionBodyNumber Integer
+data FunctionArgument = FunctionName String | FunctionInfixName String | FunctionParameter String | FunctionString String | FunctionNumber Integer
deriving Show
----
diff --git a/test.fun b/test.fun
index 2f68648..f1ded1d 100644
--- a/test.fun
+++ b/test.fun
@@ -1,8 +1,9 @@
-+ :+: Unsigned32 : Unsigned32 : Unsigned32 % inline
-b : 5 ab "baum"
+do+ :+: Unsigned32 : Unsigned32 : Unsigned32 % inline
+b : ab "baum"
test :+: Unsigned32 : Unsigned32 : Unsigned32 % inline
-ab 123 : bab 5
+ab 123 : 5
+_ _ : 0
text :-: [Unsigned8]
: "hallo"