Skip to content

Instantly share code, notes, and snippets.

@takahisa
Created December 14, 2011 13:12
Show Gist options
  • Save takahisa/1476521 to your computer and use it in GitHub Desktop.
Save takahisa/1476521 to your computer and use it in GitHub Desktop.
module Esolang
open System
open System.IO
open FParsec
open FParsec.CharParsers
open FParsec.Primitives
open FParsec.Error
type Pattern =
| As of Pattern * Pattern
| Seq of Pattern * Pattern
| Identity of string
type Term =
| List of Expression list
| Seq of Expression * Expression
| Identity of string
and Expression =
| Production of Pattern * Expression
| Term of Term
type Parser<'T> = Parser<'T,unit>
let pExpressionList,pExpressionListImpl = createParserForwardedToRef()
let pExpression,pExpressionImpl = createParserForwardedToRef()
let pProduction,pProductionImpl = createParserForwardedToRef()
let pPattern,pPatternImpl = createParserForwardedToRef()
let pTerm,pTermImpl = createParserForwardedToRef()
let pId : Parser<string> =
many1Satisfy2L
<| (fun x -> isLetter x || x = '_')
<| (fun x -> isLetter x || isDigit x || x = '_')
<| "identifier"
do pExpressionListImpl :=
pchar '{' >>. many pExpression .>> pchar '}'
do pExpressionImpl :=
pProduction <|>
(pTerm |>> Expression.Term)
do pProductionImpl :=
((pPattern .>> pchar '|' .>>. pProduction) |>> Expression.Production)
do pPatternImpl :=
(pipe3 pId (pchar ':') pPattern (fun x y z -> Pattern.As (Pattern.Identity x,z))) <|>
(pipe3 pId (pchar '.') pPattern (fun x y z -> Pattern.Seq (Pattern.Identity x,z))) <|>
(pId |>> Pattern.Identity)
do pTermImpl :=
(pExpressionList |>> Term.List) <|>
((pExpression .>> pchar '.' .>>. pExpression) |>> Term.Seq ) <|>
(pId |>> Term.Identity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment