summaryrefslogtreecommitdiff
path: root/src/Language/Fiddle/Compiler
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-01-09 01:07:25 -0700
committerJosh Rahm <joshuarahm@gmail.com>2023-01-09 01:07:25 -0700
commita33b80dbf64303fe376419216c1245a0238ea37d (patch)
tree18477f448abe49d9c384ff0b24d1874eb83afdaa /src/Language/Fiddle/Compiler
parentdef481d234ce5e1671d9faaa539477de8cd14640 (diff)
downloadfiddle-a33b80dbf64303fe376419216c1245a0238ea37d.tar.gz
fiddle-a33b80dbf64303fe376419216c1245a0238ea37d.tar.bz2
fiddle-a33b80dbf64303fe376419216c1245a0238ea37d.zip
Crude compilation pipeline starting to take shape.
This simply does a Stage0 -> Stage1 conversion. Namely, parse the text, check for syntax errors, squeeze the resulting ast and ... that's it.
Diffstat (limited to 'src/Language/Fiddle/Compiler')
-rw-r--r--src/Language/Fiddle/Compiler/Stage0.hs61
-rw-r--r--src/Language/Fiddle/Compiler/Stage1.hs9
2 files changed, 70 insertions, 0 deletions
diff --git a/src/Language/Fiddle/Compiler/Stage0.hs b/src/Language/Fiddle/Compiler/Stage0.hs
new file mode 100644
index 0000000..d00d7cb
--- /dev/null
+++ b/src/Language/Fiddle/Compiler/Stage0.hs
@@ -0,0 +1,61 @@
+module Language.Fiddle.Compiler.Stage0 (toStage0, toStage1) where
+
+import Control.Monad.Identity (Identity)
+import Control.Monad.Writer
+import qualified Data.Text
+import Language.Fiddle.Ast
+import Language.Fiddle.Compiler
+import qualified Language.Fiddle.Parser
+import Language.Fiddle.Types (Commented, SourceSpan(..))
+import Text.Parsec (ParseError, errorPos)
+import Text.Parsec.Error (errorMessages, showErrorMessages)
+
+newtype Stage0Diagnostic = SyntaxError String
+
+toStage0 ::
+ String ->
+ Data.Text.Text ->
+ Compile () (FiddleUnit Stage1 (Either ParseError) (Commented SourceSpan))
+toStage0 filePath text =
+ case Language.Fiddle.Parser.parseFiddleText filePath text of
+ Left pe -> do
+ tell [parseErrorToDiagnostic pe]
+ hoistMaybe Nothing
+ Right a -> return a
+
+-- Gets the AST ready for Stage1 processing .This will report primarily
+-- SyntaxErrors and errors parsing the tree.
+--
+-- In the process, the tree is un-deferred and all parts of the
+toStage1 ::
+ FiddleUnit Stage1 (Either ParseError) a ->
+ Compile () (FiddleUnit Stage1 Identity a)
+toStage1 ast = do
+ alter
+ ( \case
+ (Left l) -> do
+ tell [parseErrorToDiagnostic l]
+ return (Left l)
+ r -> return r
+ )
+ return
+ ast
+
+ hoistMaybe $
+ case squeeze ast of
+ (Left _) -> Nothing
+ (Right a) -> Just a
+
+parseErrorToDiagnostic :: ParseError -> Diagnostic
+parseErrorToDiagnostic pe =
+ Diagnostic
+ Error
+ ( showErrorMessages
+ "or"
+ "unknown"
+ "expecting"
+ "unexpected"
+ "end of body or input (maybe a missing semicolon?)"
+ (errorMessages pe)
+ )
+ (SourceSpan (errorPos pe) (errorPos pe))
diff --git a/src/Language/Fiddle/Compiler/Stage1.hs b/src/Language/Fiddle/Compiler/Stage1.hs
new file mode 100644
index 0000000..d3f3e10
--- /dev/null
+++ b/src/Language/Fiddle/Compiler/Stage1.hs
@@ -0,0 +1,9 @@
+module Language.Fiddle.Compiler.Stage1 (toStage2) where
+
+import Control.Monad.Identity (Identity)
+import Language.Fiddle.Ast
+
+-- The second stage is a simplified version of the AST without anonymous
+-- declarations.
+toStage2 :: FiddleUnit Stage1 Identity a -> FiddleUnit Stage2 Identity a
+toStage2 = undefined