about summary refs log tree commit diff stats
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/tile/data.mu51
-rw-r--r--apps/tile/environment.mu69
2 files changed, 94 insertions, 26 deletions
diff --git a/apps/tile/data.mu b/apps/tile/data.mu
index e913a369..dd30dde0 100644
--- a/apps/tile/data.mu
+++ b/apps/tile/data.mu
@@ -2,7 +2,6 @@ type sandbox {
   setup: (handle line)
   data: (handle line)
   # display data
-  cursor-word: (handle word)
   cursor-call-path: (handle call-path-element)
   expanded-words: (handle call-path)
   #
@@ -79,23 +78,14 @@ fn initialize-sandbox _sandbox: (addr sandbox) {
   var line-ah/eax: (addr handle line) <- get sandbox, data
   allocate line-ah
   var line/eax: (addr line) <- lookup *line-ah
-  var cursor-word-ah/esi: (addr handle word) <- get sandbox, cursor-word
-  allocate cursor-word-ah
-  initialize-line line, cursor-word-ah
+  initialize-line line
 }
 
 # initialize line with a single empty word
-# if 'out' is non-null, save the word there as well
-fn initialize-line _line: (addr line), out: (addr handle word) {
+fn initialize-line _line: (addr line) {
   var line/esi: (addr line) <- copy _line
   var word-ah/eax: (addr handle word) <- get line, data
   allocate word-ah
-  {
-    compare out, 0
-    break-if-=
-    var dest/edi: (addr handle word) <- copy out
-    copy-object word-ah, dest
-  }
   var word/eax: (addr word) <- lookup *word-ah
   initialize-word word
 }
@@ -115,7 +105,7 @@ fn create-primitive-functions _self: (addr handle function) {
   var body-ah/eax: (addr handle line) <- get f, body
   allocate body-ah
   var body/eax: (addr line) <- lookup *body-ah
-  initialize-line body, 0
+  initialize-line body
   var curr-word-ah/ecx: (addr handle word) <- get body, data
   allocate curr-word-ah
   var curr-word/eax: (addr word) <- lookup *curr-word-ah
@@ -142,7 +132,7 @@ fn create-primitive-functions _self: (addr handle function) {
   var body-ah/eax: (addr handle line) <- get f, body
   allocate body-ah
   var body/eax: (addr line) <- lookup *body-ah
-  initialize-line body, 0
+  initialize-line body
   var curr-word-ah/ecx: (addr handle word) <- get body, data
   allocate curr-word-ah
   var curr-word/eax: (addr word) <- lookup *curr-word-ah
@@ -169,7 +159,7 @@ fn create-primitive-functions _self: (addr handle function) {
   var body-ah/eax: (addr handle line) <- get f, body
   allocate body-ah
   var body/eax: (addr line) <- lookup *body-ah
-  initialize-line body, 0
+  initialize-line body
   var curr-word-ah/ecx: (addr handle word) <- get body, data
   allocate curr-word-ah
   var curr-word/eax: (addr word) <- lookup *curr-word-ah
@@ -185,6 +175,37 @@ fn create-primitive-functions _self: (addr handle function) {
   # TODO: populate prev pointers
 }
 
+fn function-body in: (addr handle function), _word: (addr handle word), out: (addr handle line) {
+  var function-name-storage: (handle array byte)
+  var function-name-ah/ecx: (addr handle array byte) <- address function-name-storage
+  var word-ah/esi: (addr handle word) <- copy _word
+  var word/eax: (addr word) <- lookup *word-ah
+  var gap-ah/eax: (addr handle gap-buffer) <- get word, scalar-data
+  var gap/eax: (addr gap-buffer) <- lookup *gap-ah
+  gap-buffer-to-string gap, function-name-ah
+  var _function-name/eax: (addr array byte) <- lookup *function-name-ah
+  var function-name/esi: (addr array byte) <- copy _function-name
+  var curr-ah/ecx: (addr handle function) <- copy in
+  $function-body:loop: {
+    var _curr/eax: (addr function) <- lookup *curr-ah
+    var curr/edx: (addr function) <- copy _curr
+    compare curr, 0
+    break-if-=
+    var curr-name-ah/eax: (addr handle array byte) <- get curr, name
+    var curr-name/eax: (addr array byte) <- lookup *curr-name-ah
+    var found?/eax: boolean <- string-equal? curr-name, function-name
+    compare found?, 0  # false
+    {
+      break-if-=
+      var src/eax: (addr handle line) <- get curr, body
+      copy-object src, out
+      break $function-body:loop
+    }
+    curr-ah <- get curr, next
+    loop
+  }
+}
+
 fn populate-text-with _out: (addr handle array byte), _in: (addr array byte) {
   var in/esi: (addr array byte) <- copy _in
   var n/ecx: int <- length in
diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu
index 78462817..b41202cb 100644
--- a/apps/tile/environment.mu
+++ b/apps/tile/environment.mu
@@ -58,10 +58,13 @@ fn initialize-environment-with-fake-screen _self: (addr environment), nrows: int
 fn process _self: (addr environment), key: grapheme {
 $process:body: {
     var self/esi: (addr environment) <- copy _self
+    var functions/ecx: (addr handle function) <- get self, functions
     var sandbox-ah/eax: (addr handle sandbox) <- get self, sandboxes
     var _sandbox/eax: (addr sandbox) <- lookup *sandbox-ah
-    var sandbox/esi: (addr sandbox) <- copy _sandbox
-    var cursor-word-ah/edi: (addr handle word) <- get sandbox, cursor-word
+    var sandbox/edi: (addr sandbox) <- copy _sandbox
+    var cursor-word-storage: (handle word)
+    var cursor-word-ah/ebx: (addr handle word) <- address cursor-word-storage
+    get-cursor-word sandbox, functions, cursor-word-ah
     var _cursor-word/eax: (addr word) <- lookup *cursor-word-ah
     var cursor-word/ecx: (addr word) <- copy _cursor-word
     compare key, 0x445b1b  # left-arrow
@@ -76,12 +79,11 @@ $process:body: {
         break $process:body
       }
       # otherwise, move to end of prev word
-      var prev-word-ah/edx: (addr handle word) <- get cursor-word, prev
+      var prev-word-ah/eax: (addr handle word) <- get cursor-word, prev
       var prev-word/eax: (addr word) <- lookup *prev-word-ah
       {
         compare prev-word, 0
         break-if-=
-        copy-object prev-word-ah, cursor-word-ah
         cursor-to-end prev-word
         var cursor-call-path/eax: (addr handle call-path-element) <- get sandbox, cursor-call-path
         decrement-final-element cursor-call-path
@@ -100,12 +102,11 @@ $process:body: {
         break $process:body
       }
       # otherwise, move to start of next word
-      var next-word-ah/edx: (addr handle word) <- get cursor-word, next
+      var next-word-ah/eax: (addr handle word) <- get cursor-word, next
       var next-word/eax: (addr word) <- lookup *next-word-ah
       {
         compare next-word, 0
         break-if-=
-        copy-object next-word-ah, cursor-word-ah
         cursor-to-start next-word
         var cursor-call-path/eax: (addr handle call-path-element) <- get sandbox, cursor-call-path
         increment-final-element cursor-call-path
@@ -124,12 +125,11 @@ $process:body: {
         break $process:body
       }
       # otherwise delete current word and move to end of prev word
-      var prev-word-ah/edx: (addr handle word) <- get cursor-word, prev
+      var prev-word-ah/eax: (addr handle word) <- get cursor-word, prev
       var prev-word/eax: (addr word) <- lookup *prev-word-ah
       {
         compare prev-word, 0
         break-if-=
-        copy-object prev-word-ah, cursor-word-ah
         cursor-to-end prev-word
         delete-next prev-word
         var cursor-call-path/eax: (addr handle call-path-element) <- get sandbox, cursor-call-path
@@ -142,8 +142,6 @@ $process:body: {
       break-if-!=
       # insert new word
       append-word cursor-word-ah
-      var next-word-ah/ecx: (addr handle word) <- get cursor-word, next
-      copy-object next-word-ah, cursor-word-ah
       var cursor-call-path/eax: (addr handle call-path-element) <- get sandbox, cursor-call-path
       increment-final-element cursor-call-path
       break $process:body
@@ -168,6 +166,53 @@ $process:body: {
 }
 }
 
+fn get-cursor-word _sandbox: (addr sandbox), functions: (addr handle function), out: (addr handle word) {
+  var sandbox/esi: (addr sandbox) <- copy _sandbox
+  var cursor-call-path/edi: (addr handle call-path-element) <- get sandbox, cursor-call-path
+  var line/ecx: (addr handle line) <- get sandbox, data
+  get-word-from-path line, functions, cursor-call-path, out
+}
+
+fn get-word-from-path _line: (addr handle line), functions: (addr handle function), _path-ah: (addr handle call-path-element), out: (addr handle word) {
+  # if (path->next)
+  #   get-word-from-path(line, functions, path->next, out)
+  #   line = function-body(functions, out)
+  # word-index(line, path->index-in-body, out)
+  var path-ah/eax: (addr handle call-path-element) <- copy _path-ah
+  var _path/eax: (addr call-path-element) <- lookup *path-ah
+  var path/ecx: (addr call-path-element) <- copy _path
+  var next-ah/edx: (addr handle call-path-element) <- get path, next
+  var next/eax: (addr call-path-element) <- lookup *next-ah
+  var line-ah/ebx: (addr handle line) <- copy _line
+  {
+    compare next, 0
+    break-if-=
+    get-word-from-path _line, functions, next-ah, out
+    function-body functions, out, line-ah
+  }
+  var n/ecx: (addr int) <- get path, index-in-body
+  var line/eax: (addr line) <- lookup *line-ah
+  var words/eax: (addr handle word) <- get line, data
+  word-index words, *n, out
+}
+
+fn word-index _words: (addr handle word), _n: int, out: (addr handle word) {
+$word-index:body: {
+  var n/ecx: int <- copy _n
+  {
+    compare n, 0
+    break-if-!=
+    copy-object _words, out
+    break $word-index:body
+  }
+  var words-ah/eax: (addr handle word) <- copy _words
+  var words/eax: (addr word) <- lookup *words-ah
+  var next/eax: (addr handle word) <- get words, next
+  n <- decrement
+  word-index next, n, out
+}
+}
+
 fn toggle-cursor-word _sandbox: (addr sandbox) {
 $toggle-cursor-word:body: {
   var sandbox/esi: (addr sandbox) <- copy _sandbox
@@ -230,7 +275,9 @@ fn render-sandbox screen: (addr screen), functions: (addr handle function), bind
   var _line/eax: (addr line) <- lookup *line-ah
   var line/ecx: (addr line) <- copy _line
   # cursor-word
-  var cursor-word-ah/eax: (addr handle word) <- get sandbox, cursor-word
+  var cursor-word-storage: (handle word)
+  var cursor-word-ah/ebx: (addr handle word) <- address cursor-word-storage
+  get-cursor-word sandbox, functions, cursor-word-ah
   var _cursor-word/eax: (addr word) <- lookup *cursor-word-ah
   var cursor-word/ebx: (addr word) <- copy _cursor-word
   # cursor-col