Skip to content

Feature/parsing - #14

Open
hayleigh-dot-dev wants to merge 67 commits into
masterfrom
feature/parsing
Open

Feature/parsing#14
hayleigh-dot-dev wants to merge 67 commits into
masterfrom
feature/parsing

Conversation

@hayleigh-dot-dev

@hayleigh-dot-dev hayleigh-dot-dev commented Jul 20, 2019

Copy link
Copy Markdown
Collaborator

Parsing

This PR adds proper command parsing support such that we can take user input like move 3 north and wrangle that into some data that the game can actually understand. This is achieved by a multistep process:

  • The input string is split on whitespace and each word is converted into one of a handful of basic tokens. Commands such as /help or walk are encoded as commands, but everything else becomes a basic Word token.
  • A pass of the newly created token stream attempts to contextualise Words by matching them against all known game terms in a dictionary. This has provisions for multi-word concepts such as the item dragon claws.
  • The now contextualised token stream is run against so-called templates. Templates are built on predicate functions that check atomic qualities of a token, for example...
[ any([ isExact("walk"), isExact("move") ]), hasType(TokenType.Direction) ]
  • ... Ideally templates are human-readable enough that whoever is reading this PR can get some basic understanding of what this is doing.

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 Result type has also been created.

export type Result<E, A>
  = Ok<A>
  | Err<E>

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 withDefault function that unboxes a Result that succeeded, or returns a default value if it error'd out. There's also map and mapErr respectively that can transform the value contained in a result much like you can map over values in a list.

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 Trymunx left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

You're missing some new lines at the end of files.

Comment thread src/game/Commands/Result.ts Outdated
Comment thread src/game/Commands/Token.ts Outdated
Comment thread src/game/Commands/lexer.ts Outdated
Comment thread src/game/Commands/templates.ts Outdated
@hayleigh-dot-dev hayleigh-dot-dev added the enhancement New feature or request label Jul 21, 2019

@Alpvax Alpvax left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not issues, but possibly things to improve. Feel free to ignore if you disagree.

Comment thread src/game/commands/templates.ts
Comment thread src/game/state/gStates/gsMain.ts Outdated
Comment thread src/game/commands/dictionary.ts
"flee",
"move",
"run",
"walk",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Adding directions to this list would allow leaving off the move command, retaining existing functionality.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Yup, that's already been discussed but will be added later.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants