From be5690bf05707107bc6f02005af82399b0c1c655 Mon Sep 17 00:00:00 2001 From: seh Date: Mon, 18 Mar 2019 03:44:58 -0500 Subject: add work on modules example --- Modules/README.md | 33 +++++++++++++++++++++++++++++++++ Modules/mkfile | 10 ++++++++++ Modules/modules.b | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ Modules/persons.b | 22 ++++++++++++++++++++++ Modules/persons.m | 11 +++++++++++ Modules/towns.b | 22 ++++++++++++++++++++++ Modules/towns.m | 14 ++++++++++++++ 7 files changed, 160 insertions(+) create mode 100644 Modules/README.md create mode 100644 Modules/mkfile create mode 100644 Modules/modules.b create mode 100644 Modules/persons.b create mode 100644 Modules/persons.m create mode 100644 Modules/towns.b create mode 100644 Modules/towns.m (limited to 'Modules') diff --git a/Modules/README.md b/Modules/README.md new file mode 100644 index 0000000..93715d7 --- /dev/null +++ b/Modules/README.md @@ -0,0 +1,33 @@ +# Modules + +Limbo supports compartmentalization of functionality through the dynamic loading and unloading of names and definitions through modules. + +Disclaimer: At the time of writing I am not exceptionally well-versed with modules in Limbo. All assertions should be taken with a grain of salt. + +## Source + +### + + + +## Demo + + ; modules + 0 + 0 + 1 + 0 + Name: Mars + Size: 2 + Members: + → Spike + → Ed + ; + +## Exercises + +- Can you access `persons->population` from `modules`? +- Could you make a global variable by placing said variable in its respective module definition? +- What happens if you remove the `import` statements for `Person` and `Town` in various `.b` files? +- What happens if you include `persons.m` in `modules.b`? +- What happens if you include `persons.m` in `modules.b` and remove the include for `persons.m` in `towns.m`? diff --git a/Modules/mkfile b/Modules/mkfile new file mode 100644 index 0000000..f5bdae5 --- /dev/null +++ b/Modules/mkfile @@ -0,0 +1,10 @@ +Context, nil: list of string); +}; + +init(nil: ref Draw->Context, nil: list of string) { + sys = load Sys Sys->PATH; + + persons = load Persons "./persons.dis"; + towns = load Towns "./towns.dis"; + + persons->init(); + towns->init(); + + print("%d\n", persons->getpop()); + print("%d\n", towns->persons->getpop()); + + p := persons->mkperson(); + p.name = "Spike"; + p.age = 27; + + print("%d\n", persons->getpop()); + print("%d\n", towns->persons->getpop()); + + t := towns->mktown(); + t.pop = array[] of {p, ref Person(13, "Ed")}; + t.name = "Mars"; + + print("%s\n", t.stringify()); + + exit; +} diff --git a/Modules/persons.b b/Modules/persons.b new file mode 100644 index 0000000..1e64578 --- /dev/null +++ b/Modules/persons.b @@ -0,0 +1,22 @@ +implement Persons; + +include "persons.m"; + +population: int; + +init() { + population = 0; +} + +getpop(): int { + return population; +} + +mkperson(): ref Person { + population++; + return ref Person; +} + +Person.stringify(p: self ref Person): string { + return p.name; +} diff --git a/Modules/persons.m b/Modules/persons.m new file mode 100644 index 0000000..37cc408 --- /dev/null +++ b/Modules/persons.m @@ -0,0 +1,11 @@ +Persons: module { + init: fn(); + mkperson: fn(): ref Person; + getpop: fn(): int; + + Person: adt { + age: int; + name: string; + stringify: fn(p: self ref Person): string; + }; +}; diff --git a/Modules/towns.b b/Modules/towns.b new file mode 100644 index 0000000..c417462 --- /dev/null +++ b/Modules/towns.b @@ -0,0 +1,22 @@ +implement Towns; + +include "towns.m"; + +init() { + persons = load Persons "./persons.dis"; +} + +mktown(): ref Town { + return ref Town; +} + +Town.stringify(t: self ref Town): string { + Person: import persons; + + s := "Name: " + t.name + "\nSize: " + string len t.pop + "\nMembers:"; + + for(i := 0; i < len t.pop; i++) + s += "\n→ " + t.pop[i].stringify(); + + return s; +} diff --git a/Modules/towns.m b/Modules/towns.m new file mode 100644 index 0000000..b9eab0a --- /dev/null +++ b/Modules/towns.m @@ -0,0 +1,14 @@ +include "persons.m"; + +Towns: module { + init: fn(); + mktown: fn(): ref Town; + + persons: Persons; + + Town: adt { + pop: array of ref Persons->Person; + name: string; + stringify: fn(t: self ref Town): string; + }; +}; -- cgit v1.2.3