Skip to content

Commit

Permalink
fsxc: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
knocte committed Sep 4, 2024
1 parent bf62186 commit 1a41205
Showing 1 changed file with 31 additions and 47 deletions.
78 changes: 31 additions & 47 deletions fsxc/Fsxc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,17 @@ module Program =

let ReadScriptContents(origScript: FileInfo) : List<LineAction> =

let readPreprocessorLine(line: string) : PreProcessorAction =
let readPreprocessorLine(line: string) : Option<PreProcessorAction> =
if (line.StartsWith("#!")) then
PreProcessorAction.Skip
Some PreProcessorAction.Skip
elif (line.StartsWith LOAD_PREPROCESSOR) then
let fileToLoad =
line.Substring(
LOAD_PREPROCESSOR.Length,
line.Length - LOAD_PREPROCESSOR.Length - 1
)

PreProcessorAction.Load fileToLoad
PreProcessorAction.Load fileToLoad |> Some
elif line.StartsWith REFNUGET_PREPROCESSOR then
let libToRef =
line.Substring(
Expand Down Expand Up @@ -269,7 +269,7 @@ module Program =
else
libToRef, None

PreProcessorAction.NugetRef(libName, maybeVersion)
PreProcessorAction.NugetRef(libName, maybeVersion) |> Some

elif (line.StartsWith REF_PREPROCESSOR) then
let libToRef =
Expand All @@ -278,53 +278,35 @@ module Program =
line.Length - REF_PREPROCESSOR.Length - 1
)

PreProcessorAction.Ref libToRef
PreProcessorAction.Ref libToRef |> Some
else
failwithf "Unrecognized preprocessor line: %s" line
None

let rec readLines
(lines: seq<string>)
(readState: ReadState)
(acc: List<LineAction>)
: List<LineAction> =

let isFsiPreProcessorAction(line: string) =
if not(line.StartsWith "#") then
false
elif line.StartsWith "#if"
|| line.StartsWith "#else"
|| line.StartsWith "#endif" then
false
else
true

match Seq.tryHead lines with
| Some line ->
let rest = Seq.tail lines

let newAcc, newState =
let noStateChange() =
if isFsiPreProcessorAction line then
let lineAction =
{
Line = line
LineKind =
LineKind.PreProcessorAction(
readPreprocessorLine line
)
}
let lineKind =
match readPreprocessorLine line with
| Some action -> LineKind.PreProcessorAction action
| None -> LineKind.Normal

let newAcc = lineAction :: acc
newAcc, readState
else
let lineAction =
{
Line = line
LineKind = LineKind.Normal
}
let newAcc =
{
Line = line
LineKind = lineKind
}
:: acc

let newAcc = lineAction :: acc
newAcc, readState
newAcc, readState

match readState with
| IgnoreLinesUntilNextPreProcessorConditional ->
Expand Down Expand Up @@ -355,24 +337,26 @@ module Program =

if trimmedLine.StartsWith "#if"
&& line.Contains "LEGACY_FRAMEWORK" then
match trimmedLine with
| "#if LEGACY_FRAMEWORK" ->
let newState =
match trimmedLine with
| "#if LEGACY_FRAMEWORK" ->
#if LEGACY_FRAMEWORK
acc,
DeliverLinesUntilNextPreProcessorConditional
DeliverLinesUntilNextPreProcessorConditional
#else
acc, IgnoreLinesUntilNextPreProcessorConditional
IgnoreLinesUntilNextPreProcessorConditional
#endif
| "#if !LEGACY_FRAMEWORK" ->
| "#if !LEGACY_FRAMEWORK" ->
#if LEGACY_FRAMEWORK
acc, IgnoreLinesUntilNextPreProcessorConditional
IgnoreLinesUntilNextPreProcessorConditional
#else
acc,
DeliverLinesUntilNextPreProcessorConditional
DeliverLinesUntilNextPreProcessorConditional
#endif
| _ ->
failwith
"Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define"

| _ ->
failwith
"Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define"

acc, newState
else
noStateChange()

Expand Down

0 comments on commit 1a41205

Please sign in to comment.