在開發Library時,因為各個Forth系統不一定實作Library所用到的符式詞(Forth word),需要用到[IF]...[ELSE]...[THEN]積木適時補上缺漏
以下範例說明,如何選擇哪些段落需要執行,可到JForthBlocks線上試玩版 jforthblocks.appspot.com試玩看看
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
這裡是Jimmy的實驗室用來發表Jimmy的Scratch程式作品,還有LEGO WEDO & NXT機器人設計 & Android, (本部落格引用圖文為教育教學目的合理使用) 想與我(Jimmy的爸爸 )聯絡 jimmyscratchlab@gmail.com
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: 3dup dup dup dup ;
: 4dup dup dup dup dup ;
: 5dup dup dup dup dup dup ;
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
function call(xt){// high level definition inner execution loop
highLevelLooping=true; ip=xt;
do{
var id=dictionary[ip++];
//if(typeof(id)=='object'){var v=id['lit'];stack.push(v);continue;}
........
........
}
}
function exec(src){ //source code interpreting loop
.....
.....
do{token=nexttoken(); word=token;
var id=findword(token);
......
......
if(typeof(n)==='string'||n<=0||n>0){
if(compiling)
compilecode('doLit',n);// compile an literal
//compilecode({lit : n});
......
......
}while(error===0&&ntib<tib.length);}
}
![]() |
寄件者 scratchlab02 |
function call(xt){// high level definition inner execution loop
highLevelLooping=true; ip=xt;
do{
var id=dictionary[ip++];
if(typeof(id)=='object'){var v=id['lit'];stack.push(v);continue;}
........
........
}
}
function exec(src){ //source code interpreting loop
.....
.....
do{token=nexttoken(); word=token;
var id=findword(token);
......
......
if(typeof(n)==='string'||n<=0||n>0){
if(compiling)
//compilecode('doLit',n);// compile an literal
compilecode({lit : n});
......
......
}while(error===0&&ntib<tib.length);}
}
![]() |
寄件者 scratchlab02 |
<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="procedures_defnoreturn" notchtype="Top_Bottom_Right" inline="false" x="137" y="55">
<title name="NAME">TestLiteral</title>
<statement name="STACK">
<block type="math_PushNumber" notchtype="TOP_BOTTOM">
<title name="NUM">5000000</title>
<next>
<block type="control_ForNext" notchtype="Top_Bottom_Right" inline="false">
<statement name="STACK">
<block type="math_PushNumber" notchtype="TOP_BOTTOM_RIGHT" inline="false">
<title name="NUM">0</title>
<value name="NEXTWORD">
<block type="math_PushNumber" notchtype="LEFT_RIGHT" inline="false">
<title name="NUM">0</title>
<value name="NEXTWORD">
<block type="stack_2drop" notchtype="LEFT_RIGHT" inline="false"></block>
</value>
</block>
</value>
<next>
<block type="math_PushNumber" notchtype="TOP_BOTTOM_RIGHT" inline="false">
<title name="NUM">0</title>
<value name="NEXTWORD">
<block type="math_PushNumber" notchtype="LEFT_RIGHT" inline="false">
<title name="NUM">0</title>
<value name="NEXTWORD">
<block type="stack_2drop" notchtype="LEFT_RIGHT" inline="false"></block>
</value>
</block>
</value>
</block>
</next>
</block>
</statement>
</block>
</next>
</block>
</statement>
</block>
<block type="utility_milliseconds" notchtype="LEFT_RIGHT" inline="false" x="89" y="289">
<value name="NEXTWORD">
<block type="procedures_callnoreturn" notchtype="LEFT_RIGHT" inline="false">
<mutation name="TestLiteral"></mutation>
<value name="NEXTWORD">
<block type="utility_milliseconds" notchtype="LEFT_RIGHT" inline="false">
<value name="NEXTWORD">
<block type="stack_swap" notchtype="LEFT_RIGHT" inline="false">
<value name="NEXTWORD">
<block type="math_Minus" notchtype="LEFT_RIGHT" inline="false">
<value name="NEXTWORD">
<block type="print_DotS" notchtype="LEFT_RIGHT" inline="false"></block>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</xml>
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: const create , 0 , does> @ >r r> ; 0 const a 1000 const b see const see a see b create cc 11 , 22 , see cc ' const ' a ' b ' cc a b .s
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
' * .s 7 5 rot execute .
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: test begin dup 1- dup 0 = until ; 10 test .s
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: test 10 0 do i loop ; test .s
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: test 0 0 ?do i loop ; test .s
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: test 99 0 do i 3 +loop ; test .s
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: test if s" TOS is non-zero" then ; 1 test .s
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: test if s" TOS is non-zero" then ; 0 test .s
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: test if s" TOS is non-zero." else s" TOS is zero." then ; 1 test 0 test .s執行exit積木,Forth將從目前的高階積木離開,此動作會清掉返回堆疊(Return Stack)頂端, 所以若高階積木有用到>r積木將資料暫存於返回堆疊(Return Stack),則需在執行exit積木之前, 先執行r>積木將暫存資料從返回堆疊(Return Stack)移回資料堆疊(Data Stack)
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
: test dup 1 = if drop s" one" exit then 2 = if s" two" exit then ; 2 test .s
準備畫紙 dup dup s" 魒" swap s" https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVYD_pvecdEHosP10xCBcjK4N1RtCEfVB-BAACzD-ncI6VBDTPkz32Z3gF05Gz5LvgS9PcOAfMyfIxR4FsV0sKEgbrktTLUSBKjLzBgdqdkACqH-ClBp3njpahDhUa-C2rYpz7s937zOoI/s435/Screenshot-10.png" 160 120 320 240 貼圖 swap s" 魒" swap 320 240 200 畫圓 s" a={fill: '#000', 'fill-opacity': .5, 'stroke-width': 5}" javaScript 設圖形屬性 swap over over dup 產生群組 s" 魒" swap rot dup dup s" 魒" swap 24.833 26.917 26.667 畫圓 dup s" a={stroke: '#ccc', fill: '#fff', 'fill-opacity': .4, 'stroke-width': 2}" javaScript 設圖形屬性 rot rot s" 魒" swap s" M12.582,9.551C3.251,16.237,0.921,29.021,7.08,38.564l-2.36,1.689l4.893,2.262l4.893,2.262l-0.568-5.36l-0.567-5.359l-2.365,1.694c-4.657-7.375-2.83-17.185,4.352-22.33c7.451-5.338,17.817-3.625,23.156,3.824c5.337,7.449,3.625,17.813-3.821,23.152l2.857,3.988c9.617-6.893,11.827-20.277,4.935-29.896C35.591,4.87,22.204,2.658,12.582,9.551z" 畫線 dup s" a={stroke: 'none', fill: '#000'}" javaScript 設圖形屬性 swap s" 魒" swap 24.833 26.917 26.667 畫圓 dup s" a={fill: '#fff', opacity: 0}" javaScript 設圖形屬性 同一群組 20 150 平移 1 jsArrayItem value butt1[2] over value img 30 value angle : 旋轉 s" 魒" img s" 魒" s" transform" angle -30 + to angle angle s" r" swap strconcat s" ,320,240" strconcat jsNewObj 1000 s" <>" 動作特效 drop selfid ; 旋轉 butt1[2] swap setEventHandler click 往最前移 .s這是Jimmy's papa第一次拉了那麼多積木,完成後頭昏腦脹,不知道是否動用到平常不用的95%腦細胞的關係,呵呵!!!
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |
![]() |
寄件者 scratchlab02 |