R a i n b o w  Forth                       
                                                                
                       becomes a WebApp                         
                                                                
                                                                
                                                                
                                                                
                         Brad Nelson                            
                      November 15, 2008                         
                                                                
                                                                
                                                                
300

                          Overview                              
                                                                
                                                                
                      * colorForth                              
                      * RainbowForth                            
                      * WebApps                                 
                      * Javascript                              
                      * AppEngine                               
                      * Security/Sharing                        
                      * Process Model                           
                      * Demo                                    
                      * Future Directions                       
                                                                
                                                                
                                                                
                                                                
301

                         colorForth                             
                                                                
         * Use color to simplify the Forth interpreter          
         * Back to basics (not ANS-Forth)                       
                                                                
                                                                
         : square ( n--n )   dup * ;  [ 4 square ]              
                                                                
                          becomes                               
                                                                
            square n--n   dup * ;   4 square                    
                                                                
                                                                
                                                                
                                                                
                                                                
302

                    Meaning of the colors                       
                                                                
                                                                
 red - define a word                                            
 yellow - execute a word                                        
 green - compile a word into the definition of the current word 
 cyan - compile a word, even if it is a macro                   
 blue - get the address of a word (not present in colorForth)   
 magenta - create a variable                                    
 white - comments                                               
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
303

                    Meaning of the colors                       
                                                                
                                                                
                   word         : word                          
                   word         [ word ]                        
                   word         word                            
                   word         ' word compile,                 
                   word         ' word                          
                   word         variable word                   
                   word         ( word )                        
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
304

                      RainbowForth 0.1                          
                                                                
                                                                
               * My first Forth implementation                  
               * x86 machine code                               
               * Bootstrapped from C                            
               * Cheated on the editor                          
               * Platform specific syscalls                     
               * Windows / Linux                                
               * 23 blocks for core system                      
               * 8 blocks to put up colored boxes               
               * Subroutine threaded                            
                                                                
                                                                
                                                                
                                                                
305

                      RainbowForth 0.2                          
                                                                
                                                                
                 * Make it work on a webpage                    
                 * Javascript / AJAX                            
                 * Store state on the server                    
                 * Allow online collaboration                   
                 * Web Application                              
                 * AppEngine                                    
                 * Do the editor in Forth (~10 blocks)          
                 * ~300 lines of Python                         
                 * ~900 lines of Javascript                     
                 * Subroutine threaded                          
                                                                
                                                                
                                                                
306

                      Web Applications                          
                                                                
                                                                
                  * Browser as thin client                      
                  * Use from anywhere                           
                  * Keep your data in the cloud                 
                  * No need to install                          
                  * No need to upgrade                          
                  * Inherently cross platform                   
                  * Tied to the network :-(                     
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
307

                         Javascript                             
                                                                
                                                                
                  * Interpreted                                 
                  * Weakly typed                                
                  * Avaiable on most browser                    
                  * Lisp like closures                          
                  * Driven by browser events                    
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
308

                     Forth in Javascript                        
                                                                
                                                                
               * Arrays for dstack, rstack, heap                
               * Dictionary for the dictionary                  
               * Closures for built-in words                    
               * Poor man threads                               
               * Text console in the DOM                        
               * HTTP POST to read/write blocks                 
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
309

                      Google AppEngine                          
                                                                
                                                                
            * Host WebApps on Google servers                    
            * Designed to scale                                 
            * Python (Django-like)                              
            * Free hosting up to relatively high limits         
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
310

                      Security/Sharing                          
                                                                
                                                                
                * Single number space                           
                * Each block owned by one user                  
                * Cache blocks on read/write                    
                * Blocks readable by all                        
                * Core blocks setup by me                       
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
311

                        Process Model                           
                                                                
                                                                
                    * Per thread data:                          
                      - data stack                              
                      - return stack                            
                      - instruction pointer                     
                      - console location                        
                                                                
                    * Global data:                              
                      - heap                                    
                      - dictionary                              
                                                                
                                                                
                                                                
                                                                
312

                       Threading Model                          
                                                                
                                                                
             * Execute thread until it yields                   
             * Yield to system when all threads block           
             * Yield to system if too much time passes          
             * Run threads if key events come in                
             * Run threads on periodic timer tick               
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
313

                            DEMO                                
                                                                
                                                                
                                                                
               DEMO DEMO DEMO                                   
                  DEMO DEMO DEMO                                
                     DEMO DEMO DEMO                             
                        DEMO DEMO DEMO                          
                           DEMO DEMO DEMO                       
                              DEMO DEMO DEMO                    
                                 DEMO DEMO DEMO                 
                                                                
                                                                
                                                                
                                                                
                                                                
314

                      Future Directions                         
                                                                
                                                                
               * Cross compile native code?                     
               * Move to SCM like sharing model?                
               * Use Gears to run offline?                      
               * Actually use it for something?                 
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
315