Started writing parser

This commit is contained in:
2024-02-23 04:29:18 -06:00
parent 16e5799dc0
commit d97bb8b115
4 changed files with 82 additions and 10 deletions

View File

@@ -1,15 +1,18 @@
module Main (main) where
import Parser (parseFile)
-- import Parser (parseFile)
process :: String -> IO ()
process line = do
let res = parseFile line
case res of
Left err -> print err
Right ex -> mapM_ print ex
-- process :: String -> IO ()
-- process line = do
-- let res = parseFile line
-- case res of
-- Left err -> print err
-- Right ex -> mapM_ print ex
-- main :: IO ()
-- main = do
-- fileText <- readFile "test.vi"
-- process fileText
main :: IO ()
main = do
fileText <- readFile "test.vi"
process fileText
main = undefined

1
src/Lexer.hs Normal file
View File

@@ -0,0 +1 @@
module Lexer where

67
src/Parser.hs Normal file
View File

@@ -0,0 +1,67 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
import Control.Applicative hiding (many)
import Control.Monad
import Data.Text (Text)
import qualified Data.Text as T
import Data.Void
import Text.Megaparsec hiding (State)
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
type Parser = Parsec Void Text
runParser :: Text -> IO ()
runParser = parseTest (parseProgram <* eof)
newtype Program = Program
{ statements :: [Statement]
}
deriving (Show)
data Statement
= Import [Identifier] [Identifier]
| Expr Expression
| Function Identifier [Identifier] Block
| Return Expression
| Conditional Expression Block (Maybe Block)
deriving (Show)
data Expression = E deriving (Show)
type Block = [Statement]
type Identifier = String
parseProgram :: Parser Program
parseProgram = do
void $ many space
statements <- parseStatements
return Program {..}
parseStatements :: Parser [Statement]
parseStatements =
many $
choice
[ parseImport,
parseExpression,
parseFunction,
parseReturn,
parseConditional
]
parseImport :: Parser Statement
parseImport = undefined
parseExpression :: Parser Statement
parseExpression = undefined
parseFunction :: Parser Statement
parseFunction = undefined
parseReturn :: Parser Statement
parseReturn = undefined
parseConditional :: Parser Statement
parseConditional = undefined

1
src/Syntax.hs Normal file
View File

@@ -0,0 +1 @@
module Syntax where