- Rexx 教程
- Rexx - 首頁
- Rexx - 概述
- Rexx - 環境
- Rexx - 安裝
- Rexx - 外掛安裝
- Rexx - 基本語法
- Rexx - 資料型別
- Rexx - 變數
- Rexx - 運算子
- Rexx - 陣列
- Rexx - 迴圈
- Rexx - 決策
- Rexx - 數字
- Rexx - 字串
- Rexx - 函式
- Rexx - 棧
- Rexx - 檔案I/O
- Rexx - 檔案函式
- Rexx - 子程式
- Rexx - 內建函式
- Rexx - 系統命令
- Rexx - XML
- Rexx - Regina
- Rexx - 解析
- Rexx - 訊號
- Rexx - 除錯
- Rexx - 錯誤處理
- Rexx - 面向物件
- Rexx - 可移植性
- Rexx - 擴充套件函式
- Rexx - 指令
- Rexx - 實現
- Rexx - Netrexx
- Rexx - Brexx
- Rexx - 資料庫
- 手持裝置和嵌入式系統
- Rexx - 效能
- Rexx - 最佳程式設計實踐
- Rexx - 圖形使用者介面
- Rexx - Reginald
- Rexx - Web程式設計
- Rexx 有用資源
- Rexx - 快速指南
- Rexx - 有用資源
- Rexx - 討論
Rexx - 基本語法
為了理解 Rexx 的基本語法,讓我們先來看一個簡單的 Hello World 程式。
示例
/* Main program */ say "Hello World"
可以看到 Hello World 程式是多麼簡單。它只是一行簡單的指令碼,用於執行 Hello World 程式。
關於上述程式,需要注意以下幾點:
say 命令用於將值輸出到控制檯。
/* */ 用於 Rexx 中的註釋。
上述程式的輸出將是:
Hello World
語句的通用形式
在 Rexx 中,讓我們看看程式的通用形式。請看下面的例子。
/* Main program */ say add(5,6) exit add: parse arg a,b return a + b
上述程式的輸出將是:
11
讓我們回顧一下我們從上述程式中瞭解到的內容:
Add 是一個定義為將兩個數字相加的函式。
在主程式中,5 和 6 的值用作 add 函式的引數。
exit 關鍵字用於退出主程式。這用於區分主程式和 add 函式。
add 函式用“:”符號區分。
parse 語句用於解析傳入的引數。
最後,return 語句用於返回數值的和。
子程式和函式
在 Rexx 中,程式碼通常被分成子程式和函式。子程式和函式用於將程式碼分成不同的邏輯單元。子程式和函式之間的關鍵區別在於函式返回值,而子程式不返回值。
以下是子程式和函式在加法實現方面的關鍵區別示例:
函式實現
/* Main program */ say add(5,6) exit add: parse arg a,b return a + b
子程式實現
/* Main program */ add(5,6) exit add: parse arg a,b say a + b
兩個程式的輸出都將是值 11。
執行命令
Rexx 可以用作各種基於命令的系統的控制語言。Rexx 在這些系統中執行命令的方式如下:當 Rexx 遇到既不是指令也不是賦值的程式行時,它會將該行視為一個要計算然後傳遞給環境的字串表示式。
例如:
示例
/* Main program */ parse arg command command "file1" command "file2" command "file3" exit
此程式中的三行類似程式碼都是字串表示式,它將檔名(包含在字串常量中)新增到命令名(作為引數給出)。生成的字串將傳遞給環境,作為命令執行。命令完成後,“rc”變數將設定為命令的退出程式碼。
上述程式的輸出如下:
sh: file1: command not found
3 *-* command "file1"
>>> " file1"
+++ "RC(127)"
sh: file2: command not found
4 *-* command "file2"
>>> " file2"
+++ "RC(127)"
sh: file3: command not found
5 *-* command "file3"
>>> " file3"
+++ "RC(127)"
Rexx 中的關鍵字
REXX 的自由語法意味著某些符號在特定上下文中保留供語言處理器使用。
在特定的指令中,某些符號可能被保留用於分隔指令的各個部分。這些符號被稱為關鍵字。REXX 關鍵字的示例包括DO 指令中的 WHILE 和THEN(在這種情況下充當子句終止符)在IF 或 WHEN 子句之後。
除了這些情況外,只有作為子句中第一個標記的簡單符號,並且不後跟“=”或“:”,才會檢查其是否為指令關鍵字。您可以在子句中的其他地方自由使用這些符號,而不會將其視為關鍵字。
Rexx 中的註釋
註釋用於記錄您的程式碼。單行註釋透過在該行的任何位置使用 /* */ 來標識。
例如:
/* Main program */ /* Call the add function */ add(5,6) /* Exit the main program */ exit add: /* Parse the arguments passed to the add function */ parse arg a,b /* Display the added numeric values */ say a + b
註釋也可以寫在程式碼行之間,如下面的程式所示:
/* Main program */ /* Call the add function */ add(5,6) /* Exit the main program */ exit add: parse /* Parse the arguments passed to the add function */ arg a,b /* Display the added numeric values */ say a + b
上述程式的輸出將是:
11
您也可以在註釋中包含多行,如下面的程式所示:
/* Main program The below program is used to add numbers Call the add function */ add(5,6) exit add: parse arg a,b say a + b
上述程式的輸出將是:
11