summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2017-02-10 02:34:23 (EST)
committer P. J. McDermott <pj@pehjota.net>2017-02-10 02:34:23 (EST)
commit2714cfb5211b28c1c2f4e57c3cb072ed4de15209 (patch)
treeab173f6d429555318fe4143167ba5cc490039ca9
parent297e4a9bb6f0086b6287a61820188be7cc240082 (diff)
downloadeggshell-2714cfb5211b28c1c2f4e57c3cb072ed4de15209.zip
eggshell-2714cfb5211b28c1c2f4e57c3cb072ed4de15209.tar.gz
eggshell-2714cfb5211b28c1c2f4e57c3cb072ed4de15209.tar.bz2
research/oop3.sh: New file
-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