diff options
Diffstat (limited to 'research')
-rw-r--r-- | research/oop.sh | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/research/oop.sh b/research/oop.sh new file mode 100644 index 0000000..5e5dbf6 --- /dev/null +++ b/research/oop.sh @@ -0,0 +1,94 @@ +array_n=0 + +array_new() +{ + array_n=$((${array_n} + 1)) + retval="__arr_${array_n}" +} + +array_set() +{ + local arr="${1}" + local idx="${2}" + local val="${3}" + + eval "__arr_${arr}_${idx}=\${val}" +} + +array_get() +{ + local arr="${1}" + local idx="${2}" + + eval "retval=\${__arr_${arr}_${idx}}" +} + +array_call() +{ + local arr="${1}" + local idx="${2}" + local fn= + + eval "fn=\${__arr_${arr}_${idx}}" + "${fn}" ${arr} +} + +color_new() +{ + local r="${1}" + local g="${2}" + local b="${3}" + local self= + + array_new + self=${retval} + array_set ${self} r "${r}" + array_set ${self} g "${g}" + array_set ${self} b "${b}" + array_set ${self} print color_print + + retval=${self} +} + +color_get_r() +{ + local self="${1}" + + array_get ${self} r +} + +color_get_g() +{ + local self="${1}" + + array_get ${self} g +} + +color_get_b() +{ + local self="${1}" + + array_get ${self} b +} + +color_print() +{ + local self="${1}" + + array_get ${self} r + printf '(%d, ' ${retval} + array_get ${self} g + printf '%d, ' ${retval} + array_get ${self} b + printf '%d)' ${retval} +} + +color_new 255 0 0 +c=${retval} +color_get_r ${c} +printf 'R: %d\n' ${retval} +color_get_g ${c} +printf 'G: %d\n' ${retval} +color_get_b ${c} +printf 'B: %d\n' ${retval} +array_call ${c} print |