This commit is contained in:
mtgmonkey
2025-11-29 00:43:30 +01:00
parent d419b0f40d
commit be41a03eee
2 changed files with 153 additions and 1 deletions

View File

@@ -7,7 +7,37 @@ module Main (main) where
import Relude
import Types
-- MAIN --
main :: IO ()
main = putTextLn "hallo Welt"
main = do
putTextLn "hallo Welt"
putStrLn $ generateGLSL vertShader
putStrLn $ generateGLSL fragShader
fragShader :: Program
fragShader =
[ VersionDeclaration 450 Core
, VariableDeclaration Nothing In fragColorIn
, VariableDeclaration Nothing Out fragColorOut
, MainStart
, VariableAssignment fragColorOut fragColorIn
]
vertShader :: Program
vertShader =
[ VersionDeclaration 450 Core
, VariableDeclaration (Just $ Location 0) In vertexPosition
, VariableDeclaration (Just $ Location 1) In vertexColor
, VariableDeclaration Nothing Out fragColorOut
, MainStart
, VariableAssignment GL_POSITION vertexPosition
, VariableAssignment fragColorIn vertexColor
]
fragColorIn = Variable "fragColorIn" $ GLSLVec4 GLSLFloat
fragColorOut = Variable "fragColorOut" $ GLSLVec4 GLSLFloat
vertexPosition = Variable "vertexPosition" $ GLSLVec4 GLSLFloat
vertexColor = Variable "vertexColor" $ GLSLVec4 GLSLFloat

122
src/Types.hs Normal file
View File

@@ -0,0 +1,122 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Types where
-- IMPORTS --
import Relude
import qualified Text.Show
-- TYPES --
type Program = Expressions
-- version declaration
type RequestedGLVersion = Int
data RequestedGLType
= Core
instance Show RequestedGLType where
show Core = "core"
-- variables
data ParameterQualifier
= In
| Out
instance Show ParameterQualifier where
show In = "in"
show Out = "out"
data LayoutQualifier
= Location Int
type Variables = [Variable]
type VariableName = String
data Variable
= Variable
VariableName
GLSLType
| GL_POSITION
nameOf :: Variable -> String
nameOf GL_POSITION = "gl_position"
nameOf (Variable name _) = name
data GLSLType
= GLSLFloat
| GLSLVec4 GLSLType
instance Show GLSLType where
show (GLSLVec4 _) = "vec4"
show GLSLFloat = "float"
-- expressions
type Expressions = [Expression]
data Expression
= VersionDeclaration
RequestedGLVersion
RequestedGLType
| VariableDeclaration
(Maybe LayoutQualifier)
ParameterQualifier
Variable
| MainStart
| VariableAssignment
Variable
Variable
generateGLSL :: Program -> String
generateGLSL [] = "}"
generateGLSL (expression:expressions) =
(++)
(
case expression of
VersionDeclaration
requestedGLVersion
requestedGLType ->
"#version " ++
(show requestedGLVersion) ++
" " ++
(show requestedGLType) ++
"\n"
VariableDeclaration
(Just layoutQualifier@(Location location))
parameterQualifier
(Variable variableName glslType) ->
"layout (location = " ++
(show location) ++
") " ++
(show parameterQualifier) ++
" " ++
(show glslType) ++
" " ++
variableName ++
";\n"
VariableDeclaration
Nothing
parameterQualifier
(Variable variableName glslType) ->
(show parameterQualifier) ++
" " ++
(show glslType) ++
" " ++
variableName ++
";\n"
MainStart ->
"void main()\n{\n"
VariableAssignment
variable0
variable1 ->
(nameOf variable0) ++
" = " ++
(nameOf variable1) ++
";\n"
)
$ generateGLSL expressions