commit 48f10f36de278a73cccc71e2a5b4faee49761b0a
parent 28acdfba98e04cc216f2abf6a866931375058db6
Author: Byron Torres <b@torresjrjr.com>
Date: Tue, 11 Apr 2023 01:07:28 +0100
rearrange command and execute code
Diffstat:
M | command.ha | | | 91 | ------------------------------------------------------------------------------- |
A | execute.ha | | | 90 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 90 insertions(+), 91 deletions(-)
diff --git a/command.ha b/command.ha
@@ -86,97 +86,6 @@ fn lookupcmd(name: rune) commandfn = {
};
};
-// Executes the session's .cmd command.
-fn execute(s: *session, cmd: *command) (void | error) = {
- // TODO: move this into the cmd_* functions?
- match (exec_addrs(s, cmd)) {
- case void => void;
- case let err: error =>
- delete(cmd.linenums[..]);
- return errormsg(s, err);
- };
- // TODO: write finish_cmd()
- // TODO: finish the command at a higher level (main()) ?.
- defer delete(cmd.linenums[..]);
-
- match (lookupcmd(cmd.cmdname)(s, cmd)) {
- case void => void;
- case let err: error =>
- return errormsg(s, err);
- };
-};
-
-fn exec_addrs(s: *session, cmd: *command) (void | error) = {
- for (let i = 0z; i < len(cmd.addrs); i += 1) {
- const addr = cmd.addrs[i];
- let n = exec_addr(s, addr)?;
- debug("exec_addrs(): offs={}", addr.lineoffset);
- n = n + addr.lineoffset: size; // beware of negatives
- debug("exec_addrs(): n={}", n);
- append(cmd.linenums, n);
-
- for (let j = 0z; j < len(cmd.linenums); j += 1) {
- debug("exec_addrs(): cmd.linenums[{}]={}", j, cmd.linenums[j]);
- };
-
- if (addr.setcurrentline) {
- s.buf.cursor = n;
- };
- };
-};
-
-fn exec_addr(s: *session, addr: address) (size | error) = {
- match (addr.addrtype) {
- case let n: size =>
- return addr_linenum(&s.buf, n)?;
- case currentline =>
- return s.buf.cursor;
- case lastline =>
- return addr_lastline(&s.buf);
- case let m: rune =>
- return addr_mark(&s.buf, m)?;
- case let rad: regexaddr =>
- return addr_regex(&s.buf, rad, s.buf.cursor)?;
- };
-};
-
-
-fn get_range(s: *session, lns: *[]size, a: size, b: size) ((size, size) | invalidaddress) = {
- debug("get_range(): len(lns)={}", len(lns));
- const (a, b) = if (len(lns) == 0) {
- yield (a, b);
- } else if (len(lns) == 1) {
- yield (lns[0], lns[0]);
- } else {
- yield (lns[ len(lns) - 2 ], lns[ len(lns) - 1 ]);
- };
- debug("get_range(): (a, b)=({}, {})", a, b);
- if (a < 0 || a > b || b >= len(s.buf.lines)) {
- return invalidaddress;
- };
- return (a, b);
-};
-
-fn get_linenum(lns: []size, n: size) size = {
- if (len(lns) == 0) {
- return n;
- } else {
- return lns[len(lns)-1];
- };
-};
-
-fn assert_noaddrs(s: *session, lns: []size) (void | unexpectedaddress) = {
- if (len(lns) != 0) {
- return unexpectedaddress;
- };
-};
-
-fn assert_nonzero(s: *session, n: size) (void | invalidaddress) = {
- if (n < 1) {
- return invalidaddress;
- };
-};
-
fn cmd_append(s: *session, cmd: *command) (void | error) = {
const n = get_linenum(cmd.linenums, s.buf.cursor);
diff --git a/execute.ha b/execute.ha
@@ -0,0 +1,90 @@
+// Executes the session's .cmd command.
+fn execute(s: *session, cmd: *command) (void | error) = {
+ // TODO: move this into the cmd_* functions?
+ match (exec_addrs(s, cmd)) {
+ case void => void;
+ case let err: error =>
+ delete(cmd.linenums[..]);
+ return errormsg(s, err);
+ };
+ // TODO: write finish_cmd()
+ // TODO: finish the command at a higher level (main()) ?.
+ defer delete(cmd.linenums[..]);
+
+ match (lookupcmd(cmd.cmdname)(s, cmd)) {
+ case void => void;
+ case let err: error =>
+ return errormsg(s, err);
+ };
+};
+
+fn exec_addrs(s: *session, cmd: *command) (void | error) = {
+ for (let i = 0z; i < len(cmd.addrs); i += 1) {
+ const addr = cmd.addrs[i];
+ let n = exec_addr(s, addr)?;
+ debug("exec_addrs(): offs={}", addr.lineoffset);
+ n = n + addr.lineoffset: size; // beware of negatives
+ debug("exec_addrs(): n={}", n);
+ append(cmd.linenums, n);
+
+ for (let j = 0z; j < len(cmd.linenums); j += 1) {
+ debug("exec_addrs(): cmd.linenums[{}]={}", j, cmd.linenums[j]);
+ };
+
+ if (addr.setcurrentline) {
+ s.buf.cursor = n;
+ };
+ };
+};
+
+fn exec_addr(s: *session, addr: address) (size | error) = {
+ match (addr.addrtype) {
+ case let n: size =>
+ return addr_linenum(&s.buf, n)?;
+ case currentline =>
+ return s.buf.cursor;
+ case lastline =>
+ return addr_lastline(&s.buf);
+ case let m: rune =>
+ return addr_mark(&s.buf, m)?;
+ case let rad: regexaddr =>
+ return addr_regex(&s.buf, rad, s.buf.cursor)?;
+ };
+};
+
+
+fn get_range(s: *session, lns: *[]size, a: size, b: size) ((size, size) | invalidaddress) = {
+ debug("get_range(): len(lns)={}", len(lns));
+ const (a, b) = if (len(lns) == 0) {
+ yield (a, b);
+ } else if (len(lns) == 1) {
+ yield (lns[0], lns[0]);
+ } else {
+ yield (lns[ len(lns) - 2 ], lns[ len(lns) - 1 ]);
+ };
+ debug("get_range(): (a, b)=({}, {})", a, b);
+ if (a < 0 || a > b || b >= len(s.buf.lines)) {
+ return invalidaddress;
+ };
+ return (a, b);
+};
+
+fn get_linenum(lns: []size, n: size) size = {
+ if (len(lns) == 0) {
+ return n;
+ } else {
+ return lns[len(lns)-1];
+ };
+};
+
+fn assert_noaddrs(s: *session, lns: []size) (void | unexpectedaddress) = {
+ if (len(lns) != 0) {
+ return unexpectedaddress;
+ };
+};
+
+fn assert_nonzero(s: *session, n: size) (void | invalidaddress) = {
+ if (n < 1) {
+ return invalidaddress;
+ };
+};