+
2
|
list
|
skin
|
login
|
editor
α-wwwiki
::
substitution
user:none
(6458 bytes)
_h1 substitution {div {@ style="display:none"} {define y () °° var args = [].slice.call(arguments).join( ' ' ); var style = "background:grey; color:white;" + "text-align:center; padding:0 5px;"; return '{span {@ style="' + style + '"}' + args + '}'; °°}} _p A web page is a tree, a sequence of leaves (words) connected by branches (HTML operators) to a single root. The browser's engine parses this tree and displays the page. The wiki code is a tree, a sequence of leaves (words) connected by branches (wiki operators) to a single root. So, the wiki engine as nothing to do then translating a tree in another tree. _p The wiki engine parses the tree of s-expression begining by the leaves (innermost). It's a process of substitutions (replacements) which stops when there is no more s-expression to substitute. See more in page [[parser]]. _h6 a first example _p We analyze the parsing of the formula giving the root(s) of a second degree equation ax{sup 2}+bx+c=0. _p See more about this equation in page [[equation]]. {pre 1) the infix notation : (-b + √( b{sup 2}-4ac ))/(2a) is not a tree, 2) the prefix notation : (/ (+ -b (sqrt (- (* b b) (* 4 a c)))) (* 2 a)) is a tree. 3) So, we will use the prefix notation. Note : The Math functions are not included in the lambdatalk's core dictionary, so the sqrt function must be first created : °°{define sqrt (x) return Math.sqrt( x ); }°° -> {define sqrt (x) return Math.sqrt( x ); } The substitution is done in 6 steps : 0. (/ (+ -b (sqrt (- (* b b) (* 4 a c)))) (* 2 a)) 1. (/ (+ -b (sqrt (- {y b{sup 2}} {y 4ac}))) {y 2a}) 2. (/ (+ -b (sqrt {y b{sup 2} - 4ac})) {y 2a}) 3. (/ (+ -b {y sqrt(b{sup 2} - 4ac)}) {y 2a}) 4. (/ {y -b + sqrt(b{sup 2} - 4ac)} {y 2a}) 5. {y -b + sqrt(b{sup 2} - 4ac) /{y 2a}} Example for values a = 1, b = -1, c = -1 : °°{/ {+ {- -1} {sqrt {- {* -1 -1} {* 4 1 -1}}}} {* 2 1}}°° -> {/ {+ {- -1} {sqrt {- {* -1 -1} {* 4 1 -1}}}} {* 2 1}} Here are the 6 steps : 0. (/ (+ 1 (sqrt (- (* -1 -1) (* 4 1 -1)))) (* 2 1)) 1. (/ (+ 1 (sqrt (- {y 1} {y -4}))) {y 2}) 2. (/ (+ 1 (sqrt {y 5})) {y 2}) 3. (/ (+ 1 {y 2.236}) {y 2}) 4. (/ {y 3.236} {y 2}) 5. {y 1.618} A graphical representation of the tree : 0. (/ (+ 1 (sqrt (- (* -1 -1) (* 4 1 -1)))) (* 2 1)) \/ / \/ / / \/ / \ / \ / / \/ \ \ / / \ \/ / 1. (/ (+ 1 (sqrt (- 1 -4))) 2) \ / / / \ / / / \ / / / \ / / \ / / \ / / \ / / \/ / / / 2. (/ (+ 1 (sqrt 5)) 2) \ / / \ / / \ / / \ / / \ / / / / 3. (/ (+ 1 2.236) 2) \/ / / \ / / \ / / \ / / \/ / 4. (/ 3.236 2) \ / / \ / / \ / / \ / \ / \ / \ / \ / \ / / 5. 1.618 } _p It's the way Lambdatalk works everywhere. No need to tokenize and to build a tree (it's soon a tree) before a recursive parsing and evaluation. Grace to a single pattern written in the powerful Regular Expressions language, the s-expressions are catched and translated to HTML expressions, from the innermost leaves of the tree to its root. And it's done ! _h6 Another example _p The previous example concerned a numerical substitution. We analyze now the way lambdatalk evaluates a paragraph, say : {pre °° {div {@ style="color:red; border:1px solid black; font:normal 1.2em arial;"} This is a paragraph with a {b bold word} and another {i {u italicized and underlined word}} } °°} {div {@ style="color:red; border:1px solid black; font:normal 1.2em arial;"} This is a paragraph with a {b bold word} and another {i {u italicized and underlined word}} } _p The evaluation/transformation is made in 3 steps : {pre °° 1) the engine catches and evaluates the three innermost S-expressions: - {@ style="color:red; border:1px solid black; font:normal 1.2em arial;"} - {b bold word} - {u italicized and underlined word} {div ''[''@ style="color:red; border:1px solid black; font:normal 1.2em arial;"'']'' This is a paragraph with a < b> bold word< /b> and another {i < u> italicized and underlined word< />} } 2) then catches and evaluates {i < u> italicized and underlined word< />} : {div ''[''@ style="color:red; border:1px solid black; font:normal 1.2em arial;"'']'' This is a paragraph with a < b> bold word< /b> and another < i> < u> italicized and underlined word< />< /i> } 3) and the whole {div .... } S-expression : < div style="color:red; border:1px solid black; font:normal 1.2em arial;"> This is a paragraph with a < b> bold word< /b> and another < i> < u> italicized and underlined word< />< /i> < /div> There are no more S-expressions and this string is sent to the browser. °°} _p The unique syntax to display a bunch of words in bold style : °°{b a bunch of words}°° -> {b a bunch of words}, or to evaluate a math operation °°{* 1 2 3 4}°° -> {* 1 2 3 4}, makes things easy, isnt'it ? _p And when lambdatalk doesn't know what to do with a S-expression, ie °°{I am not a valid S-expression}°°, it just changes curly braces in round braces and leaves it unchanged : {I am not a valid S-expression}. No error message, just ignore.