Introduced a cyclic dependency
This commit is contained in:
parent
4592a89ebf
commit
f35ca83d7a
5 changed files with 68 additions and 3 deletions
|
@ -13,6 +13,7 @@ import Text.Parsec (ParsecT, (<|>), (<?>), choice, option, many, many1, digit, l
|
|||
|
||||
import qualified Ubc.Parse.Syntax.Language as UbcLanguage
|
||||
import Data.Ratio ((%))
|
||||
import Ubc.Parse.Syntax.Statement (Statement)
|
||||
|
||||
data Expression = Binary BinaryOperator Expression Expression
|
||||
| Unary UnaryOperator Expression
|
||||
|
@ -22,10 +23,12 @@ data Expression = Binary BinaryOperator Expression Expression
|
|||
| Variable String
|
||||
| If Expression Expression (Maybe Expression) -- if then else
|
||||
| Loop Expression Expression -- condition body
|
||||
| Block [Expression]
|
||||
| Block [Statement]
|
||||
deriving (Show)
|
||||
|
||||
data UnaryOperator = LogicNot
|
||||
deriving (Show)
|
||||
|
||||
data BinaryOperator = Plus
|
||||
| Minus
|
||||
| Multiply
|
||||
|
|
|
@ -65,6 +65,7 @@ languageDef = LanguageDef {
|
|||
, "else"
|
||||
, "while"
|
||||
, "until"
|
||||
, "type"
|
||||
]
|
||||
, reservedOpNames = [ "+"
|
||||
, "-"
|
||||
|
|
|
@ -1,11 +1,49 @@
|
|||
module Ubc.Parse.Syntax.Statement
|
||||
( Statement(..)
|
||||
)
|
||||
, parseStatement)
|
||||
where
|
||||
|
||||
import Ubc.Parse.Syntax.VariableType (VariableType)
|
||||
import Ubc.Parse.Syntax.Expression (Expression)
|
||||
import Ubc.Parse.Syntax.Expression (Expression, expressionParser)
|
||||
import Ubc.Parse.Syntax.TypeExpression (TypeExpression)
|
||||
import Text.Parsec (choice, ParsecT, try)
|
||||
import qualified Ubc.Parse.Syntax.Language as UbcLanguage
|
||||
import qualified Ubc.Parse.Syntax.VariableType as VariableType
|
||||
import qualified Ubc.Parse.Syntax.TypeExpression as TypeExpression
|
||||
import Ubc.Parse.Syntax.ParserState (ParserState)
|
||||
import Control.Monad ((<$!>))
|
||||
|
||||
type VariableName = String
|
||||
type TypeName = String
|
||||
|
||||
data Statement = VariableDefinition VariableType VariableName Expression
|
||||
| TypeDefinition TypeName TypeExpression
|
||||
| ExpressionStatement Expression
|
||||
|
||||
parseStatement :: Monad m => ParsecT String ParserState m Statement
|
||||
parseStatement = choice [ variableDefinition
|
||||
, typeDefinition
|
||||
, ExpressionStatement <$!> expressionParser
|
||||
]
|
||||
|
||||
typeDefinition :: Monad m => ParsecT String ParserState m Statement
|
||||
typeDefinition = do
|
||||
UbcLanguage.reserved "type"
|
||||
|
||||
name <- UbcLanguage.identifier
|
||||
|
||||
UbcLanguage.reservedOperator "="
|
||||
|
||||
TypeDefinition name <$> TypeExpression.parseTypeExpression
|
||||
|
||||
|
||||
variableDefinition :: Monad m => ParsecT String u m Statement
|
||||
variableDefinition = do
|
||||
(variableType, variableName) <- try $ do
|
||||
variableType <- UbcLanguage.typeName
|
||||
variableName <- UbcLanguage.identifier
|
||||
_ <- UbcLanguage.reservedOperator "="
|
||||
return (VariableType.fromString variableType, variableName)
|
||||
|
||||
VariableDefinition variableType variableName <$> expressionParser
|
||||
|
||||
|
|
22
src/Ubc/Parse/Syntax/TypeExpression.hs
Normal file
22
src/Ubc/Parse/Syntax/TypeExpression.hs
Normal file
|
@ -0,0 +1,22 @@
|
|||
module Ubc.Parse.Syntax.TypeExpression
|
||||
( TypeExpression(..)
|
||||
, parseTypeExpression
|
||||
)
|
||||
where
|
||||
import Ubc.Parse.Syntax.VariableType (VariableType)
|
||||
import Ubc.Parse.Syntax.Data.Struct (Struct)
|
||||
import Text.Parsec (choice, ParsecT)
|
||||
import qualified Ubc.Parse.Syntax.Language as UbcLanguage
|
||||
import qualified Ubc.Parse.Syntax.VariableType as VariableType
|
||||
import Control.Monad ((<$!>))
|
||||
import qualified Ubc.Parse.Syntax.Struct as Struct
|
||||
import Ubc.Parse.Syntax.ParserState (ParserState)
|
||||
|
||||
data TypeExpression = TypeAlias VariableType
|
||||
| StructExpression Struct
|
||||
|
||||
parseTypeExpression :: Monad m => ParsecT String ParserState m TypeExpression
|
||||
parseTypeExpression = choice
|
||||
[ TypeAlias . VariableType.fromString <$!> UbcLanguage.typeName
|
||||
, StructExpression <$!> Struct.parseStruct
|
||||
]
|
|
@ -38,6 +38,7 @@ library
|
|||
Ubc.Parse.Syntax.Scope.StructScope
|
||||
Ubc.Parse.Syntax.Statement
|
||||
Ubc.Parse.Syntax.Struct
|
||||
Ubc.Parse.Syntax.TypeExpression
|
||||
Ubc.Parse.Syntax.Types
|
||||
Ubc.Parse.Syntax.VariableType
|
||||
other-modules:
|
||||
|
|
Loading…
Reference in a new issue