Lookaheads

Now, we don’t want to explicitly check each character in our program against our list of possible tokens manually, so lets create a basic helper function.

function lookaheadString (str: string): boolean {
  const parts = str.split('')

  for (let i = 0; i < parts.length; i++) {
    if (input[currentPosition + i] !== parts[i]) {
      return false
    }
  }

  return true
}

A couple of our tokens can be matched against their string equivalent:

  • new
  • print
  • =
  • line breaks

Some of our more advanced tokens - ones that might have other tokens between them (think comments), or ones that require us to capture their values (like literals) - will be processed separately.

This lookaheadString function will take a string, and then 'look ahead' in our input to see if the proceeding characters, after our current position, match.

Something to note is that we're using currentPosition + i instead of currentPositio++. We want don't actually want to increment currentPosition unless our string completely matches, as we'll need to roll back to what currentPosition started as to continue checking strings.

To help facilitate us matching strings to tokens, lets create a map for our tokens:

const tokenStringMap: Array<{
  key: string,
  value: Token
}> = [
  { key: '\n', value: { type: TokenType.LineBreak } },
  { key: 'new', value: { type: TokenType.VariableDeclaration } },
  { key: '=', value: { type: TokenType.AssignmentOperator } },
  { key: 'print', value: { type: TokenType.ConsoleLog } }
]

Now that we have this, we can use it to loop over things, and actually start processing our input.