From 839e278e43c20221d6e0fd0824eb01db38b2f77c Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Fri, 17 Jan 2025 21:13:00 +0100 Subject: [PATCH] Removed complicated validation, Basic Expression parsing --- src/Ubc/Parse.hs | 15 +++++++++++--- src/Ubc/Parse/Data/Struct.hs | 6 +++--- src/Ubc/Parse/ParserState.hs | 4 +++- src/Ubc/Parse/Scope/StructScope.hs | 8 ++++---- src/Ubc/Parse/Struct.hs | 32 +++++++----------------------- src/Ubc/Parse/VariableType.hs | 5 ++++- ubcc.cabal | 3 +++ 7 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/Ubc/Parse.hs b/src/Ubc/Parse.hs index 8d2dd91..e0e8acf 100644 --- a/src/Ubc/Parse.hs +++ b/src/Ubc/Parse.hs @@ -1,6 +1,15 @@ module Ubc.Parse -( someFunc ) +( parseScript ) where -someFunc :: IO () -someFunc = putStrLn "help" +import Text.Parsec ( runPT, ParseError, SourceName, ParsecT ) + + +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 () diff --git a/src/Ubc/Parse/Data/Struct.hs b/src/Ubc/Parse/Data/Struct.hs index 3568966..86d9838 100644 --- a/src/Ubc/Parse/Data/Struct.hs +++ b/src/Ubc/Parse/Data/Struct.hs @@ -3,12 +3,12 @@ module Ubc.Parse.Data.Struct ) where -import Data.Map (Map) - import Ubc.Parse.VariableType (VariableType) +type VariableName = String + data Struct = Struct { name :: String - , memberVariables :: Map String VariableType + , memberVariables :: [(VariableName, VariableType)] } deriving (Show) diff --git a/src/Ubc/Parse/ParserState.hs b/src/Ubc/Parse/ParserState.hs index 0ba17c4..7b92580 100644 --- a/src/Ubc/Parse/ParserState.hs +++ b/src/Ubc/Parse/ParserState.hs @@ -8,12 +8,14 @@ module Ubc.Parse.ParserState where import Ubc.Parse.Scope (Scope) +import Ubc.Parse.Config (Config) data ParserState = ParserState { scopes :: [Scope] + , config :: Config } -initialState :: ParserState +initialState :: Config -> ParserState initialState = ParserState [] pushScope :: Scope -> ParserState -> ParserState diff --git a/src/Ubc/Parse/Scope/StructScope.hs b/src/Ubc/Parse/Scope/StructScope.hs index 47b1ee5..1fe5540 100644 --- a/src/Ubc/Parse/Scope/StructScope.hs +++ b/src/Ubc/Parse/Scope/StructScope.hs @@ -4,16 +4,16 @@ module Ubc.Parse.Scope.StructScope ) where -import Data.Map (Map) - import Ubc.Parse.VariableType (VariableType) +type VariableName = String + data StructScope = StructScope { 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} where newVariables = f oldVariables diff --git a/src/Ubc/Parse/Struct.hs b/src/Ubc/Parse/Struct.hs index b5e16ed..54fe2da 100644 --- a/src/Ubc/Parse/Struct.hs +++ b/src/Ubc/Parse/Struct.hs @@ -5,7 +5,7 @@ module Ubc.Parse.Struct where import Data.Functor ( (<&>) ) -import Control.Arrow ( Arrow(second) ) +import Control.Arrow ( (>>>) ) import Text.Parsec ( char, @@ -15,13 +15,11 @@ import Text.Parsec many, modifyState, try, - unexpected, ParsecT ) import Ubc.Parse.ParserState (ParserState) import Ubc.Parse.Scope.StructScope (StructScope(..)) import Ubc.Parse.Scope (Scope(..)) -import Ubc.Parse.Types (checkTypeValidity) import Ubc.Parse.Data.Struct (Struct(..)) 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.VariableType as VariableType -import qualified Data.Map as Map parseStruct :: Monad m => ParsecT String ParserState m Struct parseStruct = do @@ -39,7 +36,7 @@ parseStruct = do structIdentifier <- UbcLanguage.identifier let structScope = StructScope { structName = structIdentifier - , variables = Map.empty + , variables = [] } modifyState (ParserState.pushScope . ScopeStruct $ structScope) @@ -66,24 +63,9 @@ structVariableOrFunction = do parseVariable :: Monad m => String -> String -> ParsecT String ParserState m () parseVariable variableType variableName = do _ <- UbcLanguage.semicolon - -- TODO: Validate type - (_, structScope) <- getState <&> second Scope.expectScopeStruct . ParserState.popScope - - -- check variable name - 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') + modifyState (ParserState.modifyScope (Scope.expectScopeStruct + >>> StructScope.modifyVariables ((variableName, VariableType.fromString variableType):) + >>> Scope.ScopeStruct + ) + ) diff --git a/src/Ubc/Parse/VariableType.hs b/src/Ubc/Parse/VariableType.hs index 17b3025..5921f5f 100644 --- a/src/Ubc/Parse/VariableType.hs +++ b/src/Ubc/Parse/VariableType.hs @@ -4,7 +4,10 @@ module Ubc.Parse.VariableType ) where -data VariableType = BuiltInI32 | BuiltInU32 | BuiltInF32 | UserStruct String +data VariableType = BuiltInI32 + | BuiltInU32 + | BuiltInF32 + | UserStruct String deriving (Show, Eq) fromString :: String -> VariableType diff --git a/ubcc.cabal b/ubcc.cabal index 0b07d73..fae95c2 100644 --- a/ubcc.cabal +++ b/ubcc.cabal @@ -26,7 +26,10 @@ source-repository head library exposed-modules: Ubc.Parse + Ubc.Parse.Config Ubc.Parse.Data.Struct + Ubc.Parse.Expression + Ubc.Parse.File Ubc.Parse.Language Ubc.Parse.ParserState Ubc.Parse.Scope