Skip to content

Commit

Permalink
Support 'as' pattern in Elm syntax pattern
Browse files Browse the repository at this point in the history
+ Add a scenario using the 'as' keyword in patterns in case-of block to automated tests.
+ Expand the compilation of Elm syntax patterns to support this pattern type and add the resulting declaration.
+ Expand the composition of display strings for Elm values to add parens where needed, like nested choice type tags.
  • Loading branch information
Viir committed Jul 11, 2023
1 parent 07a0e52 commit 5a1e1d6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,36 +216,69 @@ submissionResponseFromResponsePineValue responseValue =
Err ("Failed to encode as Elm value: " ++ error)

Ok valueAsElmValue ->
Ok { displayText = elmValueAsExpression valueAsElmValue }
Ok { displayText = Tuple.first (elmValueAsExpression valueAsElmValue) }


elmValueAsExpression : ElmValue -> String
elmValueAsExpression : ElmValue -> ( String, { needsParens : Bool } )
elmValueAsExpression elmValue =
let
applyNeedsParens : ( String, { needsParens : Bool } ) -> String
applyNeedsParens ( expressionString, { needsParens } ) =
if needsParens then
"(" ++ expressionString ++ ")"

else
expressionString
in
case elmValue of
ElmList list ->
"[" ++ (list |> List.map elmValueAsExpression |> String.join ",") ++ "]"
( "[" ++ (list |> List.map (elmValueAsExpression >> Tuple.first) |> String.join ",") ++ "]"
, { needsParens = False }
)

ElmInteger integer ->
integer |> BigInt.toString
( integer |> BigInt.toString
, { needsParens = False }
)

ElmChar char ->
"'" ++ (char |> String.fromChar) ++ "'"
( "'" ++ (char |> String.fromChar) ++ "'"
, { needsParens = False }
)

ElmString string ->
string |> Json.Encode.string |> Json.Encode.encode 0
( string |> Json.Encode.string |> Json.Encode.encode 0
, { needsParens = False }
)

ElmRecord fields ->
if fields == [] then
( if fields == [] then
"{}"

else
"{ " ++ (fields |> List.map (\( fieldName, fieldValue ) -> fieldName ++ " = " ++ elmValueAsExpression fieldValue) |> String.join ", ") ++ " }"
else
"{ "
++ (fields
|> List.map
(\( fieldName, fieldValue ) ->
fieldName ++ " = " ++ Tuple.first (elmValueAsExpression fieldValue)
)
|> String.join ", "
)
++ " }"
, { needsParens = False }
)

ElmTag tagName tagArguments ->
tagName :: (tagArguments |> List.map elmValueAsExpression) |> String.join " "
( tagName
:: (tagArguments |> List.map (elmValueAsExpression >> applyNeedsParens))
|> String.join " "
, { needsParens = tagArguments /= [] }
)

ElmInternal desc ->
"<" ++ desc ++ ">"
( "<" ++ desc ++ ">"
, { needsParens = False }
)


elmValueAsJson : ElmValue -> Json.Encode.Value
Expand All @@ -272,7 +305,7 @@ elmValueAsJson elmValue =
Json.Encode.list identity [ Json.Encode.string tagName, Json.Encode.list elmValueAsJson tagArguments ]

ElmInternal _ ->
Json.Encode.string (elmValueAsExpression elmValue)
Json.Encode.string (Tuple.first (elmValueAsExpression elmValue))


pineValueAsElmValue : Pine.Value -> Result String ElmValue
Expand Down Expand Up @@ -1606,6 +1639,15 @@ compileElmSyntaxPattern elmPattern =
|> List.map (\fieldName -> ( fieldName, [ RecordFieldDeconstruction fieldName ] ))
}

Elm.Syntax.Pattern.AsPattern (Elm.Syntax.Node.Node _ aliasedPattern) (Elm.Syntax.Node.Node _ alias) ->
compileElmSyntaxPattern aliasedPattern
|> Result.map
(\aliasedResult ->
{ aliasedResult
| declarations = ( alias, [] ) :: aliasedResult.declarations
}
)

Elm.Syntax.Pattern.ParenthesizedPattern parenthesized ->
compileElmSyntaxPattern (Elm.Syntax.Node.value parenthesized)

Expand Down
2 changes: 1 addition & 1 deletion implement/elm-time/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ElmTime;

public class Program
{
public static string AppVersionId => "2023-07-10";
public static string AppVersionId => "2023-07-11";

private static int AdminInterfaceDefaultPort => 4000;

Expand Down
4 changes: 2 additions & 2 deletions implement/elm-time/elm-time.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>ElmTime</RootNamespace>
<AssemblyName>elm-time</AssemblyName>
<AssemblyVersion>2023.0710.0.0</AssemblyVersion>
<FileVersion>2023.0710.0.0</FileVersion>
<AssemblyVersion>2023.0711.0.0</AssemblyVersion>
<FileVersion>2023.0711.0.0</FileVersion>
<Nullable>enable</Nullable>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[]],[[],[Just Nothing]],[[Just (Just 4)],[Just (Just 5)]]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let
deconsMaybeMaybe maybeMaybe =
case maybeMaybe of
Nothing ->
[ [] ]

(Just Nothing) as justNothing ->
[ [], [ justNothing ] ]

(Just (Just x)) as justJust ->
[ [ justJust ]
, [ Just (Just (x + 1)) ]
]
in
[ deconsMaybeMaybe Nothing
, deconsMaybeMaybe (Just Nothing)
, deconsMaybeMaybe (Just (Just 4))
]

0 comments on commit 5a1e1d6

Please sign in to comment.