Compile-time calculation: Difference between revisions

Added XLISP
(J: try to compensate for mediawiki/php mishandling of spam issue)
(Added XLISP)
Line 974:
main = constant <'3628800',''>
</pre>
 
 
=={{header|XLISP}}==
Macros can be used to evaluate expressions at compile time:
<lang lisp>(defmacro f10-at-compile-time () (* 2 3 4 5 6 7 8 9 10))</lang>
If the expression is <i>quoted</i>, however, it is <i>not</i> evaluated—it is inserted 'as is', and will be evaluated at run time:
<lang lisp>(defmacro f10-at-run-time () '(* 2 3 4 5 6 7 8 9 10))</lang>
To show what is going on, first start a REPL and define little functions that just invoke each macro:
<lang lisp>[1] (defun test-f10-ct () (f10-at-compile-time))
 
TEST-F10-CT
[2] (defun test-f10-rt () (f10-at-run-time))
 
TEST-F10-RT</lang>
Then use <tt>DECOMPILE</tt> to examine the bytecode generated for each function. First, the one where the calculation was performed at compile time:
<pre>[3] (decompile test-f10-ct)
 
TEST-F10-CT:0000 12 00 ARGSEQ 00 ; ()
TEST-F10-CT:0002 04 03 LIT 03 ; 3628800
TEST-F10-CT:0004 0d RETURN
()</pre>
Here, 10! is included as the literal number 3628800. By contrast, if we decompile the function that uses the <tt>F10-AT-RUN-TIME</tt> macro:
<pre>[4] (decompile test-f10-rt)
 
TEST-F10-RT:0000 12 00 ARGSEQ 00 ; ()
TEST-F10-RT:0002 04 03 LIT 03 ; 10
TEST-F10-RT:0004 10 PUSH
TEST-F10-RT:0005 04 04 LIT 04 ; 9
TEST-F10-RT:0007 10 PUSH
TEST-F10-RT:0008 04 05 LIT 05 ; 8
TEST-F10-RT:000a 10 PUSH
TEST-F10-RT:000b 04 06 LIT 06 ; 7
TEST-F10-RT:000d 10 PUSH
TEST-F10-RT:000e 04 07 LIT 07 ; 6
TEST-F10-RT:0010 10 PUSH
TEST-F10-RT:0011 04 08 LIT 08 ; 5
TEST-F10-RT:0013 10 PUSH
TEST-F10-RT:0014 04 09 LIT 09 ; 4
TEST-F10-RT:0016 10 PUSH
TEST-F10-RT:0017 04 0a LIT 0a ; 3
TEST-F10-RT:0019 10 PUSH
TEST-F10-RT:001a 04 0b LIT 0b ; 2
TEST-F10-RT:001c 10 PUSH
TEST-F10-RT:001d 05 0c GREF 0c ; *
TEST-F10-RT:001f 0c 09 TCALL 09
()</pre>
we see that it includes the instructions necessary to find the answer but not the answer itself.
 
=={{header|XPL0}}==
519

edits