aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/shell/state.go
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2025-06-14 20:52:21 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2025-06-15 11:51:16 -0400
commit6b71d6419505546bdffd1ce3295fb3e01d466449 (patch)
treef788b620e38286c0177c189dcca7ebc2ce64b19c /pkg/shell/state.go
parent97a2d99d0a3f1609d3d2264e4e54c119ec3801ff (diff)
Add query optimizer
Diffstat (limited to 'pkg/shell/state.go')
-rw-r--r--pkg/shell/state.go60
1 files changed, 43 insertions, 17 deletions
diff --git a/pkg/shell/state.go b/pkg/shell/state.go
index ea62604..bd7ecc8 100644
--- a/pkg/shell/state.go
+++ b/pkg/shell/state.go
@@ -12,10 +12,11 @@ import (
type ValueType int
const (
- INVALID ValueType = iota
- STRING
- TOKENS
- CLAUSE
+ VAL_INVALID ValueType = iota
+ VAL_INT
+ VAL_STRING
+ VAL_TOKENS
+ VAL_CLAUSE
)
type Value struct {
@@ -25,27 +26,50 @@ type Value struct {
type State map[string]Value
+func (t ValueType) String() string {
+ switch t {
+ case VAL_INVALID:
+ return "Invalid"
+ case VAL_INT:
+ return "Integer"
+ case VAL_STRING:
+ return "String"
+ case VAL_TOKENS:
+ return "Tokens"
+ case VAL_CLAUSE:
+ return "Clause"
+ default:
+ return "Unkown"
+ }
+}
+
func (v Value) String() string {
switch v.Type {
- case STRING:
+ case VAL_INT:
+ i, ok := v.Val.(int)
+ if !ok {
+ return "Corrupted Type (expected int)"
+ }
+ return fmt.Sprint(i)
+ case VAL_STRING:
s, ok := v.Val.(string)
if !ok {
return "Corrupted Type (expected string)"
}
return s
- case TOKENS:
+ case VAL_TOKENS:
ts, ok := v.Val.([]query.Token)
if !ok {
return "Corrupted Type (expected []query.Token)"
}
return query.TokensStringify(ts)
- case CLAUSE:
+ case VAL_CLAUSE:
rootClause, ok := v.Val.(*query.Clause)
if !ok {
- return "Corrupted Type (expected query.Clause)"
+ return "Corrupted Type (expected *query.Clause)"
}
return rootClause.String()
- case INVALID:
+ case VAL_INVALID:
return "Invalid"
}
return fmt.Sprintf("Unknown @ %p", v.Val)
@@ -58,13 +82,15 @@ func (s State) String() string {
b.WriteString(k)
b.WriteByte(':')
switch v.Type {
- case INVALID:
+ case VAL_INVALID:
b.WriteString(" Invalid")
- case STRING:
+ case VAL_INT:
+ b.WriteString(" Integer")
+ case VAL_STRING:
b.WriteString(" String")
- case TOKENS:
+ case VAL_TOKENS:
b.WriteString(" Tokens")
- case CLAUSE:
+ case VAL_CLAUSE:
b.WriteString(" Clause")
default:
fmt.Fprintf(&b, " Unknown (%d)", v.Val)
@@ -88,7 +114,7 @@ func (s State) CmdTokenize(input string) (Value, bool) {
if !ok {
fmt.Fprintln(os.Stderr, "Cannot tokenize: no variable with name", input)
return Value{}, false
- } else if variable.Type != STRING {
+ } else if variable.Type != VAL_STRING {
fmt.Fprintln(os.Stderr, "Cannot tokenize: variable is not a string")
return Value{}, false
}
@@ -101,7 +127,7 @@ func (s State) CmdTokenize(input string) (Value, bool) {
}
}
tokens := query.Lex(rawQuery)
- return Value{TOKENS, tokens}, true
+ return Value{VAL_TOKENS, tokens}, true
}
func (s State) CmdParse(args string) (Value, error) {
@@ -121,7 +147,7 @@ func (s State) CmdParse(args string) (Value, error) {
if !ok {
fmt.Fprintln(os.Stderr, "Cannot parse: no variable with name", args)
return Value{}, errors.New("variable does not exist")
- } else if variable.Type != TOKENS {
+ } else if variable.Type != VAL_TOKENS {
fmt.Fprintln(os.Stderr, "Cannot parse: variable is not []query.Tokens")
return Value{}, errors.New("bad variable type")
}
@@ -138,5 +164,5 @@ func (s State) CmdParse(args string) (Value, error) {
if err != nil {
return Value{}, err
}
- return Value{CLAUSE, *clause}, err
+ return Value{VAL_CLAUSE, *clause}, err
}