Words and Help; use ≡ for definitions.

By using ≡ for definitions I can put docs in the DEFS string and easily
detect definitions by having them be each on their own line.
This commit is contained in:
Simon Forman 2022-09-07 13:13:46 -07:00
parent 29e6f160c6
commit 67fd88f68a
1 changed files with 84 additions and 39 deletions

View File

@ -30,6 +30,7 @@ This script implements an interpreter for a dialect of Joy.
''' '''
from functools import wraps from functools import wraps
from inspect import getdoc
from re import Scanner from re import Scanner
from traceback import print_exc from traceback import print_exc
import operator import operator
@ -511,6 +512,40 @@ def SimpleFunctionWrapper(f):
return inner return inner
@inscribe
def words(stack, expression, dictionary):
'''
Put a list of all the words in alphabetical order onto the stack.
'''
w = ()
for name in reversed(sorted(dictionary)):
if name.startswith('_'):
continue
w = (Symbol(name), ()), w
return (w, stack), expression, dictionary
HELP_TEMPLATE = '''\
==== Help on %s ====
%s
---- end ( %s )
'''
@inscribe
def help_(stack, expression, dictionary):
'''
Accepts a quoted symbol on the top of the stack and prints its docs.
'''
((symbol, _), stack) = stack
word = dictionary[symbol]
print(HELP_TEMPLATE % (symbol, getdoc(word), symbol))
return stack, expression, dictionary
''' '''
@ -852,7 +887,11 @@ for F in (
class Def(object): class Def(object):
tribar = '\u2261' # ≡
def __init__(self, name, body): def __init__(self, name, body):
self.__doc__ = f'{name} {self.tribar} {expression_to_string(body)}'
self.__name__ = name self.__name__ = name
self.body = tuple(iter_stack(body)) self.body = tuple(iter_stack(body))
@ -863,26 +902,31 @@ class Def(object):
@classmethod @classmethod
def load_definitions(class_, stream, dictionary): def load_definitions(class_, stream, dictionary):
for line in stream: for line in stream:
if line.lstrip().startswith('#'): if class_.tribar not in line:
continue continue
name, body = text_to_expression(line) name, body = text_to_expression(line.replace(class_.tribar, ''))
if name not in dictionary: if name not in dictionary:
inscribe(class_(name, body), dictionary) inscribe(class_(name, body), dictionary)
DEFS = '''\ DEFS = '''\
-- 1 -
? dup bool Start with increment and decrement:
&& nulco [nullary [false]] dip branch
++ 1 + -- 1 -
|| nulco [nullary] dip [true] branch ++ 1 +
!- 0 >=
<{} [] swap
<<{} [] rollup ? dup bool
abs dup 0 < [] [neg] branch && nulco [nullary [false]] dip branch
anamorphism [pop []] swap [dip swons] genrec || nulco [nullary] dip [true] branch
app1 grba infrst !- 0 >=
app2 [grba swap grba swap] dip [infrst] cons ii <{} [] swap
<<{} [] rollup
abs dup 0 < [] [neg] branch
anamorphism [pop []] swap [dip swons] genrec
app1 grba infrst
app2 [grba swap grba swap] dip [infrst] cons ii
app3 3 appN app3 3 appN
appN [grabN] codi map disenstacken appN [grabN] codi map disenstacken
at drop first at drop first
@ -892,35 +936,36 @@ binary unary popd
ccccons ccons ccons ccccons ccons ccons
ccons cons cons ccons cons cons
clear [] swaack pop clear [] swaack pop
cleave fork popdd cleave fork popdd
clop cleave popdd clop cleave popdd
cmp [[>] swap] dipd [ifte] ccons [=] swons ifte cmp [[>] swap] dipd [ifte] ccons [=] swons ifte
codi cons dip codi cons dip
codireco codi reco codireco codi reco
dinfrirst dip infrst dinfrirst dip infrst
dipd [dip] codi dipd [dip] codi
disenstacken ? [uncons ?] loop pop disenstacken ? [uncons ?] loop pop
down_to_zero [0 >] [dup --] while down_to_zero [0 >] [dup --] while
drop [rest] times drop [rest] times
dupd [dup] dip dupd [dup] dip
dupdd [dup] dipd dupdd [dup] dipd
dupdip dupd dip dupdip dupd dip
dupdipd dup dipd dupdipd dup dipd
enstacken stack [clear] dip enstacken stack [clear] dip
first uncons pop first uncons pop
flatten <{} [concat] step flatten <{} [concat] step
fork [i] app2 fork [i] app2
fourth rest third fourth rest third
gcd true [tuck mod dup 0 >] loop pop gcd true [tuck mod dup 0 >] loop pop
genrec [[genrec] ccccons] nullary swons concat ifte genrec [[genrec] ccccons] nullary swons concat ifte
grabN <{} [cons] times grabN <{} [cons] times
grba [stack popd] dip grba [stack popd] dip
hypot [sqr] ii + sqrt hypot [sqr] ii + sqrt
ifte [nullary] dipd swap branch ifte [nullary] dipd swap branch
ii [dip] dupdip i ii [dip] dupdip i
infra swons swaack [i] dip swaack infra swons swaack [i] dip swaack
infrst infra first infrst infra first
make_generator [codireco] ccons make_generator [codireco] ccons
manual [] words [help] step pop
mod % mod %
neg 0 swap - neg 0 swap -
not [true] [false] branch not [true] [false] branch
@ -929,10 +974,10 @@ nullary [stack] dinfrirst
of swap at of swap at
pam [i] map pam [i] map
pm [+] [-] clop pm [+] [-] clop
popd [pop] dip popd [pop] dip
popdd [pop] dipd popdd [pop] dipd
popop pop pop popop pop pop
popopop pop popop popopop pop popop
popopd [popop] dip popopd [popop] dip
popopdd [popop] dipd popopdd [popop] dipd
product 1 swap [*] step product 1 swap [*] step
@ -942,8 +987,8 @@ range_to_zero unit [down_to_zero] infra
reco rest cons reco rest cons
rest uncons popd rest uncons popd
reverse <{} shunt reverse <{} shunt
roll> swap swapd roll> swap swapd
roll< swapd swap roll< swapd swap
rollup roll> rollup roll>
rolldown roll< rolldown roll<
rrest rest rest rrest rest rest
@ -960,8 +1005,8 @@ stackd [stack] dip
step_zero 0 roll> step step_zero 0 roll> step
stuncons stack uncons stuncons stack uncons
sum [+] step_zero sum [+] step_zero
swapd [swap] dip swapd [swap] dip
swons swap cons swons swap cons
swoncat swap concat swoncat swap concat
sqr dup mul sqr dup mul
tailrec [i] genrec tailrec [i] genrec
@ -970,16 +1015,16 @@ ternary binary popd
third rest second third rest second
tuck dup swapd tuck dup swapd
unary nullary popd unary nullary popd
uncons [first] [rest] cleave uncons [first] [rest] cleave
unit [] cons unit [] cons
unquoted [i] dip unquoted [i] dip
unswons uncons swap unswons uncons swap
while swap nulco dupdipd concat loop while swap nulco dupdipd concat loop
x dup i x dup i
step [_step0] x step [_step0] x
_step0 _step1 [popopop] [_stept] branch _step0 _step1 [popopop] [_stept] branch
_step1 [?] dipd roll< _step1 [?] dipd roll<
_stept [uncons] dipd [dupdipd] dip x _stept [uncons] dipd [dupdipd] dip x
times [_times0] x times [_times0] x
_times0 _times1 [popopop] [_timest] branch _times0 _times1 [popopop] [_timest] branch
_times1 [dup 0 >] dipd roll< _times1 [dup 0 >] dipd roll<