Skip to content

Commit

Permalink
Do not crash compilation on source using 'Debug' functions
Browse files Browse the repository at this point in the history
Insert stubs for now for 'Debug.log' and 'Debug.toString'
  • Loading branch information
Viir committed Jan 1, 2024
1 parent a957661 commit da1a174
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 27 deletions.
102 changes: 78 additions & 24 deletions implement/elm-time/ElmTime/compile-elm-program/src/ElmCompiler.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import FirCompiler
, Expression(..)
, ModuleImports
, countListElementsExpression
, emitExpressionInDeclarationBlock
, emitWrapperForPartialApplication
, equalCondition
, equalCondition_Pine
Expand Down Expand Up @@ -1807,35 +1806,90 @@ getDeclarationValueFromCompilation ( localModuleName, nameInModule ) compilation
canonicalModuleName =
Dict.get localModuleName compilation.moduleAliases
|> Maybe.withDefault localModuleName
in
case compilation.availableModules |> Dict.get canonicalModuleName of
Nothing ->
Err
("Did not find module '"
++ String.join "." canonicalModuleName
++ "'. There are "
++ (String.fromInt (Dict.size compilation.availableModules)
++ " declarations in this scope: "
++ String.join ", " (List.map (String.join ".") (Dict.keys compilation.availableModules))
)
)

Just moduleValue ->
case Dict.get nameInModule moduleValue.declarations of
continueWithDefault () =
case compilation.availableModules |> Dict.get canonicalModuleName of
Nothing ->
Err
("Did not find '"
++ nameInModule
++ "' in module '"
("Did not find module '"
++ String.join "." canonicalModuleName
++ "'. There are "
++ String.fromInt (Dict.size moduleValue.declarations)
++ " names available in that module: "
++ String.join ", " (Dict.keys moduleValue.declarations)
++ (String.fromInt (Dict.size compilation.availableModules)
++ " declarations in this scope: "
++ String.join ", " (List.map (String.join ".") (Dict.keys compilation.availableModules))
)
)

Just declarationValue ->
Ok declarationValue
Just moduleValue ->
case Dict.get nameInModule moduleValue.declarations of
Nothing ->
Err
("Did not find '"
++ nameInModule
++ "' in module '"
++ String.join "." canonicalModuleName
++ "'. There are "
++ String.fromInt (Dict.size moduleValue.declarations)
++ " names available in that module: "
++ String.join ", " (Dict.keys moduleValue.declarations)
)

Just declarationValue ->
Ok declarationValue
in
case Dict.get canonicalModuleName getDeclarationValueFromCompilationOverrides of
Nothing ->
continueWithDefault ()

Just overrides ->
case Dict.get nameInModule overrides of
Just overrideValue ->
overrideValue

Nothing ->
continueWithDefault ()


getDeclarationValueFromCompilationOverrides : Dict.Dict (List String) (Dict.Dict String (Result String Pine.Value))
getDeclarationValueFromCompilationOverrides =
[ ( [ "Debug" ]
, [ ( "log"
-- TODO: mapping for Debug.log so we can get messages.
, FunctionExpression
[ [ ( "message", [] ) ], [ ( "payload", [] ) ] ]
(ReferenceExpression "payload")
|> FirCompiler.emitExpression
{ moduleImports =
{ importedModules = Dict.empty
, importedDeclarations = Dict.empty
}
, declarationsDependencies = Dict.empty
, environmentFunctions = []
, environmentDeconstructions = Dict.empty
}
|> Result.andThen evaluateAsIndependentExpression
)
, ( "toString"
-- TODO: mapping for Debug.toString
, FunctionExpression
[ [ ( "elm_value", [] ) ] ]
(LiteralExpression (valueFromString "Debug.toString is not implemented yet"))
|> FirCompiler.emitExpression
{ moduleImports =
{ importedModules = Dict.empty
, importedDeclarations = Dict.empty
}
, declarationsDependencies = Dict.empty
, environmentFunctions = []
, environmentDeconstructions = Dict.empty
}
|> Result.andThen evaluateAsIndependentExpression
)
]
|> Dict.fromList
)
]
|> Dict.fromList


compileLookupForInlineableDeclaration : ( List String, String ) -> Pine.Value -> Expression
Expand Down Expand Up @@ -1886,7 +1940,7 @@ emitModuleDeclarations :
emitModuleDeclarations stackBefore declarations =
declarations.supportingDeclarations
|> Dict.union declarations.exposedDeclarations
|> emitExpressionInDeclarationBlock stackBefore
|> FirCompiler.emitExpressionInDeclarationBlock stackBefore
|> (\builder ->
declarations.exposedDeclarations
|> Dict.toList
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 @@ -18,7 +18,7 @@ namespace ElmTime;

public class Program
{
public static string AppVersionId => "2023-12-31";
public static string AppVersionId => "2024-01-01";

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>net8.0</TargetFramework>
<RootNamespace>ElmTime</RootNamespace>
<AssemblyName>elm-time</AssemblyName>
<AssemblyVersion>2023.1231.0.0</AssemblyVersion>
<FileVersion>2023.1231.0.0</FileVersion>
<AssemblyVersion>2024.0101.0.0</AssemblyVersion>
<FileVersion>2024.0101.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 @@
"payload"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Debug.log "testing debug message" "payload"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Debug.toString is not implemented yet"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Debug.toString [ 11, 13, 17, 19 ]

0 comments on commit da1a174

Please sign in to comment.