1. Maxim Moiseev
  2. Wepr

Commits

Maxim Moiseev  committed fb0deff

Clause fold added

  • Participants
  • Parent commits 0321c63
  • Branches default

Comments (0)

Files changed (5)

File src/Wepr/FoldTest.fsx

View file
+#r "System.Core.dll"
+#r "FSharp.PowerPack.dll"
+#r "bin\debug\Wepr.dll"
+
+open System
+open Wepr
+open Printf
+
+let main () =
+    let clause = Wepr.Parse "(Value.Length = 3 AND Value.Length in (1, 2, 3)) and Value in ('abc', 'bca')"
+    let toString =
+        ClauseHelper.fold
+            (fun ss cond value -> sprintf "%A %A %A" ss cond value)
+            (fun l op r -> sprintf "%A\n\t%A\n\t%A\n" op l r)
+    toString clause |> printfn "%s"
+        
+    
+do main ()

File src/Wepr/Wepr.fsproj

View file
     <Compile Include="parser.fsi" />
     <Compile Include="parser.fs" />
     <Compile Include="lexer.fs" />
+    <Compile Include="clauseFold.fs" />
     <Compile Include="expr.fs" />
     <None Include="Sample.fsx" />
+    <None Include="FoldTest.fsx" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="FSharp.PowerPack">

File src/Wepr/clause.fs

View file
 type LogOp =
     | Or
     | And
+    override this.ToString () = 
+        match this with
+        | Or  -> "or"
+        | And -> "and"
 
 type Cond =
     | Eq
     | EndsWith
     | In
     
+    override this.ToString () =
+        match this with
+        | Eq                -> "=="
+        | Neq               -> "!="
+        | Greater           -> ">"
+        | Less              -> "<"
+        | GreaterOrEqual    -> ">="
+        | LessOrEqual       -> "<="
+        | Contains          -> "Contains"
+        | StartsWith        -> "StartsWith"
+        | EndsWith          -> "EndsWith"
+        | In                -> "In"
+
 type Item =
     | Prop of string array
     

File src/Wepr/clauseFold.fs

View file
+namespace Wepr
+
+module ClauseHelper =
+
+    let fold simpleF complexF clause =
+        let unitem = function
+            | Prop ss -> ss
+        let rec loop clause cont =
+            match clause with
+            | Simple (item, condition, value) ->
+                cont (simpleF (unitem item) condition value)
+            | Complex (l, op, r) ->
+                loop l (fun lval ->
+                loop r (fun rval ->
+                cont (complexF lval op rval)))
+        loop clause id

File src/Wepr/expr.fs

View file
 [<AutoOpen>]        
 module Aux =
     let (!!) : string -> byte[] = System.Text.Encoding.ASCII.GetBytes
-    
-type IClauseVisitor =
-    abstract member And : unit -> unit
-    abstract member Or  : unit -> unit
-    
-module ClauseFold =
 
-    let rec visitClause (visitor:#IClauseVisitor) = function
-    | Simple (item, cond, value) -> 
-        ()
-    | Complex (left, logOp, right) ->
-        visitClause visitor left
-        visitClause visitor right
-        match logOp with
-        | And -> visitor.And ()
-        | Or  -> visitor.Or ()
-        
-    
 module Wepr =
     open Microsoft.FSharp.Text.Lexing
     open Microsoft.FSharp.Text.Parsing.ParseHelpers