-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add forth exercise #7
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
41ede6f
Add test cases for forth
quintuple-mallard 67cafd3
Merge branch 'main' into add-forth
quintuple-mallard d648a01
Fix bug in tests, draft solution
quintuple-mallard e3f1e50
More changes to exemplar
quintuple-mallard ec98d3a
Merge branch 'main' into add-forth
quintuple-mallard a3a5768
Finally... I did it
quintuple-mallard 0f8914a
Add difficulty to forth exercise
quintuple-mallard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Instructions | ||
|
|
||
| Implement an evaluator for a very simple subset of Forth. | ||
|
|
||
| [Forth][forth] | ||
| is a stack-based programming language. | ||
| Implement a very basic evaluator for a small subset of Forth. | ||
|
|
||
| Your evaluator has to support the following words: | ||
|
|
||
| - `+`, `-`, `*`, `/` (integer arithmetic) | ||
| - `DUP`, `DROP`, `SWAP`, `OVER` (stack manipulation) | ||
|
|
||
| Your evaluator also has to support defining new words using the customary syntax: `: word-name definition ;`. | ||
|
|
||
| To keep things simple the only data type you need to support is signed integers of at least 16 bits size. | ||
|
|
||
| You should use the following rules for the syntax: a number is a sequence of one or more (ASCII) digits, a word is a sequence of one or more letters, digits, symbols or punctuation that is not a number. | ||
| (Forth probably uses slightly different rules, but this is close enough.) | ||
|
|
||
| Words are case-insensitive. | ||
|
|
||
| [forth]: https://en.wikipedia.org/wiki/Forth_%28programming_language%29 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "authors": [ | ||
| "quintuple-mallard" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "forth.nu" | ||
| ], | ||
| "test": [ | ||
| "tests.nu" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.nu" | ||
| ] | ||
| }, | ||
| "blurb": "Implement an evaluator for a very simple subset of Forth." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| def macro? [item] { | ||
| let trimmed = ($item | str trim) | ||
| ($trimmed | find --regex "^:\\s+[^\\s]+\\s+.+\\s+;$") == $trimmed | ||
| } | ||
|
|
||
| def parse_definition [def: string] { | ||
| let parsed = ($def | parse ": {name} {body} ;") | ||
| let name = ($parsed.name | get 0 | str trim | str downcase) | ||
| let tokens = ($parsed.body | get 0 | str trim | split row " ") | ||
| { | ||
| name: $name, | ||
| tokens: $tokens | ||
| } | ||
| } | ||
|
|
||
| def replace_tokens [tokens: list<string>, defname: string, definition: list<string>] { | ||
| mut result = [] | ||
| for token in $tokens { | ||
| let token_lower = ($token | str downcase | str trim) | ||
| let name_lower = ($defname | str downcase | str trim) | ||
| if $token_lower == $name_lower { | ||
| for deftoken in $definition { | ||
| $result = ($result | append $deftoken) | ||
| } | ||
| } else { | ||
| $result = ($result | append $token) | ||
| } | ||
| } | ||
| $result | ||
| } | ||
|
|
||
| def expand_macros [instructions: list<string>] { | ||
| mut defs = [] | ||
| mut program = [] | ||
|
|
||
| for instr in $instructions { | ||
| if (macro? $instr) { | ||
| let def = parse_definition $instr | ||
| if ($def.name | find --regex "^-?\\d+$") == $def.name { | ||
| error make {msg: "illegal operation"} | ||
| } | ||
| mut tokens = $def.tokens | ||
| for prev_def in $defs { | ||
| $tokens = (replace_tokens $tokens $prev_def.name $prev_def.tokens) | ||
| } | ||
| let matches = ($defs | where name == $def.name) | ||
| if ($matches | length) > 0 { | ||
| $defs = ($defs | where name != $def.name | append { | ||
| name: $def.name, | ||
| tokens: $tokens | ||
| }) | ||
| } else { | ||
| $defs = ($defs | append { | ||
| name: $def.name, | ||
| tokens: $tokens | ||
| }) | ||
| } | ||
| } else { | ||
| $program = ($program | append $instr) | ||
| } | ||
| } | ||
|
|
||
| mut result = [] | ||
| for line in $program { | ||
| mut tokens = ($line | split row " ") | ||
| mut changed = true | ||
|
|
||
| while $changed { | ||
| $changed = false | ||
| mut next = [] | ||
|
|
||
| for token in $tokens { | ||
| let token_lower = ($token | str downcase) | ||
| let matches = ($defs | where name == $token_lower) | ||
| if ($matches | length) > 0 { | ||
| let match = ($matches | first) | ||
| for deftoken in $match.tokens { | ||
| $next = ($next | append $deftoken) | ||
| } | ||
| $changed = true | ||
| } else { | ||
| $next = ($next | append $token) | ||
| } | ||
| } | ||
| $tokens = $next | ||
| } | ||
|
|
||
| $result = ($result | append $tokens) | ||
| } | ||
|
|
||
| $result | flatten | str join " " | ||
| } | ||
|
|
||
| def perform [stack: list<int>, action: string] { | ||
| if ($action | find --regex "-?\\d") == $action { # Literals | ||
| $stack | append ($action | into int) | ||
| } else if $action in ["+", "-", "*", "/", "over", "swap"] { # Binary actions | ||
| match ($stack | length) { | ||
| 0 => (error make {msg: "empty stack"}) | ||
| 1 => (error make {msg: "only one value on the stack"}) | ||
| } | ||
| match $action { | ||
| "+" => ( | ||
| ( | ||
| ( | ||
| ($stack | get (($stack | length) - 2)) | ||
| ) + ($stack | get (($stack | length) - 1)) | ||
| ) | prepend ($stack | slice ..-3) | ||
| ) | ||
| "-" => ( | ||
| ( | ||
| ( | ||
| ($stack | get (($stack | length) - 2)) | ||
| ) - ($stack | get (($stack | length) - 1)) | ||
| ) | prepend ($stack | slice ..-3) | ||
| ) | ||
| "*" => ( | ||
| ( | ||
| ( | ||
| ($stack | get (($stack | length) - 2)) | ||
| ) * ($stack | get (($stack | length) - 1)) | ||
| ) | prepend ($stack | slice ..-3) | ||
| ) | ||
| "/" => ( | ||
| try { | ||
| ( | ||
| ( | ||
| (($stack | get (($stack | length) - 2))) // ($stack | get (($stack | length) - 1)) | ||
| ) | prepend ($stack | slice ..-3) | ||
| ) | ||
| } catch { | ||
| error make {msg: "divide by zero"} | ||
| } | ||
| ) | ||
| "over" => ([ | ||
| ($stack | get (($stack | length) - 2)), | ||
| ($stack | get (($stack | length) - 1)), | ||
| ($stack | get (($stack | length) - 2)) | ||
| ] | prepend ($stack | slice ..-3)) | ||
| "swap" => ([ | ||
| ($stack | get (($stack | length) - 1)), | ||
| ($stack | get (($stack | length) - 2)) | ||
| ] | prepend ($stack | slice ..-3)) | ||
| } | ||
| } else if $action in ["dup", "drop"] { | ||
| if ($stack | length) == 0 { | ||
| error make {msg: "empty stack"} | ||
| } | ||
| match $action { | ||
| "dup" => ([ | ||
| ($stack | get (($stack | length) - 1)), | ||
| ($stack | get (($stack | length) - 1)) | ||
| ] | prepend ($stack | slice ..-2)) | ||
| "drop" => ($stack | slice ..-2) | ||
| } | ||
| } else { | ||
| error make {msg: "undefined operation"} | ||
| } | ||
| } | ||
|
|
||
| export def evaluate [instructions: list<string>] { | ||
| let program_str = expand_macros $instructions | ||
| let tokens = ($program_str | split row " " | where {|x| $x != ""}) | ||
|
|
||
| mut stack: list<int> = [] | ||
| for token in $tokens { | ||
| let norm = ($token | str downcase | str trim) | ||
| $stack = perform $stack $norm | ||
| } | ||
| $stack | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.