summaryrefslogtreecommitdiffstats
path: root/research
diff options
context:
space:
mode:
Diffstat (limited to 'research')
-rw-r--r--research/oop3.sh74
1 files changed, 74 insertions, 0 deletions
diff --git a/research/oop3.sh b/research/oop3.sh
new file mode 100644
index 0000000..fe35d01
--- /dev/null
+++ b/research/oop3.sh
@@ -0,0 +1,74 @@
+# Color.esh
+
+# Functions in a namespace are never exported to other namespaces.
+namespace Color
+
+equal(a b)
+{
+ [ "x$[${a}->get_r]" = "x$[${b}->get_r]" ] && return 1
+ [ "x$[${a}->get_g]" = "x$[${b}->get_g]" ] && return 1
+ [ "x$[${a}->get_b]" = "x$[${b}->get_b]" ] && return 1
+ return 0
+}
+
+new(class r g b)
+{
+ self=(r=${r} g=${g} b=${b})
+ classify ${self} ${class}
+ return ${self}
+}
+
+get_r(self)
+{
+ return ${self[r]}
+}
+
+set_r(self r)
+{
+ self[r]=${r}
+}
+
+get_g(self)
+{
+ return ${self[g]}
+}
+
+set_g(self g)
+{
+ self[g]=${g}
+}
+
+get_b(self)
+{
+ return ${self[b]}
+}
+
+set_b(self b)
+{
+ self[b]=${b}
+}
+
+print(self)
+{
+ printf '(%d, %d, %d)' ${self[r]} ${self[g]} ${self[b]}
+}
+
+RED=$[Color->new 255 0 0]
+GREEN=$[Color->new 0 255 0]
+BLUE=$[Color->new 0 0 255]
+
+# main.esh
+
+c=$[Color->new 255 0 0]
+printf 'R: %d\n' "$[${c}->get_r]"
+printf 'G: %d\n' "$[${c}->get_g]"
+printf 'B: %d\n' "$[${c}->get_b]"
+${c}->print
+printf '\n'
+
+${Color::RED}->print
+printf '\n'
+
+if Color::equal ${c} ${Color::RED}; then
+ printf '%s is red\n' "$[${c}->print]"
+fi