luna-lib 1.3.0

a light weight interpreted programming language
Documentation
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="../../img/favicon.ico">
    <title>Statements &mdash; Luna Docs</title>
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:400,700">
    <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/tonsky/FiraCode@1.206/distr/fira_code.css">
    <link rel="stylesheet" href="//use.fontawesome.com/releases/v5.8.1/css/all.css">
    <link rel="stylesheet" href="//use.fontawesome.com/releases/v5.8.1/css/v4-shims.css">
    <link rel="stylesheet" href="../../css/theme.css">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
    <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
    <script>
        hljs.initHighlightingOnLoad();
    </script> 
</head>

<body ontouchstart="">
    <div id="container">
        <aside>
            <div class="home">
                <div class="title">
                    <button class="hamburger"></button>
                    <a href="../.." class="site-name"> Luna Docs</a>
                </div>
                <div class="search">
                    <div role="search">
    <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
        <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
    </form>
</div>
                </div>
            </div>
            <nav class="nav">
                <ul class="root">
                    <li class="toctree-l1"><a class="nav-item" href="../..">Home</a><a class="nav-item" href="../..">Home</a></li>
                    <li class="toctree-l1"><button class="section nav-item">Getting Started</button>
<ul class="subnav">
    <li class="toctree-l2"><a class="nav-item" href="../../getting_started/installation/">Installation</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../getting_started/environment/">Environment</a></li>
</ul></li>
                    <li class="toctree-l1"><button class="section nav-item">Guide</button>
<ul class="subnav">
    <li class="toctree-l2"><a class="nav-item" href="../grammar/">Grammar</a></li>
    <li class="toctree-l2 current"><a class="nav-item current" href="./">Statements</a>
<ul class="subnav">
<li class="toctree-l3"><a class="nav-item toc" href="#chunk">Chunk</a></li>
<li class="toctree-l3"><a class="nav-item toc" href="#block">Block</a></li>
<li class="toctree-l3"><a class="nav-item toc" href="#statement">Statement</a></li>
</ul></li>
    <li class="toctree-l2"><a class="nav-item" href="../expressions/">Expressions</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../basic-values/">Basic Values</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../variables/">Variables</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../objects/">Objects</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../functions/">Functions</a></li>
</ul></li>
                    <li class="toctree-l1"><button class="section nav-item">Standard Modules</button>
<ul class="subnav">
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/globals/">Globals</a></li>
    <li class="toctree-l2"><button class="section nav-item hide">Type Modules</button>
<ul class="subnav hide">
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/int_module/">Int Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/float_module/">Float Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/bool_module/">Bool Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/char_module/">Char Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/string_module/">String Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/vector_module/">Vector Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/object_module/">Object Module</a></li>
</ul></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/math/">Math</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/io/">IO</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/fs/">FS</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/env/">Env</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/net/">Net</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/os/">OS</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/typed/">Typed</a></li>
</ul></li>
                </ul>
            </nav>
            <div class="repo">
    <div class="previous"><a href="../grammar/">&laquo; Previous</a></div>
    <div class="next"><a href="../expressions/">Next &raquo;</a></div>
</div>
        </aside>
        <div id="spacer"><button class="arrow"></button></div>
        <main>
            <div class="home-top">
                <button class="hamburger"></button>
                <a href="../.." class="site-name"> Luna Docs</a>
            </div>
            <div id="main">
                <nav class="breadcrumbs">
<ul>
    <li>Guide</li>
</ul>
</nav>
                <div id="content"><h1 id="statements">Statements</h1>
<p>A Luna program is basically a list of statements, called a <strong>Chunk</strong>.</p>
<h2 id="chunk">Chunk</h2>
<p>A list of statements with no seperator</p>
<h2 id="block">Block</h2>
<p>Just a list of statements like a <a href="#chunk">Chunk</a>, but it requires <code>{</code> at the start and <code>}</code> at the end.</p>
<h2 id="statement">Statement</h2>
<p>A statement has a lot of variants, like let-bindings, function definitions, etc. But it also can as well just be a <a href="#block">Block</a></p>
<h3 id="let-binding">Let Binding</h3>
<p>This is what you define local variables with. The assignees can be simple identifiers or object/vector destructurings.</p>
<p>Examples: <code>let a = 1</code>, <code>let { name, age } = person</code>, <code>let [first, second, third] = numbers</code></p>
<h3 id="let-else-binding">Let-Else Binding</h3>
<p>Like a <a href="#let-binding">Let Binding</a>, except if the assigned value is <code>null</code>, the else block will be executed</p>
<pre><code>let name = person.name else {
    print(&quot;no name :(&quot;)
    return null
}
</code></pre>
<h3 id="assign">Assign</h3>
<p>This is how you assign new values to existing variables. The assignees can be simple identifiers or a field/index into an object/vector</p>
<p>Examples: <code>a = 2</code></p>
<h3 id="assign-operation">Assign Operation</h3>
<p>Written the same way as <a href="#assign">Assign</a> expect with any of the following assign operators, only one assignee and only one expression:</p>
<table>
<thead>
<tr>
<th>assign operators</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>+=</code></td>
</tr>
<tr>
<td><code>-=</code></td>
</tr>
<tr>
<td><code>*=</code></td>
</tr>
<tr>
<td><code>/=</code></td>
</tr>
<tr>
<td><code>^=</code></td>
</tr>
<tr>
<td><code>%=</code></td>
</tr>
</tbody>
</table>
<p>Examples: <code>a += 1</code>, <code>b *= 2</code></p>
<h3 id="call">Call</h3>
<p>This is to call functions with arguments. The callee can either be a simple identifier or a field/index of an object/vector.</p>
<p>Examples: <code>print("Hello, World!")</code>, <code>self.add(1)</code></p>
<h3 id="self-call">Self Call</h3>
<p>Just like a <a href="#call">Call</a> only written with a <code>:</code> followed by an identifier. This calls the function with the first argument being the value before the <code>:</code>. This is used to call objects with themselfs for object-oriented design patterns.</p>
<p>Examples: <code>vector:push(1)</code>, <code>person:greet()</code></p>
<h3 id="function-definition">Function Definition</h3>
<p>This is used to <strong>globally</strong> define functions with parameters.</p>
<p>Examples:</p>
<pre><code>fn add(a, b) {
    return a + b
}
</code></pre>
<h3 id="let-function-definition">Let-Function Definition</h3>
<p>This is used to <strong>locally</strong> define functions with parameters in the current scope.</p>
<p>Examples:</p>
<pre><code>let fn add(a, b) {
    return a + b
}
</code></pre>
<h3 id="if-branch">If-Branch</h3>
<p>Classical if-branch like any other. Has an optional else-case which can either be a block or another if-branch.</p>
<p>Examples:</p>
<pre><code>if cond {
    print(&quot;yay&quot;)
}
</code></pre>
<pre><code>if cond {
    print(&quot;yay&quot;)
} else {
    print(&quot;nay&quot;)
}
</code></pre>
<pre><code>if cond {
    print(&quot;yay&quot;)
} else if cond2 {
    print(&quot;semi-yay&quot;)
}
</code></pre>
<h3 id="if-branch_1">If-Branch</h3>
<p>Assignes the right side of the equal to the parameter on the left and only enters the branch, if the value is not <code>null</code>.</p>
<p>Examples:</p>
<pre><code>if let name = person.name {
    print(name)
}
</code></pre>
<pre><code>if let name = person.name {
    print(name)
} else {
    print(&quot;anonymous&quot;)
}
</code></pre>
<pre><code>if let name = person.name {
    print(name)
} else if let nickname = person.nickname {
    print(nickname)
}
</code></pre>
<h3 id="match">Match</h3>
<p>Classical match-case statement to check over multiple variants of one value.</p>
<p>Examples:</p>
<pre><code>match cmd {
    &quot;exit&quot; =&gt; { os.exit() }
    cmd if cmd:len() &gt; 0 =&gt; {
        print(&quot;invalid command: %s&quot;:format(cmd))
    }
    cmd =&gt; {}
}
</code></pre>
<h3 id="while-loop">While-Loop</h3>
<p>Classical while-loop like any other. body has to be a block</p>
<p>Examples:</p>
<pre><code>while a &lt; 10 {
    a += 1
}
</code></pre>
<h3 id="while-let-loop">While-Let-Loop</h3>
<p>Assigns the right side of the equals to parameter on the left side. If it is <code>null</code>, the loop is exited. </p>
<p>Examples:</p>
<pre><code>while let n = range:next() {
    print(n)
}
</code></pre>
<h3 id="for-loop">For-Loop</h3>
<p>Python-like for-loop that requires an iterable to be right of the <code>in</code> keyword. Only one variable can be given for each iteration. An iterable can either be an user-object with a <code>next</code> method, an object with the meta variable <code>__next</code> which is a function, or just a function. </p>
<p>Examples:</p>
<pre><code>for n in [1, 2, 3]:iter() {
    print(n)
}
</code></pre>
<pre><code>for key in keys(map) {
    print(key, map[key])
}
</code></pre>
<h3 id="return">Return</h3>
<p>This is used to return a value to the caller and end execution of the current function. </p>
<p>Examples: <code>return a + b</code>, <code>return null</code></p>
<h3 id="break">Break</h3>
<p>This is used to break out of any loop.</p>
<p>Examples:</p>
<pre><code>while true {
    if a &gt;= 10 {
        break
    }
    a += 1
}
</code></pre>
<h3 id="continue">Continue</h3>
<p>This is used to continue to the next iteration of the loop.</p>
<p>Examples:</p>
<pre><code>for n in numbers:iter() {
    if n == 5 {
        continue
    }
    print(n)
}
</code></pre></div>
                <footer>
    <div class="footer-buttons">
        <div class="previous"><a href="../grammar/" title="Grammar"><span>Previous</span></a></div>
        <div class="next"><a href="../expressions/" title="Expressions"><span>Next</span></a></div>
    </div>
    <div class="footer-note">
        <p>
            Built with <a href="http://www.mkdocs.org">MkDocs</a> using
            <a href="https://github.com/daizutabi/mkdocs-ivory">Ivory theme</a>.
        </p>
    </div>
</footer>
            </div>
        </main>
    </div>
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js"></script>
    <script src="../../search/main.js"></script>
</body>

</html>