shithub: orca

Download patch

ref: e4627a7fd73156147bf9e7e6cd58f58b135d8407
parent: 22bc9ddfd476693f8ecca6b6708275c353f1ce61
author: cancel <cancel@cancel.fm>
date: Sat Dec 29 16:00:40 EST 2018

Cleanup, change to use indexing storage for `V` operator

--- a/sim.c
+++ b/sim.c
@@ -10,7 +10,7 @@
     'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', // 24 - 35
 };
 
-enum { Glyphs_index_max = sizeof indexed_glyphs };
+enum { Glyphs_index_count = sizeof indexed_glyphs };
 
 // Always returns 0 through (sizeof indexed_glyphs) - 1, and works on
 // capitalized glyphs as well. The index of the lower-cased glyph is returned
@@ -62,8 +62,10 @@
 }
 #endif
 
+static Usz safe_index_of(Glyph c) { return index_of(c) % 36; }
+
 static inline Glyph glyph_of(Usz index) {
-  assert(index < Glyphs_index_max);
+  assert(index < Glyphs_index_count);
   return indexed_glyphs[index];
 }
 
@@ -70,7 +72,7 @@
 static Glyph glyphs_add(Glyph a, Glyph b) {
   Usz ia = index_of(a);
   Usz ib = index_of(b);
-  return indexed_glyphs[(ia + ib) % Glyphs_index_max];
+  return indexed_glyphs[(ia + ib) % Glyphs_index_count];
 }
 
 static inline bool glyph_is_lowercase(Glyph g) { return g & (1 << 5); }
@@ -369,7 +371,7 @@
   oe->octave = (U8)usz_clamp(octave_num, 1, 9);
   oe->note = note_num;
   oe->velocity = midi_velocity_of(velocity_g);
-  oe->bar_divisor = (U8)usz_clamp(index_of(length_g), 1, Glyphs_index_max);
+  oe->bar_divisor = (U8)usz_clamp(index_of(length_g), 1, Glyphs_index_count);
 END_OPERATOR
 
 BEGIN_OPERATOR(osc)
@@ -670,44 +672,26 @@
 END_OPERATOR
 
 BEGIN_OPERATOR(variable)
-  // hacky until we clean up
   LOWERCASE_REQUIRES_BANG;
   PORT(0, -1, IN | PARAM);
-  PORT(0, 1, IN);
+  PORT(0, 1, IN | PARAM);
   PORT(1, 0, OUT);
-  {
-    Glyph left = PEEK(0, -1);
-    Usz var_idx;
-    if (left >= 'A' && left <= 'Z') {
-      var_idx = (Usz)('Z' - left);
-    } else if (left >= 'a' && left <= 'z') {
-      var_idx = (Usz)(('Z' - 'A') + ('z' - left) + 1);
-    } else {
-      goto next_phase;
-    }
-    Glyph right = PEEK(0, 1);
-    if (right == '.')
-      goto next_phase;
-    extra_params->vars_slots[var_idx] = right;
-  }
-next_phase : {
   Glyph left = PEEK(0, -1);
-  if (left != '.')
-    return;
   Glyph right = PEEK(0, 1);
-  Usz var_idx;
-  if (right >= 'A' && right <= 'Z') {
-    var_idx = (Usz)('Z' - right);
-  } else if (right >= 'a' && right <= 'z') {
-    var_idx = (Usz)(('Z' - 'A') + ('z' - right) + 1);
-  } else {
+  if (right == '.')
     return;
+  if (left == '.') {
+    // Read
+    Usz var_idx = safe_index_of(right);
+    Glyph result = extra_params->vars_slots[var_idx];
+    if (result == '.')
+      return;
+    POKE(1, 0, result);
+  } else {
+    // Write
+    Usz var_idx = safe_index_of(left);
+    extra_params->vars_slots[var_idx] = right;
   }
-  Glyph result = extra_params->vars_slots[var_idx];
-  if (result == '.')
-    return;
-  POKE(1, 0, result);
-}
 END_OPERATOR
 
 BEGIN_OPERATOR(teleport)
@@ -748,7 +732,7 @@
 void orca_run(Gbuffer gbuf, Mbuffer mbuf, Usz height, Usz width,
               Usz tick_number, Oevent_list* oevent_list,
               Piano_bits piano_bits) {
-  Glyph vars_slots[('Z' - 'A' + 1) + ('z' - 'a' + 1)];
+  Glyph vars_slots[Glyphs_index_count];
   memset(vars_slots, '.', sizeof(vars_slots));
   mbuffer_clear(mbuf, height, width);
   oevent_list_clear(oevent_list);