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

Add house exercise #117

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "house",
"name": "House",
"uuid": "c9a8b4d1-b034-460f-95ea-56a6e8ce24e0",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "luhn",
"name": "Luhn",
Expand Down
105 changes: 105 additions & 0 deletions exercises/practice/house/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Instructions

Recite the nursery rhyme 'This is the House that Jack Built'.

> [The] process of placing a phrase of clause within another phrase of clause is called embedding.
> It is through the processes of recursion and embedding that we are able to take a finite number of forms (words and phrases) and construct an infinite number of expressions.
> Furthermore, embedding also allows us to construct an infinitely long structure, in theory anyway.

- [papyr.com][papyr]

The nursery rhyme reads as follows:

```text
This is the house that Jack built.

This is the malt
that lay in the house that Jack built.

This is the rat
that ate the malt
that lay in the house that Jack built.

This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
```

[papyr]: https://papyr.com/hypertextbooks/grammar/ph_noun.htm
19 changes: 19 additions & 0 deletions exercises/practice/house/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"house.s"
],
"test": [
"house_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Output the nursery rhyme 'This is the House that Jack Built'.",
"source": "British nursery rhyme",
"source_url": "https://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built"
}
47 changes: 47 additions & 0 deletions exercises/practice/house/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.data
lyrics:
.string "This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n"

table:
.hword -1, 388, 367, 350, 330, 309, 266, 231, 189, 144, 98, 61, 7

.text
.globl recite

/* extern void recite(char *buffer, int start_verse, int end_verse); */
recite:
lsl x1, x1, #1 /* offset into start_verse for table */
lsl x2, x2, #1 /* offset into end_verse for table */

adrp x5, table
add x5, x5, :lo12:table

adrp x3, lyrics
add x3, x3, :lo12:lyrics

add x1, x1, x5 /* address of offset of start verse */
add x2, x2, x5 /* address of offset of end verse */
add x4, x3, 7 /* end of "This is" */
mov x6, x3 /* "This is" */

.line:
ldrb w7, [x6], #1
strb w7, [x0], #1
cmp x6, x4
bne .line

ldrh w8, [x1] /* offset of current verse */
add x6, x3, x8 /* " the ..." for current verse */

.copy:
ldrb w7, [x6], #1
strb w7, [x0], #1
cbnz w7, .copy

sub x0, x0, #1 /* output address for next verse */
mov x6, x3 /* "This is" */
cmp x1, x2
add x1, x1, #2 /* address of offset of next verse */
bne .line

ret
52 changes: 52 additions & 0 deletions exercises/practice/house/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[28a540ff-f765-4348-9d57-ae33f25f41f2]
description = "verse one - the house that jack built"

[ebc825ac-6e2b-4a5e-9afd-95732191c8da]
description = "verse two - the malt that lay"

[1ed8bb0f-edb8-4bd1-b6d4-b64754fe4a60]
description = "verse three - the rat that ate"

[64b0954e-8b7d-4d14-aad0-d3f6ce297a30]
description = "verse four - the cat that killed"

[1e8d56bc-fe31-424d-9084-61e6111d2c82]
description = "verse five - the dog that worried"

[6312dc6f-ab0a-40c9-8a55-8d4e582beac4]
description = "verse six - the cow with the crumpled horn"

[68f76d18-6e19-4692-819c-5ff6a7f92feb]
description = "verse seven - the maiden all forlorn"

[73872564-2004-4071-b51d-2e4326096747]
description = "verse eight - the man all tattered and torn"

[0d53d743-66cb-4351-a173-82702f3338c9]
description = "verse nine - the priest all shaven and shorn"

[452f24dc-8fd7-4a82-be1a-3b4839cfeb41]
description = "verse 10 - the rooster that crowed in the morn"

[97176f20-2dd3-4646-ac72-cffced91ea26]
description = "verse 11 - the farmer sowing his corn"

[09824c29-6aad-4dcd-ac98-f61374a6a8b7]
description = "verse 12 - the horse and the hound and the horn"

[d2b980d3-7851-49e1-97ab-1524515ec200]
description = "multiple verses"

[0311d1d0-e085-4f23-8ae7-92406fb3e803]
description = "full rhyme"
36 changes: 36 additions & 0 deletions exercises/practice/house/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/house/house.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl recite

recite:
ret
Loading