1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
module Language.GLSL.Syntax where
-- TODO:
-- - add support for 'array of strings' ?
-- - add support for macro preprocessing
-- - add support for optional macro #include
-- - applicative style (see http://github.com/markusle/husky)?
-- - type checking
-- - check for constant expression where expected
-- - error reporting
-- - pretty-printing
-- - basic queries (inputs and outputs of the shader)
-- - support GLSL 1.40?
-- - proper testing (HUnit and QuickCheck)
-- - use hpc with the tests
-- - scoping
-- - clean module import/export
-- - order of Syntax data types and Pretty instances should be the same
-- - build with no warning
-- - use hlint
-- - push to github
-- - push to hackage
-- - use parsec 3
-- - handle all possible newlines (\n, \r, \r\n, \n\r)
-- - 80-columns clean
-- - lot of restriction of Samplers use (section 4.1.7),
-- well in fact, for plenty of things.
----------------------------------------------------------------------
-- Abstract syntax tree
----------------------------------------------------------------------
data TranslationUnit = TranslationUnit [ExternalDeclaration] -- at least one
deriving (Show, Eq)
data ExternalDeclaration =
-- function declarations should be at top level (page 28)
FunctionDeclaration FunctionPrototype
| FunctionDefinition FunctionPrototype Compound
| Declaration Declaration
deriving (Show, Eq)
-- TODO clean
data Declaration =
-- e.g. layout (origin_upper_left) in vec4 gl_FragCoord;
-- struct name { ... };
-- struct name { ... } name;
InitDeclaration InvariantOrType [InitDeclarator]
| Precision PrecisionQualifier TypeSpecifierNoPrecision
| Block TypeQualifier String [Field] (Maybe (String, Maybe (Maybe Expr))) -- constant expression
-- e.g. layout (origin_upper_left) in; TODO check if it is only used for default layout.
| TQ TypeQualifier
deriving (Show, Eq)
-- TODO regroup String (Maybe (Maybe Expr)) as Declarator and use it for
-- StructDeclarator.
data InitDeclarator = InitDecl String (Maybe (Maybe Expr)) (Maybe Expr) -- constant expression; assignment expression
deriving (Show, Eq)
data InvariantOrType = InvariantDeclarator | TypeDeclarator FullType
deriving (Show, Eq)
data FunctionPrototype = FuncProt FullType String [ParameterDeclaration]
deriving (Show, Eq)
data ParameterDeclaration =
ParameterDeclaration (Maybe ParameterTypeQualifier)
(Maybe ParameterQualifier)
TypeSpecifier
(Maybe (String, Maybe Expr)) -- constant expression
deriving (Show, Eq)
data FullType = FullType (Maybe TypeQualifier) TypeSpecifier
deriving (Show, Eq)
-- sto
-- lay [sto]
-- int [sto]
-- inv [sto]
-- inv int sto
data TypeQualifier =
TypeQualSto StorageQualifier
| TypeQualLay LayoutQualifier (Maybe StorageQualifier)
| TypeQualInt InterpolationQualifier (Maybe StorageQualifier)
| TypeQualInv InvariantQualifier (Maybe StorageQualifier)
| TypeQualInv3 InvariantQualifier InterpolationQualifier StorageQualifier
deriving (Show, Eq)
data TypeSpecifier = TypeSpec (Maybe PrecisionQualifier) TypeSpecifierNoPrecision
deriving (Show, Eq)
data InvariantQualifier = Invariant
deriving (Show, Eq)
data InterpolationQualifier =
Smooth
| Flat
| NoPerspective
deriving (Show, Eq)
data LayoutQualifier = Layout [LayoutQualifierId]
deriving (Show, Eq)
data LayoutQualifierId = LayoutQualId String (Maybe Expr) -- TODO Expr should be IntConstant
deriving (Show, Eq)
data Statement =
-- declaration statement
DeclarationStatement Declaration
-- jump statement
| Continue
| Break
| Return (Maybe Expr)
| Discard -- fragment shader only
-- compound statement
| CompoundStatement Compound
-- expression statement
| ExpressionStatement (Maybe Expr)
-- selection statement
| SelectionStatement Expr Statement (Maybe Statement)
-- switch statement
| SwitchStatement Expr [Statement]
| CaseLabel CaseLabel
-- iteration statement
| While Condition Statement -- no new scope
| DoWhile Statement Expr
| For (Either (Maybe Expr) Declaration) (Maybe Condition) (Maybe Expr) Statement
-- 1st stmt: expression or declaration, 2nd: no new scope
deriving (Show, Eq)
data Compound = Compound [Statement]
deriving (Show, Eq)
data Condition =
Condition Expr
| InitializedCondition FullType String Expr -- assignment expression
deriving (Show, Eq)
data CaseLabel = Case Expr | Default
deriving (Show, Eq)
data StorageQualifier =
Const
| Attribute -- vertex only
| Varying
| CentroidVarying
| In
| Out
| CentroidIn
| CentroidOut
| Uniform
deriving (Show, Eq)
data TypeSpecifierNoPrecision = TypeSpecNoPrecision TypeSpecifierNonArray (Maybe (Maybe Expr)) -- constant expression
deriving (Show, Eq)
data TypeSpecifierNonArray =
Void
| Float
| Int
| UInt
| Bool
| Vec2
| Vec3
| Vec4
| BVec2
| BVec3
| BVec4
| IVec2
| IVec3
| IVec4
| UVec2
| UVec3
| UVec4
| Mat2
| Mat3
| Mat4
| Mat2x2
| Mat2x3
| Mat2x4
| Mat3x2
| Mat3x3
| Mat3x4
| Mat4x2
| Mat4x3
| Mat4x4
| Sampler1D
| Sampler2D
| Sampler3D
| SamplerCube
| Sampler1DShadow
| Sampler2DShadow
| SamplerCubeShadow
| Sampler1DArray
| Sampler2DArray
| Sampler1DArrayShadow
| Sampler2DArrayShadow
| ISampler1D
| ISampler2D
| ISampler3D
| ISamplerCube
| ISampler1DArray
| ISampler2DArray
| USampler1D
| USampler2D
| USampler3D
| USamplerCube
| USampler1DArray
| USampler2DArray
| Sampler2DRect
| Sampler2DRectShadow
| ISampler2DRect
| USampler2DRect
| SamplerBuffer
| ISamplerBuffer
| USamplerBuffer
| Sampler2DMS
| ISampler2DMS
| USampler2DMS
| Sampler2DMSArray
| ISampler2DMSArray
| USampler2DMSArray
| StructSpecifier (Maybe String) [Field]
| TypeName String -- TODO user-defined type, should verify if it is declared
deriving (Show, Eq)
data PrecisionQualifier = HighP | MediumP | LowP
deriving (Show, Eq)
-- TODO The type qualifier can be present only when there is one or more declarators.
-- There other restrictions, see 4.1.8.
data Field = Field (Maybe TypeQualifier) TypeSpecifier [StructDeclarator]
deriving (Show, Eq)
data StructDeclarator = StructDeclarator String (Maybe (Maybe Expr)) -- constant expression
deriving (Show, Eq)
data Expr =
-- primaryExpression
Variable String
| IntConstant IntConstantKind Integer
| FloatConstant Float
| BoolConstant Bool
-- postfixExpression
| Bracket Expr Expr
| FieldSelection Expr String
| MethodCall Expr FunctionIdentifier Parameters
| FunctionCall FunctionIdentifier Parameters
| PostInc Expr
| PostDec Expr
| PreInc Expr
| PreDec Expr
-- unary expression
| UnaryPlus Expr
| UnaryNegate Expr
| UnaryNot Expr
| UnaryOneComplement Expr
-- binary expression
| Mul Expr Expr
| Div Expr Expr
| Mod Expr Expr
| Add Expr Expr
| Sub Expr Expr
| LeftShift Expr Expr
| RightShift Expr Expr
| Lt Expr Expr
| Gt Expr Expr
| Lte Expr Expr
| Gte Expr Expr
| Equ Expr Expr
| Neq Expr Expr
| BitAnd Expr Expr
| BitXor Expr Expr
| BitOr Expr Expr
| And Expr Expr
| Or Expr Expr
| Selection Expr Expr Expr -- ternary _ ? _ : _ operator
-- assignment, the left Expr should be unary expression
| Equal Expr Expr
| MulAssign Expr Expr
| DivAssign Expr Expr
| ModAssign Expr Expr
| AddAssign Expr Expr
| SubAssign Expr Expr
| LeftAssign Expr Expr
| RightAssign Expr Expr
| AndAssign Expr Expr
| XorAssign Expr Expr
| OrAssign Expr Expr
-- sequence
| Sequence Expr Expr
deriving (Show, Eq)
data IntConstantKind = Hexadecimal | Octal | Decimal
deriving (Show, Eq)
data Parameters = ParamVoid | Params [Expr]
deriving (Show, Eq)
data ParameterQualifier = InParameter | OutParameter | InOutParameter
deriving (Show, Eq)
data ParameterTypeQualifier = ConstParameter
deriving (Show, Eq)
data FunctionIdentifier =
-- TODO could be refine (I think a precision qualifier is not permitted,
-- nor a complete struct definition)
FuncIdTypeSpec TypeSpecifier
| FuncId String
deriving (Show, Eq)
|