Skip to content

Commit

Permalink
cleanup: Use a single NonNull constructor for nullability info.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 9, 2022
1 parent ca88599 commit ae90b02
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ haskell_library(
],
),
src_strip_prefix = "src",
version = "0.0.13",
version = "0.0.14",
visibility = ["//visibility:public"],
deps = [
":lexer",
Expand Down
2 changes: 1 addition & 1 deletion cimple.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/Language/Cimple/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/Language/Cimple/MapAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
17 changes: 8 additions & 9 deletions src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/Language/Cimple/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/Language/Cimple/TraverseAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/Language/Cimple/TreeParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) }
Expand Down Expand Up @@ -185,8 +184,7 @@ CommentableDecl :: { TextNode }
CommentableDecl
: functionDecl { $1 }
| functionDefn { $1 }
| non_null { $1 }
| nullable { $1 }
| nonNull { $1 }
| aggregateDecl { $1 }
| struct { $1 }
| typedef { $1 }
Expand Down

0 comments on commit ae90b02

Please sign in to comment.