Removed complicated validation, Basic Expression parsing

This commit is contained in:
VegOwOtenks 2025-01-17 21:13:00 +01:00
parent 1c1e25a881
commit 839e278e43
7 changed files with 36 additions and 37 deletions

View file

@ -1,6 +1,15 @@
module Ubc.Parse module Ubc.Parse
( someFunc ) ( parseScript )
where where
someFunc :: IO () import Text.Parsec ( runPT, ParseError, SourceName, ParsecT )
someFunc = putStrLn "help"
import qualified Ubc.Parse.ParserState as ParserState
import qualified Ubc.Parse.Config as Config
parseScript :: Monad m => SourceName -> String -> m (Either ParseError ())
parseScript = runPT topLevelParser (ParserState.initialState Config.defaultConfig)
topLevelParser :: ParsecT s u m ()
topLevelParser = return ()

View file

@ -3,12 +3,12 @@ module Ubc.Parse.Data.Struct
) )
where where
import Data.Map (Map)
import Ubc.Parse.VariableType (VariableType) import Ubc.Parse.VariableType (VariableType)
type VariableName = String
data Struct = Struct data Struct = Struct
{ name :: String { name :: String
, memberVariables :: Map String VariableType , memberVariables :: [(VariableName, VariableType)]
} }
deriving (Show) deriving (Show)

View file

@ -8,12 +8,14 @@ module Ubc.Parse.ParserState
where where
import Ubc.Parse.Scope (Scope) import Ubc.Parse.Scope (Scope)
import Ubc.Parse.Config (Config)
data ParserState = ParserState data ParserState = ParserState
{ scopes :: [Scope] { scopes :: [Scope]
, config :: Config
} }
initialState :: ParserState initialState :: Config -> ParserState
initialState = ParserState [] initialState = ParserState []
pushScope :: Scope -> ParserState -> ParserState pushScope :: Scope -> ParserState -> ParserState

View file

@ -4,16 +4,16 @@ module Ubc.Parse.Scope.StructScope
) )
where where
import Data.Map (Map)
import Ubc.Parse.VariableType (VariableType) import Ubc.Parse.VariableType (VariableType)
type VariableName = String
data StructScope = StructScope data StructScope = StructScope
{ structName :: String { structName :: String
, variables :: Map String VariableType , variables :: [(VariableName, VariableType)]
} }
modifyVariables :: (Map String VariableType -> Map String VariableType) -> StructScope -> StructScope modifyVariables :: ([(VariableName, VariableType)] -> [(VariableName, VariableType)]) -> StructScope -> StructScope
modifyVariables f scope@StructScope{variables = oldVariables} = scope{variables = newVariables} modifyVariables f scope@StructScope{variables = oldVariables} = scope{variables = newVariables}
where where
newVariables = f oldVariables newVariables = f oldVariables

View file

@ -5,7 +5,7 @@ module Ubc.Parse.Struct
where where
import Data.Functor ( (<&>) ) import Data.Functor ( (<&>) )
import Control.Arrow ( Arrow(second) ) import Control.Arrow ( (>>>) )
import Text.Parsec import Text.Parsec
( char, ( char,
@ -15,13 +15,11 @@ import Text.Parsec
many, many,
modifyState, modifyState,
try, try,
unexpected,
ParsecT ) ParsecT )
import Ubc.Parse.ParserState (ParserState) import Ubc.Parse.ParserState (ParserState)
import Ubc.Parse.Scope.StructScope (StructScope(..)) import Ubc.Parse.Scope.StructScope (StructScope(..))
import Ubc.Parse.Scope (Scope(..)) import Ubc.Parse.Scope (Scope(..))
import Ubc.Parse.Types (checkTypeValidity)
import Ubc.Parse.Data.Struct (Struct(..)) import Ubc.Parse.Data.Struct (Struct(..))
import qualified Ubc.Parse.Language as UbcLanguage import qualified Ubc.Parse.Language as UbcLanguage
@ -30,7 +28,6 @@ import qualified Ubc.Parse.ParserState as ParserState
import qualified Ubc.Parse.Scope as Scope import qualified Ubc.Parse.Scope as Scope
import qualified Ubc.Parse.VariableType as VariableType import qualified Ubc.Parse.VariableType as VariableType
import qualified Data.Map as Map
parseStruct :: Monad m => ParsecT String ParserState m Struct parseStruct :: Monad m => ParsecT String ParserState m Struct
parseStruct = do parseStruct = do
@ -39,7 +36,7 @@ parseStruct = do
structIdentifier <- UbcLanguage.identifier structIdentifier <- UbcLanguage.identifier
let structScope = StructScope let structScope = StructScope
{ structName = structIdentifier { structName = structIdentifier
, variables = Map.empty , variables = []
} }
modifyState (ParserState.pushScope . ScopeStruct $ structScope) modifyState (ParserState.pushScope . ScopeStruct $ structScope)
@ -66,24 +63,9 @@ structVariableOrFunction = do
parseVariable :: Monad m => String -> String -> ParsecT String ParserState m () parseVariable :: Monad m => String -> String -> ParsecT String ParserState m ()
parseVariable variableType variableName = do parseVariable variableType variableName = do
_ <- UbcLanguage.semicolon _ <- UbcLanguage.semicolon
-- TODO: Validate type
(_, structScope) <- getState <&> second Scope.expectScopeStruct . ParserState.popScope modifyState (ParserState.modifyScope (Scope.expectScopeStruct
>>> StructScope.modifyVariables ((variableName, VariableType.fromString variableType):)
-- check variable name >>> Scope.ScopeStruct
if (/= Nothing) . Map.lookup variableName . StructScope.variables $ structScope )
then do )
unexpected $ "variable name: \"" ++ variableName ++ "\", this name is already defined"
else do
return ()
-- check type
isKnownType <- checkTypeValidity variableType
if not isKnownType
then do
unexpected $ "variable type: \"" ++ variableType ++ "\", this type is not defined"
else do
return ()
let structScope' = StructScope.modifyVariables (Map.insert variableName (VariableType.fromString variableType)) structScope
modifyState (ParserState.pushScope $ ScopeStruct structScope')

View file

@ -4,7 +4,10 @@ module Ubc.Parse.VariableType
) )
where where
data VariableType = BuiltInI32 | BuiltInU32 | BuiltInF32 | UserStruct String data VariableType = BuiltInI32
| BuiltInU32
| BuiltInF32
| UserStruct String
deriving (Show, Eq) deriving (Show, Eq)
fromString :: String -> VariableType fromString :: String -> VariableType

View file

@ -26,7 +26,10 @@ source-repository head
library library
exposed-modules: exposed-modules:
Ubc.Parse Ubc.Parse
Ubc.Parse.Config
Ubc.Parse.Data.Struct Ubc.Parse.Data.Struct
Ubc.Parse.Expression
Ubc.Parse.File
Ubc.Parse.Language Ubc.Parse.Language
Ubc.Parse.ParserState Ubc.Parse.ParserState
Ubc.Parse.Scope Ubc.Parse.Scope