Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement GLSL parser #24 #25

Merged
merged 5 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/AST/Utils/Shader.elm
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fromString =

escape : String -> String
escape =
String.foldl
String.foldr
(\char acc ->
case char of
'\u{000D}' ->
Expand Down
144 changes: 80 additions & 64 deletions compiler/src/Parse/Shader.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module Parse.Shader exposing (shader)
import AST.Source as Src
import AST.Utils.Shader as Shader
import Data.Map as Dict
import Data.Name as Name
import Language.GLSL.Parser as GLP
import Language.GLSL.Syntax as GLS
import Parse.Primitives as P exposing (Col, Parser, Row)
import Reporting.Annotation as A
import Reporting.Error.Syntax as E
import Utils.Crash as Crash



Expand Down Expand Up @@ -65,7 +67,7 @@ parseBlock =
newPos - pos6

block =
String.slice off len src
String.left len (String.dropLeft off src)

newState =
P.State src (newPos + 2) end indent newRow (newCol + 2)
Expand Down Expand Up @@ -114,31 +116,32 @@ eatShader src pos end row col =

parseGlsl : Row -> Col -> String -> Parser E.Expr Shader.Types
parseGlsl startRow startCol src =
-- case GLP.parse src of
-- Ok (GLS.TranslationUnit decls) ->
-- P.succeed (List.foldr addInput emptyTypes (List.concatMap extractInputs decls))
-- Err err ->
-- let
-- pos =
-- Parsec.errorPos err
-- row =
-- fromIntegral (Parsec.sourceLine pos)
-- col =
-- fromIntegral (Parsec.sourceColumn pos)
-- msg =
-- Parsec.showErrorMessages
-- "or"
-- "unknown parse error"
-- "expecting"
-- "unexpected"
-- "end of input"
-- (Parsec.errorMessages err)
-- in
-- if row == 1 then
-- failure startRow (startCol + 6 + col) msg
-- else
-- failure (startRow + row - 1) col msg
Debug.todo "parseGlsl"
case GLP.parse src of
Ok ( _, _, GLS.TranslationUnit decls ) ->
P.pure (List.foldr addInput emptyTypes (List.concatMap extractInputs decls))

Err err ->
-- let
-- pos =
-- Parsec.errorPos err
-- row =
-- fromIntegral (Parsec.sourceLine pos)
-- col =
-- fromIntegral (Parsec.sourceColumn pos)
-- msg =
-- Parsec.showErrorMessages
-- "or"
-- "unknown parse error"
-- "expecting"
-- "unexpected"
-- "end of input"
-- (Parsec.errorMessages err)
-- in
-- if row == 1 then
-- failure startRow (startCol + 6 + col) msg
-- else
-- failure (startRow + row - 1) col msg
Debug.todo ("parseGlsl: " ++ Debug.toString err)


failure : Row -> Col -> String -> Parser E.Expr a
Expand All @@ -157,41 +160,54 @@ emptyTypes =
Shader.Types Dict.empty Dict.empty Dict.empty


addInput : ( GLS.StorageQualifier, Shader.Type, String ) -> Shader.Types -> Shader.Types
addInput ( qual, tipe, name ) (Shader.Types attribute uniform varying) =
case qual of
GLS.Attribute ->
Shader.Types (Dict.insert compare name tipe attribute) uniform varying

-- addInput : ( GLS.StorageQualifier, Shader.Type, String ) -> Shader.Types -> Shader.Types
-- addInput ( qual, tipe, name ) glDecls =
-- case qual of
-- GLS.Attribute ->
-- { glDecls | attribute = Dict.insert (Name.fromChars name) tipe glDecls.attribute }
-- GLS.Uniform ->
-- { glDecls | uniform = Dict.insert (Name.fromChars name) tipe glDecls.uniform }
-- GLS.Varying ->
-- { glDecls | varying = Dict.insert (Name.fromChars name) tipe glDecls.varying }
-- _ ->
-- Debug.crash "Should never happen due to `extractInputs` function"
-- extractInputs : GLS.ExternalDeclaration -> List ( GLS.StorageQualifier, Shader.Type, String )
-- extractInputs decl =
-- case decl of
-- GLS.Declaration (GLS.InitDeclaration (GLS.TypeDeclarator (GLS.FullType (Just (GLS.TypeQualSto qual)) (GLS.TypeSpec _ prec (GLS.TypeSpecNoPrecision tipe _ mexpr1)))) [ GLS.InitDecl name _ mexpr2 _ mexpr3 ]) ->
-- if List.member qual [ GLS.Attribute, GLS.Varying, GLS.Uniform ] then
-- case tipe of
-- GLS.Vec2 ->
-- [ ( qual, Shader.V2, name ) ]
-- GLS.Vec3 ->
-- [ ( qual, Shader.V3, name ) ]
-- GLS.Vec4 ->
-- [ ( qual, Shader.V4, name ) ]
-- GLS.Mat4 ->
-- [ ( qual, Shader.M4, name ) ]
-- GLS.Int ->
-- [ ( qual, Shader.Int, name ) ]
-- GLS.Float ->
-- [ ( qual, Shader.Float, name ) ]
-- GLS.Sampler2D ->
-- [ ( qual, Shader.Texture, name ) ]
-- _ ->
-- []
-- else
-- []
-- _ ->
-- []
GLS.Uniform ->
Shader.Types attribute (Dict.insert compare name tipe uniform) varying

GLS.Varying ->
Shader.Types attribute uniform (Dict.insert compare name tipe varying)

_ ->
Crash.crash "Should never happen due to `extractInputs` function"


extractInputs : GLS.ExternalDeclaration -> List ( GLS.StorageQualifier, Shader.Type, String )
extractInputs decl =
case decl of
GLS.Declaration (GLS.InitDeclaration (GLS.TypeDeclarator (GLS.FullType (Just (GLS.TypeQualSto qual)) (GLS.TypeSpec _ (GLS.TypeSpecNoPrecision tipe _)))) [ GLS.InitDecl name _ _ ]) ->
if List.member qual [ GLS.Attribute, GLS.Varying, GLS.Uniform ] then
case tipe of
GLS.Vec2 ->
[ ( qual, Shader.V2, name ) ]

GLS.Vec3 ->
[ ( qual, Shader.V3, name ) ]

GLS.Vec4 ->
[ ( qual, Shader.V4, name ) ]

GLS.Mat4 ->
[ ( qual, Shader.M4, name ) ]

GLS.Int ->
[ ( qual, Shader.Int, name ) ]

GLS.Float ->
[ ( qual, Shader.Float, name ) ]

GLS.Sampler2D ->
[ ( qual, Shader.Texture, name ) ]

_ ->
[]

else
[]

_ ->
[]
5 changes: 4 additions & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"andre-dietrich/parser-combinators": "4.1.0",
"dasch/levenshtein": "1.0.3",
"elm/browser": "1.0.2",
"elm/bytes": "1.0.8",
Expand All @@ -29,8 +30,10 @@
},
"indirect": {
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.3"
"elm/virtual-dom": "1.0.3",
"pilatch/flip": "1.0.0"
}
},
"test-dependencies": {
Expand Down
Loading