美思 [Nim] 程式設計教學:建置開發環境

Facebook Twitter LinkedIn LINE Skype EverNote GMail Yahoo Email

安裝 Nim

由於 Nim 程式碼會先轉為 C 程式碼後,再從 C 程式碼轉為機械碼,除了 Nim 編譯器以外,還要安裝 C 編譯器。

Windows

下載 Nim 主程式的壓縮檔後,將其移到任意位置,像是 C:\nim-0.17.2 等,筆者習慣保留版本號,日後要更新時直接更換資料夾即可。

不過,Nim 還需要 C/C++ 編譯環境才能轉為可用的執行檔。在 Windows 上建議用 msys2,這套軟體是 GCC (GNU Compiler Collection) 在 Windows 上的移植版本,包括 C 編譯器、相關開發工具、常用的 C 函式庫等。

要在 PATH 變數加入兩個新的位置:

  • Nim 主程式所在的位置之下的 bin 資料夾,例:C:\nim-0.17.2\bin
  • %USERPROFILE%\.nimble\bin (Nim 套件的位置)

Nim 主程式內有一個 finish.exe,可協助使用者自動設定第一個路徑。

類 Unix 系統

比較簡單的方式是透過 choosenim 這個終端機小工具來管理 Nim 版本,透過以下指令安裝此工具:

$ curl https://nim-lang.org/choosenim/init.sh -sSf | sh

choosenim 會收集一些匿名資料,以協助 Nim 的發展,讀者可自行選擇是否要支持 Nim 專案:

choosenim-init: Downloading choosenim-0.3.2_macosx_amd64
    Prompt: Can choosenim record and send anonymised telemetry data? [y/n]
        ... Anonymous aggregate user analytics allow us to prioritise
        ... fixes and features based on how, where and when people use Nim.
        ... For more details see: https://goo.gl/NzUEPf.
    Answer: y

接著,系統會自動編譯 Nim,並出現一些相關的訊息:

Downloading Nim 0.18.0 from nim-lang.org
[##################################################] 100.0% 0kb/s
 Extracting nim-0.18.0.tar.gz
   Building Nim 0.18.0
   Building tools (nimble, nimgrep, nimsuggest)
  Installed component 'nim'
      Hint: Binary 'nim' isn't in your PATH. Add '/Users/apple/.nimble/bin' to your PATH.
  Installed component 'nimble'
      Hint: Binary 'nimble' isn't in your PATH. Add '/Users/apple/.nimble/bin' to your PATH.
  Installed component 'nimgrep'
      Hint: Binary 'nimgrep' isn't in your PATH. Add '/Users/apple/.nimble/bin' to your PATH.
  Installed component 'nimsuggest'
      Hint: Binary 'nimsuggest' isn't in your PATH. Add '/Users/apple/.nimble/bin' to your PATH.
   Switched to Nim 0.18.0
choosenim-init: ChooseNim installed in /Users/apple/.nimble/bin
choosenim-init: You must now ensure that the Nimble bin dir is in your PATH.
choosenim-init: Place the following line in the ~/.profile or ~/.bashrc file.
choosenim-init:     export PATH=/Users/apple/.nimble/bin:$PATH

最後再手動將 Nim 編譯器加入路徑即可。

如果想透過 Nim 原始碼自行編譯也可以,可使用 GCC 或 clang 等 C 編譯器。以下以 0.17.2 版為例:

$ tar -xf nim-0.17.2.tar.xz
$ cd nim-0.17.2
$ sh build.sh
$ bin/nim c koch
$ ./koch tools

將編譯好的主程式所在的整個資料夾移動到任意位置,像 /opt 等。

要在 PATH 變數加入兩個新的位置:

  • Nim 主程式所在的位置之下的 bin 資料夾,例:/opt/nim-0.17.2/bin
  • $HOME/.nimble/bin (Nim 套件的位置)

支援 Nim 的編輯器

常見的編輯器如下:

  • Atom
  • Visual Studio Code
  • Sublime Text
  • Vim
  • Emacs

筆者目前使用 Visual Studio Code,讀者也可以嘗試自己喜好的編輯器。

註:Vim 和 Emacs 較難上手,初學者可先試其他的編輯器。根據 Nim 2017 年調查,大多數 Nim 程式設計者使用 Visual Studio Code。

Hello World

接下來,我們用 Hello World 程式熟悉撰寫 Nim 程式的流程。

使用編輯器建立 hello.nim 檔案,加入以下內容:

echo "Hello World"

使用 Nim 編譯器編譯並執行此程式:

$ nim c --run hello.nim
(省略一些訊息)
Hello World

先編譯再執行 (在類 Unix 平台上):

$ nim c hello.nim
$ ./hello

先編譯再執行 (在 Windows 平台上):

C:\> nim c hello.nim
C:\> .\hello

預設情形下,編譯出來的執行檔會加入一些有助於除錯的訊息;發布正式版本程式時,可將這些訊息移除,並加入一些優化:

$ nim c -d:release hello.nim

除了轉為 C 程式碼以外,也可以轉為 C++、Objective C、JavaScript 程式碼。

轉為 C++ 程式碼:

$ nim cpp hello.nim

轉為 Objective C 程式碼:

$ nim objc hello.nim

轉為於瀏覽器中運行的 JavaScript 程式碼:

$ nim js hello.nim

轉為於 Node.js 環境中運行的 JavaScript 程式碼:

$ nim js -d:nodejs hello.nim

程式碼的編碼

Nim 程式碼一定要用 UTF-8 編碼或其子集,不支援其他編碼。前文所述的編輯器皆支援編碼轉換,可自行查閱各編輯器的說明文件。一般來說,使用英文撰寫程式碼 (包括註解) 比較不會碰到編碼的問題。

註解

註解是給程式設計者看的,不會影響程式的運行。

單行註解

使用 # (井號) 來註解說明文字,在井號後到行尾間的文字都視為註解。

# Print out "Hello World" in console.
echo "Hello World"

多行註解

使用 #[]# 將多行說明文字包起來:

#[Comment here.
Multiple lines
are not a problem.]#

多行註解可內嵌:

#[  #[ Multiline comment in already
  commented out code. ]#
proc p[T](x: T) = discard
]#

縮進

Nim 吸收 Python 的語法,程式碼區塊需要縮進 (identation),目前僅能用空格 (space) 來縮進,不能用 TAB 來縮進;根據 Nim 官方文件,習慣上縮進 2 格空白;若依照 Python 社群的習慣,縮進 4 格空白也可以。

關於作者

身為資訊領域碩士,美思認為開發應用程式的目的是為社會帶來價值。如果在這個過程中該軟體能成為永續經營的項目,那就是開發者和使用者雙贏的局面。

美思喜歡用開源技術來解決各式各樣的問題,但必要時對專有技術也不排斥。閒暇之餘,美思將所學寫成文章,放在這個網站上和大家分享。