2023-10-31 01:43:18 +00:00
|
|
|
/*
|
|
|
|
* Salix - Go templating engine
|
|
|
|
* Copyright (C) 2023 Elara Musayelyan
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-10-29 01:10:17 +00:00
|
|
|
package salix
|
|
|
|
|
|
|
|
import (
|
2023-10-31 00:16:45 +00:00
|
|
|
"errors"
|
2023-10-29 01:10:17 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2023-10-31 01:38:53 +00:00
|
|
|
"go.elara.ws/salix/ast"
|
2023-10-29 01:10:17 +00:00
|
|
|
)
|
|
|
|
|
2023-10-31 00:16:45 +00:00
|
|
|
var (
|
|
|
|
ErrModulusFloat = errors.New("modulo operation cannot be performed on floats")
|
|
|
|
ErrTypeMismatch = errors.New("mismatched types")
|
|
|
|
ErrLogicalNonBool = errors.New("logical operations may only be performed on boolean values")
|
|
|
|
ErrInOpInvalidTypes = errors.New("the in operator can only be used on strings, arrays, and slices")
|
|
|
|
)
|
|
|
|
|
2023-10-29 01:10:17 +00:00
|
|
|
func (t *Template) evalExpr(expr ast.Expr, local map[string]any) (any, error) {
|
2023-10-30 22:02:41 +00:00
|
|
|
val, err := t.getValue(expr.First, local)
|
2023-10-29 01:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
a := reflect.ValueOf(val)
|
|
|
|
|
|
|
|
for _, exprB := range expr.Rest {
|
2023-10-30 22:02:41 +00:00
|
|
|
val, err := t.getValue(exprB.First, local)
|
2023-10-29 01:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b := reflect.ValueOf(val)
|
2023-10-31 00:16:45 +00:00
|
|
|
|
|
|
|
result, err := t.performOp(a, b, exprB.Operator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a = reflect.ValueOf(result)
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.Interface(), nil
|
|
|
|
}
|
|
|
|
|
2023-10-31 00:16:45 +00:00
|
|
|
func (t *Template) performOp(a, b reflect.Value, op ast.Operator) (any, error) {
|
|
|
|
if op.Value == "in" {
|
2023-10-29 01:10:17 +00:00
|
|
|
switch b.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
if a.CanConvert(b.Type().Elem()) {
|
|
|
|
a = a.Convert(b.Type().Elem())
|
|
|
|
} else {
|
2023-10-31 00:16:45 +00:00
|
|
|
return nil, t.posError(op, "%w (%s and %s)", ErrTypeMismatch, a.Type(), b.Type())
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
if a.Kind() != reflect.String {
|
2023-10-31 00:16:45 +00:00
|
|
|
return nil, t.posError(op, "%w (%s and %s)", ErrTypeMismatch, a.Type(), b.Type())
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
2023-10-31 00:16:45 +00:00
|
|
|
default:
|
|
|
|
return nil, t.posError(op, "%w (got %s and %s)", ErrInOpInvalidTypes, a.Type(), b.Type())
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
} else if b.CanConvert(a.Type()) {
|
|
|
|
b = b.Convert(a.Type())
|
|
|
|
} else {
|
2023-10-31 00:16:45 +00:00
|
|
|
return nil, t.posError(op, "%w (%s and %s)", ErrTypeMismatch, a.Type(), b.Type())
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 00:16:45 +00:00
|
|
|
switch op.Value {
|
2023-10-29 01:10:17 +00:00
|
|
|
case "==":
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Equal(b), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case "&&":
|
|
|
|
if a.Kind() != reflect.Bool || b.Kind() != reflect.Bool {
|
2023-10-31 00:16:45 +00:00
|
|
|
return nil, t.posError(op, "%w", ErrLogicalNonBool)
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Bool() && b.Bool(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case "||":
|
|
|
|
if a.Kind() != reflect.Bool || b.Kind() != reflect.Bool {
|
2023-10-31 00:16:45 +00:00
|
|
|
return nil, t.posError(op, "%w", ErrLogicalNonBool)
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Bool() || b.Bool(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case ">=":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() >= b.Int(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() >= b.Uint(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Float() >= b.Float(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
case "<=":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() <= b.Int(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() <= b.Uint(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Float() <= b.Float(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
case ">":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() > b.Int(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() > b.Uint(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Float() > b.Float(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
case "<":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() < b.Int(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() < b.Uint(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Float() < b.Float(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
case "+":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() + b.Int(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() + b.Uint(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Float() + b.Float(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
case "-":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() - b.Int(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() - b.Uint(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Float() - b.Float(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
case "*":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() * b.Int(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() * b.Uint(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Float() * b.Float(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
case "/":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() / b.Int(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() / b.Uint(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Float() / b.Float(), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
2023-10-30 23:59:37 +00:00
|
|
|
case "%":
|
|
|
|
switch a.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Int() % b.Int(), nil
|
2023-10-30 23:59:37 +00:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2023-10-31 00:16:45 +00:00
|
|
|
return a.Uint() % b.Uint(), nil
|
2023-10-30 23:59:37 +00:00
|
|
|
case reflect.Float64, reflect.Float32:
|
2023-10-31 00:16:45 +00:00
|
|
|
return nil, t.posError(op, "%w", ErrModulusFloat)
|
|
|
|
}
|
2023-10-29 01:10:17 +00:00
|
|
|
case "in":
|
|
|
|
if a.Kind() == reflect.String && b.Kind() == reflect.String {
|
2023-10-31 00:16:45 +00:00
|
|
|
return strings.Contains(b.String(), a.String()), nil
|
2023-10-29 01:10:17 +00:00
|
|
|
} else {
|
|
|
|
for i := 0; i < b.Len(); i++ {
|
|
|
|
if a.Equal(b.Index(i)) {
|
2023-10-31 00:16:45 +00:00
|
|
|
return true, nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-31 00:16:45 +00:00
|
|
|
return false, nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-31 00:16:45 +00:00
|
|
|
return false, nil
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|