Skip to content

[WIP] Code generator random Fortran programs #310

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions fortran-src.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ library
Language.Fortran.AST.Literal.Complex
Language.Fortran.AST.Literal.Real
Language.Fortran.Common.Array
Language.Fortran.Generate
Language.Fortran.Intrinsics
Language.Fortran.LValue
Language.Fortran.Parser
Expand Down Expand Up @@ -186,6 +187,7 @@ library
, happy >=1.19
build-depends:
GenericPretty >=1.2.2 && <2
, QuickCheck >=2.10 && <2.15
, array ==0.5.*
, base >=4.6 && <5
, binary >=0.8.3.0 && <0.11
Expand Down Expand Up @@ -249,6 +251,7 @@ executable fortran-src
ghc-options: -Wall -fno-warn-tabs
build-depends:
GenericPretty >=1.2.2 && <2
, QuickCheck >=2.10 && <2.15
, array ==0.5.*
, base >=4.6 && <5
, binary >=0.8.3.0 && <0.11
Expand Down
2 changes: 2 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ dependencies:
- singletons-th >= 3.0 && < 3.4
- singletons-base >= 3.0 && < 3.4

- QuickCheck >=2.10 && <2.15

library:
source-dirs: src
ghc-options: -fno-warn-tabs
Expand Down
45 changes: 45 additions & 0 deletions src/Language/Fortran/Generate.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Language.Fortran.Generate where

import Language.Fortran.AST
import Test.QuickCheck

import Language.Fortran.Util.Position
import Language.Fortran.PrettyPrint
import Language.Fortran.Version

import Text.PrettyPrint
import Text.PrettyPrint.HughesPJ

-- instance Gen SrcSpan where
-- arbitrary = do
-- start <- arbitrary
-- end <- arbitrary
-- return $ SrcSpan start end

-- instance Gen Position where
-- arbitrary = do
-- absOffset <- arbitrary
-- col <- arbitrary
-- line <- arbitrary
-- filePath <- arbitrary
-- pragmaOffset <- arbitrary
-- return $ Position absOffset col line filePath pragmaOffset

instance Arbitrary a => Arbitrary (Value a) where
arbitrary = oneof
[ do x <- arbitrary :: Gen Integer
pure $ ValInteger (show x) Nothing
, do s <- arbitrary :: Gen String
pure $ ValString s
, ValLogical <$> arbitrary <*> pure Nothing
, pure $ ValVariable "myVar"
]

-- Generate a list of 10 values and pretty print
-- the results
demo :: IO ()
demo = do
values :: [Value ()] <- generate $ vectorOf 10 arbitrary
let prettyValues = map (pprint' Fortran90) values
mapM_ (putStrLn . render) prettyValues
putStrLn $ "Generated " ++ show (length values) ++ " values."
Loading