diff --git a/Makefile b/Makefile index 313b80d..e6b6ceb 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ BINDIR=$(PREFIX)/bin all: diff -diff: +diff: cmd/diff/main.ha $(HARE) build $(HAREFLAGS) -o $@ cmd/$@/ clean: diff --git a/cmd/diff/main.ha b/cmd/diff/main.ha index 943c971..5d1dbcf 100644 --- a/cmd/diff/main.ha +++ b/cmd/diff/main.ha @@ -1,5 +1,79 @@ use fmt; +use io; +use os; +use strings; + + +const DELIM = "\n"; + export fn main() void = { - fmt::println("Hello world!")!; + const path = os::args[1]; + const fh = os::open(path)!; + + const other_path = os::args[2]; + const other_fh = os::open(other_path)!; + + compare_files(fh, other_fh); +}; + + +fn compare_files(lhh: io::handle, rhh: io::handle) uint = { + // get lines from left hand file provided + const lhh_contents = io::drain(lhh)!; + const lhh_contents = strings::fromutf8(lhh_contents)!; + const lhh_lines = strings::split(lhh_contents, DELIM)!; + const lhh_num_lines = len(lhh_lines); + // get lines from right hand file provided + const rhh_contents = io::drain(rhh)!; + const rhh_contents = strings::fromutf8(rhh_contents)!; + const rhh_lines = strings::split(rhh_contents, DELIM)!; + const rhh_num_lines = len(rhh_lines); + // iteration + let lhh_i = 0z; + let rhh_i = 0z; + let cmp_res = 0i; + let rhh_mis_buffer: []*str = []; + let lhh_mis_buffer: []*str = []; + + + for (let i = 0z; lhh_i < lhh_num_lines && rhh_i < rhh_num_lines; i += 1) { + for (let j = 0z; + rhh_i < rhh_num_lines && lhh_i < lhh_num_lines; + j += 1) { + cmp_res = strings::compare(rhh_lines[rhh_i], lhh_lines[lhh_i]); + if (cmp_res < 0) { + append(rhh_mis_buffer, &rhh_lines[rhh_i])!; + rhh_i += 1; + } else if (cmp_res > 0) { + append(lhh_mis_buffer, &lhh_lines[lhh_i])!; + lhh_i += 1; + } else { + break; + }; + }; + if (len(rhh_mis_buffer) > 0 && len(lhh_mis_buffer) > 0) { + for (let j = 0z; j < len(lhh_mis_buffer); j += 1) { + fmt::printfln("< {}", *lhh_mis_buffer[j])!; + }; + fmt::println("---")!; + for (let j = 0z; j < len(rhh_mis_buffer); j += 1) { + fmt::printfln("> {}", *rhh_mis_buffer[j])!; + }; + for (let j = 0z; j < len(rhh_mis_buffer); j += 1) { + delete(rhh_mis_buffer[j]); + }; + for (let j = 0z; j < len(lhh_mis_buffer); j += 1) { + delete(lhh_mis_buffer[j]); + }; + }; + + lhh_i += 1; + rhh_i += 1; + }; + + free(rhh_mis_buffer); + free(lhh_mis_buffer); + + return 0; }; diff --git a/testing/files/double_hello.ha b/testing/files/double_hello.ha new file mode 100644 index 0000000..668258c --- /dev/null +++ b/testing/files/double_hello.ha @@ -0,0 +1,7 @@ +use fmt; + +export fn main() void = { + const hello_str = "Hello world!"; + fmt::println(hello_str)!; + fmt::println("Hello world!")!; +}; diff --git a/testing/files/err_test.ha b/testing/files/err_test.ha new file mode 100644 index 0000000..61ef873 --- /dev/null +++ b/testing/files/err_test.ha @@ -0,0 +1,5 @@ +use fmt; + +export fn main() void = { + fmt::println("Hello world!"); +}; diff --git a/testing/files/inc.ha b/testing/files/inc.ha new file mode 100644 index 0000000..7c34f42 --- /dev/null +++ b/testing/files/inc.ha @@ -0,0 +1,17 @@ +use fmt; +use io; +use os; + + +export fn main() void = { + let num = 4; + inc(&num); + inc(&num); + fmt::printfln("num = {}", num)!; + inc(&num); + fmt::printfln("num = {}", num)!; +}; + +fn inc(num: *int) void = { + *num += 1; +}; diff --git a/testing/files/inc_dec.ha b/testing/files/inc_dec.ha new file mode 100644 index 0000000..6a7c5ae --- /dev/null +++ b/testing/files/inc_dec.ha @@ -0,0 +1,21 @@ +use fmt; +use io; +use os; + + +export fn main() void = { + let num = 4; + inc(&num); + inc(&num); + fmt::printfln("num = {}", num)!; + dec(&num); + fmt::printfln("num = {}", num)!; +}; + +fn dec(num: *int) void = { + *num -= 1; +}; + +fn inc(num: *int) void = { + *num += 1; +}; diff --git a/testing/files/long_test.ha b/testing/files/long_test.ha new file mode 100644 index 0000000..10b8779 --- /dev/null +++ b/testing/files/long_test.ha @@ -0,0 +1,6 @@ +use fmt; + +export fn main() void = { + const hello_str = "Hello world!"; + fmt::println(hello_str)!; +}; diff --git a/testing/files/other_test.ha b/testing/files/other_test.ha new file mode 100644 index 0000000..cdc1f0a --- /dev/null +++ b/testing/files/other_test.ha @@ -0,0 +1,5 @@ +use fmt; + +export fn main() void = { + fmt::println("Hello world!")!; +}; diff --git a/testing/files/test.ha b/testing/files/test.ha new file mode 100644 index 0000000..cdc1f0a --- /dev/null +++ b/testing/files/test.ha @@ -0,0 +1,5 @@ +use fmt; + +export fn main() void = { + fmt::println("Hello world!")!; +};