mirror of
https://github.com/Smaug123/WoofWare.PrattParser
synced 2025-10-06 01:48:39 +00:00
If-then-else
This commit is contained in:
@@ -12,6 +12,7 @@ type Expr =
|
||||
| Factorial of Expr
|
||||
| Paren of Expr
|
||||
| IfThenElse of Expr * Expr * Expr
|
||||
| IfThen of Expr * Expr
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Expr =
|
||||
@@ -28,6 +29,8 @@ module Expr =
|
||||
let ifThenElse ifClause thenClause elseClause =
|
||||
Expr.IfThenElse (ifClause, thenClause, elseClause)
|
||||
|
||||
let ifThen ifClause thenClause = Expr.IfThen (ifClause, thenClause)
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
type TokenType =
|
||||
| Plus
|
||||
|
@@ -71,6 +71,24 @@ module Parser =
|
||||
|
||||
Expr.paren contents, rest
|
||||
|
||||
elif firstToken.Type = TokenType.If then
|
||||
let ifClause, rest = parseInner inputString rest 0
|
||||
|
||||
match rest with
|
||||
| [] -> failwith "if requires a trailing then"
|
||||
| head :: _ when head.Type <> TokenType.Then -> failwithf "if was not followed by then, got: %+A" head
|
||||
| _ :: rest ->
|
||||
|
||||
let thenClause, rest = parseInner inputString rest 0
|
||||
|
||||
match rest with
|
||||
| [] -> Expr.ifThen ifClause thenClause, rest
|
||||
| head :: _ when head.Type <> TokenType.Else -> Expr.ifThen ifClause thenClause, rest
|
||||
| _ :: rest ->
|
||||
|
||||
let elseClause, rest = parseInner inputString rest 0
|
||||
Expr.ifThenElse ifClause thenClause elseClause, rest
|
||||
|
||||
else
|
||||
|
||||
match Token.prefixPrecedence firstToken.Type with
|
||||
|
Reference in New Issue
Block a user