Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get some nice testing in here #10

Merged
merged 13 commits into from
May 25, 2024
40 changes: 40 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,45 @@ build-suite-test name *BUILD_ARGS: build
{{minipp}} -i "$dir/${name}.mini" -o "$dir/{{name}}.ll" {{BUILD_ARGS}}
clang "$dir/{{name}}.ll" -o "$dir/{{name}}"

check-llvm path *BUILD_ARGS:
#!/usr/bin/env bash
set -euxo pipefail

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'

name="{{join(parent_directory(path), file_stem(path))}}"
clang "${name}.ll" -o "${name}"
input="${name}.input"
if [[ -f "{{join(parent_directory(path),'input')}}" ]] && [[ ! -f $input ]]; then
input="{{join(parent_directory(path),'input')}}"
fi
if [[ -f "${input}" ]]; then
${name} < "${input}" > "${name}.output"
else
${name} > "${name}.output"
fi
expected="${name}.expected"
if [[ -f "{{join(parent_directory(path),'output.expected')}}" ]] && [[ ! -f $expected ]]; then
expected="{{join(parent_directory(path),'output.expected')}}"
fi
if [[ -f ${expected} ]]; then
ok=0
comp=$(diff "${name}.output" "${expected}" || ok=1)
if [ $ok -eq 0 ]; then
echo -e "${GREEN}SUCCESS${NC}"
echo "Output file: ${ouput}"
else
echo -e "${RED}FAIL${NC}"
echo -e "${comp}"
fi
else
echo -e "${YELLOW}No output file found${NC}"
echo "Output file: ${ouput}"
fi

nix:
sudo nix develop --extra-experimental-features nix-command --extra-experimental-features flakes
143 changes: 143 additions & 0 deletions src/hanoi_local.mini
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Towers of Hanoi

struct plate
{
int size;
struct plate plateUnder;
};

struct plate peg1;
struct plate peg2;
struct plate peg3;
int numMoves;

fun move(int from, int to) void
{
struct plate plateToMove;

if (from == 1) {
plateToMove = peg1;
peg1 = peg1.plateUnder;
}
else
{
if (from == 2) {
plateToMove = peg2;
peg2 = peg2.plateUnder;
}
else {
plateToMove = peg3;
peg3 = peg3.plateUnder;
}
}

if (to == 1) {
plateToMove.plateUnder = peg1;
peg1 = plateToMove;
}
else
{
if (to == 2) {
plateToMove.plateUnder = peg2;
peg2 = plateToMove;
}
else
{
plateToMove.plateUnder = peg3;
peg3 = plateToMove;
}
}

numMoves = numMoves + 1;
}

fun hanoi(int n, int from, int to, int other) void
{
if (n == 1) {
move(from, to);
}
else
{
hanoi(n - 1, from, other, to);
move(from, to);
hanoi(n - 1, other, to, from);
}
}

fun printPeg(struct plate peg) void
{
struct plate aPlate;

aPlate = peg;

print 66 endl;
while (aPlate != null)
{
print 67 endl;
print aPlate.size endl;
print 68 endl;
aPlate = aPlate.plateUnder;
print 69 endl;
}
}

fun main() int
{
int count, numPlates;
struct plate aPlate;

peg1 = null;
peg2 = null;
peg3 = null;
numMoves = 0;

numPlates = read;

if (numPlates >= 1)
{
count = numPlates;

while (count != 0)
{
aPlate = new plate;
aPlate.size = count;
aPlate.plateUnder = peg1;
peg1 = aPlate;
count = count - 1;
}

# Print the peg number followed by any plates
# it has starting from the top of the stack
# to the bottom. At this point, peg 1
# should have all the plates.
print 1 endl;
printPeg(peg1);
print 2 endl;
printPeg(peg2);
print 3 endl;
printPeg(peg3);

hanoi(numPlates, 1, 3, 2);

# At this point, peg 3
# should have all the plates.
print 1 endl;
printPeg(peg1);
print 2 endl;
printPeg(peg2);
print 3 endl;
printPeg(peg3);

# Print the number of moves.
print numMoves endl;

while (peg3 != null)
{
aPlate = peg3;
peg3 = peg3.plateUnder;
delete aPlate;
}
}

return 0;
}
22 changes: 22 additions & 0 deletions src/inter_fun_structs.mini
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

struct Node {
struct Node n;
int a;
};

fun comparevalue(struct Node head) void {
struct Node currnode;
currnode = head;
if(head.a != 0){
print currnode.n.a endl;
}
}

fun main() void{
struct Node head;
head = new Node;
head.n = new Node;
head.n.a = 2;
head.a = 1;
comparevalue(head);
}
Loading
Loading