Skip to content

Commit

Permalink
Bump Fantomas and Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
iwillspeak committed Jun 14, 2023
1 parent aba5169 commit 27c9c0c
Show file tree
Hide file tree
Showing 31 changed files with 357 additions and 646 deletions.
8 changes: 1 addition & 7 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@
"dotnet-format"
]
},
"fantomas-tool": {
"version": "5.0.0-alpha-002",
"commands": [
"fantomas"
]
},
"fantomas": {
"version": "6.0.5",
"commands": [
"fantomas"
]
}
}
}
}
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
# https://github.com/dotnet/fsharp/issues/12320
dotnet pack --configuration $(configuration) --output=$(Build.ArtifactStagingDirectory)
# Check code format. Fantomas for F#, dotnet-format for C#
dotnet tool run fantomas --check --recurse .
dotnet tool run fantomas --check .
dotnet tool run dotnet-format --check
- task: PublishTestResults@2
condition: always()
Expand Down
136 changes: 57 additions & 79 deletions src/Feersum.CompilerServices/Binding/Binder.fs

Large diffs are not rendered by default.

63 changes: 26 additions & 37 deletions src/Feersum.CompilerServices/Binding/Libraries.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ type LibrarySignature<'a> =
module private Utils =
/// Map the exports in a given library signature
let mapExports mapper signature =
{ signature with Exports = signature.Exports |> mapper }
{ signature with
Exports = signature.Exports |> mapper }

/// Recognise a list of strings as a library name
let parseLibraryName (diags: DiagnosticBag) name =
Expand All @@ -80,8 +81,8 @@ module private Utils =

let parseNameElement element =
match element.Kind with
| AstNodeKind.Constant (SyntaxConstant.Number (namePart)) -> Ok(namePart |> sprintf "%g")
| AstNodeKind.Ident (namePart) ->
| AstNodeKind.Constant(SyntaxConstant.Number(namePart)) -> Ok(namePart |> sprintf "%g")
| AstNodeKind.Ident(namePart) ->
if Seq.exists (isInvalidChar) (namePart.ToCharArray()) then
"Library names should not contain complex characters"
|> Diagnostic.Create LibraryDiagnostics.improperLibraryName element.Location
Expand All @@ -101,35 +102,27 @@ module private Utils =
/// Try and parse a node as an identifier
let parseIdentifier =
function
| { Kind = AstNodeKind.Ident (id) } -> Ok(id)
| { Kind = AstNodeKind.Ident(id) } -> Ok(id)
| node ->
Result.Error(Diagnostic.Create LibraryDiagnostics.invalidLibraryName node.Location "Expected identifier")

/// Parse a list of identifiers into a list of strings
let parseIdentifierList idents =
idents
|> List.map parseIdentifier
|> Result.collect
idents |> List.map parseIdentifier |> Result.collect

/// Parse a library declaration form
let rec parseLibraryDeclaration (diags: DiagnosticBag) declaration =
match declaration.Kind with
| AstNodeKind.Form ({ Kind = AstNodeKind.Ident (special) } :: body) ->
| AstNodeKind.Form({ Kind = AstNodeKind.Ident(special) } :: body) ->
parseLibraryDeclarationForm diags declaration.Location special body
| _ ->
diags.Emit LibraryDiagnostics.malformedLibraryDecl declaration.Location "Expected library declaration"
LibraryDeclaration.Error

and parseLibraryDeclarationForm diags position special body =
match special with
| "export" ->
body
|> List.choose (parseExportDeclaration diags)
|> LibraryDeclaration.Export
| "import" ->
body
|> List.map (parseImportDeclaration diags)
|> LibraryDeclaration.Import
| "export" -> body |> List.choose (parseExportDeclaration diags) |> LibraryDeclaration.Export
| "import" -> body |> List.map (parseImportDeclaration diags) |> LibraryDeclaration.Import
| "begin" -> LibraryDeclaration.Begin body
| s ->
sprintf "Unrecognised library declaration %s" s
Expand All @@ -139,13 +132,13 @@ module private Utils =

and tryParseRename rename =
match rename with
| [ { Kind = AstNodeKind.Ident (int) }; { Kind = AstNodeKind.Ident (ext) } ] -> Ok({ From = int; To = ext })
| [ { Kind = AstNodeKind.Ident(int) }; { Kind = AstNodeKind.Ident(ext) } ] -> Ok({ From = int; To = ext })
| _ -> Result.Error("invalid rename")

and parseExportDeclaration diags export =
match export.Kind with
| AstNodeKind.Ident (plain) -> Some(ExportSet.Plain plain)
| AstNodeKind.Form ({ Kind = AstNodeKind.Ident ("rename") } :: rename) ->
| AstNodeKind.Ident(plain) -> Some(ExportSet.Plain plain)
| AstNodeKind.Form({ Kind = AstNodeKind.Ident("rename") } :: rename) ->
match rename |> tryParseRename with
| Ok renamed -> Some(ExportSet.Renamed renamed)
| Result.Error e ->
Expand All @@ -158,21 +151,21 @@ module private Utils =
and parseImportDeclaration diags import =
let parseImportForm parser fromSet body resultCollector =
match parser body with
| Ok (bound) -> resultCollector (parseImportDeclaration diags fromSet, bound)
| Result.Error (diag) ->
| Ok(bound) -> resultCollector (parseImportDeclaration diags fromSet, bound)
| Result.Error(diag) ->
diags.Add(diag)
ImportSet.Error

match import.Kind with
| AstNodeKind.Form ({ Kind = AstNodeKind.Ident ("only") } :: (fromSet :: filters)) ->
| AstNodeKind.Form({ Kind = AstNodeKind.Ident("only") } :: (fromSet :: filters)) ->
parseImportForm (parseIdentifierList) fromSet filters ImportSet.Only
| AstNodeKind.Form ({ Kind = AstNodeKind.Ident ("except") } :: (fromSet :: filters)) ->
| AstNodeKind.Form({ Kind = AstNodeKind.Ident("except") } :: (fromSet :: filters)) ->
parseImportForm (parseIdentifierList) fromSet filters ImportSet.Except
| AstNodeKind.Form ({ Kind = AstNodeKind.Ident ("rename") } :: (fromSet :: renames)) ->
| AstNodeKind.Form({ Kind = AstNodeKind.Ident("rename") } :: (fromSet :: renames)) ->
let parseRenames renames =
let parseRename node =
match node with
| { Kind = AstNodeKind.Form (f) } ->
| { Kind = AstNodeKind.Form(f) } ->
tryParseRename (f)
|> Result.mapError (Diagnostic.Create LibraryDiagnostics.malformedLibraryDecl node.Location)
| _ ->
Expand All @@ -183,11 +176,11 @@ module private Utils =
renames |> List.map parseRename |> Result.collect

parseImportForm (parseRenames) fromSet renames ImportSet.Renamed
| AstNodeKind.Form ([ { Kind = AstNodeKind.Ident ("prefix") }; fromSet; prefix ]) ->
| AstNodeKind.Form([ { Kind = AstNodeKind.Ident("prefix") }; fromSet; prefix ]) ->
parseImportForm (parseIdentifier) fromSet prefix ImportSet.Prefix
| _ ->
match parseLibraryName diags import with
| Ok (name) -> ImportSet.Plain name
| Ok(name) -> ImportSet.Plain name
| Result.Error _ -> ImportSet.Error


Expand All @@ -206,36 +199,32 @@ module Libraries =
/// Recursively check a library name matches
let rec matchLibraryName left right =
match (left, right) with
| (l :: lrest, r :: rrest) ->
if l = r then
matchLibraryName lrest rrest
else
false
| (l :: lrest, r :: rrest) -> if l = r then matchLibraryName lrest rrest else false
| ([], []) -> true
| _ -> false

/// Resolve a library import
let rec resolveImport libraries =
function
| ImportSet.Error -> Result.Error "invalid import set"
| ImportSet.Except (inner, except) ->
| ImportSet.Except(inner, except) ->
resolveImport libraries inner
|> Result.map (mapExports (List.filter (fun (id, _) -> List.contains id except |> not)))
| ImportSet.Only (inner, only) ->
| ImportSet.Only(inner, only) ->
resolveImport libraries inner
|> Result.map (mapExports (List.filter (fun (id, _) -> List.contains id only)))
| ImportSet.Plain lib ->
match Seq.tryFind (fun signature -> matchLibraryName lib signature.LibraryName) libraries with
| Some (signature) -> Ok(signature)
| Some(signature) -> Ok(signature)
| _ ->
lib
|> prettifyLibraryName
|> sprintf "Could not find library %s"
|> Result.Error
| ImportSet.Prefix (inner, prefix) ->
| ImportSet.Prefix(inner, prefix) ->
resolveImport libraries inner
|> Result.map (mapExports (List.map (fun (name, storage) -> (prefix + name, storage))))
| ImportSet.Renamed (inner, renames) ->
| ImportSet.Renamed(inner, renames) ->
let processRenames (name, storage) =
match List.tryFind (fun (x: SymbolRename) -> x.From = name) renames with
| Some rename -> (rename.To, storage)
Expand Down
72 changes: 29 additions & 43 deletions src/Feersum.CompilerServices/Binding/Lower.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,22 @@ type private CaptureConversionCtx =
/// expression. The returned list only contains _directly_ captured values.
let rec private findCaptured =
function
| Application (app, args) -> List.append (findCaptured app) (List.collect findCaptured args)
| If (cond, cons, els) ->
| Application(app, args) -> List.append (findCaptured app) (List.collect findCaptured args)
| If(cond, cons, els) ->
(findCaptured cond)
|> List.append (findCaptured cons)
|> List.append (
Option.map findCaptured els
|> Option.defaultValue []
)
| Seq (exprs) -> List.collect findCaptured exprs
| Lambda (formals, body) ->
|> List.append (Option.map findCaptured els |> Option.defaultValue [])
| Seq(exprs) -> List.collect findCaptured exprs
| Lambda(formals, body) ->
let isFree =
function
| Arg _
| Local _ -> true
| _ -> false

body.Captures |> List.filter isFree
| Store (_, v) ->
Option.map findCaptured v
|> Option.defaultValue []
| SequencePoint (inner, _) -> findCaptured inner
| Store(_, v) -> Option.map findCaptured v |> Option.defaultValue []
| SequencePoint(inner, _) -> findCaptured inner
| e -> []

/// Finds all local variables in the given node. This is later used to compact
Expand All @@ -42,21 +37,16 @@ let rec private findLocals node =
| _ -> Set.empty

match node with
| Load (storage) -> fromStorage storage
| Application (app, args) -> Set.union (findLocals app) (Seq.map (findLocals) args |> Set.unionMany)
| If (cond, cons, els) ->
| Load(storage) -> fromStorage storage
| Application(app, args) -> Set.union (findLocals app) (Seq.map (findLocals) args |> Set.unionMany)
| If(cond, cons, els) ->
(findLocals cond)
|> Set.union (findLocals cons)
|> Set.union (
Option.map (findLocals) els
|> Option.defaultValue Set.empty
)
| Seq (exprs) -> Seq.map (findLocals) exprs |> Set.unionMany
| SequencePoint (inner, _) -> findLocals inner
| Store (storage, v) ->
let inner =
Option.map (findLocals) v
|> Option.defaultValue Set.empty
|> Set.union (Option.map (findLocals) els |> Option.defaultValue Set.empty)
| Seq(exprs) -> Seq.map (findLocals) exprs |> Set.unionMany
| SequencePoint(inner, _) -> findLocals inner
| Store(storage, v) ->
let inner = Option.map (findLocals) v |> Option.defaultValue Set.empty

Set.union (fromStorage storage) inner
| e -> Set.empty
Expand All @@ -74,23 +64,18 @@ let private mappingsForExpr expr =
let uncapturedLocals = Set.difference (findLocals expr) captured

let localReWrites =
uncapturedLocals
|> Seq.indexed
|> Seq.map (fun (i, s) -> (s, Local i))
uncapturedLocals |> Seq.indexed |> Seq.map (fun (i, s) -> (s, Local i))

Seq.append captureReWrites localReWrites
|> Map.ofSeq
Seq.append captureReWrites localReWrites |> Map.ofSeq

/// Re-write a storage location to move captured values to the environment.
let rec private rewriteStorage ctx =
function
| Captured s ->
match ctx.Parent with
| Some (parent) -> Captured(rewriteStorage parent s)
| Some(parent) -> Captured(rewriteStorage parent s)
| None -> ice "Capture chain does not match nesting"
| s ->
Map.tryFind s ctx.Mappings
|> Option.defaultValue s
| s -> Map.tryFind s ctx.Mappings |> Option.defaultValue s

/// Re-write the environment size, if needed.
let private rewriteEnv env mappings =
Expand Down Expand Up @@ -121,11 +106,11 @@ let private rewriteLocals locals mappings =
let rec private rewriteExpression ctx =
function
| Load s -> Load(rewriteStorage ctx s)
| Store (s, v) -> Store(rewriteStorage ctx s, Option.map (rewriteExpression ctx) v)
| Application (app, args) -> Application(rewriteExpression ctx app, List.map (rewriteExpression ctx) args)
| If (cond, cons, els) ->
| Store(s, v) -> Store(rewriteStorage ctx s, Option.map (rewriteExpression ctx) v)
| Application(app, args) -> Application(rewriteExpression ctx app, List.map (rewriteExpression ctx) args)
| If(cond, cons, els) ->
If(rewriteExpression ctx cond, rewriteExpression ctx cons, Option.map (rewriteExpression ctx) els)
| Seq (exprs) ->
| Seq(exprs) ->
// This pair of functions allows us to flatten out nested sequences into
// a simpler representation.
let consolidate =
Expand All @@ -144,9 +129,9 @@ let rec private rewriteExpression ctx =
|> Seq.map ((rewriteExpression ctx) >> unfurl)
|> List.concat
|> consolidate
| Lambda (formals, root) -> Lambda(formals, (rewriteRoot (Some(ctx)) root))
| SequencePoint (inner, location) -> SequencePoint((rewriteExpression ctx inner), location)
| Library (name, mangledName, exports, body) -> Library(name, mangledName, exports, rewriteRoot None body)
| Lambda(formals, root) -> Lambda(formals, (rewriteRoot (Some(ctx)) root))
| SequencePoint(inner, location) -> SequencePoint((rewriteExpression ctx inner), location)
| Library(name, mangledName, exports, body) -> Library(name, mangledName, exports, rewriteRoot None body)
| e -> e

/// Re-write an expression tree root. This node represents a top level
Expand All @@ -157,7 +142,7 @@ and private rewriteRoot parent root =
// context so needs to be done before we create a derived `ctx`.
let captures =
match parent with
| Some (ctx) -> List.map (rewriteStorage ctx) root.Captures
| Some(ctx) -> List.map (rewriteStorage ctx) root.Captures
| _ -> root.Captures
// find out what is captured
let ctx =
Expand All @@ -175,4 +160,5 @@ and private rewriteRoot parent root =

/// Lower a bound tree to a form better suited for the emit phase.
let lower (ast: BoundSyntaxTree) =
{ ast with Root = rewriteRoot None ast.Root }
{ ast with
Root = rewriteRoot None ast.Root }
Loading

0 comments on commit 27c9c0c

Please sign in to comment.