commit bb1ff434699f2f749b672f25fc9faecbc478e4c1
parent 08909ddf1f3227d36c9e1a628dfccf6060872a4d
Author: Byron Torres <b@torresjrjr.com>
Date: Sun, 12 Sep 2021 09:47:07 +0100
Add buffer.ha, command.ha, util.ha
Diffstat:
A | buffer.ha | | | 64 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | command.ha | | | 20 | ++++++++++++++++++++ |
M | ed.ha | | | 108 | +++++-------------------------------------------------------------------------- |
A | util.ha | | | 19 | +++++++++++++++++++ |
4 files changed, 109 insertions(+), 102 deletions(-)
diff --git a/buffer.ha b/buffer.ha
@@ -0,0 +1,64 @@
+use bufio;
+use fmt;
+
+type buffer = struct {
+ head: *linenode,
+ tail: *linenode,
+};
+
+type linenode = struct {
+ value: value,
+ prev: *linenode,
+ next: *linenode,
+};
+
+type value = (headnode | str | tailnode);
+
+type headnode = void;
+
+type tailnode = void;
+
+// Initialises a new buffer.
+fn new_buffer() *buffer = {
+ const head = alloc(linenode { value = headnode, ... });
+ const tail = alloc(linenode { value = tailnode, prev = head });
+ head.next = tail;
+ return alloc(buffer {
+ head = head,
+ tail = tail,
+ });
+};
+
+// Reads text from source, writes to a buffer.
+fn read(source: *io::stream, buf: *buffer) size = {
+ let localhead = buf.head;
+ const localtail = buf.tail;
+
+ let total = 0z;
+
+ for (true) {
+ const rawline = match (bufio::scanline(source)) {
+ * => abort(),
+ io::EOF => break,
+ rawline: []u8 => rawline,
+ };
+ //defer free(rawline);
+ total += len(rawline);
+
+ const value = fmt::bsprint(rawline);
+
+ const ln = alloc(linenode {
+ value = value,
+ prev = localhead,
+ next = localtail,
+ });
+
+ localhead.next = ln;
+ localtail.prev = ln;
+
+ localhead = ln;
+ };
+
+ return total;
+};
+
diff --git a/command.ha b/command.ha
@@ -0,0 +1,20 @@
+use os;
+use fmt;
+use fs;
+
+// 'e' command
+fn cmd_edit(buf: *buffer, fname: filename) void = {
+ match (fname) {
+ void => void,
+ fname: str => match (os::open(fname)) {
+ err: fs::error => fmt::fatal("Error {}", fs::strerror(err)),
+ f: io::file => {
+ const n = read(&f, buf);
+ if (!surpress) {
+ fmt::println(n)?;
+ };
+ },
+ },
+ };
+};
+
diff --git a/ed.ha b/ed.ha
@@ -1,6 +1,5 @@
use bufio;
use fmt;
-use fs;
use getopt;
use io;
use os;
@@ -11,23 +10,6 @@ let surpress: bool = false;
type filename = (str | void);
-type buffer = struct {
- head: *linenode,
- tail: *linenode,
-};
-
-type linenode = struct {
- value: value,
- prev: *linenode,
- next: *linenode,
-};
-
-type value = (headnode | str | tailnode);
-
-type headnode = void;
-
-type tailnode = void;
-
export fn main() void = {
const help: [_]getopt::help = [
"standard line editor",
@@ -50,14 +32,13 @@ export fn main() void = {
exit_usage(help);
};
- let fname: filename = if (len(cmd.args) == 1) {
- yield void;
- } else {
- yield switch (cmd.args[0]) {
+ let fname: filename = switch (len(cmd.args) == 1) {
+ false => void,
+ true => switch (cmd.args[0]) {
"-" => fmt::fatal("Invalid filename '-'"),
"" => fmt::fatal("Invalid filename ''"),
- * => yield cmd.args[0],
- };
+ * => cmd.args[0],
+ },
};
const buf = new_buffer();
@@ -68,7 +49,7 @@ export fn main() void = {
for (true) {
const rawline = match (bufio::scanline(os::stdin)) {
- err: io::error => abort("io::error"),
+ * => abort(),
io::EOF => break,
rawline: []u8 => rawline,
};
@@ -96,80 +77,3 @@ fn parse_cmd(command: str) void = {
os::exit(1);
};
-// Initialises a new buffer.
-fn new_buffer() *buffer = {
- const head = alloc(linenode { value = headnode, ... });
- const tail = alloc(linenode { value = tailnode, prev = head });
- head.next = tail;
- return alloc(buffer {
- head = head,
- tail = tail,
- });
-};
-
-// Reads text from source, writes to a buffer.
-fn read(source: *io::stream, buf: *buffer) size = {
- let localhead = buf.head;
- const localtail = buf.tail;
-
- let total = 0z;
-
- for (true) {
- const rawline = match (bufio::scanline(source)) {
- err: io::error => abort("io::error"),
- io::EOF => break,
- rawline: []u8 => rawline,
- };
- //defer free(rawline);
- total += len(rawline);
-
- const value = fmt::bsprint(rawline);
-
- const ln = alloc(linenode {
- value = value,
- prev = localhead,
- next = localtail,
- });
-
- localhead.next = ln;
- localtail.prev = ln;
-
- localhead = ln;
- };
-
- return total;
-};
-
-// // Sequencially prints lines of a buffer.
-// fn print_buffer(buf: *buffer) void = {
-// let linenode = buf.head;
-// for (true) {
-// match (linenode.value) {
-// headnode => {
-// linenode = linenode.next;
-// },
-// s: str => {
-// fmt::println(s)?;
-// linenode = linenode.next;
-// },
-// tailnode => break
-// };
-// };
-// };
-
-// 'e' command
-fn cmd_edit(buf: *buffer, fname: filename) void = {
- match (fname) {
- void => void,
- fname: str => match (os::open(fname)) {
- err: fs::error => fmt::fatal("Error {}", fs::strerror(err)),
- f: io::file => {
- const n = read(&f, buf);
- if (!surpress) {
- fmt::println(n)?;
- };
- },
- },
- };
-};
-
diff --git a/util.ha b/util.ha
@@ -0,0 +1,19 @@
+use fmt;
+
+// Sequencially prints lines of a buffer.
+fn print_buffer(buf: *buffer) void = {
+ let linenode = buf.head;
+ for (true) {
+ match (linenode.value) {
+ headnode => {
+ linenode = linenode.next;
+ },
+ s: str => {
+ fmt::println(s)?;
+ linenode = linenode.next;
+ },
+ tailnode => break
+ };
+ };
+};
+