Feature/parsing - #14
Conversation
WIP code that allows for tokenising messages typed by the user into something more readily usable programmatically. A 'template' system allows commands to be described in terms of the *types* of values they contain rather than matching against specific values.
Trymunx
left a comment
There was a problem hiding this comment.
You're missing some new lines at the end of files.
…ple boolean checks.
…e sort of the same directory but not. ... also bend to the relentless will of our overload, eslint.
…ert the token value to a Number if it can.
Alpvax
left a comment
There was a problem hiding this comment.
Not issues, but possibly things to improve. Feel free to ignore if you disagree.
| "flee", | ||
| "move", | ||
| "run", | ||
| "walk", |
There was a problem hiding this comment.
Adding directions to this list would allow leaving off the move command, retaining existing functionality.
There was a problem hiding this comment.
Yup, that's already been discussed but will be added later.
There was a problem hiding this comment.
Allowing directions to be commands in their own right is doable, but it'll add some extra complexity to the parser as the position of a token would influence whether it is X or Y (Command or Direction in this case).
It's probably a good thing to have though, so I'll think about a reasonable way of implementing that.
Parsing
This PR adds proper command parsing support such that we can take user input like
move 3 northand wrangle that into some data that the game can actually understand. This is achieved by a multistep process:/helporwalkare encoded as commands, but everything else becomes a basic Word token.dragon claws.These steps will be updated as they are created.
Result Type
In addition to the types and code that immediately facilitates these features, a generic
Resulttype has also been created.I'll go easy on the specifics here (Alex has said he's already unlikely to make use of it elsewhere in the code) but the central idea is that Results model computations that can fail. And give us a robust mechanism for handling errors without throwing runtime exceptions.
Because the type itself is a union of two other types, the compiler should push the developer towards handling both cases whenever they are working with Results.
There are some useful functions defined alongside the type, including a
withDefaultfunction that unboxes a Result that succeeded, or returns a default value if it error'd out. There's alsomapandmapErrrespectively that can transform the value contained in a result much like you can map over values in a list.