comment: This file contains the code to implement SWITCH: on FPC (a FORTH for the PC from Tom Zimmer). Usage: SWITCH: Example: SWITCH: motor? stop start This defines 3 words ("motor?", "stop" and "start"). When you call "motor?" it will leave the current state on the stack (initially set to OFF). To switch it on you use "start" and when you want it to switch off you use "stop". What SWITCH: does is basically this, (but in one step): variable (motor) : motor? (motor) @ ; : stop (motor) off ; : start (motor) on ; The advantage over the (explicit) code above is a) you have to type less b) you do not clutter your dictionary with an unneeded word ( "(motor)" ) c) a high level definition like SWITCH: gives more room to optimization, as it can be easily implemented in assembler d) it hides any internals from the programmer, he/she simply uses it. Author: Ulrich Paul, Paul Elektronik, D-86391 Leitershofen This code is protected (copylefted) under the Gnu Public License. You may use and redistribute it, provided you ship it with the source code. Get a copy of the GPL and read the fine print. comment; : switch: ( -- ) ( ) (:) reveal \ enter name of switch, make it visible here \ leave it for the next three def's dup \ we need it again [compile] literal \ compile HERE 0 , \ alloc space of var, set it to off compile @ \ reports the setting compile unnest \ finish definition of the switch (:) reveal \ enter name of off-state (visibly) dup \ the on state needs HERE, too [compile] literal \ compile adr of switch (HERE from above) compile off \ compile code to set it off compile unnest \ finish definition of off-state (:) reveal \ enter name of on-state (visibly) [compile] literal \ compile adr of switch (HERE from above) compile on \ compile code to set it on compile unnest \ finish definition of on-state ;