Last active
December 31, 2020 17:11
-
-
Save ia7ck/ade36d8f909c429a9034b04c9cec8ca8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
<RootNamespace>fparsec_practice</RootNamespace> | |
</PropertyGroup> | |
<ItemGroup> | |
<Compile Include="Program.fs" /> | |
</ItemGroup> | |
<ItemGroup> | |
<PackageReference Include="FParsec" Version="1.1.1" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open FParsec | |
let test (p: Parser<'a, unit>) (str: string) = | |
match run p str with | |
| Success (result, _, _) -> printfn "Success: %A" result | |
| Failure (errorMsg, _, _) -> printfn "Failure: %s" errorMsg | |
let plus (a: float) (b: float) = a + b | |
[<EntryPoint>] | |
let main _ = | |
test pfloat "1.23" // Success: 1.23 | |
test pfloat "a1b2c3" // Failure | |
test pfloat " 1.23 " // Failure | |
let number = spaces >>. pfloat .>> spaces | |
test number "1.23" // Success: 1.23 | |
test number " 1.23 " // Success: 1.23 | |
let opPlus = | |
spaces >>. stringReturn "plus" plus .>> spaces | |
let xPlusY = | |
pipe3 number opPlus number (fun x op y -> op x y) | |
test xPlusY "1 plus 2" // Success: 3.0 | |
let manyPlus = chainl1 number opPlus | |
test manyPlus "1 plus 2 plus 3 plus 4" // Success: 10.0 | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment