View Full Version : the use of [ ] ['] and LITERAL
Can somebody explain the correct use of [ ] ['] and LITERAL. when use them and why? and what means "turn the compiler on/off"?
regards
RMDumse
02-25-04, 02:16 PM
We'll start with the Glossary entries:
[ ( --- )
Places the system into interpret state to ex-
ecute non-immediate word/s during compilation.
['] ( --- addr ), ( --- )
Returns and compiles the parameter field pointer address of
a word in a colon-definition.
] ( --- )
Places the system into compilation state. ]
places a non-zero value into the user variable
STATE.
So it's pretty simple for [ and ]. Basically, if you are compiling along in a definition, and want to go out of compilation and use the language for a calculator, you can. Just get out with [ and when your done return to the compilation with ]
Now there's some security on the stack, so you can't imbalance the stack, but you can put something on the stack before the ] and then tell the language to stick a value from the stack into a literal which will cause this constant to appear on the stack in execution. Walla:
: X 2 3 * 6 + ; ( compiles the 2, the 3, the multiple, the 6 and the + and SEMIS
: X [ 2 3 * 6 + ] LITERAL ; ( computes 2x3+6 compiles only 12 as literal
So using [ and ] are faster at run time.
Now what if we want to compile the address of a word, but not have it executed. Well, that is the words CFA. That's the same addess shown in the WORDS list. But, you don't want to do a ' on a string at run time. So how can you get the address of a word's CFA without doing the ' or WORDS? Get it at compile time and treat it like a literal. So ['] is something like; [ stop compiling, ' tick the word, ] and compile it as a literal. One gotcha: since we use a slightly different model than other Forths, we have a pointer to the parameter field, instead of the actual CFA. So we have another word that converts the PFAPTR to the CFA (called CFA curiously enough).
For an example:
IsoMax V0.6
HEX OK
' WORDS CFA . 5DA6 OK
: SHOW-WORDS-DEF ['] WORDS CFA 20 PDUMP ; OK
SHOW-WORDS-DEF
5DA6 9B 11A4 D2E 5DE7 130D 1313 11A4 5951
5DAE 10FC D41 D40 F D20 5D31 D2E 5D13
5DB6 D2E DB 116B D40 3 D20 5D89 5D3A
5DBE D0C FFF0 1073 6 2D 2D 2D 2D OK
5DA6 EXECUTE
----- COMMON -----
7E11 SHOW-WORDS-DEF 5604 TASK 6432 LOOPINDEX 642E LOOPINDEXES
63E4 ADCS 63DF ADC7 63DA ADC6 63D5 ADC5
63D0 ADC4 63CB ADC3 63C6 ADC2 63C1 ADC1
63BC ADC0 6367 SCIS 6362 SCI1 635D SCI0
631A SPI 6315 SPI0 624A TIMERS 6245 TD2
6240 TD1 623B TD0 6236 TC3 6231 TC2
622C TC1 6227 TC0 6222 TB3 621D TB2
6218 TB1 6213 TB0 620E TA3 6209 TA2
6204 TA1 61FF TA0 60E9 PWMIN 60E4 ISB2
60DF ISB1 60DA ISB0 60D5 FAULTB3 60D0 FAULTB2
60CB FAULTB1 60C6 FAULTB0 60C1 ISA2
( --- SPACE to proceed ESC to abort. --- )
Thanks a lot, now it is clear, I apreciate your help.
Regards
vBulletin v3.0.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.