<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bluish Coder: factor</title>
 <link href="http://bluishcoder.co.nz/tag/factor/atom.xml" rel="self"/>
 <link href="http://bluishcoder.co.nz/"/>
 <updated>2018-12-17T08:49:52+13:00</updated>
 <id>http://bluishcoder.co.nz/</id>
 <author>
   <name>Bluishcoder</name>
   <email>admin@bluishcoder.co.nz</email>
 </author>

 
 <entry>
   <title>Factor Web Framework Example</title>
   <link href="http://bluishcoder.co.nz/2008/10/11/factor-web-framework-example.html"/>
   <updated>2008-10-11T12:57:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2008/10/11/factor-web-framework-example</id>
   <content type="html">&lt;p&gt;The &lt;a href=&quot;http://www.factorcode.org/&quot;&gt;Factor&lt;/a&gt; web framework was recently redeveloped and my &lt;a href=&quot;http://bluishcoder.co.nz/2007/11/writing-web-applications-in-factor.html&quot;&gt;previous post&lt;/a&gt; on the topic is out of date. I&#39;m re-learning how to write Factor web applications as I work through a simple app I&#39;m building.&lt;/p&gt;

&lt;p&gt;At first I was overwhelmed by the large number of features in the new framework, but once I got a bit of an understanding I was pleasantly surprised how easy it was to put an application together.&lt;/p&gt;

&lt;p&gt;Starting the web server hasn&#39;t changed much. Start a web server running on port 8888 in a background thread:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USING: http.server threads ;
[ 8888 httpd ] in-thread
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A web application is written as a bunch of responders. Each responder handles a request from the client browser. An easy way of creating a responder is to create an &#39;action&#39; object. An &#39;action&#39; object has a number of slots, one of which is called &#39;display&#39;. Setting &#39;display&#39; to a quotation will cause that quotation to be called when the client browser makes a request. The quotation should return a &#39;response&#39; object. The &#39;response&#39; object defines what gets sent back to the client browser. The &#39;body&#39; slot of the &#39;response&#39; object can be a string which is returned to the browser as the contents of the request. So a basic responder is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;action&amp;gt; [
  &amp;lt;response&amp;gt;
    200 &amp;gt;&amp;gt;code
    &quot;text/plain&quot; &amp;gt;&amp;gt;content-type
    &quot;Hello World&quot; &amp;gt;&amp;gt;body
] &amp;gt;&amp;gt;display
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The web browser uses a global variable called &#39;main-responder&#39; as the first responder to try when a request is made. If we set &#39;main-responder&#39; to the value of our responder then it immediately becomes available to a web request. So our entire simple web application is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USING: http http.server threads furnace.actions ;
[ 8888 httpd ] in-thread
&amp;lt;action&amp;gt; [
  &amp;lt;response&amp;gt;
    200 &amp;gt;&amp;gt;code
    &quot;text/plain&quot; &amp;gt;&amp;gt;content-type
    &quot;Hello World&quot; &amp;gt;&amp;gt;body
] &amp;gt;&amp;gt;display main-responder set-global
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Visiting http://localhost:8888/ should display a page with &#39;Hello World&#39; as the text.&lt;/p&gt;

&lt;p&gt;A responder that works just from the root of the URL is not very practical. A dispatcher is an object that can acts as a responder that calls other responders based on part of the URL. Here&#39;s a dispatcher that calls one responder when /hello is requested, and another when /goodbye is requested.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USING: 
  furnace.actions 
  http 
  http.server 
  http.server.dispatchers 
  threads 
;
[ 8888 httpd ] in-thread

TUPLE: my-dispatcher &amp;lt; dispatcher ;

my-dispatcher new-dispatcher
  &amp;lt;action&amp;gt; [
    &amp;lt;response&amp;gt;
      200 &amp;gt;&amp;gt;code
      &quot;text/plain&quot; &amp;gt;&amp;gt;content-type
      &quot;Hello World&quot; &amp;gt;&amp;gt;body
  ] &amp;gt;&amp;gt;display &quot;hello&quot; add-responder 
  &amp;lt;action&amp;gt; [
    &amp;lt;response&amp;gt;
      200 &amp;gt;&amp;gt;code
      &quot;text/plain&quot; &amp;gt;&amp;gt;content-type
      &quot;Goodbye World&quot; &amp;gt;&amp;gt;body
  ] &amp;gt;&amp;gt;display &quot;goodbye&quot; add-responder 
main-responder set-global
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First I create a tuple called &#39;my-dispatcher&#39; that inherits from the base dispatcher class. The I create an instance of this using &#39;new-dispatcher&#39;, create my actions and add them under the paths &#39;hello&#39; and &#39;goodbye&#39;.&lt;/p&gt;

&lt;p&gt;Writing response objects gets a bit tedious. Factor provides two template systems that can be used to make writing web applications easier. One is &#39;fhtml&#39; which existed in the previous web framework incarnation. The other, chloe, is an XML based templating language, and is covered quite well &lt;a href=&quot;http://docs.factorcode.org/content/article-html.templates.chloe.html&quot;&gt;in the Factor documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To create an action that uses a Chloe template, create a &#39;page-action&#39; instead of an &#39;action&#39;. Set the &#39;template&#39; slot of the page-action to be a two element array containing the dispatcher class and the template filename (without the .xml extension):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USING: 
  furnace.actions 
  html.templates.chloe
  http 
  http.server 
  http.server.dispatchers 
  threads 
;
[ 8888 httpd ] in-thread

TUPLE: my-dispatcher &amp;lt; dispatcher ;

my-dispatcher new-dispatcher
  &amp;lt;page-action&amp;gt;
    { my-dispatcher &quot;main&quot; } &amp;gt;&amp;gt;template
  &quot;foo&quot; add-responder 
  main-responder set-global
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When this &#39;/foo&#39; is accessed on the webserver, this action will run. It will look for main.xml which needs to be a chloe template file. Here&#39;s a simple example template:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;?xml version=&#39;1.0&#39; ?&amp;gt;
&amp;lt;t:chloe xmlns:t=&quot;http://factorcode.org/chloe/1.0&quot;&amp;gt;
  &amp;lt;html&amp;gt;
    &amp;lt;body&amp;gt;
      &amp;lt;p&amp;gt;Hello!&amp;lt;/p&amp;gt;
    &amp;lt;/body&amp;gt;
  &amp;lt;/html&amp;gt;
&amp;lt;/t:chloe&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The chloe help goes into a lot of detail about how to use the template to substitute variable values, etc. I&#39;ll cover that in a later post.&lt;/p&gt;

&lt;p&gt;The web framework has a lot of other features. Sessions, Authentication, Database, Validation, etc. Hopefully this will help you get started. All the example above can be entered and run (barring typo&#39;s and unescaped html entities) in a Factor listener. &lt;a href=&quot;http://www.factorcode.org/binaries.fhtml&quot;&gt;Download it and try it!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Update: There is a wiki page on the concatenative wiki with &lt;a href=&quot;http://concatenative.org/wiki/view/Factor/Furnace&quot;&gt; more information about the web framework&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Revisiting the Linear recursion combinator in Factor four years on</title>
   <link href="http://bluishcoder.co.nz/2008/10/11/revisiting-linear-recursion-combinator.html"/>
   <updated>2008-10-11T04:09:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2008/10/11/revisiting-linear-recursion-combinator</id>
   <content type="html">&lt;p&gt;The Joy programming language has a linrec combinator for performing linear recursion. Some time ago I &lt;a href=&quot;http://tech.groups.yahoo.com/group/concatenative/message/2121&quot;&gt;took a stab at implementing linrec&lt;/a&gt; in Factor. Slava also &lt;a href=&quot;http://tech.groups.yahoo.com/group/concatenative/message/2124&quot;&gt;posted his version&lt;/a&gt; that was originally a part of Factor.&lt;/p&gt;

&lt;p&gt;As can be seen from Slava&#39;s version, juggling the stack when four quotations are involved can be problematic. I thought I&#39;d revisit linrec using Factor&#39;s locals library and see how it compares. This is my attempt at an implementation using locals:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:: linrec 
 ( if-quot:    ( -- ? ) 
   then-quot:  ( -- ) 
   else1-quot: ( -- ) 
   else2-quot: ( -- ) 
   -- )
  if-quot call [ 
    then-quot call 
  ] [ 
    else1-quot call 
    if-quot then-quot else1-quot else2-quot linrec
    else2-quot call
  ] if ; inline recursive
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is pretty readable compared to the solutions in the prior posts. Because linrec is a combinator it needs to be declared &#39;inline&#39;. It recursively calls itself so needs to be declared &#39;inline recursive&#39; to enable calls of linrec to be compiled. More details about this can be found in the &lt;a href=&quot;http://docs.factorcode.org/content/article-inference-recursive-combinators.html&quot;&gt;Factor documentation&lt;/a&gt;. Usage of the linrec word looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;5 [ dup 1 = ] [ ] [ dup 1- ] [ * ] linrec .
 =&amp;gt; 120

[ 5 [ dup 1 = ] [ ] [ dup 1- ] [ * ] linrec ] infer.
 =&amp;gt; ( -- object )

{ 1 2 3 } [ dup rest empty? ] [ first ] [ rest ] [ ] linrec
  =&amp;gt; 3

[ { 1 2 3 } [ dup rest empty? ] [ first ] [ rest ] [ ] linrec ] infer.
  =&amp;gt; ( -- object )

[ 1000 [ dup 1 = ] [ ] [ dup 1- ] [ * ] linrec drop ] time
 =&amp;gt; 22ms

: fac ( n -- n ) dup 1 = [ dup 1- fac * ] unless ;
[ 1000 fac drop ] time
 =&amp;gt; 19ms
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&#39;s nice that the &#39;fac&#39; word and the linrec definition seem to compile down to the same code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[ [ dup 1 = ] [ ] [ dup 1- ] [ * ] linrec ] optimized.
  =&amp;gt; [
      \ ( gensym ) [
          dup 1 dupd eq?
          [ drop t ]
          [ dup 1 swap tag eq? [ 1 bignum= ] [ drop f ] if ] if
          [ ] [ dup 1 - ( gensym ) * ] if
      ] label
  ]

\ fac optimized.
  =&amp;gt; [
      dup 1 dupd eq?
      [ drop t ]
      [ dup 1 swap tag eq? [ 1 bignum= ] [ drop f ] if ] if
      [ ] [ dup 1 - fac * ] if
  ]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One confusing aspect of this linrec definition is having to declare the stack effects of the quotations passed to linrec. Depending on the linear recursion task the stack effects may be different. Compare factorial vs finding the last element of a sequence:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{ [ dup 1 = ] 
  [ ] 
  [ dup 1- ] 
  [ * ] 
} [ infer. ] each
  =&amp;gt; ( object -- object object )
     ( -- )
     ( object -- object object )
     ( object object -- object)

{ 
   [ dup rest empty? ] 
   [ first ] 
   [ rest ] 
   [ ] 
} [ infer. ] each
  =&amp;gt; ( object -- object object )
     ( object -- object )
     ( object -- object )
 ( -- )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Both usages of linrec still infer and compile so I guess the fact that the stack effects balance out allows this. I&#39;m not sure whether it&#39;s just a side effect of implementation or intended.&lt;/p&gt;

&lt;p&gt;Working on this example I can say that, for me, Factor programming has become easier compared to four years ago.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Parsing JavaScript with Factor</title>
   <link href="http://bluishcoder.co.nz/2008/06/22/parsing-javascript-with-factor.html"/>
   <updated>2008-06-22T00:07:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2008/06/22/parsing-javascript-with-factor</id>
   <content type="html">&lt;p&gt;I&#39;ve made some more changes to the &lt;a href=&quot;http://bluishcoder.co.nz/2008/04/factor-parsing-dsl.html&quot;&gt;Parsing Expression Grammar library in Factor&lt;/a&gt;. Most of the changes were inspired by things that &lt;a href=&quot;http://www.cs.ucla.edu/~awarth/ometa/&quot;&gt;OMeta&lt;/a&gt; can do. The grammar I used for testing is an &lt;a href=&quot;http://jarrett.cs.ucla.edu/ometa-js/#JavaScript_Compiler&quot;&gt;OMeta-JS grammar&lt;/a&gt; for a subset of JavaScript. First the list of changes.&lt;/p&gt;

&lt;p&gt;Actions in the EBNF syntax receive an AST (Abstract Syntax Tree) on the stack. The action quotation is expected to have stack effect ( ast -- ast ). It modifies the AST and leaves the new version on the stack. This led to code that looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;expr = lhs &#39;+&#39; rhs =&amp;gt; [[ first3 nip + ]]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Nothing wrong with that, but a later change added variables to the EBNF grammar to make it more readable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;expr = lhs:x &#39;+&#39; rhs:y =&amp;gt; [[ drop x y + ]]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Code that uses variables a lot end up with a lot of &#39;drop&#39; usage as the first word. I made a change recommended by &lt;a href=&quot;http://factor-language.blogspot.com/&quot;&gt;Slava&lt;/a&gt; to have the action add the drop automatically depending on the stack effect of the action. So now this code works:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;expr = lhs:x &#39;+&#39; rhs:y =&amp;gt; [[ x y + ]]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So now if you use variables in a rule, the stack effect of the action should be ( -- ast ). If you don&#39;t, it should be ( ast -- ast ).&lt;/p&gt;

&lt;p&gt;I added a way for one EBNF parser to call rules defined in another. This allows creating grammars which are hybrids of existing parsers. Or just to reuse common things like string handling expressions. These calls are called &#39;foreign&#39; calls and appear on the right hand side of a rule in angle brackets. Here is a parser that parses strings between double quotation marks:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EBNF: parse-string
StringBody = (!(&#39;&quot;&#39;) .)* 
String= &#39;&quot;&#39; StringBody:b &#39;&quot;&#39; =&amp;gt; [[ b &amp;gt;string ]]
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To call the &#39;String&#39; rule from another parser:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EBNF: parse-two-strings
TwoStrings = &amp;lt;foreign parse-string String&amp;gt; &amp;lt;foreign parse-string String&amp;gt;
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &amp;lt;foreign&gt; call in this example takes two arguments. The first is the name of an existing EBNF: defined parser. The second is the rule in that parser to invoke. It can also be used like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EBNF: parse-two-strings
TwoString = &amp;lt;foreign parse-string&amp;gt; &amp;lt;foreign parse-string&amp;gt;
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If the first argument is the name of an EBNF: defined parser and no second argument is given, then the main rule of that parser is used. The main rule is the last rule in the parser body. A final way foreign can be used:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;: a-token ( -- parser ) &quot;a&quot; token ;

EBNF: parse-abc
abc = &amp;lt;foreign a-token&amp;gt; &#39;b&#39; &#39;c&#39;
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If the first argument given to foreign is not an EBNF: defined parser, it is assumed that it has stack effect ( -- parser ) and it will be called to return the parser to be used.&lt;/p&gt;

&lt;p&gt;It is now possible to override the tokenizer in an EBNF defined parser. Usually the sequence to be parsed is an array of characters or a string. Terminals in a rule match successive characters in the array or string. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EBNF: foo
rule = &quot;++&quot; &quot;--&quot;
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This parser when run with the string &quot;++--&quot; or the array { CHAR: + CHAR: + CHAR: - CHAR: - } will succeed with an AST of { &quot;++&quot; --&quot; }. If you want to add whitespace handling to the grammar you need to put it between the terminals:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EBNF: foo
space = (&quot; &quot; | &quot;\r&quot; | &quot;\t&quot; | &quot;\n&quot;)
spaces = space* =&amp;gt; [[ drop ignore ]]
rule = spaces &quot;++&quot; spaces &quot;--&quot; spaces
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In a large grammar this gets tedious and makes the grammar hard to read. Instead you can write a rule to split the input sequence into tokens, and have the grammar operate on these tokens. This is how the previous example might look:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EBNF: foo
space = (&quot; &quot; | &quot;\r&quot; | &quot;\t&quot; | &quot;\n&quot;)
spaces = space* =&amp;gt; [[ drop ignore ]]
tokenizer = spaces ( &quot;++&quot; | &quot;--&quot; )
rule = &quot;++&quot; &quot;--&quot;
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&#39;tokenizer&#39; is the name of a built in rule. Once defined it is called to retrieve the next complete token from the input sequence. So the first part of &#39;rule&#39; is to try and match &quot;++&quot;. It calls the tokenizer to get the next complete token. This ignores spaces until it finds a &quot;++&quot; or &quot;--&quot;. It is as if the input sequence for the parser was actually { &quot;++&quot; &quot;--&quot; } instead of the string &quot;++--&quot;. With the new tokenizer &quot;....&quot; sequences in the grammar are matched for equality against the token, rather than a string comparison against successive items in the sequence. This can be used to match an AST from a tokenizer:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;TUPLE: ast-number value ;
TUPLE: ast-string value ;

EBNF: foo-tokenizer
space = (&quot; &quot; | &quot;\r&quot; | &quot;\t&quot; | &quot;\n&quot;)
spaces = space* =&amp;gt; [[ drop ignore ]]

number = [0-9]* =&amp;gt; [[ &amp;gt;string string&amp;gt;number ast-number boa ]]
string = &amp;lt;foreign string-parser String&amp;gt; =&amp;gt; [[ ast-string boa ]]
operator = (&quot;+&quot; | &quot;-&quot;)

token = spaces ( number | string | operator )
tokens = tokenizer*
;EBNF

ENBF: foo
tokenizer = &amp;lt;foreign foo-tokenizer token&amp;gt;

number = . ?[ ast-number? ]? =&amp;gt; [[ value&amp;gt;&amp;gt; ]]
string = . ?[ ast-string? ]? =&amp;gt; [[ value&amp;gt;&amp;gt; ]]

rule = string:a number:b &quot;+&quot; number:c =&amp;gt; [[ a b c + 2array ]]
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example I split the tokenizer into a separate parser and use &#39;foreign&#39; to call it from the main one. This allows testing of the tokenizer separately:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;123 456 +&quot; foo-tokenizer ast&amp;gt;&amp;gt; .
=&amp;gt; { T{ ast-number f 123 } T{ ast-number f 456 } &quot;+&quot; }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &#39;.&#39; EBNF production means match a single object in the source sequence. Usually this is a character. With the replacement tokenizer it is either a number object, a string object or a string containing the operator. Using a tokenizer in language grammars makes it easier to deal with whitespace. Defining tokenizers in this way has the advantage of the tokenizer and parser working in one pass. There is no tokenization occurring over the whole string followed by the parse of that result. It tokenizes as it needs too. You can even switch tokenizers multiple times during a grammar. Rules use the tokenizer that was defined lexically before the rule. This is usefull in the JavaScript grammar:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EBNF: javascript
tokenizer         = default 
nl                = &quot;\r&quot; &quot;\n&quot; | &quot;\n&quot;

tokenizer         = &amp;lt;foreign tokenize-javascript Tok&amp;gt;
...
End                = !(.)
Name               = . ?[ ast-name?   ]?   =&amp;gt; [[ value&amp;gt;&amp;gt; ]] 
Number             = . ?[ ast-number? ]?   =&amp;gt; [[ value&amp;gt;&amp;gt; ]]
String             = . ?[ ast-string? ]?   =&amp;gt; [[ value&amp;gt;&amp;gt; ]]
RegExp             = . ?[ ast-regexp? ]?   =&amp;gt; [[ value&amp;gt;&amp;gt; ]]
SpacesNoNl         = (!(nl) Space)* =&amp;gt; [[ ignore ]]
Sc                 = SpacesNoNl (nl | &amp;amp;(&quot;}&quot;) | End)| &quot;;&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here the rule &#39;nl&#39; is defined using the default tokenizer of sequential characters (&#39;default&#39; has the special meaning of the built in tokenizer). This is followed by using the JavaScript tokenizer for the remaining rules. This tokenizer strips out whitespace and newlines. Some rules in the grammar require checking for a newline. In particular the automatic semicolon insertion rule (managed by the &#39;Sc&#39; rule here). If there is a newline, the semicolon can be optional in places.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;do&quot; Stmt:s &quot;while&quot; &quot;(&quot; Expr:c &quot;)&quot; Sc    =&amp;gt; [[ s c ast-do-while boa ]]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Even though the JavaScript tokenizer has removed the newlines, the &#39;nl&#39; rule can be used to detect them since it is using the default tokenizer. This allows grammars to mix and match the tokenizer as required to make them more readable.&lt;/p&gt;

&lt;p&gt;The JavaScript grammar is in the peg.javascript.parser vocabulary. The tokenizer is in peg.javascript.tokenizer. You can run it using the &#39;parse-javascript&#39; word in peg.javascript:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USE: peg.javascript
&quot;var a=&#39;hello&#39;; alert(a);&quot; parse-javascript ast&amp;gt;&amp;gt; pprint
T{ ast-begin f
  V{
      T{ ast-var f &quot;a&quot; T{ ast-string f &quot;hello&quot; } }
      T{ ast-call f
        T{ ast-get f &quot;alert&quot; } V{ T{ ast-get f &quot;a&quot; } } }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The grammar is only for a subset of a JavaScript like language. It doesn&#39;t handle all of JavaScript yet. I&#39;ll continue to work on it as a testbed to improve EBNF. One thing I need to add next is decent error handling of a failed parse.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Collection of Factor Articles in PDF</title>
   <link href="http://bluishcoder.co.nz/2008/04/04/collection-of-factor-articles-in-pdf.html"/>
   <updated>2008-04-04T12:45:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2008/04/04/collection-of-factor-articles-in-pdf</id>
   <content type="html">&lt;p&gt;Factor has experienced some rapid change in the libraries and language over the past few years. I&#39;ve written a few blog posts about Factor in the past and many of them suffer from bitrot due to this, making it hard to try out the examples in the latest Factor versions.&lt;/p&gt;

&lt;p&gt;I&#39;ve collected some of these articles, put them in a PDF document, and am slowly working through them so they are up to date with recent Factor versions. The document is split into sections for the articles that should work, and those that don&#39;t. Even the out of date articles make intersting reading for examples on how to use Factor.&lt;/p&gt;

&lt;p&gt;There&#39;s nothing new in the document since it&#39;s a collection of things available from my blog posts, but having the central document makes it easier to update, print out, and read.&lt;/p&gt;

&lt;p&gt;You can download it from &lt;a href=&quot;http://bluishcoder.co.nz/factor-articles.pdf&quot;&gt;factor-articles.pdf&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The git repository with the LaTeX source is hosted on github at &lt;a href=&quot;http://github.com/doublec/factor-articles&quot;&gt;http://github.com/doublec/factor-articles&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Comments and suggestions welcome.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Factor Parsing DSL</title>
   <link href="http://bluishcoder.co.nz/2008/04/04/factor-parsing-dsl.html"/>
   <updated>2008-04-04T00:24:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2008/04/04/factor-parsing-dsl</id>
   <content type="html">&lt;p&gt;I&#39;ve been doing some experimenting with the &lt;a href=&quot;http://bluishcoder.co.nz/2007/11/embedded-grammars-in-factor.html&quot;&gt;emedded grammar&lt;/a&gt; code I wrote for Factor, trying to make it easier to use and a bit more useful for real world projects.&lt;/p&gt;

&lt;p&gt;My inspiration for the changes has been seeing the kinds of things &lt;a href=&quot;http://www.cs.ucla.edu/~awarth/ometa/&quot;&gt;OMeta&lt;/a&gt; can do and the examples in the &lt;a href=&quot;http://vpri.org/html/writings.htm&quot;&gt;Steps towards the reinvention of programming&lt;/a&gt; from the &lt;a href=&quot;http://vpri.org/&quot;&gt;Viewpoints Research Institute&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The original parsing expression grammar code (in the vocab &#39;peg&#39;) produced a data structure composed of Factor tuples that was interpreted at runtime via a call to the word &#39;parse&#39;. It still has the data structure of tuples but now it can be compiled into Factor quotations, which are then compiled to native machine code via the Factor compiler. The &#39;parse&#39; word calls &#39;compile&#39; on the datastructure and calls it.&lt;/p&gt;

&lt;p&gt;I created a parsing word that allows you to embed the peg expression directly in code, have it compiled to a quotation at parse time, and then called at runtime. Usage looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;1+2&quot; [EBNF expr=[0-9] &#39;+&#39; [0-9] EBNF] call
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The older peg code had an &lt;code&gt;&amp;lt;EBNF ... EBNF&amp;gt;&lt;/code&gt; embedded language and each rule in the language was translated to a Factor word. That&#39;s now changed to an EBNF: definition. An example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EBNF: expr 
digit    = [0-9]            =&amp;gt; [[ digit&amp;gt; ]]
number   = (digit)+         =&amp;gt; [[ 10 digits&amp;gt;integer ]]
value    =   number 
           | (&quot;(&quot; exp &quot;)&quot;)  =&amp;gt; [[ second ]]

fac      =   fac:x &quot;*&quot; value:y  =&amp;gt; [[ drop x y * ]]
           | fac:x &quot;/&quot; value:y  =&amp;gt; [[ drop x y / ]]
           | number

exp      =   exp:x &quot;+&quot; fac:y    =&amp;gt; [[ drop x y + ]]
           | exp:x &quot;-&quot; fac:y    =&amp;gt; [[ drop x y - ]]
           | fac
;EBNF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates a word, &#39;expr&#39;, that runs the last rule in the embedded language (the &#39;exp&#39; rule in this case) on the string passed to it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;1+2*3+4&quot; expr parse-result-ast .
 =&amp;gt; 11
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you&#39;ve used peg.ebnf before you&#39;ll see some new features in this code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can do character ranges using code like &lt;code&gt;[a-zA-Z]&lt;/code&gt; to match upper and lowercase characters, etc.&lt;/li&gt;
&lt;li&gt;Factor quotations can be embedded to process the results of choices. Anything between the &lt;code&gt;[[ ... ]]&lt;/code&gt; will be run when that choice succeeds and the result put in the abstract syntax tree for that rule. The default AST produced by the rule is on the stack of the quotation. The example above drops this in some cases, and transforms it in others.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rule elements can have variable names suffixed to it and these can be referenced in the action quotations. This is implemented using &lt;a href=&quot;http://factor-language.blogspot.com/2008/03/some-improvements-to-locals-let-and.html&quot;&gt;locals&lt;/a&gt;. This can be seen in EBNF code like this:&lt;/p&gt;

&lt;p&gt;  exp:x &quot;+&quot; exp:y =&gt; [[ drop x y + ]]&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Usually that rule produces an AST consisting of a 3 element sequence, each element being the AST produced by the rules elements. The action quotation is transformed into:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[let* | x [ 0 over nth ] 
        y [ 2 over nth ] |
  drop x y + 
] with-locals
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is efficient and makes the grammar easier to read.
* Another major new feature is grammars now handle direct and indirect left recursion. I implemented this from the VPRI paper &lt;a href=&quot;http://vpri.org/html/writings.htm&quot;&gt;Packrat Parsers Can Support Left Recursion&lt;/a&gt;. It makes converting existing grammars to peg grammars much easier.
* Semantic actions have been added. These are like normal &lt;code&gt;[[ ... ]]&lt;/code&gt; actions except they have stack effect ( ast -- bool ). Given an abstract syntax tree from the preceding element, it should return a boolean indicating whether the parse succeeded or not. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;odd= [0-9] ?[ digit&amp;gt; odd? ]?
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;Some of the syntax has changed. Previously { ... } was used for repeating zero or more times and &lt;code&gt;[ ... ]&lt;/code&gt; was for optional. Now I use &lt;code&gt;(...)*&lt;/code&gt;, &lt;code&gt;(...)+&lt;/code&gt;, &lt;code&gt;(...)?&lt;/code&gt;, for zero or more, one or more, and optional respectively. The dot (.) is used to match anything at that point. Terminals can be enclosed in single or double quotations.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is a &#39;rule&#39; word that can be used to get a single rule from a compiled grammar:&lt;/p&gt;

&lt;p&gt;  EBNF: foo
  number=([0-9])+
  expr  = number &#39;+&#39; number
  ;EXPR
  &quot;1+2&quot; foo parse-result-ast =&gt; { &quot;1&quot; &quot;+&quot; &quot;2&quot; }
  &quot;1+2&quot; &quot;number&quot; \ foo rule parse parse-result-ast =&gt; &quot;1&quot;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Notice the &#39;rule&#39; word returns the parser object rather than the compiled quotation. This is useful for testing and further combining with other parsers.&lt;/p&gt;

&lt;p&gt;These changes are in the main Factor repository. There is the peg.pl0 and peg.expr vocabs as examples. The peg.ebnf code is in an experimental state and is likely to change a lot until I&#39;m satisfied with it so be aware that it might not be wise to use it in stable code unless you&#39;re happy with tracking the changes. I welcome feedback and ideas though.&lt;/p&gt;

&lt;p&gt;One feature I&#39;m currently working on but haven&#39;t put in the main repository yet is the ability to use the embedded grammar DSL to operate over Factor arrays and tuples. This allows writing an embedded grammar to produced an AST, and another embedded grammar to transform that AST into something else. Here&#39;s what code to transform an AST currently looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;TUPLE: plus lhs rhs ;

EBNF: adder
num   = . ?[ number? ]? 
plus  = . ?[ plus? ]? expr:a expr:b =&amp;gt; [[ drop a b + ]]
expr  =  plus | num
;EBNF

T{ plus f 1 T{ plus f 2 3 } } adder parse-result-ast .

=&amp;gt; 6
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This uses features I&#39;ve already discussed, like semantic actions, to detect the type of the object. The difference is that the parser produced by EBNF: operates not on strings, but on an abstract sequence type that is implemented for strings, sequence, and tuples. I&#39;m still playing around with ideas for this but I think it&#39;ll be a useful way to reuse grammars and string them together to perform tasks.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Embedded Grammars in Factor</title>
   <link href="http://bluishcoder.co.nz/2007/11/28/embedded-grammars-in-factor.html"/>
   <updated>2007-11-28T23:11:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2007/11/28/embedded-grammars-in-factor</id>
   <content type="html">&lt;p&gt;I&#39;m working on a new parser combinator library for Factor based on &lt;a href=&quot;http://en.wikipedia.org/wiki/Parsing_expression_grammar&quot;&gt;Parsing Expression Grammars&lt;/a&gt; and &lt;a href=&quot;http://pdos.csail.mit.edu/~baford/packrat/&quot;&gt;Packrat parsers&lt;/a&gt;. This is based on what I learnt from writing a &lt;a href=&quot;http://bluishcoder.co.nz/2007/10/javascript-packrat-parser.html&quot;&gt;packrat parser in Javascript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It&#39;s progressing quite well and already fixes some problems in the &lt;a href=&quot;http://bluishcoder.co.nz/2006/10/factor-parser-combinator-example.html&quot;&gt;existing parser combinator library&lt;/a&gt; I wrote. The main issue with that one is it&#39;s not tail recursive and some combinations of parsers can run out of call stack.&lt;/p&gt;

&lt;p&gt;The new library is in the &#39;&lt;a href=&quot;http://bluishcoder.co.nz/responder/help/show-vocab?vocab=peg&quot;&gt;peg&lt;/a&gt;&#39; vocabulary. I haven&#39;t yet implemented the packrat side of things though so it is slow on large grammars and inputs.&lt;/p&gt;

&lt;p&gt;I&#39;ve also done a proof of concept of something I&#39;ve been meaning to do for awhile. That is writing a parser for &lt;a href=&quot;http://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form&quot;&gt;EBNF&lt;/a&gt; (or similar) that produces Factor code in the form of parser combinators to implement the described grammar. The code for this is in the &#39;&lt;a href=&quot;http://bluishcoder.co.nz/responder/help/show-vocab?vocab=peg.ebnf&quot;&gt;peg.ebnf&lt;/a&gt;&#39; vocabulary. It allows you to embed an EBNF-like language and have Factor words generated for each rule:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;EBNF
digit  = &#39;1&#39; | &#39;2&#39; | &#39;3&#39; | &#39;4&#39; .
number = digit { digit } .
expr   = number {(&#39;+&#39; | &#39;-&#39; ) number } .
EBNF&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This example would create three Factor words. &#39;digit&#39;, &#39;number&#39; and &#39;expr&#39;. These words return parsers that can be used as normal:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;123&quot; number parse
&quot;1&quot; digit parse
&quot;1+243+342&quot; expr parse
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The EBNF accepted allows for choice, zero or more repetition, optional (exactly 0 or 1), and grouping. The generated AST is pretty ugly so by default it works best as a syntax checker. You can modify the generated AST with action productions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;EBNF
digit  = &#39;1&#39; | &#39;2&#39; | &#39;3&#39; | &#39;4&#39; =&amp;gt; convert-to-digit .
number = digit { digit }       =&amp;gt; convert-to-number .
expr   = number &#39;+&#39; number     =&amp;gt; convert-to-expr .
EBNF&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;An action is a factor word after the &#39;=&gt;&#39;. The word receives the AST produced from the rule on the stack and it can replace that with a new value that will be used in the AST. So &#39;convert-to-expr&#39; above might produce a tuple holding the expression values (by default, a sequence of terms in the rule are stored in a vector):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;TUPLE: ast-expr lhs operator rhs ;
C: &amp;lt;ast-expr&amp;gt; ast-expr
: convert-to-expr ( old -- new )
  first3 &amp;lt;ast-expr&amp;gt; ;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The generated code is currently pretty ugly, mainly due to it being a quick proof of concept. I&#39;ll try doing a few grammars and tidy it up, changing the interface if needed, as I go along.&lt;/p&gt;

&lt;p&gt;As an experiment I did a grammar for the &lt;a href=&quot;http://en.wikipedia.org/wiki/PL/0&quot;&gt;PL/0 programming language&lt;/a&gt;. It&#39;s in &#39;&lt;a href=&quot;http://bluishcoder.co.nz/responder/help/show-vocab?vocab=peg.pl0&quot;&gt;peg.pl0&lt;/a&gt;&#39;. The grammar from the wikipedia article is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;program = block &quot;.&quot; .

block = [ &quot;const&quot; ident &quot;=&quot; number {&quot;,&quot; ident &quot;=&quot; number} &quot;;&quot;]
        [ &quot;var&quot; ident {&quot;,&quot; ident} &quot;;&quot;]
        { &quot;procedure&quot; ident &quot;;&quot; block &quot;;&quot; } statement .

statement = [ ident &quot;:=&quot; expression | &quot;call&quot; ident |
            &quot;begin&quot; statement {&quot;;&quot; statement } &quot;end&quot; |
            &quot;if&quot; condition &quot;then&quot; statement |
            &quot;while&quot; condition &quot;do&quot; statement ].

condition = &quot;odd&quot; expression |
            expression (&quot;=&quot;|&quot;#&quot;|&quot;&amp;lt;&quot;|&quot;&amp;lt;=&quot;|&quot;&amp;gt;&quot;|&quot;&amp;gt;=&quot;) expression .

expression = [ &quot;+&quot;|&quot;-&quot;] term { (&quot;+&quot;|&quot;-&quot;) term}.

term = factor {(&quot;*&quot;|&quot;/&quot;) factor}.

factor = ident | number | &quot;(&quot; expression &quot;)&quot;.&amp;lt;/pre&amp;gt;The Factor grammar is very similar:&amp;lt;pre&amp;gt;: ident ( -- parser )
  CHAR: a CHAR: z range 
  CHAR: A CHAR: Z range 2array choice repeat1 
  [ &amp;gt;string ] action ;

: number ( -- parser )
  CHAR: 0 CHAR: 9 range repeat1 [ string&amp;gt;number ] action ;

&amp;lt;EBNF
program = block &#39;.&#39; .
block = [ &#39;const&#39; ident &#39;=&#39; number { &#39;,&#39; ident &#39;=&#39; number } &#39;;&#39; ]
        [ &#39;var&#39; ident { &#39;,&#39; ident } &#39;;&#39; ]
        { &#39;procedure&#39; ident &#39;;&#39; [ block &#39;;&#39; ] } statement .
statement = [ ident &#39;:=&#39; expression | &#39;call&#39; ident |
              &#39;begin&#39; statement {&#39;;&#39; statement } &#39;end&#39; |
              &#39;if&#39; condition &#39;then&#39; statement |
              &#39;while&#39; condition &#39;do&#39; statement ] .
condition = &#39;odd&#39; expression |
            expression (&#39;=&#39; | &#39;#&#39; | &#39;&amp;lt;=&#39; | &#39;&amp;lt;&#39; | &#39;&amp;gt;=&#39; | &#39;&amp;gt;&#39;) expression .
expression = [&#39;+&#39; | &#39;-&#39;] term {(&#39;+&#39; | &#39;-&#39;) term } .
term = factor {(&#39;*&#39; | &#39;/&#39;) factor } .
factor = ident | number | &#39;(&#39; expression &#39;)&#39;
EBNF&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This grammar as defined works and can parse PL/0 programs. I&#39;ll extend this as I improve the EBNF routines, adding actions, etc to generated a decent AST.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Writing Web Applications in Factor</title>
   <link href="http://bluishcoder.co.nz/2007/11/21/writing-web-applications-in-factor.html"/>
   <updated>2007-11-21T16:33:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2007/11/21/writing-web-applications-in-factor</id>
   <content type="html">&lt;p&gt;There was a request on &lt;a href=&quot;http://ircbrowse.com/channel/concatenative&quot;&gt;#concatenative&lt;/a&gt; for information on how to write web applications in &lt;a href=&quot;http://www.factorcode.org/&quot;&gt;Factor&lt;/a&gt;. I went through a few steps on how to get started. I&#39;m repeating it here for others that might be interested.&lt;/p&gt;

&lt;p&gt;There are a number of different ways of writing web applications in Factor but for this approach I&#39;m using the furnace framework.&lt;/p&gt;

&lt;p&gt;The first step is to start the web server. This lives in the vocab &#39;http.server&#39;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USE: http.server
[ 8888 httpd ] in-thread
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will start an instance of the server on port 8888 in another thread, to allow us to continue to enter commands in the listener.&lt;/p&gt;

&lt;p&gt;By default web applications are accessed on the URL path /responder/name, where &#39;name&#39; is the name of the web application.&lt;/p&gt;

&lt;p&gt;Accessing the web application path runs an &#39;action&#39;. An action produces HTML output which gets sent back to the client browser. A web application has a default &#39;action&#39; that gets run (the equivalent of an index.html), and can have other actions that are specified in the URL. Some examples:&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;http://localhost:8888/responder/foo&lt;/dt&gt;
&lt;dd&gt;Runs the default action for the &#39;foo&#39; web application&lt;/dd&gt;
&lt;dt&gt;http://localhost:8888/responder/foo/doit&lt;/dt&gt;
&lt;dd&gt;Runs the &#39;doit&#39; action&lt;/dd&gt;
&lt;dt&gt;http://localhost:8888/responder/foo/hello?name=chris&lt;/dt&gt;
&lt;dd&gt;Runs the &#39;hello&#39; action giving the argument &#39;name&#39; with the value &#39;chris&#39;&lt;/dd&gt;
&lt;/dl&gt;


&lt;p&gt;The syntax for furnace URL&#39;s is therefore http://servername:port/responder/{webappname}/{action}?{arguments}&lt;/p&gt;

&lt;p&gt;Furnace web application must exist under the &#39;webapps&#39; vocabulary. So accessing /responder/foo will look for furnace details in the vocabulary &#39;webapps.foo&#39;.&lt;/p&gt;

&lt;p&gt;A furnace web application is registered with the http server using the &#39;web-app&#39; word. It takes three arguments on the stack:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;\ web-app effect-in . 
=&amp;gt; { &quot;name&quot; &quot;default&quot; &quot;path&quot; }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &#39;name&#39; is the vocabulary name of the web application with out the &#39;webapps.&#39; prefix. &#39;default&#39; is the name of the action that gets run when the web application URL is accessed. &#39;path&#39; is the location of any template files the web application uses.&lt;/p&gt;

&lt;p&gt;An action is a word that outputs data to be sent to the browser. It can be as simple as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;: doit ( -- ) serving-text &quot;I am here&quot; print ;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The word must be registered as an action:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;\ doit { } define-action
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now accessing the URL for the web application with &#39;doit&#39; at the end of the path will result in &#39;I am here&#39; being sent to the browser. Note the &#39;serving-text&#39; call. That outputs the headers for the mime type and the standard HTTP response. There is also a &#39;serving-html&#39;, or you could write the headers manually.&lt;/p&gt;

&lt;p&gt;Actions can take arguments. These are placed on the stack for the word that is called:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;: hello ( name -- ) serving-text &quot;Hello &quot; write print ;
\ hello { { &quot;hello&quot; } } define-action
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So the complete code for the simplest of web applications is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USE: http.server
[ 8888 httpd ] in-thread
IN: webapps.test
USE: furnace

: index serving-text &quot;We&#39;re alive!&quot; print ;
\ index { } define-action 

: hello ( name -- ) serving-text &quot;Hello &quot; write print ;
\ hello { { &quot;name&quot; } } define-action

&quot;test&quot; &quot;index&quot; &quot;.&quot; web-app
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Accessing http://localhost:8888/responder/test will run the &#39;index&#39; action. This is what we passed as the &#39;default&#39; parameter on the stack to the &#39;web-app&#39; word. Accessing http://localhost:8888/responder/test/hello?name=chris will run the &#39;hello&#39; action.&lt;/p&gt;

&lt;p&gt;There is also the facility to have template files, very much like JSP. The &#39;path&#39; parameter to &#39;web-app&#39; defines the location of these. Inside your action word you can call &#39;render-template&#39; to run the template and have it sent to the browser:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;: runme ( -- ) f &quot;page&quot; &quot;Title&quot; render-template ;
\ runme { } define-action
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will load the &#39;page.furnace&#39; file in the path given to &#39;web-app&#39;. It should contain standard HTML with embedded Factor code inside &lt;code&gt;&amp;lt;%&lt;/code&gt; and &lt;code&gt;%&amp;gt;&lt;/code&gt; tags. It will be run and sent to the client. The &#39;f&#39; passed in this example can be an instance of a tuple (an object) and the template can access the slots of that instance to display data, etc.&lt;/p&gt;

&lt;p&gt;There is quite a bit more that can be done. There is a continuation based workflow system, validators for actions, etc. There is also much more that needs to be done. handling sessions, cookies, etc. Hopefully this post gives a quick introduction and allows you to get started.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>How to publish a Git repository</title>
   <link href="http://bluishcoder.co.nz/2007/09/22/how-to-publish-git-repository.html"/>
   <updated>2007-09-22T22:44:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2007/09/22/how-to-publish-git-repository</id>
   <content type="html">&lt;p&gt;This post is aimed mainly at &lt;a href=&quot;http://www.factorcode.org/&quot;&gt;Factor developers&lt;/a&gt; who need to make their repository accessible to Slava to retrieve patches. Factor has recently moved to using &lt;a href=&quot;http://git.or.cz/&quot;&gt;git&lt;/a&gt; as the version control system.&lt;/p&gt;

&lt;p&gt;To set up a repository on a server you should clone the existing Factor repository using the &#39;--bare&#39; option:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone --bare http://www.factorcode.org/git/factor.git factor.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A bare repository is one without a checked out working copy of the code. It only contains the git database. As a general rule you should never push into a repository that contains changes in the working copy. To ensure this doesn&#39;t happen, we&#39;re making the server repository a bare repository - it has no working copy.&lt;/p&gt;

&lt;p&gt;Copy the &#39;factor.git&#39; directory onto your server. I put it in &#39;/git/factor.git&#39;. Now if you have changes on your local machine that you want to push to your repository you can use something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git push yourname@yourserver.com:/git/factor.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you want to push changes from a specific branch in your local repository:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git push yourname@yourserver.com:/git/factor.git mybranch:master
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To publish the remote repository you have two options. You can publish via the HTTP protocol, or via the git protocol. The first is slower but usable by people behind restrictive firewalls, while the second is more efficient but requires an open port. I suggest doing both.&lt;/p&gt;

&lt;p&gt;To publish via HTTP, you must make the file &#39;hooks/post-update&#39; executable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;chmod +x /git/factor.git/hooks/post-update
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This gets executed whenever something is pushed to the repository. It runs a command &#39;git-update-server-info&#39; which updates some files that makes the HTTP retrieval work. You should also run this once manually:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd /git/factor.git
git-update-server-info
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now make the /git directory published via your webserver (I symbolic link to it in the server&#39;s doc-root). People can pull from the repository with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git pull http://yourserver.com/git/factor.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To set up the git protocol you need to run the &#39;git-daemon&#39; command. You pass it a directory which is the root of your git repositories. It will make public all git repositories underneath that root that have the file &#39;git-daemon-export-ok&#39; in it. So first create this file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;touch /git/factor.git/git-daemon-export-ok
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Run the daemon with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git-daemon --verbose /git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &#39;--verbose&#39; will give you output showing the results of connecting to it. I run this from within a screen session. You can set it up to automatically run using whatever features your server OS has. Now people can retrieve via the git protocol:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git pull git://yourserver.com/git/factor.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;My repository is accessible from both protocols:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone http://www.double.co.nz/git/factor.git
git clone git://double.co.nz/git/factor.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also &lt;a href=&quot;http://www.double.co.nz/cgi-bin/gitweb.cgi?p=factor.git;a=summary&quot;&gt;browse it using gitweb&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Distributed Channels in Factor</title>
   <link href="http://bluishcoder.co.nz/2007/09/12/distributed-channels-in-factor.html"/>
   <updated>2007-09-12T12:58:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2007/09/12/distributed-channels-in-factor</id>
   <content type="html">&lt;p&gt;Following on from my &lt;a href=&quot;http://bluishcoder.co.nz/2007/09/concurrent-channels-for-factor.html&quot;&gt;Channels  implementation&lt;/a&gt;, I&#39;ve now added &#39;Remote Channels&#39;. These are distributed channels that allow you to access channels in separate Factor instances, even on different machines on the network. It&#39;s based on my &lt;a href=&quot;http://bluishcoder.co.nz/2006/08/distributed-concurrency-in-factor.html&quot;&gt;Distributed Concurrency&lt;/a&gt; work.&lt;/p&gt;

&lt;p&gt;A channel can be made accessible by remote Factor nodes using the &#39;publish&#39; word. Given a channel this will return a long Id value that can be used by remote nodes to use the channel. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;channel&amp;gt; [ sieve ] spawn drop publish .
 =&amp;gt; &quot;ID12345678901234567890.....&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From a remote node you can create a &amp;lt;remote-channel&gt; which contains the hostname and port of the node containing the channel, and the Id of that channel:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;foo.com&quot; 9000 &amp;lt;node&amp;gt; &quot;ID1234...&quot; &amp;lt;remote-channel&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can use &#39;from&#39; and &#39;to&#39; on the remote channel exactly as you can on normal channels. The data is marshalled over the network using the serialization library.
Remote channels are implemented using distributed concurrency so you must start a node on the Factor instance you are using. This is done with &#39;start-node&#39; giving the hostname and port:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;foo.com&quot; 9000 start-node
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once this is done all published channels become available. Note that the hostname and port must be accessible by the remote machine so it can connect to send the data you request.&lt;/p&gt;

&lt;p&gt;As an experiment I published the prime number sieve example mentioned in my &lt;a href=&quot;http://bluishcoder.co.nz/2007/09/concurrent-channels-for-factor.html&quot;&gt;last post&lt;/a&gt;. It&#39;s running on one of my servers. To make it easy to create a &amp;lt;remote-channel&gt; without needing to know the hostname and port I serialized the &amp;lt;remote-channel&gt; instance, saved it in a file and made it available as [...server down sorry...].&lt;/p&gt;

&lt;p&gt;You can load this file into Factor, deserialize it and get the &amp;lt;remote-channel&gt; instance. You can then call &#39;from&#39; on it to get the next prime number in the series. Until they get so big that my Factor instance is DOS&#39;d of course! The code to do this is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USING: serialization http.client 
       channels.remote concurrency.distributed ;

&quot;yourhostname-or-ip-address.com&quot; 9000 start-server
&quot;[server-down-sorry]/prime.ser&quot; http-get-stream 2nip 
[ deserialize ] with-stream
dup from .
dup from .
...etc...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &#39;9000&#39; can be any port number openly accessible on your machine. A current Factor bug means you may get an error in &#39;start-server&#39; about an address already assigned if you run Linux. This is due to an interaction with ipv6 - you can ignore it, the server will start fine. &#39;start-server&#39; needs to be run whenever you start our Factor instance.&lt;/p&gt;

&lt;p&gt;The &#39;dup from .&#39; duplicates the &amp;lt;remote-channel&gt;, gets the next number from it and prints it. It may not be in sequence as other users may have gotten the next number before you.&lt;/p&gt;

&lt;p&gt;There is a lot of room for improvement and additions to the code. Feel free to hack at it and send in patches. Let me know some ideas on how this could be used in &#39;real world&#39; applications.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Concurrent Channels for Factor</title>
   <link href="http://bluishcoder.co.nz/2007/09/05/concurrent-channels-for-factor.html"/>
   <updated>2007-09-05T10:28:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2007/09/05/concurrent-channels-for-factor</id>
   <content type="html">&lt;p&gt;Rob Pike gave a talk at Google &lt;a href=&quot;http://video.google.com/videoplay?docid=810232012617965344&quot;&gt;about the NewSqueak programming language&lt;/a&gt;, and specifically how it&#39;s concurrency features work. NewSqueak uses channels and is based on the concepts of &lt;a href=&quot;http://www.usingcsp.com/&quot;&gt;Communicating Sequential Processes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To play around with some of the ideas in the video I created a &lt;a href=&quot;http://bluishcoder.co.nz/repos/factor/Factor/extra/channels/channels.factor&quot;&gt;quick implementation of channels for Factor&lt;/a&gt; and &lt;a href=&quot;http://bluishcoder.co.nz/repos/factor/Factor/extra/channels/examples/examples.factor&quot;&gt;converted a couple of the NewSqueak examples&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &amp;lt;channel&gt; word creates a channel that threads can send data to and receive data from. The &#39;to&#39; word sends data to a channel. It is a synchronous send and blocks waiting for a receiver if there is none. The &#39;from&#39; word receives data from a channel. It will block if there is no sender.&lt;/p&gt;

&lt;p&gt;There can be multiple senders waiting, and if a thread then receives on the channel, a random sender will be released to send its data. There can also be multiple receivers blocking. A random one is selected to receive the data when a sender becomes available.&lt;/p&gt;

&lt;p&gt;I&#39;m not sure if I&#39;ve got the best stack effect ordering for the words but it gives something to experiment with and work out the best interface. I&#39;ve not yet done a &#39;select&#39; or &#39;mux&#39; words.&lt;/p&gt;

&lt;p&gt;Here is the &#39;counter&#39; example ported from NewSqueak:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;: (counter) ( channel n -- )
    [ swap to ] 2keep 1+ (counter) ;

: counter ( channel -- )
    2 (counter) ;    

: counter-test ( -- n1 n2 n3 )
    &amp;lt;channel&amp;gt; [ counter ] spawn drop 
    [ from ] keep [ from ] keep from ;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Given a channel, the &#39;counter&#39; word will send numbers to it, incrementing them by one, starting from two. &#39;counter-test&#39; creates a channel, spawns a process to run &#39;counter&#39;, and then receives a few values from the channel.&lt;/p&gt;

&lt;p&gt;It&#39;ll be interesting to compare how well CSP works in a concatenative language vs the &lt;a href=&quot;http://radio.weblogs.com/0102385/2005/07/29.html&quot;&gt;message passing concurrency library&lt;/a&gt; I implemented previously. The CSP implementation is simple enough that I should be able to get it going in the &lt;a href=&quot;http://factor.bluishcoder.co.nz/responder/fjsc/&quot;&gt;Factor-&gt;Javascript system&lt;/a&gt; fairly easily. That would give CSP on the browser.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>
