mirror of
https://github.com/flynx/Slang.git
synced 2025-10-29 10:40:07 +00:00
refactored the bootstrap and added some new words...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
2fc89940c9
commit
22a22fd083
@ -120,6 +120,7 @@ function runCommand(){
|
|||||||
var c = commands[commands.length-1]
|
var c = commands[commands.length-1]
|
||||||
c.innerText = command.innerText
|
c.innerText = command.innerText
|
||||||
c.focus()
|
c.focus()
|
||||||
|
window.scrollTo(0,document.body.scrollHeight)
|
||||||
}, false)
|
}, false)
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
|||||||
165
Slang/slang.js
165
Slang/slang.js
@ -399,7 +399,7 @@ var NAMESPACE = {
|
|||||||
var e = context.stack.pop()
|
var e = context.stack.pop()
|
||||||
if(i < 0){
|
if(i < 0){
|
||||||
var l = context.stack[context.stack.length-1]
|
var l = context.stack[context.stack.length-1]
|
||||||
l.splice(l.length + i + 1, 0, e)
|
l.splice(l.length + i, 0, e)
|
||||||
} else {
|
} else {
|
||||||
context.stack[context.stack.length-1].splice(i, 0, e)
|
context.stack[context.stack.length-1].splice(i, 0, e)
|
||||||
}
|
}
|
||||||
@ -413,12 +413,13 @@ var NAMESPACE = {
|
|||||||
return context.stack[context.stack.length-1].length
|
return context.stack[context.stack.length-1].length
|
||||||
},
|
},
|
||||||
// b c -- b
|
// b c -- b
|
||||||
'each': function(context){
|
'map': function(context){
|
||||||
var c = context.stack.pop()
|
var c = context.stack.pop()
|
||||||
var b = context.stack[context.stack.length-1]
|
var b = context.stack[context.stack.length-1]
|
||||||
for(var i=0; i < b.length; i++){
|
for(var i=0; i < b.length; i++){
|
||||||
// exec block in a separate context...
|
// exec block in a separate context...
|
||||||
var res = run({
|
var res = run({
|
||||||
|
//stack: [i, b[i], c],
|
||||||
stack: [b[i], c],
|
stack: [b[i], c],
|
||||||
code: ['exec'],
|
code: ['exec'],
|
||||||
// NOTE: this can have side-effects on the context...
|
// NOTE: this can have side-effects on the context...
|
||||||
@ -544,13 +545,69 @@ var BOOTSTRAP = [
|
|||||||
' not interpreted in any way.',
|
' not interpreted in any way.',
|
||||||
'',
|
'',
|
||||||
'-------------------------------------------------------------------------------',
|
'-------------------------------------------------------------------------------',
|
||||||
|
'',
|
||||||
|
'Basic words for block manipulation:',
|
||||||
|
'',
|
||||||
|
'Get block length',
|
||||||
|
' [ 1 2 3 ] len',
|
||||||
|
' Result:',
|
||||||
|
' 3',
|
||||||
|
'',
|
||||||
|
'Pop element form block tail',
|
||||||
|
' [ 1 2 3 ] pop',
|
||||||
|
' Result:',
|
||||||
|
' [ 1 2 ] 3',
|
||||||
|
'',
|
||||||
|
'Push element to block tail',
|
||||||
|
' [ 1 2 3 ] 4 push',
|
||||||
|
' Result:',
|
||||||
|
' [ 1 2 3 4 ]',
|
||||||
|
'',
|
||||||
|
'NOTE: all indexes can be negative values, these will indicate the',
|
||||||
|
' position relative to the tail, -1 being the last element.',
|
||||||
|
'',
|
||||||
|
'Get element at position (0)',
|
||||||
|
' [ 1 2 3 ] -1 at',
|
||||||
|
' Result:',
|
||||||
|
' [ 1 2 3 ] 3',
|
||||||
|
'',
|
||||||
|
'Put element (123) at position (0)',
|
||||||
|
' [ 1 2 3 ] 123 0 to',
|
||||||
|
' Result:',
|
||||||
|
' [ 123 2 3 ]',
|
||||||
|
'',
|
||||||
|
'Put element (123) before position (0)',
|
||||||
|
' [ 1 2 3 ] 123 0 before',
|
||||||
|
' Result:',
|
||||||
|
' [ 123 1 2 3 ]',
|
||||||
|
'',
|
||||||
|
'Expand block to stack -- "block 2 stack"',
|
||||||
|
' [ 1 2 3 ] b2s',
|
||||||
|
' Result:',
|
||||||
|
' 1 2 3',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'-------------------------------------------------------------------------------',
|
||||||
's2b drop',
|
's2b drop',
|
||||||
'--',
|
'--',
|
||||||
'-- With that out of the way, let\'s start with the bootstrap...',
|
'-- With that out of the way, let\'s start with the bootstrap...',
|
||||||
'--',
|
|
||||||
'-- make a new block instance shorthand...',
|
|
||||||
':: [] [ [ ] clone ]',
|
|
||||||
'',
|
'',
|
||||||
|
'-- misc...',
|
||||||
|
':: . ( x -- ) [ drop ]',
|
||||||
|
':: .. ( x -- ) [ print drop ]',
|
||||||
|
'',
|
||||||
|
':: ne ( a -- b ) [ eq not ]',
|
||||||
|
':: isT ( a -- b ) [ not not true eq ]',
|
||||||
|
':: isF ( a -- b ) [ not isT ]',
|
||||||
|
'',
|
||||||
|
':: inc ( a -- b ) [ 1 add ]',
|
||||||
|
':: dec ( a -- b ) [ 1 sub ]',
|
||||||
|
'',
|
||||||
|
':: ! ( a -- b ) [ [ dup 1 ne ] ? [ dup 1 sub ! mul ] ]',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'-- Stack/code manipulation...',
|
||||||
':: _swap ( x | y -- y | x ) [ 1 1 _swapN ]',
|
':: _swap ( x | y -- y | x ) [ 1 1 _swapN ]',
|
||||||
':: _push ( x | -- | x ) [ 0 _swapN ]',
|
':: _push ( x | -- | x ) [ 0 _swapN ]',
|
||||||
':: _pull ( | x -- x | ) [ 0 swap _swapN ]',
|
':: _pull ( | x -- x | ) [ 0 swap _swapN ]',
|
||||||
@ -560,15 +617,16 @@ var BOOTSTRAP = [
|
|||||||
'-- like exec but will run a block in current context...',
|
'-- like exec but will run a block in current context...',
|
||||||
':: b2c [ len rot b2s tor 0 _swapN ]',
|
':: b2c [ len rot b2s tor 0 _swapN ]',
|
||||||
'',
|
'',
|
||||||
':: . ( x -- ) [ drop ]',
|
|
||||||
':: .. ( x -- ) [ print drop ]',
|
|
||||||
'',
|
|
||||||
':: swap2 ( a _ b -- b _ a ) [ swap rot swap tor swap ]',
|
':: swap2 ( a _ b -- b _ a ) [ swap rot swap tor swap ]',
|
||||||
':: dup2 ( a b -- a b a b ) [ dup swap2 dup rot swap2 tor swap ]',
|
':: dup2 ( a b -- a b a b ) [ dup swap2 dup rot swap2 tor swap ]',
|
||||||
'',
|
'',
|
||||||
':: ne ( a -- b ) [ eq not ]',
|
'-- this is here for devel use only',
|
||||||
':: isT ( a -- b ) [ not not true eq ]',
|
':: _clear ( ... -- ) [ s2b print drop ] ',
|
||||||
':: isF ( a -- b ) [ not isT ]',
|
':: _stack_size ( -- l ) [ s2b len swap b2s tor ] ',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'-- Flow control...',
|
||||||
'',
|
'',
|
||||||
'-- Classic conditional word:',
|
'-- Classic conditional word:',
|
||||||
'-- [ cond ] [ A ] [ B ] if',
|
'-- [ cond ] [ A ] [ B ] if',
|
||||||
@ -601,12 +659,50 @@ var BOOTSTRAP = [
|
|||||||
' [ exec [ _run_then 1 ] and [ swap _run_else 2 ] or b2s 2 _swapN ]',
|
' [ exec [ _run_then 1 ] and [ swap _run_else 2 ] or b2s 2 _swapN ]',
|
||||||
'',
|
'',
|
||||||
'',
|
'',
|
||||||
'-- list/block 2\'nd gen stuff...',
|
|
||||||
':: push ( b e i -- b ) [ -1 before ]',
|
|
||||||
'',
|
'',
|
||||||
|
'-- List/block 2\'nd gen stuff...',
|
||||||
|
'-- make a new block instance shorthand...',
|
||||||
|
':: [] [ [ ] clone ]',
|
||||||
|
'',
|
||||||
|
'-- insert element after index...',
|
||||||
|
':: after ( b e i -- b ) [',
|
||||||
|
' -- special case, when at end, need to push the alement after it...',
|
||||||
|
' dup [ -1 eq ] ?',
|
||||||
|
' [ . push ]',
|
||||||
|
' else',
|
||||||
|
' [ inc before ] ]',
|
||||||
|
'',
|
||||||
|
'-- push element to tail of block...',
|
||||||
|
':: push ( b e -- b ) [ swap len rot swap tor to ]',
|
||||||
|
'',
|
||||||
|
'-- Filter the block via a condition...',
|
||||||
|
'--',
|
||||||
|
'-- the condition block must have the folowing stack effect: elem -- bool',
|
||||||
|
':: filter ( b c -- b ) [',
|
||||||
|
' -- prepare the condition...',
|
||||||
|
' \\ dup 0 before',
|
||||||
|
' -- define the template...',
|
||||||
|
' [ TEST ? [ ] else [ . ] ] clone',
|
||||||
|
' -- prepare the template...',
|
||||||
|
' swap 0 to',
|
||||||
|
' -- do the filtering',
|
||||||
|
' map ]',
|
||||||
|
'',
|
||||||
|
'-- Create a block containing a range numbers form 0 to n-1...',
|
||||||
|
':: range ( n -- b ) [',
|
||||||
|
' -- initial state...',
|
||||||
|
' [ dup isNumber ] ? ',
|
||||||
|
' [ [] swap ]',
|
||||||
|
' -- get first elem...',
|
||||||
|
' else',
|
||||||
|
' [ 0 at ]',
|
||||||
|
' -- we got to the end...',
|
||||||
|
' [ dup 0 eq ] ? ',
|
||||||
|
' [ drop ]',
|
||||||
|
' -- dec push new and continue...',
|
||||||
|
' else',
|
||||||
|
' [ 1 sub 0 before range ] ]',
|
||||||
'',
|
'',
|
||||||
':: inc ( a -- b ) [ 1 add ]',
|
|
||||||
':: dec ( a -- b ) [ 1 sub ]',
|
|
||||||
'',
|
'',
|
||||||
'',
|
'',
|
||||||
'-- Meta-word examples (experimental)...',
|
'-- Meta-word examples (experimental)...',
|
||||||
@ -646,29 +742,30 @@ var BOOTSTRAP = [
|
|||||||
'infix: > gt',
|
'infix: > gt',
|
||||||
'',
|
'',
|
||||||
'',
|
'',
|
||||||
'-- this is here for devel use only',
|
|
||||||
':: _clear ( ... -- ) [ s2b print drop ] ',
|
|
||||||
':: _stack_size ( -- l ) [ s2b len swap b2s tor ] ',
|
|
||||||
'',
|
'',
|
||||||
|
'-- Tests and examples...',
|
||||||
'',
|
'',
|
||||||
'-- tests and examples...',
|
'-- Mandatory "hello word" word example...',
|
||||||
':: hi ( -- ) [ "Hello World!" print drop ]',
|
':: hi ( -- ) [ "Hello World!" print drop ]',
|
||||||
':: ! ( a -- b ) [ [ dup 1 ne ] ? [ dup 1 sub ! mul ] ]',
|
'',
|
||||||
':: range ( n -- b ) [',
|
'-- Create a block containg a range of n numbers form 0 and adding s to',
|
||||||
' -- initial state...',
|
'-- each next number...',
|
||||||
' [ dup isNumber ] ? ',
|
|
||||||
' [ [] swap ]',
|
|
||||||
' -- get first elem...',
|
|
||||||
' else',
|
|
||||||
' [ 0 at ]',
|
|
||||||
' -- we got to the end...',
|
|
||||||
' [ dup 0 eq ] ? ',
|
|
||||||
' [ drop ]',
|
|
||||||
' -- dec push new and continue...',
|
|
||||||
' else',
|
|
||||||
' [ 1 sub 0 before range ] ]',
|
|
||||||
':: range2 ( n s -- b )',
|
':: range2 ( n s -- b )',
|
||||||
' [ swap range swap [] swap push \\ * 0 before each ]',
|
' [ swap range swap [] swap push \\ * 0 before map ]',
|
||||||
|
'',
|
||||||
|
'-- Sum up the elements of a block...',
|
||||||
|
':: sum ( L -- s ) [',
|
||||||
|
' -- empty list, sum is 0...',
|
||||||
|
' [ len 0 eq ] ?',
|
||||||
|
' [ . 0 ]',
|
||||||
|
' else',
|
||||||
|
' -- sum of list of len 1 is it\'s content, so just pop it...',
|
||||||
|
' [ [ len 1 eq ] ?',
|
||||||
|
' [ pop swap . ]',
|
||||||
|
' -- and now recursively sum up elements till the list is 1 in length...',
|
||||||
|
' else',
|
||||||
|
' [ pop rot pop tor add push sum ] ] ]',
|
||||||
|
'',
|
||||||
''].join('\n')
|
''].join('\n')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user