Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
02404a5
Implemented getting file paths from user using stdin.
Feb 14, 2025
de970bd
Figured out how to get arguments passed from the command line.
Feb 14, 2025
2d828f9
Added some test files.
Feb 15, 2025
9e19978
Added lines to read contents of provided files into memory.
Feb 17, 2025
69a4016
Decided to add extra empty lines to 'long_test.ha' to demonstrate how…
Feb 17, 2025
21d4ab8
Added a new test file 'err_test.ha' which does not deal with the poss…
Feb 17, 2025
cd37385
Added function stub for comparison function.
Feb 17, 2025
337a581
Filled out parameters for comparison function. Moved code to read fil…
Feb 17, 2025
fb268b3
Got rid of extra lines in 'long_test.ha' because it was more pertinen…
Feb 17, 2025
731946d
Implemented a straightforward comparison of lines in a file. Implemen…
Feb 17, 2025
ba1361b
Added 'double_hello.ha' to test how 'diff' handles when different sec…
Mar 1, 2025
9f68250
Rigged a while loop to disconnect indices for accessing files.
Mar 1, 2025
111b9a7
Implemented a buffer to hold all lines from right-hand file which do …
Mar 1, 2025
4ba84ea
Added a TODO for memory leak.
Mar 1, 2025
21a3094
Changed buffer of lines to be a buffer to addresses to strings - this…
Mar 1, 2025
5ef9999
Added loop to remove lines from buffer after contents have been print…
Mar 1, 2025
8a75cf8
Realized 'diff' program uses the signed difference between two string…
May 8, 2025
eb6cee3
Had to swap in which order the buffered differing lines get printed t…
May 8, 2025
b923db9
Took out reporting of the files being compared - not necessary.
May 8, 2025
329bd1e
Added two other testing files for testing robustness of implementatio…
May 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BINDIR=$(PREFIX)/bin

all: diff

diff:
diff: cmd/diff/main.ha
$(HARE) build $(HAREFLAGS) -o $@ cmd/$@/

clean:
Expand Down
76 changes: 75 additions & 1 deletion cmd/diff/main.ha
Original file line number Diff line number Diff line change
@@ -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;
};
7 changes: 7 additions & 0 deletions testing/files/double_hello.ha
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use fmt;

export fn main() void = {
const hello_str = "Hello world!";
fmt::println(hello_str)!;
fmt::println("Hello world!")!;
};
5 changes: 5 additions & 0 deletions testing/files/err_test.ha
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use fmt;

export fn main() void = {
fmt::println("Hello world!");
};
17 changes: 17 additions & 0 deletions testing/files/inc.ha
Original file line number Diff line number Diff line change
@@ -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;
};
21 changes: 21 additions & 0 deletions testing/files/inc_dec.ha
Original file line number Diff line number Diff line change
@@ -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;
};
6 changes: 6 additions & 0 deletions testing/files/long_test.ha
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use fmt;

export fn main() void = {
const hello_str = "Hello world!";
fmt::println(hello_str)!;
};
5 changes: 5 additions & 0 deletions testing/files/other_test.ha
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use fmt;

export fn main() void = {
fmt::println("Hello world!")!;
};
5 changes: 5 additions & 0 deletions testing/files/test.ha
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use fmt;

export fn main() void = {
fmt::println("Hello world!")!;
};