From ae90b02495a993802173701cd9a286490202f826 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 9 Feb 2022 00:10:51 +0000 Subject: [PATCH] cleanup: Use a single `NonNull` constructor for nullability info. --- BUILD.bazel | 2 +- cimple.cabal | 2 +- src/Language/Cimple/Ast.hs | 3 +-- src/Language/Cimple/MapAst.hs | 6 ++---- src/Language/Cimple/Parser.y | 17 ++++++++--------- src/Language/Cimple/Pretty.hs | 14 +++++++++----- src/Language/Cimple/TraverseAst.hs | 9 +++------ src/Language/Cimple/TreeParser.y | 6 ++---- 8 files changed, 27 insertions(+), 32 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 20bb8c4..cdff06d 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -83,7 +83,7 @@ haskell_library( ], ), src_strip_prefix = "src", - version = "0.0.13", + version = "0.0.14", visibility = ["//visibility:public"], deps = [ ":lexer", diff --git a/cimple.cabal b/cimple.cabal index ad4b482..2a94466 100644 --- a/cimple.cabal +++ b/cimple.cabal @@ -1,5 +1,5 @@ name: cimple -version: 0.0.13 +version: 0.0.14 synopsis: Simple C-like programming language homepage: https://toktok.github.io/ license: GPL-3 diff --git a/src/Language/Cimple/Ast.hs b/src/Language/Cimple/Ast.hs index b5c747d..c0e6129 100644 --- a/src/Language/Cimple/Ast.hs +++ b/src/Language/Cimple/Ast.hs @@ -108,8 +108,7 @@ data NodeF lexeme a | FunctionPrototype a lexeme [a] | CallbackDecl lexeme lexeme | Ellipsis - | NonNull [lexeme] a - | Nullable [lexeme] a + | NonNull [lexeme] [lexeme] a -- Constants | ConstDecl a lexeme | ConstDefn Scope a lexeme a diff --git a/src/Language/Cimple/MapAst.hs b/src/Language/Cimple/MapAst.hs index 19e381c..b004d1d 100644 --- a/src/Language/Cimple/MapAst.hs +++ b/src/Language/Cimple/MapAst.hs @@ -254,10 +254,8 @@ instance MapAst itext otext (Node (Lexeme itext)) where Fix <$> (FunctionPrototype <$> recurse ty <*> recurse name <*> recurse params) CallbackDecl ty name -> Fix <$> (CallbackDecl <$> recurse ty <*> recurse name) - NonNull args f -> - Fix <$> (NonNull <$> recurse args <*> recurse f) - Nullable args f -> - Fix <$> (Nullable <$> recurse args <*> recurse f) + NonNull nonnull nullable f -> + Fix <$> (NonNull <$> recurse nonnull <*> recurse nullable <*> recurse f) Ellipsis -> pure $ Fix Ellipsis ConstDecl ty name -> diff --git a/src/Language/Cimple/Parser.y b/src/Language/Cimple/Parser.y index 9f65656..c12d561 100644 --- a/src/Language/Cimple/Parser.y +++ b/src/Language/Cimple/Parser.y @@ -633,19 +633,18 @@ FunctionDecl :: { StringNode } FunctionDecl : FunctionDeclarator { $1 Global } | static FunctionDeclarator { $2 Static } -| Nullable FunctionDeclarator { $1 $ $2 Global } -| Nullable static FunctionDeclarator { $1 $ $3 Static } +| NonNull FunctionDeclarator { $1 $ $2 Global } +| NonNull static FunctionDeclarator { $1 $ $3 Static } -Nullable :: { StringNode -> StringNode } -Nullable -: nullable '(' Ints ')' { Fix . Nullable $3 } -| non_null '(' Ints ')' { Fix . NonNull $3 } -| nullable '(' Ints ')' non_null '(' Ints ')' { Fix . Nullable $3 . Fix . NonNull $7 } +NonNull :: { StringNode -> StringNode } +NonNull +: non_null '(' ')' { Fix . NonNull [] [] } +| nullable '(' Ints ')' { Fix . NonNull [] $3 } +| non_null '(' Ints ')' nullable '(' Ints ')' { Fix . NonNull $3 $7 } Ints :: { [StringLexeme] } Ints -: { [] } -| IntList { reverse $1 } +: IntList { reverse $1 } IntList :: { [StringLexeme] } IntList diff --git a/src/Language/Cimple/Pretty.hs b/src/Language/Cimple/Pretty.hs index 2cc3617..5fa5f7e 100644 --- a/src/Language/Cimple/Pretty.hs +++ b/src/Language/Cimple/Pretty.hs @@ -28,8 +28,8 @@ kwExtern = dullgreen $ text "extern" kwFor = dullred $ text "for" kwGoto = dullred $ text "goto" kwIf = dullred $ text "if" -kwNullable = dullgreen $ text "nullable" kwNonNull = dullgreen $ text "non_null" +kwNullable = dullgreen $ text "nullable" kwReturn = dullred $ text "return" kwSizeof = dullred $ text "sizeof" kwStaticAssert = dullred $ text "static_assert" @@ -384,10 +384,14 @@ ppNode = foldFix go vcat enums ) <$> rbrace <+> dullgreen (ppLexeme ty) <> semi - NonNull args f -> - kwNonNull <> ppIntList args <$> f - Nullable args f -> - kwNullable <> ppIntList args <$> f + NonNull [] [] f -> + kwNonNull <> text "()" <$> f + NonNull nonnull [] f -> + kwNonNull <> ppIntList nonnull <$> f + NonNull [] nullable f -> + kwNullable <> ppIntList nullable <$> f + NonNull nonnull nullable f -> + kwNonNull <> ppIntList nonnull <+> kwNullable <> ppIntList nullable <$> f -- Statements VarDeclStmt decl Nothing -> decl <> semi diff --git a/src/Language/Cimple/TraverseAst.hs b/src/Language/Cimple/TraverseAst.hs index 9676ff0..008f23f 100644 --- a/src/Language/Cimple/TraverseAst.hs +++ b/src/Language/Cimple/TraverseAst.hs @@ -340,12 +340,9 @@ instance TraverseAst text (Node (Lexeme text)) where pure () Ellipsis -> pure () - NonNull args f -> do - _ <- recurse args - _ <- recurse f - pure () - Nullable args f -> do - _ <- recurse args + NonNull nonnull nullable f -> do + _ <- recurse nonnull + _ <- recurse nullable _ <- recurse f pure () ConstDecl ty name -> do diff --git a/src/Language/Cimple/TreeParser.y b/src/Language/Cimple/TreeParser.y index 50241de..01f021e 100644 --- a/src/Language/Cimple/TreeParser.y +++ b/src/Language/Cimple/TreeParser.y @@ -115,8 +115,7 @@ import Language.Cimple.Lexer (Lexeme) functionDefn { Fix (FunctionDefn{}) } functionPrototype { Fix (FunctionPrototype{}) } ellipsis { Fix (Ellipsis) } - non_null { Fix (NonNull{}) } - nullable { Fix (Nullable{}) } + nonNull { Fix (NonNull{}) } -- Constants constDecl { Fix (ConstDecl{}) } constDefn { Fix (ConstDefn{}) } @@ -185,8 +184,7 @@ CommentableDecl :: { TextNode } CommentableDecl : functionDecl { $1 } | functionDefn { $1 } -| non_null { $1 } -| nullable { $1 } +| nonNull { $1 } | aggregateDecl { $1 } | struct { $1 } | typedef { $1 }