Skip to content
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

scanner: Change Token to Interface Type #181

Open
DeedleFake opened this issue Feb 12, 2019 · 0 comments
Open

scanner: Change Token to Interface Type #181

DeedleFake opened this issue Feb 12, 2019 · 0 comments

Comments

@DeedleFake
Copy link
Owner

DeedleFake commented Feb 12, 2019

Currently, scanner.Token is a struct with four fields, line number, column, type, and value. Especially with the new macro system, this is not an ideal structure for a number of reasons:

  • In at least one place, token information is passed around for just the type and value, and then the other fields are filled in later. This is awkward because it means that the user can set fields with data that is just simply ignored, but seems like it wouldn't necessarily be.
  • Separating type and value info is mostly pointless. That information can be handled by the Go type system instead, which would also prevent invalid types and, more importantly, invalid type and value combinations.

Instead of this, I think it makes sense to replace the current Token type with one something like the following:

type Token struct {
  Line, Col int
  Val TokenValue
}

type TokenValue interface {
  token()
  String() string
}

The various token types can then be implemented as concrete types that represent their actual values. This means as well that the Macro token type can be changed more simply from a [2]string to a struct { Name string; Input string }, and macros themselves can return []TokenValues instead of []Tokens.

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

No branches or pull requests

1 participant