No description
Find a file
ryukoposting 1745ab4abb Merge branch 'devel' into 'master'
Merge devel into master

See merge request ryukoposting/nim-ladder!2
2019-03-06 00:58:20 +00:00
src more docs, link in nimble description 2019-01-27 20:54:21 -06:00
tests First Commit 2019-01-20 18:24:54 -06:00
.gitlab-ci.yml fix CI 2019-03-05 18:11:36 -06:00
ladder.nimble more docs, link in nimble description 2019-01-27 20:54:21 -06:00
LICENSE Add LICENSE 2019-01-24 02:07:53 +00:00
README.md add docs link to readme 2019-01-27 20:56:29 -06:00

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:

  • contact takes an expression returing a boolean, as well as a code block. any non-coil expression inside a contact will only run if the expression is true. In other words, with the exception of coils, contact works exactly like an if statement.
  • coils are what make ladder logic unique from a basic if statement. coil sets or clears a boolean, and it can only be put inside a contact block. If a contact's expression is false, coil will set the boolean variable given to it to false. If its contact is true, coil will set its given boolean to true. Thus, coil will 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