Skip to content

Comments

Add forth practice exercise#495

Merged
ryanplusplus merged 3 commits intomainfrom
forth
Aug 24, 2024
Merged

Add forth practice exercise#495
ryanplusplus merged 3 commits intomainfrom
forth

Conversation

@ryanplusplus
Copy link
Member

No description provided.

end)

it('can consist of built-in words', function()
assert.are.same({ 1, 1, 1 }, forth.evaluate(': dup-twice dup dup ;1 dup-twice'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.are.same({ 1, 1, 1 }, forth.evaluate(': dup-twice dup dup ;1 dup-twice'))
assert.are.same({ 1, 1, 1 }, forth.evaluate(': dup-twice dup dup ; 1 dup-twice'))

Actual Forth is extremely word-oriented, where ;1 can be an actual command. ; should be a distinct word.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you, I didn't know that. Do you think it makes sense to take instructions as an array of instructions instead of a concatenated string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what other tracks do. Having a single string of instructions will make the definition processing slightly more difficult: start with a : and process words until you get to ; -- in most other tracks, you get an array element where the string starts with : and ends with ;.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I modeled this after how Crystal did it and didn't think to check elsewhere.

Comment on lines 64 to 65
for chunk in s:gmatch('[^;]+') do
eval_chunk(chunk, stack, user_words)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you don't know, ; is not a statement separator, it is only the terminating word of a : definition. Newlines in forth are just spaces.

You'll probably want to trim leading whitespace from the chunk.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is great feedback. All I know about forth I learned from doing this exercise in another language so I appreciate the tips.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All I know about forth I learned from doing this exercise

Me too until I did the 8th track.

@glennj
Copy link
Contributor

glennj commented Aug 24, 2024

It's exciting to get new Lua exercises. Thanks for doing it.

@ryanplusplus ryanplusplus merged commit 09d8496 into main Aug 24, 2024
@ryanplusplus ryanplusplus deleted the forth branch August 24, 2024 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants