github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/parser/opcode/opcode.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package opcode
    15  
    16  import (
    17  	"github.com/vescale/zgraph/parser/format"
    18  )
    19  
    20  // Op is opcode type.
    21  type Op int
    22  
    23  // List operators.
    24  const (
    25  	LogicAnd Op = iota + 1
    26  	LogicOr
    27  	GE
    28  	LE
    29  	EQ
    30  	NE
    31  	LT
    32  	GT
    33  	Plus
    34  	Minus
    35  	And
    36  	Or
    37  	Mod
    38  	Xor
    39  	Div
    40  	Mul
    41  	Not
    42  	IntDiv
    43  	LogicXor
    44  	NullEQ
    45  	In
    46  	Case
    47  	Regexp
    48  	IsNull
    49  	IsTruth
    50  	IsFalsity
    51  	Concat
    52  )
    53  
    54  var ops = [...]struct {
    55  	name      string
    56  	literal   string
    57  	isKeyword bool
    58  }{
    59  	LogicAnd: {
    60  		name:      "and",
    61  		literal:   "AND",
    62  		isKeyword: true,
    63  	},
    64  	LogicOr: {
    65  		name:      "or",
    66  		literal:   "OR",
    67  		isKeyword: true,
    68  	},
    69  	LogicXor: {
    70  		name:      "xor",
    71  		literal:   "XOR",
    72  		isKeyword: true,
    73  	},
    74  	GE: {
    75  		name:      "ge",
    76  		literal:   ">=",
    77  		isKeyword: false,
    78  	},
    79  	LE: {
    80  		name:      "le",
    81  		literal:   "<=",
    82  		isKeyword: false,
    83  	},
    84  	EQ: {
    85  		name:      "eq",
    86  		literal:   "=",
    87  		isKeyword: false,
    88  	},
    89  	NE: {
    90  		name:      "ne",
    91  		literal:   "<>",
    92  		isKeyword: false,
    93  	},
    94  	LT: {
    95  		name:      "lt",
    96  		literal:   "<",
    97  		isKeyword: false,
    98  	},
    99  	GT: {
   100  		name:      "gt",
   101  		literal:   ">",
   102  		isKeyword: false,
   103  	},
   104  	Plus: {
   105  		name:      "plus",
   106  		literal:   "+",
   107  		isKeyword: false,
   108  	},
   109  	Minus: {
   110  		name:      "minus",
   111  		literal:   "-",
   112  		isKeyword: false,
   113  	},
   114  	And: {
   115  		name:      "bitand",
   116  		literal:   "&",
   117  		isKeyword: false,
   118  	},
   119  	Or: {
   120  		name:      "bitor",
   121  		literal:   "|",
   122  		isKeyword: false,
   123  	},
   124  	Mod: {
   125  		name:      "mod",
   126  		literal:   "%",
   127  		isKeyword: false,
   128  	},
   129  	Xor: {
   130  		name:      "bitxor",
   131  		literal:   "^",
   132  		isKeyword: false,
   133  	},
   134  	Div: {
   135  		name:      "div",
   136  		literal:   "/",
   137  		isKeyword: false,
   138  	},
   139  	Mul: {
   140  		name:      "mul",
   141  		literal:   "*",
   142  		isKeyword: false,
   143  	},
   144  	Not: {
   145  		name:      "not",
   146  		literal:   "not ",
   147  		isKeyword: true,
   148  	},
   149  	IntDiv: {
   150  		name:      "intdiv",
   151  		literal:   "DIV",
   152  		isKeyword: true,
   153  	},
   154  	NullEQ: {
   155  		name:      "nulleq",
   156  		literal:   "<=>",
   157  		isKeyword: false,
   158  	},
   159  	In: {
   160  		name:      "in",
   161  		literal:   "IN",
   162  		isKeyword: true,
   163  	},
   164  	Case: {
   165  		name:      "case",
   166  		literal:   "CASE",
   167  		isKeyword: true,
   168  	},
   169  	Regexp: {
   170  		name:      "regexp",
   171  		literal:   "REGEXP",
   172  		isKeyword: true,
   173  	},
   174  	IsNull: {
   175  		name:      "isnull",
   176  		literal:   "IS NULL",
   177  		isKeyword: true,
   178  	},
   179  	IsTruth: {
   180  		name:      "istrue",
   181  		literal:   "IS TRUE",
   182  		isKeyword: true,
   183  	},
   184  	IsFalsity: {
   185  		name:      "isfalse",
   186  		literal:   "IS FALSE",
   187  		isKeyword: true,
   188  	},
   189  	Concat: {
   190  		name:      "concat",
   191  		literal:   "||",
   192  		isKeyword: false,
   193  	},
   194  }
   195  
   196  // String implements Stringer interface.
   197  func (o Op) String() string {
   198  	return ops[o].name
   199  }
   200  
   201  // IsKeyword returns whether the operator is a keyword.
   202  func (o Op) IsKeyword() bool {
   203  	return ops[o].isKeyword
   204  }
   205  
   206  // Restore the Op into a Writer
   207  func (o Op) Restore(ctx *format.RestoreCtx) error {
   208  	info := &ops[o]
   209  	if info.isKeyword {
   210  		ctx.WriteKeyWord(info.literal)
   211  	} else {
   212  		ctx.WritePlain(info.literal)
   213  	}
   214  	return nil
   215  }