Skip to content

Commit

Permalink
v.1.0.0 (contd)
Browse files Browse the repository at this point in the history
* add typecasting functions: (array, str, int, float)
* add more tests
  • Loading branch information
foo123 committed Apr 6, 2019
1 parent 1b58a29 commit 56208d4
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 31 deletions.
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ A simple, fast and flexible **eXpression Parser Engine** (with custom functions

**see also:**

* [Contemplate](https://github.com/foo123/Contemplate) a light-weight template engine for Node/XPCOM/JS, PHP, Python
* [HtmlWidget](https://github.com/foo123/HtmlWidget) html widgets used as (template) plugins and/or standalone for PHP, Node/XPCOM/JS, Python (can be used as plugins for Contemplate engine as well)
* [Contemplate](https://github.com/foo123/Contemplate) a light-weight template engine for Node.js / Browser / XPCOM Javascript, PHP, Python
* [HtmlWidget](https://github.com/foo123/HtmlWidget) html widgets used as (template) plugins and/or standalone for Node.js / Browser / XPCOM Javascript, PHP, Python (can be used as plugins for Contemplate engine as well)
* [Tao](https://github.com/foo123/Tao.js) A simple, tiny, isomorphic, precise and fast template engine for handling both string and live dom based templates
* [ModelView](https://github.com/foo123/modelview.js) a light-weight and flexible MVVM framework for JavaScript/HTML5
* [ModelView MVC jQueryUI Widgets](https://github.com/foo123/modelview-widgets) plug-n-play, state-full, full-MVC widgets for jQueryUI using modelview.js (e.g calendars, datepickers, colorpickers, tables/grids, etc..) (in progress)
* [Dromeo](https://github.com/foo123/Dromeo) a flexible, agnostic router for Node/XPCOM/JS, PHP, Python
* [PublishSubscribe](https://github.com/foo123/PublishSubscribe) a simple and flexible publish-subscribe pattern implementation for Node/XPCOM/JS, PHP, Python
* [Regex Analyzer/Composer](https://github.com/foo123/RegexAnalyzer) Regular Expression Analyzer and Composer for Node/XPCOM/JS, PHP, Python
* [StringTemplate](https://github.com/foo123/StringTemplate) simple and flexible string templates for PHP, Python, Node/XPCOM/JS
* [GrammarTemplate](https://github.com/foo123/GrammarTemplate) versatile and intuitive grammar-based templating for PHP, Python, Node/XPCOM/JS
* [Dialect](https://github.com/foo123/Dialect) a simple cross-platform SQL construction for PHP, Python, Node/XPCOM/JS
* [Abacus](https://github.com/foo123/Abacus) a fast combinatorics and computation library for Node/XPCOM/JS, PHP, Python
* [Dromeo](https://github.com/foo123/Dromeo) a flexible, agnostic router for Node.js / Browser / XPCOM Javascript, PHP, Python
* [PublishSubscribe](https://github.com/foo123/PublishSubscribe) a simple and flexible publish-subscribe pattern implementation for Node.js / Browser / XPCOM Javascript, PHP, Python
* [Regex Analyzer/Composer](https://github.com/foo123/RegexAnalyzer) Regular Expression Analyzer and Composer for Node.js / Browser / XPCOM Javascript, PHP, Python
* [StringTemplate](https://github.com/foo123/StringTemplate) simple and flexible string templates for Node.js / Browser / XPCOM Javascript, PHP, Python
* [GrammarTemplate](https://github.com/foo123/GrammarTemplate) versatile and intuitive grammar-based templating for Node.js / Browser / XPCOM Javascript, PHP, Python
* [Dialect](https://github.com/foo123/Dialect) a simple cross-platform SQL construction for Node.js / Browser / XPCOM Javascript, PHP, Python
* [Abacus](https://github.com/foo123/Abacus) a fast combinatorics and computation library for Node.js / Browser / XPCOM Javascript, PHP, Python
* [Simulacra](https://github.com/foo123/Simulacra) a simulation, algebraic, probability and combinatorics PHP package for scientific computations
* [RT](https://github.com/foo123/RT) client-side real-time communication for Node/XPCOM/JS with support for Poll/BOSH/WebSockets
* [RT](https://github.com/foo123/RT) client-side real-time communication for Node.js / Browser / XPCOM Javascript with support for Poll / BOSH / WebSockets
* [Asynchronous](https://github.com/foo123/asynchronous.js) a simple manager for async, linearised, parallelised, interleaved and sequential tasks for JavaScript


Expand Down Expand Up @@ -62,7 +62,7 @@ Configuration is intutive, easy and flexible (see examples).

However, since `Xpresion` is a parser engine, adding a default configuration, it can itself be a parser as well (out-of-the-box).

**NOTE:** `Xpresion` (v.1.0.0+) uses [GrammarTemplates](https://github.com/foo123/GrammarTemplate) for operator output. This is more flexible than simply using [StringTemplates](https://github.com/foo123/StringTemplate) as previously, since it supports blocks of optional code, default values, optional placeholders and many other things. Only make sure to escape (with `\` character) all `"<"`,`">"`,`"["`,`"]"` literal characters inside the output you define for operators and functions (see default configuration example below)
**NOTE:** `Xpresion` (v.1.0.0+) uses [Grammar Templates](https://github.com/foo123/GrammarTemplate) for operator output. This is more flexible than simply using [String Templates](https://github.com/foo123/StringTemplate) as previously, since it supports blocks of optional code, default values, optional placeholders and many other things. Only make sure to escape (with `\` character) all `"<"`,`">"`,`"["`,`"]"` literal characters inside the output you define for operators and functions (see default configuration example below)

**Features:**

Expand Down Expand Up @@ -898,11 +898,21 @@ Xpresion.defaultConfiguration(Configuration({
,'output' : 'parseInt(<$.0>)'
,'otype' : T_NUM
}
,'float' : {
'input' : 'float'
,'output' : 'parseFloat(<$.0>)'
,'otype' : T_NUM
}
,'str' : {
'input' : 'str'
,'output' : 'String(<$.0>)'
,'otype' : T_STR
}
,'array' : {
'input' : 'array'
,'output' : 'Fn.ary(<$.0>)'
,'otype' : T_ARY
}
,'clamp' : {
'input' : 'clamp'
,'output' : 'Fn.clamp(<$.0>)'
Expand Down Expand Up @@ -965,6 +975,9 @@ Xpresion.defaultConfiguration(Configuration({
if ( l > 0 ) { for(i=0; i<l; i++) s += args[i]; s = s/l;}
return s;
}
,'ary' : function( x ) {
return is_array(x) ? x : [x];
}
,'ary_eq' : function( a1, a2 ) {
var l = a1.length, i;
if ( l===a2.length )
Expand Down Expand Up @@ -1006,8 +1019,8 @@ running in linear-time ( `O(n)` ) in the input sequence

#### TODO

* use [GrammarTemplate](https://github.com/foo123/GrammarTemplate) for more powerful and flexible rewrite output [DONE]
* use [Grammar Template](https://github.com/foo123/GrammarTemplate) for more powerful and flexible rewrite output [DONE]
* add full support for optional arguments in `operators`/`functions` [ALMOST DONE]
* add full support for (`xml`-like) `tags` in `expressions`
* performance tests
* add support for (`xml`-like) `tags` in `expressions`
* performance/unit tests [DONE PARTIALLY]

8 changes: 8 additions & 0 deletions src/js/Xpresion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,11 @@ Xpresion.init = function( ) {
,'output' : 'String(<$.0>)'
,'otype' : T_STR
}
,'array' : {
'input' : 'array'
,'output' : 'Fn.ary(<$.0>)'
,'otype' : T_ARY
}
,'clamp' : {
'input' : 'clamp'
,'output' : 'Fn.clamp(<$.0>)'
Expand Down Expand Up @@ -2825,6 +2830,9 @@ Xpresion.init = function( ) {
if ( l > 0 ) { for(i=0; i<l; i++) s += args[i]; s = s/l;}
return s;
}
,'ary' : function( x ) {
return is_array(x) ? x : [x];
}
,'ary_eq' : function( a1, a2 ) {
var l = a1.length, i;
if ( l===a2.length )
Expand Down
2 changes: 1 addition & 1 deletion src/js/Xpresion.min.js

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions src/php/Xpresion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2884,6 +2884,11 @@ function($curr){return true;},
,'output' => 'strval(<$.0>)'
,'otype' => Xpresion::T_STR
)
,'array' => array(
'input' => 'array'
,'output' => '$Fn-\\>ary(<$.0>)'
,'otype' => Xpresion::T_ARY
)
,'clamp' => array(
'input' => 'clamp'
,'output' => '$Fn-\\>clamp(<$.0>)'
Expand Down Expand Up @@ -2947,16 +2952,19 @@ function($curr){return true;},
foreach ($values as $v) $s += $v;
return $l > 0 ? $s/$l : $s;
}
,'ary_merge'=> function($a1, $a2) {
return array_merge((array)$a1, (array)$a2);
,'ary' => function( $x ) {
return is_array($x) ? $x : array($x);
}
,'ary_eq' => function($a1, $a2) {
,'ary_eq' => function( $a1, $a2 ) {
return ((array)$a1) == ((array)$a2);
}
,'match' => function($str, $regex) {
,'ary_merge'=> function( $a1, $a2 ) {
return array_merge((array)$a1, (array)$a2);
}
,'match' => function( $str, $regex ) {
return (bool)preg_match($regex, $str, $m);
}
,'contains' => function($o, $i) {
,'contains' => function( $o, $i ) {
if ( is_string($o) ) return (false !== strpos($o, strval($i)));
elseif ( XpresionUtils::is_assoc_array($o) ) return array_key_exists($i, $o);
elseif ( is_array($o) ) return in_array($i, $o);
Expand Down
10 changes: 8 additions & 2 deletions src/python/Xpresion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2569,6 +2569,11 @@ def ary_eq(a1, a2):
,'output' : 'str(<$.0>)'
,'otype' : T_STR
}
,'array' : {
'input' : 'array'
,'output' : 'Fn.ary(<$.0>)'
,'otype' : T_ARY
}
,'clamp' : {
'input' : 'clamp'
,'output' : 'Fn.clamp(<$.0>)'
Expand Down Expand Up @@ -2609,9 +2614,10 @@ def ary_eq(a1, a2):
,'len' : lambda v: 0 if v is None else (len(v) if isinstance(v,(str,list,tuple,dict)) else 1)
,'sum' : sum
,'avg' : avg
,'ary_merge': ary_merge
,'ary' : lambda x: x if isinstance(x,list) else (list(x) if isinstance(x,tuple) else [x])
,'ary_eq' : ary_eq
,'match' : lambda s, regex: bool(re.search(regex, s ))
,'ary_merge': ary_merge
,'match' : lambda s, regex: bool(re.search(regex, s))
,'contains' : lambda o, i: bool(i in o)
,'time' : php_time
,'date' : php_date
Expand Down
40 changes: 38 additions & 2 deletions test/out-js
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
Xpresion.VERSION 1.0.0

==========================================
Expression: array("string")
Variables : []
Evaluator : Fn.ary("string")
Data : {}
Result : ["string"]
==========================================
Expression: array(["ar","ra","y"])
Variables : []
Evaluator : Fn.ary(["ar","ra","y"])
Data : {}
Result : ["ar","ra","y"]
==========================================
Expression: str(2)
Variables : []
Evaluator : String(2)
Data : {}
Result : "2"
==========================================
Expression: str("2")
Variables : []
Evaluator : String("2")
Data : {}
Result : "2"
==========================================
Expression: int(2)
Variables : []
Evaluator : parseInt(2)
Data : {}
Result : 2
==========================================
Expression: int("2")
Variables : []
Evaluator : parseInt("2")
Data : {}
Result : 2
==========================================
Error: Xpresion Error: Unknown token "Math.pow(this)" at "Math.pow(this)"
==========================================
Expand Down Expand Up @@ -226,13 +262,13 @@ Expression: date("Y-m-d H:i:s")
Variables : []
Evaluator : Fn.date("Y-m-d H:i:s")
Data : {}
Result : "2019-03-20 11:53:52"
Result : "2019-04-06 17:01:05"
==========================================
Expression: time()
Variables : []
Evaluator : Fn.time()
Data : {}
Result : 1553075632
Result : 1554559265
==========================================
Expression: date("Y-m-d H:i:s", time())
Variables : []
Expand Down
72 changes: 68 additions & 4 deletions test/out-php
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
Xpresion.VERSION 1.0.0

==========================================
Expression: array("string")
Variables : []
Evaluator : $Fn->ary("string")
Data : array(0) {
}

Result : array(1) {
[0]=>
string(6) "string"
}

==========================================
Expression: array(["ar","ra","y"])
Variables : []
Evaluator : $Fn->ary(array("ar","ra","y"))
Data : array(0) {
}

Result : array(3) {
[0]=>
string(2) "ar"
[1]=>
string(2) "ra"
[2]=>
string(1) "y"
}

==========================================
Expression: str(2)
Variables : []
Evaluator : strval(2)
Data : array(0) {
}

Result : string(1) "2"

==========================================
Expression: str("2")
Variables : []
Evaluator : strval("2")
Data : array(0) {
}

Result : string(1) "2"

==========================================
Expression: int(2)
Variables : []
Evaluator : intval(2)
Data : array(0) {
}

Result : int(2)

==========================================
Expression: int("2")
Variables : []
Evaluator : intval("2")
Data : array(0) {
}

Result : int(2)

==========================================
Xpresion Error: Unknown token "hex2dec($this)" at hex2dec($this)
==========================================
Expand Down Expand Up @@ -146,11 +210,11 @@ Variables : [v]
Evaluator : Xpresion::GET($Var["v"],array("key","0","key"))
Data : array(1) {
["v"]=>
object(stdClass)#221 (1) {
object(stdClass)#242 (1) {
["key"]=>
array(2) {
[0]=>
object(stdClass)#135 (1) {
object(stdClass)#147 (1) {
["key"]=>
string(7) "correct"
}
Expand Down Expand Up @@ -271,7 +335,7 @@ Evaluator : date("Y-m-d H:i:s")
Data : array(0) {
}

Result : string(19) "2019-03-20 10:53:54"
Result : string(19) "2019-04-06 16:01:13"

==========================================
Expression: time()
Expand All @@ -280,7 +344,7 @@ Evaluator : time()
Data : array(0) {
}

Result : int(1553075634)
Result : int(1554559273)

==========================================
Expression: date("Y-m-d H:i:s", time())
Expand Down
42 changes: 39 additions & 3 deletions test/out-py
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
Xpresion Module loaded succesfully
Xpresion.VERSION 1.0.0

==========================================
Expression: array("string")
Variables : []
Evaluator : Fn.ary("string")
Data : {}
Result : ['string']
==========================================
Expression: array(["ar","ra","y"])
Variables : []
Evaluator : Fn.ary(["ar","ra","y"])
Data : {}
Result : ['ar', 'ra', 'y']
==========================================
Expression: str(2)
Variables : []
Evaluator : str(2)
Data : {}
Result : '2'
==========================================
Expression: str("2")
Variables : []
Evaluator : str("2")
Data : {}
Result : '2'
==========================================
Expression: int(2)
Variables : []
Evaluator : int(2)
Data : {}
Result : 2
==========================================
Expression: int("2")
Variables : []
Evaluator : int("2")
Data : {}
Result : 2
==========================================
Xpresion Error: Unknown token "math.pow(self)" at math.pow(self)
==========================================
Expand Down Expand Up @@ -109,7 +145,7 @@ Evaluator : Var["v"]
Expression: $v.key.0.key
Variables : [v]
Evaluator : Xpresion.GET(Var["v"],["key","0","key"])
Data : {'v': <__main__.TestObject object at 0x021E64F0>}
Data : {'v': <__main__.TestObject object at 0x021B64F0>}
Result : 'correct'
==========================================
Expression: True
Expand Down Expand Up @@ -218,13 +254,13 @@ Expression: date("Y-m-d H:i:s")
Variables : []
Evaluator : Fn.date("Y-m-d H:i:s")
Data : {}
Result : '2019-03-20 11:53:57'
Result : '2019-04-06 17:01:20'
==========================================
Expression: time()
Variables : []
Evaluator : Fn.time()
Data : {}
Result : 1553075637
Result : 1554559280
==========================================
Expression: date("Y-m-d H:i:s", time())
Variables : []
Expand Down
Loading

0 comments on commit 56208d4

Please sign in to comment.