mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
221 lines
28 KiB
HTML
221 lines
28 KiB
HTML
<?xml version="1.0" encoding="utf-8" ?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<!-- This file is generated by Nim. -->
|
|
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>NimScript</title>
|
|
|
|
<!-- Google fonts -->
|
|
<link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
|
|
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
|
|
|
|
<!-- Favicon -->
|
|
<link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
|
|
<link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
|
|
|
|
<!-- CSS -->
|
|
<link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
|
|
|
|
<!-- JS -->
|
|
<script type="text/javascript" src="dochack.js?v=2.3.1"></script>
|
|
</head>
|
|
<body>
|
|
<div class="document" id="documentId">
|
|
<div class="container">
|
|
<h1 class="title">NimScript</h1>
|
|
<p>Strictly speaking, <tt class="docutils literal"><span class="pre"><span class="Identifier">NimScript</span></span></tt> is the subset of Nim that can be evaluated by Nim's builtin virtual machine (VM). This VM is used for Nim's compiletime function evaluation features.</p>
|
|
<p>The <tt class="docutils literal"><span class="pre"><span class="program">nim</span></span></tt> executable processes the <tt class="docutils literal"><span class="pre">.nims</span></tt> configuration files in the following directories (in this order; later files overwrite previous settings):</p>
|
|
<ol class="simple"><li>If environment variable <tt class="docutils literal"><span class="pre"><span class="Identifier">XDG_CONFIG_HOME</span></span></tt> is defined, <tt class="docutils literal"><span class="pre">$XDG_CONFIG_HOME/nim/config.nims</span></tt> or <tt class="docutils literal"><span class="pre">~/.config/nim/config.nims</span></tt> (POSIX) or <tt class="docutils literal"><span class="pre">%APPDATA%/nim/config.nims</span></tt> (Windows). This file can be skipped with the <tt class="docutils literal"><span class="pre option">--skipUserCfg</span></tt> command line option.</li>
|
|
<li><tt class="docutils literal"><span class="pre">$parentDir/config.nims</span></tt> where <tt class="docutils literal"><span class="pre">$parentDir</span></tt> stands for any parent directory of the project file's path. These files can be skipped with the <tt class="docutils literal"><span class="pre option">--skipParentCfg</span></tt> command line option.</li>
|
|
<li><tt class="docutils literal"><span class="pre">$projectDir/config.nims</span></tt> where <tt class="docutils literal"><span class="pre">$projectDir</span></tt> stands for the project's path. This file can be skipped with the <tt class="docutils literal"><span class="pre option">--skipProjCfg</span></tt> command line option.</li>
|
|
<li>A project can also have a project specific configuration file named <tt class="docutils literal"><span class="pre">$project.nims</span></tt> that resides in the same directory as <tt class="docutils literal"><span class="pre">$project.nim</span></tt>. This file can be skipped with the same <tt class="docutils literal"><span class="pre option">--skipProjCfg</span></tt> command line option.</li>
|
|
</ol>
|
|
<p>For available procs and implementation details see <a class="reference external" href="nimscript.html">nimscript</a>.</p>
|
|
|
|
<h1 id="limitations">Limitations</h1><p>NimScript is subject to some limitations caused by the implementation of the VM (virtual machine):</p>
|
|
<ul class="simple"><li>Nim's FFI (foreign function interface) is not available in NimScript. This means that any stdlib module which relies on <tt class="docutils literal"><span class="pre"><span class="Identifier">importc</span></span></tt> can not be used in the VM.</li>
|
|
<li><tt class="docutils literal"><span class="pre"><span class="Keyword">ptr</span></span></tt> operations are are hard to emulate with the symbolic representation the VM uses. They are available and tested extensively but there are bugs left.</li>
|
|
<li><tt class="docutils literal"><span class="pre"><span class="Keyword">var</span> <span class="Identifier">T</span></span></tt> function arguments rely on <tt class="docutils literal"><span class="pre"><span class="Keyword">ptr</span></span></tt> operations internally and might also be problematic in some cases.</li>
|
|
<li>More than one level of <tt class="docutils literal"><span class="pre"><span class="Keyword">ref</span></span></tt> is generally not supported (for example, the type <tt class="docutils literal"><span class="pre"><span class="Keyword">ref</span> <span class="Keyword">ref</span> <span class="Identifier">int</span></span></tt>).</li>
|
|
<li>Multimethods are not available.</li>
|
|
<li><tt class="docutils literal"><span class="pre"><span class="Identifier">random</span><span class="Operator">.</span><span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> requires an <tt class="docutils literal"><span class="pre"><span class="Identifier">int64</span></span></tt> explicitly passed as argument, you <em>must</em> pass a Seed integer.</li>
|
|
</ul>
|
|
|
|
<h1 id="standard-library-modules">Standard library modules</h1><p>At least the following standard library modules are available:</p>
|
|
<ul class="simple"><li><a class="reference external" href="algorithm.html">algorithm</a></li>
|
|
<li><a class="reference external" href="base64.html">base64</a></li>
|
|
<li><a class="reference external" href="bitops.html">bitops</a></li>
|
|
<li><a class="reference external" href="chains.html">chains</a></li>
|
|
<li><a class="reference external" href="colors.html">colors</a></li>
|
|
<li><a class="reference external" href="complex.html">complex</a></li>
|
|
<li><a class="reference external" href="distros.html">distros</a></li>
|
|
<li><a class="reference external" href="editdistance.html">std/editdistance</a></li>
|
|
<li><a class="reference external" href="htmlgen.html">htmlgen</a></li>
|
|
<li><a class="reference external" href="htmlparser.html">htmlparser</a></li>
|
|
<li><a class="reference external" href="httpcore.html">httpcore</a></li>
|
|
<li><a class="reference external" href="json.html">json</a></li>
|
|
<li><a class="reference external" href="lenientops.html">lenientops</a></li>
|
|
<li><a class="reference external" href="macros.html">macros</a></li>
|
|
<li><a class="reference external" href="math.html">math</a></li>
|
|
<li><a class="reference external" href="options.html">options</a></li>
|
|
<li><a class="reference external" href="os.html">os</a></li>
|
|
<li><a class="reference external" href="parsecfg.html">parsecfg</a></li>
|
|
<li><a class="reference external" href="parsecsv.html">parsecsv</a></li>
|
|
<li><a class="reference external" href="parsejson.html">parsejson</a></li>
|
|
<li><a class="reference external" href="parsesql.html">parsesql</a></li>
|
|
<li><a class="reference external" href="parseutils.html">parseutils</a></li>
|
|
<li><a class="reference external" href="punycode.html">punycode</a></li>
|
|
<li><a class="reference external" href="random.html">random</a></li>
|
|
<li><a class="reference external" href="ropes.html">ropes</a></li>
|
|
<li><a class="reference external" href="setutils.html">std/setutils</a></li>
|
|
<li><a class="reference external" href="stats.html">stats</a></li>
|
|
<li><a class="reference external" href="strformat.html">strformat</a></li>
|
|
<li><a class="reference external" href="strmisc.html">strmisc</a></li>
|
|
<li><a class="reference external" href="strscans.html">strscans</a></li>
|
|
<li><a class="reference external" href="strtabs.html">strtabs</a></li>
|
|
<li><a class="reference external" href="strutils.html">strutils</a></li>
|
|
<li><a class="reference external" href="sugar.html">sugar</a></li>
|
|
<li><a class="reference external" href="unicode.html">unicode</a></li>
|
|
<li><a class="reference external" href="unidecode.html">unidecode</a></li>
|
|
<li><a class="reference external" href="uri.html">uri</a></li>
|
|
<li><a class="reference external" href="wordwrap.html">std/wordwrap</a></li>
|
|
<li><a class="reference external" href="xmlparser.html">xmlparser</a></li>
|
|
</ul>
|
|
<p>In addition to the standard Nim syntax (<a class="reference external" href="system.html">system</a> module), NimScripts support the procs and templates defined in the <a class="reference external" href="nimscript.html">nimscript</a> module too.</p>
|
|
<p>See also:</p>
|
|
<ul class="simple"><li><a class="reference external" href=" https://github.com/nim-lang/Nim/blob/devel/tests/test_nimscript.nims">Check the tests for more information about modules compatible with NimScript</a></li>
|
|
</ul>
|
|
|
|
<h1 id="nimscript-as-a-configuration-file">NimScript as a configuration file</h1><p>A command-line switch <tt class="docutils literal"><span class="pre option">--FOO</span></tt> is written as <tt class="docutils literal"><span class="pre"><span class="Identifier">switch</span><span class="Punctuation">(</span><span class="StringLit">"FOO"</span><span class="Punctuation">)</span></span></tt> in NimScript. Similarly, command-line <tt class="docutils literal"><span class="pre option">--FOO:VAL</span></tt> translates to <tt class="docutils literal"><span class="pre"><span class="Identifier">switch</span><span class="Punctuation">(</span><span class="StringLit">"FOO"</span><span class="Punctuation">,</span> <span class="StringLit">"VAL"</span><span class="Punctuation">)</span></span></tt>.</p>
|
|
<p>Here are few examples of using the <tt class="docutils literal"><span class="pre"><span class="Identifier">switch</span></span></tt> proc:</p>
|
|
<p><pre class="listing"><span class="Comment"># command-line: --opt:size</span>
|
|
<span class="Identifier">switch</span><span class="Punctuation">(</span><span class="StringLit">"opt"</span><span class="Punctuation">,</span> <span class="StringLit">"size"</span><span class="Punctuation">)</span>
|
|
<span class="Comment"># command-line: --define:release or -d:release</span>
|
|
<span class="Identifier">switch</span><span class="Punctuation">(</span><span class="StringLit">"define"</span><span class="Punctuation">,</span> <span class="StringLit">"release"</span><span class="Punctuation">)</span>
|
|
<span class="Comment"># command-line: --forceBuild</span>
|
|
<span class="Identifier">switch</span><span class="Punctuation">(</span><span class="StringLit">"forceBuild"</span><span class="Punctuation">)</span>
|
|
<span class="Comment"># command-line: --hint[Conf]:off or --hint:Conf:off</span>
|
|
<span class="Identifier">switch</span><span class="Punctuation">(</span><span class="StringLit">"hint"</span><span class="Punctuation">,</span> <span class="StringLit">"[Conf]:off"</span><span class="Punctuation">)</span></pre></p>
|
|
<p>NimScripts also support <tt class="docutils literal"><span class="pre option">--</span></tt> templates for convenience, which look like command-line switches written as-is in the NimScript file. So the above example can be rewritten as:</p>
|
|
<p><pre class="listing"><span class="Operator">--</span><span class="Identifier">opt</span><span class="Punctuation">:</span><span class="Identifier">size</span>
|
|
<span class="Operator">--</span><span class="Identifier">define</span><span class="Punctuation">:</span><span class="Identifier">release</span>
|
|
<span class="Operator">--</span><span class="Identifier">forceBuild</span></pre></p>
|
|
<p><strong>Note</strong>: In general, the <em>define</em> switches can also be set in NimScripts using <tt class="docutils literal"><span class="pre"><span class="Identifier">switch</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Operator">--</span></span></tt>, as shown in above examples. Few <tt class="docutils literal"><span class="pre"><span class="Identifier">define</span></span></tt> switches such as <tt class="docutils literal"><span class="pre option">-d:strip</span></tt>, <tt class="docutils literal"><span class="pre option">-d:lto</span></tt> and <tt class="docutils literal"><span class="pre option">-d:lto_incremental</span></tt> cannot be set in NimScripts.</p>
|
|
|
|
<h1 id="nimscript-as-a-build-tool">NimScript as a build tool</h1><p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">task</span></span></tt> template that the <tt class="docutils literal"><span class="pre"><span class="Identifier">system</span></span></tt> module defines allows a NimScript file to be used as a build tool. The following example defines a task <tt class="docutils literal"><span class="pre"><span class="Identifier">build</span></span></tt> that is an alias for the <tt class="docutils literal"><span class="pre option">c</span></tt> command:</p>
|
|
<p><pre class="listing"><span class="Identifier">task</span> <span class="Identifier">build</span><span class="Punctuation">,</span> <span class="StringLit">"builds an example"</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">setCommand</span> <span class="StringLit">"c"</span></pre></p>
|
|
<p>In fact, as a convention the following tasks should be available:</p>
|
|
<table border="1" class="docutils"><tr><th>Task</th><th>Description</th></tr>
|
|
<tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">help</span></span></tt></td><td>List all the available NimScript tasks along with their docstrings.</td></tr>
|
|
<tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">build</span></span></tt></td><td>Build the project with the required backend (<tt class="docutils literal"><span class="pre option">c</span></tt>, <tt class="docutils literal"><span class="pre option">cpp</span></tt> or <tt class="docutils literal"><span class="pre option">js</span></tt>).</td></tr>
|
|
<tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">tests</span></span></tt></td><td>Runs the tests belonging to the project.</td></tr>
|
|
<tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">bench</span></span></tt></td><td>Runs benchmarks belonging to the project.</td></tr>
|
|
</table><p>Look at the module <a class="reference external" href="distros.html">distros</a> for some support of the OS's native package managers.</p>
|
|
|
|
<h1 id="nimble-integration">Nimble integration</h1><p>See the <a class="reference external" href="https://github.com/nim-lang/nimble#readme">Nimble readme</a> for more information.</p>
|
|
|
|
<h1 id="standalone-nimscript">Standalone NimScript</h1><p>NimScript can also be used directly as a portable replacement for Bash and Batch files. Use <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="Identifier">myscript.nims</span></span></tt> to run <tt class="docutils literal"><span class="pre">myscript.nims</span></tt>. For example, installation of Nimble could be accomplished with this simple script:</p>
|
|
<p><pre class="listing"><span class="Identifier">mode</span> <span class="Operator">=</span> <span class="Identifier">ScriptMode</span><span class="Operator">.</span><span class="Identifier">Verbose</span>
|
|
|
|
<span class="Keyword">var</span> <span class="Identifier">id</span> <span class="Operator">=</span> <span class="DecNumber">0</span>
|
|
<span class="Keyword">while</span> <span class="Identifier">dirExists</span><span class="Punctuation">(</span><span class="StringLit">"nimble"</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Identifier">id</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">inc</span> <span class="Identifier">id</span>
|
|
|
|
<span class="Identifier">exec</span> <span class="StringLit">"git clone https://github.com/nim-lang/nimble.git nimble"</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Identifier">id</span>
|
|
|
|
<span class="Identifier">withDir</span> <span class="StringLit">"nimble"</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Identifier">id</span> <span class="Operator">&</span> <span class="StringLit">"/src"</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">exec</span> <span class="StringLit">"nim c nimble"</span>
|
|
|
|
<span class="Identifier">mvFile</span> <span class="StringLit">"nimble"</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Identifier">id</span> <span class="Operator">&</span> <span class="StringLit">"/src/nimble"</span><span class="Operator">.</span><span class="Identifier">toExe</span><span class="Punctuation">,</span> <span class="StringLit">"bin/nimble"</span><span class="Operator">.</span><span class="Identifier">toExe</span></pre></p>
|
|
<p>On Unix, you can also use the shebang <tt class="docutils literal"><span class="pre"><span class="Comment">#!/usr/bin/env nim</span></span></tt>, as long as your filename ends with <tt class="docutils literal"><span class="pre">.nims</span></tt>:</p>
|
|
<p><pre class="listing"><span class="Comment">#!/usr/bin/env nim</span>
|
|
<span class="Identifier">mode</span> <span class="Operator">=</span> <span class="Identifier">ScriptMode</span><span class="Operator">.</span><span class="Identifier">Silent</span>
|
|
|
|
<span class="Identifier">echo</span> <span class="StringLit">"hello world"</span></pre></p>
|
|
<p>Use <tt class="docutils literal"><span class="pre"><span class="Comment">#!/usr/bin/env -S nim e --hints:off</span></span></tt> to disable hints and relax the file extension constraint.</p>
|
|
|
|
<h1 id="benefits">Benefits</h1>
|
|
<h2 id="crossminusplatform">Cross-Platform</h2><p>It is a cross-platform scripting language that can run where Nim can run, e.g. you can not run Batch or PowerShell on Linux or Mac, the Bash for Linux might not run on Mac, there are no unit tests tools for Batch, etc.</p>
|
|
<p>NimScript can detect on which platform, operating system, architecture, and even which Linux distribution is running on, allowing the same script to support a lot of systems.</p>
|
|
<p>See the following (incomplete) example:</p>
|
|
<p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">distros</span>
|
|
|
|
<span class="Comment"># Architectures.</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">defined</span><span class="Punctuation">(</span><span class="Identifier">amd64</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Architecture is x86 64Bits"</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">defined</span><span class="Punctuation">(</span><span class="Identifier">i386</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Architecture is x86 32Bits"</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">defined</span><span class="Punctuation">(</span><span class="Identifier">arm</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Architecture is ARM"</span>
|
|
|
|
<span class="Comment"># Operating Systems.</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">defined</span><span class="Punctuation">(</span><span class="Identifier">linux</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Operating System is GNU Linux"</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">defined</span><span class="Punctuation">(</span><span class="Identifier">windows</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Operating System is Microsoft Windows"</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">defined</span><span class="Punctuation">(</span><span class="Identifier">macosx</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Operating System is Apple OS X"</span>
|
|
|
|
<span class="Comment"># Distros.</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">detectOs</span><span class="Punctuation">(</span><span class="Identifier">Ubuntu</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Distro is Ubuntu"</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">detectOs</span><span class="Punctuation">(</span><span class="Identifier">ArchLinux</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Distro is ArchLinux"</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">detectOs</span><span class="Punctuation">(</span><span class="Identifier">Debian</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"Distro is Debian"</span></pre></p>
|
|
|
|
<h2 id="uniform-syntax">Uniform Syntax</h2><p>The syntax, style, and rest of the ecosystem is the same as for compiled Nim, that means there is nothing new to learn, no context switch for developers.</p>
|
|
|
|
<h2 id="powerful-metaprogramming">Powerful Metaprogramming</h2><p>NimScript can use Nim's templates, macros, types, concepts, effect tracking system, and more, you can create modules that work on compiled Nim and also on interpreted NimScript.</p>
|
|
<p><tt class="docutils literal"><span class="pre"><span class="Keyword">func</span></span></tt> will still check for side effects, <tt class="docutils literal"><span class="pre"><span class="Identifier">debugEcho</span></span></tt> also works as expected, making it ideal for functional scripting metaprogramming.</p>
|
|
<p>This is an example of a third party module that uses macros and templates to translate text strings on unmodified NimScript:</p>
|
|
<p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">nimterlingua</span>
|
|
<span class="Identifier">nimterlingua</span><span class="Punctuation">(</span><span class="StringLit">"translations.cfg"</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"cat"</span> <span class="Comment"># Run with -d:RU becomes "kot", -d:ES becomes "gato", ...</span></pre></p>
|
|
<p>translations.cfg</p>
|
|
<p><pre class="listing">[cat]
|
|
ES = gato
|
|
IT = gatto
|
|
RU = kot
|
|
FR = chat</pre></p>
|
|
<ul class="simple"><li><a class="reference external" href="https://nimble.directory/pkg/nimterlingua">Nimterlingua</a></li>
|
|
</ul>
|
|
|
|
<h2 id="graceful-fallback">Graceful Fallback</h2><p>Some features of compiled Nim may not work on NimScript, but often a graceful and seamless fallback degradation is used.</p>
|
|
<p>See the following NimScript:</p>
|
|
<p><pre class="listing"><span class="Keyword">if</span> <span class="Identifier">likely</span><span class="Punctuation">(</span><span class="Identifier">true</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">discard</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">unlikely</span><span class="Punctuation">(</span><span class="Identifier">false</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">discard</span>
|
|
|
|
<span class="Keyword">proc</span> <span class="Identifier">foo</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">compiletime</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span> <span class="Identifier">echo</span> <span class="Identifier">NimVersion</span>
|
|
|
|
<span class="Keyword">static</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">echo</span> <span class="Identifier">CompileDate</span></pre></p>
|
|
<p><tt class="docutils literal"><span class="pre"><span class="Identifier">likely</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">unlikely</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">static</span><span class="Punctuation">:</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">compiletime</span><span class="Operator">.</span><span class="Punctuation">}</span></span></tt> will produce no code at all when run on NimScript, but still no error nor warning is produced and the code just works.</p>
|
|
|
|
<h2 id="evolving-scripting-language">Evolving Scripting language</h2><p>NimScript evolves together with Nim, <a class="reference external" href=" https://github.com/nim-lang/Nim/pulls?q=nimscript+is%3Amerged">occasionally new features might become available on NimScript</a>, adapted from compiled Nim or added as new features on both.</p>
|
|
|
|
<h2 id="scripting-language-with-a-package-manager">Scripting Language with a Package Manager</h2><p>You can create your own modules to be compatible with NimScript, and check <a class="reference external" href="https://nimble.directory">Nimble</a> to search for third party modules that may work on NimScript.</p>
|
|
|
|
<h2 id="devops-scripting">DevOps Scripting</h2><p>You can use NimScript to deploy to production, run tests, build projects, do benchmarks, generate documentation, and all kinds of DevOps/SysAdmin specific tasks.</p>
|
|
<ul class="simple"><li><a class="reference external" href="https://github.com/kaushalmodi/nim_config#list-available-tasks">An example of a third party NimScript that can be used as a project-agnostic tool.</a></li>
|
|
</ul>
|
|
|
|
|
|
|
|
<div class="twelve-columns footer">
|
|
<span class="nim-sprite"></span>
|
|
<br>
|
|
<small style="color: var(--hint);">Made with Nim. Generated: 2025-12-28 15:54:15 UTC</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
|
|
|
|
</body>
|
|
</html>
|