Removed complicated validation, Basic Expression parsing
This commit is contained in:
parent
1c1e25a881
commit
839e278e43
7 changed files with 36 additions and 37 deletions
|
@ -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 ()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
)
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue