Maytag Metalark avatar Maytag Metalark committed 4422a19

Working on README. Added C-code sin-lut template. Working on actually evaluating the example templates.

Comments (0)

Files changed (3)

templ/README.templ

     60:1.36602540378,
     70:1.28171276411}"
 }
-
-
         >>> } % end section "Basics"
 
+        {section "Code Generation - A Sine Lookup Table" {::
+
+
+            {let :TEMPLATE}
+            {$ :TEMPLATE "
+{v
+    {set :SIZE 10}
+}const double sine_lut[{get :SIZE}] =
+\\{
+{for i {range {get :SIZE}} {::
+    {let :THETA}
+    {$ :THETA {mult
+        {$ i}
+        {div 360 {$ :SIZE}}
+    }}
+    {spit {'
+        \"    \"
+        {round 
+            {sin {rad {$ :THETA}}}
+            4
+        }
+        ,
+        {\\t}
+        \"// i = \"
+        {get i}
+        \", theta = \"
+        {$ :THETA}
+        \" deg\"
+        {lnbrk}
+    }}
+}}\\};
+"
+            }
+            
+        <<<
+            {p
+                <<<
+                The following template shows an example of using {code templ} to
+                generate a C-code sine look-up-table:
+                >>>
+            }
+            {pre {' :syntax templ } {$ :TEMPLATE}}
+
+            {p
+                <<<
+                Which produces this:
+                >>>
+            }
+            {pre {' :syntax c } {buffer {eval {$ :TEMPLATE}}}}
+
+        >>> }} % end section "Code Generation..."
 
     }} %end section Examples
 
                     }
 
                     %else
-                    {error "Incorrect number of arguments to pre: expected one or two."}
+                    {error {glue "Incorrect number of arguments to pre: expected one or two, not " {len {$ :argv}}}}
                 }
 
                 {if
-Hello, World!
-My Name is {set :NAME "templ"}. I am the TEMplate Processing Language.
-Sometimes, {$ :NAME} likes to speak in the third person.
+{v
+    {set :SIZE 10}
+}const double sine_lut[{get :SIZE}] =
+\{
+{for i {range {get :SIZE}} {::
+    {let :THETA}
+    {$ :THETA {mult
+        {$ i}
+        {div 360 {$ :SIZE}}
+    }}
+    {spit {'
+        "    "
+        {round 
+            {sin {rad {$ :THETA}}}
+            4
+        }
+        ,
+        {\t}
+        "// i = "
+        {get i}
+        ", theta = "
+        {$ :THETA}
+        " deg"
+        {lnbrk}
+    }}
+}}\};
 
-{$ :NAME} can do math:
-    1 + 1 = {+ 1 1}
-    1 + 2 = {+ 1 2}
-    2 + 3 = {+ 2 3}
-    3 + 5 = {+ 3 5}
-    etc...
-
-{$ :NAME} can operate on strings and lists:
-    {implode "a" {' b n n s}}
-    {str {cat {' a b c } {' d e f } }}
-
-{$ :NAME} can do conditional processing:
-    {if
-        {== {+ 2 2} 5}
-        "Oh No!"
-
-        {== {+ 2 2} 4}
-        "Phew!"
-
-        "How did I get here?"
-    }
-
-{$ :NAME} can loop (and do trig):
-{for :THETA {range 0 40 10} {
-    echo "    sin(" {$ :THETA} ") = " {sin {rad {$ :THETA}}} {\n}}
-}
-
-{$ :NAME} can even do list comprehensions and user defined functions:
-{v {set
-    :MY-FUNC
-    {lambda
-        {' :THETA }
-        {:
-            {let :RADS}
-            {$ :RADS {rad {$ :THETA}}}
-
-            {echo "Processing theta=" {$ :THETA} "..." {\n}}
-
-            %return value
-            {+ {cos {$ :RADS}} {sin {$ :RADS}} }
-        }
-    }
-}}{wrap "{" "}" {implode {glue "," {\n} "    "} {gen
-    :T
-    {range 40 80 10}
-    {join ":" {$ :T} {:MY-FUNC {$ :T}}}
-}}}
-
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.