From 87845d69835cc74cb4c7b7d16869b1e5ee481908 Mon Sep 17 00:00:00 2001 From: henesy Date: Mon, 25 Feb 2019 17:42:16 -0600 Subject: add constants example --- Constants/README.md | 38 ++++++++++++++++++++++++++++++++++++++ Constants/const.b | 26 ++++++++++++++++++++++++++ README.md | 1 + 3 files changed, 65 insertions(+) create mode 100644 Constants/README.md create mode 100644 Constants/const.b diff --git a/Constants/README.md b/Constants/README.md new file mode 100644 index 0000000..a167fb3 --- /dev/null +++ b/Constants/README.md @@ -0,0 +1,38 @@ +# Constants + +Limbo does not have enum types, but does have the `con` keyword to indicate a constant value. + +## Source + +### const.b:13,14 + + n: con 7; + Red, Green, Blue: con iota; + +The variable `n` is an integer constant with a value of 7. + +The variables `Red`, `Green`, and `Blue` are integer constants with values 0, 1, and 2, respectively. + +By default, the `iota` operator initializes variables on the left side with sequential values from 0 to n. Where n is the number of variables. The `iota` operator is only valid in expressions involving `con`. + +Note: these variables are bound within the scope of this module's file. + +### const.b:19 + + s: con "Limbo"; + +The variable `s` is a string constant which is bound within the scope of the `init()` function. + +## Demo + + ; limbo const.b + ; const + 7 + Red: 0 Green: 1 Blue: 2 + Limbo + ; + +## Exercises + +- Try to declare constants of other types, does `con` allow you to do this? +- Change the `iota` statement such that the values of Red, Green, Blue are not increments of 1 and do not start at 0. diff --git a/Constants/const.b b/Constants/const.b new file mode 100644 index 0000000..2868ec7 --- /dev/null +++ b/Constants/const.b @@ -0,0 +1,26 @@ +implement Const; + +include "sys.m"; +include "draw.m"; + +sys: Sys; +print: import sys; + +Const: module { + init: fn(nil: ref Draw->Context, nil: list of string); +}; + +n: con 7; +Red, Green, Blue: con iota; + +init(nil: ref Draw->Context, nil: list of string) { + sys = load Sys Sys->PATH; + + s: con "Limbo"; + + print(string n + "\n"); + print("Red: %d Green: %d Blue: %d\n", Red, Green, Blue); + print(s + "\n"); + + exit; +} diff --git a/README.md b/README.md index 84d2cb5..0462875 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ You could then run said file with: ## Index [Hello World](./HelloWorld) +[Values](./Values) ## References -- cgit v1.2.3