README revision 5dfecf96
15dfecf96Smrg$XFree86: xc/programs/xedit/lisp/README,v 1.12 2002/11/23 08:26:47 paulo Exp $
25dfecf96Smrg
35dfecf96SmrgLAST UPDATED:	$Date: 2008/07/30 04:16:26 $
45dfecf96Smrg
55dfecf96Smrg
65dfecf96Smrg    SUMMARY
75dfecf96Smrg
85dfecf96Smrg  This is a small lisp interpreter for xedit. It implements a subset of
95dfecf96SmrgCommon Lisp and the xedit package implements several of the basic Emacs
105dfecf96Smrglisp functions.
115dfecf96Smrg
125dfecf96Smrg(shared modules not broken, but needs a redesign for better performance,
135dfecf96Smrg but won't be made available in the default build probably for a long time,
145dfecf96Smrg it would be really better to generate the interface dinamically, and/or just
155dfecf96Smrg link agains't the required libraries and use a ffi interface)
165dfecf96Smrg+------------------------------------------------------------------------
175dfecf96Smrg|   It has a very simple method for loading shared modules, slightly based on
185dfecf96Smrg| the XFree86 loader code, that is currently disabled by default. To enable it,
195dfecf96Smrg| edit lisp.cf and change BuildSharedLispModules to YES.
205dfecf96Smrg|
215dfecf96Smrg|   Assuming you have built it with BuildSharedLispModules enabled, you can build
225dfecf96Smrg| a small test application can be built in this directory running "make lsp".
235dfecf96Smrg| Two lisp programs are available in the test directory. To test the programs
245dfecf96Smrg| run "./lsp test/hello.lsp" or "./lsp test/widgets.lsp".
255dfecf96Smrg+------------------------------------------------------------------------
265dfecf96Smrg
275dfecf96Smrg  Currently, it should be used as an helper and/or a small calculator embedded
285dfecf96Smrgin xedit. For the future it should be possible to write entire interfaces
295dfecf96Smrgin the xedit text buffers.
305dfecf96Smrg
315dfecf96Smrg
325dfecf96Smrg    USAGE SUMMARY
335dfecf96Smrg
345dfecf96Smrg  To evaluate lisp expressions, put the text cursor just after the
355dfecf96Smrglisp expression and press:
365dfecf96SmrgC-x,C-e	- will evaluate it, and print the result to the message window
375dfecf96SmrgC-j	- will evaluate it, and print the result to the edit window, any
385dfecf96Smrg	  errors are printed to the message window.
395dfecf96SmrgC-g	- will send an SIGINT to the lisp process, and that process will
405dfecf96Smrg	  stop whatever it was processing and jump to the toplevel,
415dfecf96Smrg	  to wait for more input.
425dfecf96Smrg
435dfecf96SmrgNote that C-j will only work in the *scratch* buffer.
445dfecf96Smrg
455dfecf96Smrg
465dfecf96Smrg     NOTES
475dfecf96Smrg
485dfecf96Smrg  The improvements to xedit including the several possibilites to extend
495dfecf96Smrgthe editor using Lisp are expected to allow making of xedit a versatile
505dfecf96Smrgtext editor for programming, but there is code being (slowly) developed
515dfecf96Smrgthat should also make it useable as a small word processor, for things
525dfecf96Smrglike WYSIWYG html, etc.
535dfecf96Smrg  The xedit development is being done very slowly, maybe it will get
545dfecf96Smrgsomewhere someday, but it is a pet/hobby project, there is no intention
555dfecf96Smrgof making of it an end user editor (the idea is to make it an useful
565dfecf96Smrgdevelopment tool).
575dfecf96Smrg  In some aspects the development is trying to mimic several Emacs
585dfecf96Smrgfeatures, but there is no intention of competition (if xedit ever get
595dfecf96Smrgsomething better than Emacs, I hope that it serves as a motivation to
605dfecf96Smrgmake of Emacs an even better editor), actually it is expected to explore
615dfecf96Smrgdifferent areas and use alternate solutions for the implementation.
625dfecf96Smrg  Most work in a computer is done in a text editor and the more the editor
635dfecf96Smrgcan help the user the better.
645dfecf96Smrg
655dfecf96Smrg
665dfecf96Smrg(debugger is broken and very slow, no prevision for fixing it, but is
675dfecf96Smrg expected to work correctly for interpreted only code)
685dfecf96Smrg+------------------------------------------------------------------------
695dfecf96Smrg|     DEBUGGER
705dfecf96Smrg|
715dfecf96Smrg|   There is a, currently, very simple debugger implement in the interpreter.
725dfecf96Smrg| The debugger is now optional, and off by default. To make it available,
735dfecf96Smrg| you need to recompile with -DDEBUGGER.
745dfecf96Smrg| To use the debugger, run the lsp sample program as "./lsp -d", and optionally
755dfecf96Smrg| pass a second parameter, for the file to be interpreted. Once the debugger
765dfecf96Smrg| prompt is visible, type "help" for a summary of options. To leave the debugger
775dfecf96Smrg| type "continue".
785dfecf96Smrg|   Note that the debugger is still very simple, it won't work from xedit, and
795dfecf96Smrg| won't drop to the debugger on "fatal errors". It allows adding breakpoints to
805dfecf96Smrg| functions and watchpoints to variables. Support for changing data and going to
815dfecf96Smrg| the debugger on fatal errors should be added at some time.
825dfecf96Smrg+------------------------------------------------------------------------
835dfecf96Smrg
845dfecf96Smrg
855dfecf96Smrg    COMPILER
865dfecf96Smrg
875dfecf96Smrg  Now there is a very simple bytecode compiler. It is far from finished, but
885dfecf96Smrgfor simple code can show significant better performance.
895dfecf96Smrg  There is not yet an interface to compile entire files and no interface to
905dfecf96Smrgstore the generated bytecode in disk. There is an interface to bytecode
915dfecf96Smrgcompile toplevel forms as a LAMBDA NIL, but it is not yet exported.
925dfecf96Smrg  If your code needs to call GO/RETURN/RETURN-FROM as the result of an EVAL,
935dfecf96Smrgit must jump to code in the interpreter, after compiling all calls to
945dfecf96SmrgGO/RETURN/RETURN-FROM are just stack adjusting and jumps in the bytecode.
955dfecf96SmrgCATCH/THROW and UNWIND-PROTECT are running as interpreted code for now, so it
965dfecf96Smrgis safe to use these, but code in such blocks is not compiled/optimized
975dfecf96Smrg(not even macro expansion is done, as it understands that while not compiled,
985dfecf96Smrgeverything is candidate to redefinition at any time).
995dfecf96Smrg  To compile the code, just write a function, and compile it, example:
1005dfecf96Smrg
1015dfecf96Smrg	(defun fact (n)
1025dfecf96Smrg	    (if (< n 2)
1035dfecf96Smrg		1
1045dfecf96Smrg		(* n (fact (1- n)))
1055dfecf96Smrg	    )
1065dfecf96Smrg	)
1075dfecf96Smrg	FACT
1085dfecf96Smrg
1095dfecf96Smrg	(compile 'fact)
1105dfecf96Smrg	FACT
1115dfecf96Smrg	NIL
1125dfecf96Smrg	NIL
1135dfecf96Smrg
1145dfecf96Smrg	(disassemble 'fact)
1155dfecf96Smrg	Function FACT:
1165dfecf96Smrg	1 required argument: N
1175dfecf96Smrg	0 optional arguments
1185dfecf96Smrg	0 keyword parameters
1195dfecf96Smrg	No rest argument
1205dfecf96Smrg
1215dfecf96Smrg	Bytecode header:
1225dfecf96Smrg	1 element used in the stack
1235dfecf96Smrg	2 elements used in the builtin stack
1245dfecf96Smrg	0 elements used in the protected stack
1255dfecf96Smrg	Constant 0 = 1
1265dfecf96Smrg	Constant 1 = (2)
1275dfecf96Smrg	Symbol 0 = N
1285dfecf96Smrg	Builtin 0 = *
1295dfecf96Smrg	Builtin 1 = 1-
1305dfecf96Smrg	Builtin 2 = <
1315dfecf96Smrg
1325dfecf96Smrg	Initial stack:
1335dfecf96Smrg	0 = N
1345dfecf96Smrg
1355dfecf96Smrg	Bytecode stream:
1365dfecf96Smrg	   0  LOAD&PUSH (0)
1375dfecf96Smrg	   2  LOADCON&PUSH [1]	    ;  (2)
1385dfecf96Smrg	   4  CALL 2 [2]	    ;  <
1395dfecf96Smrg	   7  JUMPNIL 8
1405dfecf96Smrg	  10  LOADCON [0]	    ;  1
1415dfecf96Smrg	  12  NOOP
1425dfecf96Smrg	  13  JUMP 19
1435dfecf96Smrg	  16  LOAD&PUSH (0)
1445dfecf96Smrg	  18  LOAD&PUSH (0)
1455dfecf96Smrg	  20  CALL 1 [1]	    ;  1-
1465dfecf96Smrg	  23  LET* [0]		    ;  N
1475dfecf96Smrg	  25  LETREC 1
1485dfecf96Smrg	  27  UNLET 1
1495dfecf96Smrg	  29  BCONS1
1505dfecf96Smrg	  30  CALL 1 [0]	    ;  *
1515dfecf96Smrg	  33  RETURN
1525dfecf96Smrg	FACT
1535dfecf96Smrg
1545dfecf96Smrg
1555dfecf96Smrg  There are several optimizations that should be done at some time, I don't
1565dfecf96Smrgthink adding NOOP opcodes will help everywhere to make aligned memory reads
1575dfecf96Smrgof shorts and ints.
1585dfecf96Smrg  It should have explicitly visible registers, not the abstraction of "the
1595dfecf96Smrgcurrent value", so the code generator can choose register allocation for
1605dfecf96Smrgloop control variables, commonly used variables, etc, for example. Jumps
1615dfecf96Smrgshould have 3 types: byte relative, 2 bytes relative and 4 bytes relative.
1625dfecf96SmrgFor now there is only 2 byte relative jumps, byte relative jumps
1635dfecf96Smrgcan show a significant performance increase, but they are disable until
1645dfecf96Smrgit is decided how inlined functions will work, if it just updates the bytecode
1655dfecf96Smrgheader and cut&past the bytecode, jumps must be updated, and some jumps
1665dfecf96Smrgmay not fit anymore in a byte.
1675dfecf96Smrg
1685dfecf96Smrg
1695dfecf96Smrg    OPTIMIZATION
1705dfecf96Smrg
1715dfecf96Smrg  There are plenty of possibilities to make the interpreter run faster. Some
1725dfecf96Smrgoptimizations that can make it run quite faster in certain cases are:
1735dfecf96Smrg  o Better object memory layout and gc. The current memory allocation code
1745dfecf96Smrg    is very bad, it try to keep 3 times more free objects than the currently
1755dfecf96Smrg    used number, this can consume a lot of memory. The reason is to reduce
1765dfecf96Smrg    the gc time cost so that it will in average miss only one in every 4
1775dfecf96Smrg    collect tries.
1785dfecf96Smrg  o Implement real vectors, currently they are just a list, so it cannot
1795dfecf96Smrg    just deference a given index, and gc time is very long also.
1805dfecf96Smrg  o Most lists are never changed once created, it could somehow add an index
1815dfecf96Smrg    field in the cons cell, so that NTH/NTHCDR/LENGTH like code could just
1825dfecf96Smrg    deference the correct object, instead of traversing the CDR of every
1835dfecf96Smrg    cons. This would probably require implementing lists as vectors, while
1845dfecf96Smrg    making it easy to deference would make life harder when deleting/inserting
1855dfecf96Smrg    sublists in a list. It should also better be done in a way that does
1865dfecf96Smrg    not require a lot of objects allocated linearly.
1875dfecf96Smrg
1885dfecf96Smrg
1895dfecf96Smrg    HELPING
1905dfecf96Smrg
1915dfecf96Smrg  Send comments and code to me (paulo@XFree86.Org) or to the XFree86
1925dfecf96Smrgmailing/patch lists.
1935dfecf96Smrg
1945dfecf96Smrg--
1955dfecf96SmrgPaulo
196