mirror of
https://gitlab.com/ryukoposting/nim-ladder
synced 2026-01-02 17:54:49 +00:00
No description
|
|
||
|---|---|---|
| src | ||
| tests | ||
| .gitlab-ci.yml | ||
| ladder.nimble | ||
| LICENSE | ||
| README.md | ||
Documentation for ladder is hosted here.
ladder: Ladder Logic Macros for Nim
ladder contains the macro expressions necessary to use ladder logic
in Nim. Ladder logic is a form of logical program expression akin to
if-else, but has only one nested section instead of two. Wikipedia
has a good introduction to ladder logic.
ladder contains two macros- contact and coil. Those familiar with
ladder logic will already know what these do by their name, but to
summarize:
contacttakes an expression returing a boolean, as well as a code block. any non-coil expression inside acontactwill only run if the expression is true. In other words, with the exception of coils,contactworks exactly like an if statement.coils are what make ladder logic unique from a basic if statement.coilsets or clears a boolean, and it can only be put inside acontactblock. If a contact's expression is false,coilwill set the boolean variable given to it to false. If its contact is true,coilwill set its given boolean to true. Thus,coilwill always run when its contact is reached, even if the condition is false.
Ladder logic is rarely implemented in general-purpose programming languages, but is ubiquitous in languages used on industrial programmable logic controllers.
Example
The (somewhat contrived) example below shows a basic Nim program that sets and clears some booleans:
var
x = true
y = false
z = false
if x:
echo "hello, world!"
x = y
echo y # true
z = x and y
if x and y:
echo "true and true is true."
echo z # true
if not z:
echo "this text will not be printed."
x = not z
echo x # false
This is the equivalent program, written using ladder logic:
import ladder
var
x = true
y = false
z = false
contact x:
echo "hello, world!"
coil y
echo y # true
contact(x and y):
coil z
echo "true and true is true."
echo z # true
contact(not z):
echo "this text will not be printed."
coil x
echo x # false