Re: [dev] nscript - a little stack-based scripting language interpretter I wrote

From: Nikhilesh S <s.nikhilesh_AT_gmail.com>
Date: Fri, 27 Aug 2010 04:26:12 +0300 (AST)

I've implemented a new feature 'namespaces'. The changes are on the
'namespace' branch on github.

Now the old separation between 'functrie', 'operatortrie', 'variabletrie'
has disappeared. All the builtin functions such as 'print', 'add'
etc. have now gone into a 'builtins' namespace.

A namespace is a container of name->object mappings, and a reference to
a 'parent namespace'. When a block is run, it's run in a new namespace
whose parent is the namespace it was run in. For now, programs are run
in the builtins namespace so that they inherit the builtin variables.

So basically, you get local variables with C-ish scope rules. See
'test/namespace.ns' and 'test/ownops.ns' on the new branch for examples.

I've also pasted them here with some editing:-

#A simple function that prints '1', '2' and '3' in three separate print
#calls.

{ 3 2 1 &print 3 repeat } $print123

{
     #Redefine 'print' locally, keep old print in _print. The new print
     #function will print "Printing: <value>, "
     &print $_print
     { ', ' rot 'Printing: ' &_print 3 repeat } $print

     #Here, print123 will use the new 'print'.
     print123
} 1 repeat

#Here, print123 will use the normal 'print'.
print123

#There's no difference between operators and normal executable
#variables. Operators just have funky names. Here we create an exponent
#operator.

{
     $p $n

     #Put n on the stack p times, then multiply p times. We also put a 1 on
     #the stack to allow for p = 0.
     1
     { n } p repeat
     &* p repeat
} $^

2 3 ^ print

#Here we assign a number to the variable !^&.

42 $!^&

!^& print

#Here we assign &print to + temporarily within a block to make + print
#instead of adding numbers. Outside the block it's back to normal.

{
     &print $+

     #Prints '3'.
     3 +
} 1 repeat #TODO: Add an 'exec' function to just run a block?

2 3 + print #Here it prints '5'.

-- 
Nikhilesh S 
http://www.nikhilesh.info
Received on Fri Aug 27 2010 - 03:26:12 CEST

This archive was generated by hypermail 2.2.0 : Fri Aug 27 2010 - 02:36:02 CEST