系列第三篇確立腳本語言核心規格:非惰性計算、弱型別、支援基本型別/陣列/函數指標、隱式轉型與函數多載。採 Pascal 風格語法避開 C 語言宣告解析複雜度,編譯目標為自製指令集配合虛擬機執行。
unit 單元名;
uses 單元名1, 單元名2, ...;
type
新型別名 = 型別宣告;
...
var
變數群: 型別;
...
interface
公開函數宣告;
implementation
公開與私有函數實作
end.
boolean、integer、real、char、stringarray of 型別type MyPtr = function(a,b:integer):integer;procedure 名(參數) / function 名(參數): 回傳型別var 參考傳遞if-then-else、for-to/downto、while-do、repeat-until、begin-end 區塊continue、break、exit算術:+ - * div mod /(/ 強制回傳 real)
邏輯:and or xor not
陣列/字串索引:expr[index](0-based,引用複製語意)
unit PrimeTest;
uses IO;
interface
function IsPrime(Num: integer): boolean;
implementation
function IsPrime(Num: integer): boolean;
var i: integer;
begin
result := true;
if Num < 2 then result := false
else if Num > 2 then
for i := 2 to Num-1 do
if Num mod i = 0 then result := false;
end;
end.
[RefCount:int][Data] 結構,引用計數實現自動記憶體管理分段式堆疊,每幀包含:參數區、區域變數、暫存區、參數基址、返回位址。函數呼叫/返回時自動維護參考計數與幀指標。
A(x) { result := B(x+1, x-1); }FUNCTION_A:
00 push x; 01 push 1; 02 add; // x+1
03 push x; 04 push 1; 05 sub; // x-1
06 call FUNCTION_B;
07 pushref result; 08 assign; // 儲存返回值
09 ret 1;
FUNCTION_B:
10 push x; 11 push y; 12 mul;
13 pushref result; 14 assign;
15 ret 2;
| 需求 | 建議特性 |
|---|---|
| 宿主頻繁回呼腳本函數 | 閉包或函數指標型別 |
| 宿主 API 需接受多型參數 | 無型別或弱型別 |
| 大型 API 需命名空間/類別組織 | 類別系統 |
| 極致執行速度 | 強/弱型別 + JIT 編譯 |
| 簡單設定腳本 | 命令式、無運算式、無函數 |
具備陣列、閉包、或「結構+指標」即為圖靈完備(能表達遞迴資料結構)。
語法分析與語義分析實作細節。