\documentclass[pdf]{prosper}
\usepackage[toc,highlight,linbit,notes,hlsections]{HA-prosper}

\title{SPL and WebSPL}
\subtitle{Yet another programming paradigm}
\author{Clifford Wolf - www.clifford.at}

\DefaultTransition{Wipe}
\TitleSlideNav{FullScreen}
\NormalSlideNav{ShowBookmarks}
\LeftFoot{\href{http://www.clifford.at}{Clifford Wolf}, \today}
\RightFoot{\href{http://www.clifford.at/spl/}{SPL - http://www.clifford.at/spl/}}


\begin{document}

\maketitle

% ============================================================================

\tsectionandpart{Introduction}

\begin{slide}{Overview}
\begin{itemize}

\item SPL is an embeddable scripting language with C-like syntax.

\vspace*{.3cm}
\item It has support for arrays, hashes, objects, perl regular expressions, etc. pp.

\vspace*{.3cm}
\item The entire state of the virtual machine can be dumped at any time and
execution of the program resumed later.

\vspace*{.3cm}
\item In SPL there is a clear separation of compiler, assembler, optimizer and
virtual machine.

\vspace*{.3cm}
\item It's possible to run pre-compiled binaries, program directly in the VM
assembly, use multi threading, step-debug programs, etc. pp.

\vspace*{.3cm}
\item SPL is a very small project, so it is a good example for implementing
high-level language compilers for stack machines.

\end{itemize}
\end{slide}

\begin{slide}{Aim}
\begin{itemize}

\item Creating an interesting alternative to S-Lang for embeddable scripting
languages.

\vspace*{.5cm}
\item Creating an interesting alternative to Java and .NET for web
applications.

\vspace*{.5cm}
\item Creating a good example for simple and well designed virtual machine and
a compiler for it.

\end{itemize}
\end{slide}

\begin{slide}{Components}
\begin{itemize}

\item SPL is a small library containing the SPL components:

\vspace*{.5cm}
\item Virtual Machine: can execute SPL bytecode

\vspace*{.5cm}
\item Assembler: can convert SPL assembler to bytecode

\vspace*{.5cm}
\item Compiler: can convert SPL to SPL assembler

\vspace*{.5cm}
\item Optimizer: plugs into the assembler and optimizes the bytecode

\vspace*{.5cm}
\item Dumper: can dump and restore the VM state

\end{itemize}
\end{slide}

% ============================================================================

\tsectionandpart{Virtual Machine}

\begin{slide}{Overview}
\begin{itemize}

\item An SPL VM is basically the sum of: \\
Codepages, Tasks (threads), Nodes (variables) and builtin functions

\vspace*{.5cm}
\item Tasks have an instruction pointer and a (node) stack

\vspace*{.5cm}
\item Nodes represent all kinds of variables and form a tree-like graph

\vspace*{.5cm}
\item The builtin functions have a flat namespace and are global for the VM

\vspace*{.5cm}
\item Almost everything else is pretty strict boud to some kind of context

\end{itemize}
\end{slide}

\begin{slide}{Nodes}
\begin{itemize}

\item Nodes are "the SPL variables".

\vspace*{.5cm}
\item A node can hold various kinds of data:

\vspace*{.2cm}
\begin{itemize}
	\item Scalar
	  \begin{itemize}
		\item Integer, Float, String
	  \end{itemize}
\vspace*{.2cm}
	\item Assoziative Arrays
\vspace*{.2cm}
	\item Code Pointer
	  \begin{itemize}
		\item Functions, Return-Addresses, ...
	  \end{itemize}
\vspace*{.2cm}
	\item Hosted Namespaces
\vspace*{.2cm}
	\item Classes and Objects
\end{itemize}

\vspace*{.5cm}
\item Each node has a context pointer and type

\end{itemize}
\end{slide}

\begin{slide}{Architecture}
\begin{itemize}

\item The SPL virtual machine is a simple stack machine

\vspace*{.5cm}
\item It is using a hybrid reference counting mark recursive garbage collector

\vspace*{.5cm}
\item The instruction set listing is in {\tt spl.h}

\vspace*{.5cm}
\item Implementation details: {\tt exec.c} and {\tt state.c}

\end{itemize}
\end{slide}

% ============================================================================

\tsectionandpart{The SPL Language}

\begin{slide}{Overview}
\begin{itemize}

\item SPL is syntactically a C-like language

\vspace*{.5cm}
\item It has some concepts from JavaScript, Perl, OCaml and Nasal

\vspace*{.5cm}
\item And some new concepts (at least I think so ;-)

\vspace*{.5cm}
\item SPL is entirely typeless

\vspace*{.5cm}
\item See code examples ...

\end{itemize}
\end{slide}

\begin{slide}{Basics (1/2)}
\begin{itemize}

\item Variables must be declared with the {\tt var} keyword.

\vspace*{.3cm}
\item {\tt if}, {\tt for}, {\tt while}, {\tt return} and so on work as in C.

\vspace*{.3cm}
\item {\tt foreach i (array) array.[i] = 42;} \\
iterates over the keys of an assoziative array.

\vspace*{.3cm}
\item {\tt undef} is a constant expression for an undefined scalar value.

\vspace*{.15cm}
\item {\tt defined} is a check if the scalar value of a variable is defined.

\vspace*{.15cm}
\item {\tt declared} checks if a variable-name (key in assoziative array) exists.

\vspace*{.15cm}
\item {\tt delete} deletes an entry in an assoziative array.

\end{itemize}
\end{slide}

\begin{slide}{Basics (2/2)}
\begin{itemize}

\item The {\tt asm} statement can be used to insert assembler code.

\vspace*{.3cm}
\item {\tt debug} can be used to write to the console.

\vspace*{.5cm}
\item {\tt function add3(a, b, c) \{ return a+b+c; \}} \\
{\tt debug add3(10,15,20);} \\
defines and calls a function.

\vspace*{.5cm}
\item {\tt foo = bar;} \\
creates a copy for the node value and a reference for its childs (or at least
it looks like that.. ;-)

\vspace*{.3cm}
\item {\tt foo := bar;} \\
Create a real (recursive) copy of the node.

\end{itemize}
\end{slide}

\begin{slide}{Operators}
\begin{itemize}

\item {\tt !}, {\tt ||}, {\tt \&\&}, {\tt not}, {\tt and}, {\tt or} \\
Logical operators

\vspace*{.2cm}
\item {\tt ==}, {\tt !=}, {\tt <=}, {\tt <}, {\tt >}, {\tt >=} \\
Integer comparisons

\vspace*{.2cm}
\item {\tt .==}, {\tt .!=}, {\tt .<=}, {\tt .<}, {\tt .>}, {\tt .>=} \\
Floating point comparisons

\vspace*{.2cm}
\item {\tt $\sim$==}, {\tt $\sim$!=}, {\tt $\sim$<=}, {\tt $\sim$<}, {\tt $\sim$>}, {\tt $\sim$>=} \\
String comparisons

\vspace*{.2cm}
\item {\tt +}, {\tt -}, {\tt *}, {\tt /}, {\tt \%}, {\tt **} \\
Integer operators

\vspace*{.2cm}
\item {\tt .+}, {\tt .-}, {\tt .*}, {\tt ./} {\tt .\%}, {\tt .**}\\
Floating point operators

\vspace*{.2cm}
\item {\tt $\sim$} \\
String concatenation

\end{itemize}
\end{slide}

\begin{slide}{Objects}
\begin{itemize}

\item In SPL, there is no difference between objects and classes. So it is
called "objects" and "instances of objects".

\vspace*{.3cm}
\begin{verbatim}
object Foo {
    method init() {
        debug "Now in init() from A.";
        return this;
    }
}

object Bar Foo {
    method init() {
        debug "Now in init() from B.";
        return *A.foo();
    }
}

var foobar = new Bar();
\end{verbatim}

\end{itemize}
\end{slide}

\begin{slide}{Functions}
\begin{itemize}

\item Functions (function pointers) are just variables.
\item They can by copied, etc as any other variable.

\vspace*{.3cm}
\item While a function is exectued, the parent context is the context in which
the function has been defined, \\
not the context from which the function has been called.

\vspace*{.3cm}
\item {\tt foobar(a, b, c);} \\
calling a regular function or builtin function.

\vspace*{.3cm}
\item {\tt *foobar(a, b, c);} \\
calling a function with current context as parent context.

\vspace*{.3cm}
\item {\tt \$foobar(a, b, c);} \\
calling a builtin function.

\end{itemize}
\end{slide}

\begin{slide}{Here-documents}
\begin{itemize}

\item {\tt << FOOBAR} \\
string until FOOBAR with \$-Substitution

\vspace*{.3cm}
\item {\tt <<< this is test number \$i} \\
string until EOL with \$-Substitution

\vspace*{.3cm}
\item {\tt >> FOOBAR} \\ {\tt >>> this is another test} \\
as above but without \$-Substitution

\vspace*{.3cm}
\item {\tt << FOOBAR:} \\ {\tt >> FOOBAR:} \\
as above but with indenting character

\vspace*{.3cm}
\item {\tt <foobar>} \\ {\tt </foobar>} \\
an inline template

\end{itemize}
\end{slide}

\begin{slide}{Templates}
\begin{itemize}

\item {\tt <spl:if cond="defined userid"> \\ </spl:if>} \\
only include content if condition is true

\vspace*{.5cm}
\item {\tt <spl:foreach var="i" list="list"> \\ </spl:foreach>} \\
iterate over loop

\vspace*{.5cm}
\item {\tt <spl:code> \\ </spl:code>} \\
execute embeded function and include return value

\vspace*{.5cm}
\item {\tt <spl:var name="query"> \\ </spl:var>} \\
set variable to content

\end{itemize}
\end{slide}

\begin{slide}{Includes}
\begin{itemize}

\item {\tt \#file-as-const example11.txt} \\
insert file as string constant

\vspace*{.5cm}
\item {\tt \#file-as-code example11.code} \\
insert file as program code

\vspace*{.5cm}
\item {\tt \#file-as-template example11.tpl} \\
insert file as template with \$-Substitution \\
and interpretation of {\tt <spl:..>} tags.

\vspace*{.5cm}
\item {\tt \#embedded-file demo.txt EOF .. EOF} \\
A "file" embedded like a here-document \\
It can be accessed as {\tt *demo.txt}

\end{itemize}
\end{slide}

\begin{slide}{\$ Substitutions}
\begin{itemize}

\item {\tt \$variable} \\
Insert value of variable

\vspace*{.5cm}
\item {\tt \$\{var1 + var2\}} \\
insert result of expression

\vspace*{.5cm}
\item {\tt \$( if (x) return y; return "bla"; )} \\
insert return value of embedded function

\vspace*{.5cm}
\item {\tt \$[ this is a comment ]} \\
comments in strings, templates or here documents

\end{itemize}
\end{slide}

\begin{slide}{Advanced expressions}
\begin{itemize}

\item {\tt xml::variable} \\
The same as \hspace*{.2cm} {\tt encode\_xml(variable) }

\vspace*{.5cm}
\item {\tt (\{ if (x) return y; return "bla"; \})} \\
An embedded function

\end{itemize}
\end{slide}

% ============================================================================

\tsectionandpart{The C-API}

\begin{slide}{Overview}
\begin{itemize}

\item Include SPL as vendor-branch in your apps.

\vspace*{.5cm}
\item Two files: {\tt spl.a} and {\tt spl.h}

\vspace*{.5cm}
\item Link apps with {\tt spl.a} and {\tt -rdynamic} (for module loading)

\vspace*{.5cm}
\item SPL is very modular.
\item It is easy to take parts out or substitute them.

\vspace*{.5cm}
\item All types and functions are prefixed with {\tt spl\_}.
\item All preprocessor defines are prefixed with {\tt SPL\_}.

\vspace*{.5cm}
\item See: {\tt spl.h} and {\tt splrun.c}.

\end{itemize}
\end{slide}

\begin{slide}{Simple example (1/2)}
\begin{verbatim}
/* create VM and task structs */
struct spl_vm *vm = spl_vm_create();
struct spl_task *task = spl_task_create(vm, 0);

/* create assembler */
struct spl_asm *as = spl_asm_create();

/* compile and optimize */
char *spl_source = spl_malloc_file("example.spl", 0);
if ( spl_compiler(as, spl_source,
        "example.spl", spl_malloc_file) ) return 1;
free(spl_source);
spl_asm_add(as, SPL_OP_HALT, 0);
spl_optimizer(as);

/* dump bytecode to task, free assembler */
spl_task_setcode(task, spl_asm_dump(as));
spl_asm_destroy(as);
\end{verbatim}
\end{slide}

\begin{slide}{Simple example (2/2)}
\begin{verbatim}
/* register builtins to VM */
spl_builtin_register_all(vm);

/* runloop */
while ( task->code )
{
        /* handle scheduling */
        task = spl_schedule(task);
        if ( !task ) break;

        /* execute an instruction */
        int rlret = spl_exec(task);

        /* handle runtime error */
        if ( rlret < 0 ) return 1;
}

spl_vm_destroy(vm);
\end{verbatim}
\end{slide}

\begin{slide}{CLIB Functions}

Extending SPL with additional builtin functions is easy. \\
Here is an example from the XML module:

\vspace*{.5cm}
\begin{verbatim}
struct spl_node *handler_encode_xml(
                struct spl_task *task, void *data)
{
        char *source = spl_clib_get_string(task);
        return SPL_NEW_STRING(xml_encode(source));
}


void spl_mod_xml_init(struct spl_vm *vm,
                struct spl_module *mod, int restore)
{
        spl_clib_reg(vm, "encode_xml",
                handler_encode_xml, 0);
}
\end{verbatim}
\end{slide}

\begin{slide}{Advanced examples}
\begin{itemize}

\item Compiling and running SPL programs: \\
{\tt splrun.c}

\vspace*{.5cm}
\item Implementing loadable modules: \\
{\tt modules/mod\_termio.c}

\vspace*{.5cm}
\item Implementing hosted namespaces: \\
{\tt modules/mod\_prime.c}

\vspace*{.5cm}
\item Embedding SPL bytecode in C programs: \\
{\tt modules/mod\_wsf.*}

\end{itemize}
\end{slide}

% ============================================================================

\tsectionandpart{WebSPL, WSF, Tasks}

\begin{slide}{WebSPL}
\begin{itemize}

\item WebSPL is a framework for web application development.

\vspace*{.5cm}
\item It creates a state over the stateless HTTP protocol using the
dump/restore features of SPL.

\vspace*{.5cm}
\item I.e. it is possible to print out an updated HTML page and then call a
function which ``waits'' for the user to do anything and returns then.

\end{itemize}
\end{slide}

\begin{slide}{WSF (WebSPL Forms)}
\begin{itemize}

\item The DOM tree of a webpage is mapped to a tree of WSF objects.

\vspace*{.5cm}
\item Each WSF object must implement the method {\tt get\_html()} which creates
the DOM tree for the object and all its children as XHTML code.

\vspace*{.5cm}
\item Each WSF object is running in its own task context.

\vspace*{.5cm}
\item The WSF main loop is updating the webbrowser using a {\it
JavaScript-in-IFrame-Hack} or by reloading the entire site.

\vspace*{.5cm}
\item See {\tt wsfdemo/wsfdemo.webspl} for a WebSPL example with WebSPL Forms.

\end{itemize}
\end{slide}

\begin{slide}{WSF Dialogs}
\begin{itemize}

\item A generic component for "clicking together" WSF components.

\vspace*{.5cm}
\item A WSF Dialogs can be stored and loaded as XML files.

\vspace*{.5cm}
\item The component already is the editor.
\item A member function can be used to switch the modes.

\end{itemize}

\vspace*{.5cm}
\begin{verbatim}
load "wsf";
load "wsf_dialog";

var page = new WsfDocument();
page.root = new WsfDialog( undef );
page.root.set_edit_mode(1);
page.main();
\end{verbatim}

\end{slide}

\begin{slide}{WSF Edit}
\begin{itemize}

\item A generic component for database fronted-like components.

\vspace*{.2cm}
\item Generic load and store functions.

\vspace*{.2cm}
\item A list of fields actually present in the html output.

\vspace*{.2cm}
\item A child class for simple SQL frontend is also available.

\end{itemize}

\vspace*{.5cm}
\begin{verbatim}
object Pref WsfEditSql
{
        var sql_db = db;
        var sql_table = "users";
        var sql_key = "rowid";

        method get_html() {
                edit_init();
                return #file-as-template edit_pref.tpl;
        }
}
\end{verbatim}

\end{slide}

\begin{slide}{Tasks API}
\begin{itemize}

\item A generic environment for multithreading and co-routines in SPL.

\vspace*{.5cm}
\item The host app must support it by calling the {\tt spl\_schedule()}
function.

\vspace*{.5cm}
\item It provides functions for creating, destroying, stopping and waking up
tasks.

\vspace*{.5cm}
\item {\tt spl\_schedule()} does round-robin scheduling between the tasks.

\vspace*{.5cm}
\item Frameworks for co-routines, etc. can easily be implemented in SPL.

\end{itemize}
\end{slide}

\begin{slide}{Simple XML API}
\begin{itemize}

\item A generic API for reading and writing XML Files.

\vspace*{.5cm}
\item {\tt xmltree = xml2tree(xmlfile, \^error);} \\
read XML file (DOM parser)

\vspace*{.5cm}
\item {\tt xmlfile = tree2xml(\^xmltree);} \\
create XML from SPL DOM tree

\vspace*{.5cm}
\item {\tt encoded = encode\_xml(plaintext);} \\
xml-encode plaintext

\vspace*{.5cm}
\item {\tt encoded = xml::plaintext;} \\
as above, but using the {\tt ::} syntax

\vspace*{.5cm}
\item TODO: XPATH API for searching in xmltree objects

\end{itemize}
\end{slide}

% ============================================================================

\tsectionandpart{URLs and References}

\begin{slide}{SPL-Based Projects}
\begin{itemize}

\item BotHack \\
{\it still in design phase}

\vspace*{.5cm}
\item QCake (aka. "KCake") \\
http://www.qcake.org/

\vspace*{.5cm}
\item WebSPL \\
http://www.clifford.at/spl/

\vspace*{.5cm}
\item More to come?

\end{itemize}
\end{slide}

\begin{slide}{URLs and References}
\begin{itemize}

\item The SPL Project: \\
http://www.clifford.at/spl/

\vspace*{.5cm}
\item Clifford Wolf: \\
http://www.clifford.at/

\vspace*{.5cm}
\item LINBIT Information Technologies \\
http://www.linbit.com/

\end{itemize}
\end{slide}

% ============================================================================

\end{document}

