about summary refs log tree commit diff stats
path: root/baremetal/shell/eval.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-02-11 21:11:41 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-02-11 21:11:47 -0800
commit8bb22dcb0a3819c1cc22299aec8126a701a3267d (patch)
tree7d6c853aae9dd6d47a7e5e35a9dd020592a4027d /baremetal/shell/eval.mu
parentead6c01fa4f9291ecbac9e6a4567be87cb0e4a8b (diff)
downloadmu-8bb22dcb0a3819c1cc22299aec8126a701a3267d.tar.gz
7725 - baremetal/shell: start on evaluator
Diffstat (limited to 'baremetal/shell/eval.mu')
-rw-r--r--baremetal/shell/eval.mu36
1 files changed, 36 insertions, 0 deletions
diff --git a/baremetal/shell/eval.mu b/baremetal/shell/eval.mu
new file mode 100644
index 00000000..2506bd22
--- /dev/null
+++ b/baremetal/shell/eval.mu
@@ -0,0 +1,36 @@
+# evaluator for the Mu shell language
+# inputs:
+#   a list of lines, each a list of words, each an editable gap-buffer
+#   end: a word to stop at
+# output:
+#   a stack of values to render that summarizes the result of evaluation until 'end'
+
+# Key features of the language:
+#   { and } for grouping words
+#   break and loop for control flow within groups
+#   -> for conditionally skipping the next word or group
+
+# Example: Pushing numbers from 1 to n on the stack (single-line body with
+# conditionals and loops)
+#
+#   3 =n
+#   { n 1 <     -> break n n 1- =n loop }
+# stack:
+#     3 1 false          3 3 2  3   1
+#       3                  3 3      2
+#                                   3
+
+# Rules beyond simple postfix:
+#   If the final word is `->`, clear stack
+#   If the final word is `break`, pop top of stack
+#
+#   `{` and `}` don't affect evaluation
+#   If the final word is `{` or `}`, clear stack
+#
+#   If `->` in middle and top of stack is falsy, skip next word or group
+#
+#   If `break` in middle executes, skip to next containing `}`
+#     If no containing `}`, clear stack (incomplete)
+#
+#   If `loop` in middle executes, skip to previous containing `{`
+#     If no containing `}`, clear stack (error)