34 people like it.

Parsing string expressions, the lazy way

Sometimes you'd be surprised at what functionality you can find inside the .Net framework. Apparently the DataTable object can compute string expressions. Nice for testing your own parser implementation, and/or for lazy coders like me. Note that the DataTable is created only once and reused with each function call.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let eval = 
    let dt = new System.Data.DataTable()
    fun expr -> System.Convert.ToDouble(dt.Compute(expr,""))

// usage (FSI)          
// > eval "(1+5)*7/((3+(2-1))/(7-3))";;
// val it : float = 42.0
val eval : (string -> float)

Full name: Script.eval
val dt : System.Data.DataTable
namespace System
namespace System.Data
Multiple items
type DataTable =
  inherit MarshalByValueComponent
  new : unit -> DataTable + 2 overloads
  member AcceptChanges : unit -> unit
  member BeginInit : unit -> unit
  member BeginLoadData : unit -> unit
  member CaseSensitive : bool with get, set
  member ChildRelations : DataRelationCollection
  member Clear : unit -> unit
  member Clone : unit -> DataTable
  member Columns : DataColumnCollection
  member Compute : expression:string * filter:string -> obj
  ...

Full name: System.Data.DataTable

--------------------
System.Data.DataTable() : unit
System.Data.DataTable(tableName: string) : unit
System.Data.DataTable(tableName: string, tableNamespace: string) : unit
val expr : string
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
System.Convert.ToDouble(value: System.DateTime) : float
   (+0 other overloads)
System.Convert.ToDouble(value: bool) : float
   (+0 other overloads)
System.Convert.ToDouble(value: string) : float
   (+0 other overloads)
System.Convert.ToDouble(value: decimal) : float
   (+0 other overloads)
System.Convert.ToDouble(value: float) : float
   (+0 other overloads)
System.Convert.ToDouble(value: float32) : float
   (+0 other overloads)
System.Convert.ToDouble(value: uint64) : float
   (+0 other overloads)
System.Convert.ToDouble(value: int64) : float
   (+0 other overloads)
System.Convert.ToDouble(value: uint32) : float
   (+0 other overloads)
System.Convert.ToDouble(value: int) : float
   (+0 other overloads)
System.Data.DataTable.Compute(expression: string, filter: string) : obj
Raw view New version

More information

Link:http://fssnip.net/1D
Posted:6 years ago
Author:Arjen Kopinga
Tags: expression , parsing