Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/rc/til

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc

. /bin/maths.rc

ramfs -m /n/words

stack = ()
return = ()

nl = '
'
spacetabnl = ' 	
'

fn do {
	switch($1) {
	case dup # (a -- a a)
		stack = ($stack(1) $stack)
	case swp # (a b -- b a)
		stack = ($stack(2) $stack(1) $stack(3-))
	case drop # (a --)
		stack = ($stack(2-))
	case emit # (a --) 
		echo -n $stack(1) | tr 
 $nl | tr ¯ ' '
		stack = ($stack(2-))
	case eval # (a b (a items) --)
		eval $stack(2-`{add 2 $stack(1)})
		top =  `{add 3 $stack(1)}
		stack = ($stack($top-))
	case 'eval<' # (a b (a items) -- (eval output) )
		top =  `{add 3 $stack(1)}
		stack = (`{eval $stack(2-`{add 2 $stack(1)})} $stack($top-))
	case ~
		i = 2
		while(test $i -lt $#stack || test $i -eq $#stack && ! ~ $stack($i) $stack(1))
			i = `{inc $i}
		if(test $i -gt $#stack)
			stack = (0 $stack(2-))
		if not {
			stack = (`{sub $i 1 } $stack(2-))
		}
	case stk 
		echo $"stack
	case * 
		if(test -e /n/words/$1) { # recurse
			ifs = $spacetabnl
			for(w in `{cat /n/words/$1})
				do $w
			ifs = $nl
		}
		if not   # (a -- $1 a)
			stack = ($1 $stack)
	}
}

ifs = $nl

if(~ $1 -i)
	echo -n '> '

while(line=`{read}) {
	ifs = $spacetabnl
	words = `{echo -n $line}
	ifs = $nl
	switch($words(1)) {
	case :
		echo $words(3-) > /n/words/$words(2)
	case *
		for(w in $words)
			do $w
	}
	if(~ $1 -i)
		echo -n '> '
}



Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].