diff options
| author | Marc Vertes <mvertes@free.fr> | 2024-04-02 11:27:13 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-02 11:27:13 +0200 |
| commit | 1bff92c52b27b9a516599e172fe9852c3d99be38 (patch) | |
| tree | 26b30b5ec1a5537dcafd806d23e03a062475705d /lang/token.go | |
| parent | 362f7c9c45598b429c92e67756f41b690043e0c4 (diff) | |
chore: add linters and some lint fixes (#8)
* chore: add linters and some lint fixes
Configure some golangci-lint linters to get the code quality right.
Apply the first fixes.
Next step will be to add github actions to run lint and tests in
github CI.
* chore: more lint, fixed comments and variable names. no semantic change.
* chore: add Makefile
This makefile is intended to be used as a local substitute to github
actions.
Diffstat (limited to 'lang/token.go')
| -rw-r--r-- | lang/token.go | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/lang/token.go b/lang/token.go index 613f2c6..7ad7bf1 100644 --- a/lang/token.go +++ b/lang/token.go @@ -1,23 +1,25 @@ package lang -//go:generate stringer -type=TokenId +//go:generate stringer -type=Token -type TokenId int +// Token represents a lexical token. +type Token int +// All known tokens for the set of supported languages. const ( - Illegal TokenId = iota + Illegal Token = iota Comment Ident - // Literal values + // Literal values. Char Float Imag Int String - // Binary operators (except indicated) - // Arithmetic and bitwise binary operators + // Binary operators (except indicated). + // Arithmetic and bitwise binary operators. Add // + Sub // - Mul // * @@ -31,7 +33,7 @@ const ( AndNot // &^ Period // . - // Binary operators returning a boolean + // Binary operators returning a boolean. Equal // == Greater // > GreaterEqual // >= @@ -41,7 +43,7 @@ const ( Lor // || NotEqual // != - // Assigment operators (arithmetic and bitwise) + // Assigment operators (arithmetic and bitwise). Define // := Assign // = AddAssign // += @@ -58,7 +60,7 @@ const ( Inc // ++ Dec // -- - // Unary operations + // Unary operations. Plus // unary + Minus // unary - Addr // unary & @@ -69,17 +71,17 @@ const ( Not // unary ! Tilde // unary ~ (underlying type) - // Separators (punctuation) + // Separators (punctuation). Comma // , Semicolon // ; Colon // : - // Block tokens + // Block tokens. ParenBlock // (..) BracketBlock // [..] BraceBlock // {..} - // Reserved keywords + // Reserved keywords. Break Case Chan @@ -106,7 +108,7 @@ const ( Type Var - // Internal virtual machine tokens (no corresponding keyword) + // Internal virtual machine tokens (no corresponding keyword). Call CallX EqualSet @@ -119,8 +121,9 @@ const ( New ) -// TODO: define UnaryOp per language -var UnaryOp = map[TokenId]TokenId{ +// UnaryOp contains the set of unary operators. +// TODO: define UnaryOp per language. +var UnaryOp = map[Token]Token{ Add: Plus, // + And: Addr, // & Not: Not, // ! @@ -130,11 +133,26 @@ var UnaryOp = map[TokenId]TokenId{ Xor: BitComp, // ^ } -func (t TokenId) IsKeyword() bool { return t >= Break && t <= Var } -func (t TokenId) IsLiteral() bool { return t >= Char && t <= String } -func (t TokenId) IsOperator() bool { return t >= Add && t <= Tilde } -func (t TokenId) IsBlock() bool { return t >= ParenBlock && t <= BraceBlock } -func (t TokenId) IsBoolOp() bool { return t >= Equal && t <= NotEqual || t == Not } -func (t TokenId) IsBinaryOp() bool { return t >= Add && t <= NotEqual } -func (t TokenId) IsUnaryOp() bool { return t >= Plus && t <= Tilde } -func (t TokenId) IsLogicalOp() bool { return t == Land || t == Lor } +// IsKeyword returns true if t is a keyword. +func (t Token) IsKeyword() bool { return t >= Break && t <= Var } + +// IsLiteral returns true if t is a literal value. +func (t Token) IsLiteral() bool { return t >= Char && t <= String } + +// IsOperator returns true if t is an operator. +func (t Token) IsOperator() bool { return t >= Add && t <= Tilde } + +// IsBlock returns true if t is a block kind of token. +func (t Token) IsBlock() bool { return t >= ParenBlock && t <= BraceBlock } + +// IsBoolOp returns true if t is boolean operator. +func (t Token) IsBoolOp() bool { return t >= Equal && t <= NotEqual || t == Not } + +// IsBinaryOp returns true if t is a binary operator (takes 2 operands). +func (t Token) IsBinaryOp() bool { return t >= Add && t <= NotEqual } + +// IsUnaryOp returns true if t is an unary operator (takes 1 operand). +func (t Token) IsUnaryOp() bool { return t >= Plus && t <= Tilde } + +// IsLogicalOp returns true if t is a logical operator. +func (t Token) IsLogicalOp() bool { return t == Land || t == Lor } |
