mirror of
https://github.com/MontFerret/ferret.git
synced 2025-08-13 19:52:52 +02:00
New type system (#232)
* New type system * Fixed dot notation for HTML elements
This commit is contained in:
@@ -2,8 +2,10 @@ package operators
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -88,7 +90,7 @@ func (operator *ArrayOperator) Exec(ctx context.Context, scope *core.Scope) (cor
|
||||
}
|
||||
|
||||
func (operator *ArrayOperator) Eval(ctx context.Context, left, right core.Value) (core.Value, error) {
|
||||
err := core.ValidateType(left, core.ArrayType)
|
||||
err := core.ValidateType(left, types.Array)
|
||||
|
||||
if err != nil {
|
||||
// TODO: Return the error? AQL just returns false
|
||||
|
@@ -2,6 +2,7 @@ package operators
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
)
|
||||
|
||||
|
@@ -2,8 +2,10 @@ package operators
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
)
|
||||
|
||||
type InOperator struct {
|
||||
@@ -45,7 +47,7 @@ func (operator *InOperator) Exec(ctx context.Context, scope *core.Scope) (core.V
|
||||
}
|
||||
|
||||
func (operator *InOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
|
||||
err := core.ValidateType(right, core.ArrayType)
|
||||
err := core.ValidateType(right, types.Array)
|
||||
|
||||
if err != nil {
|
||||
// TODO: Return the error? AQL just returns false
|
||||
|
@@ -2,8 +2,10 @@ package operators
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -70,7 +72,7 @@ func (operator *LogicalOperator) Exec(ctx context.Context, scope *core.Scope) (c
|
||||
leftBool := values.ToBoolean(left)
|
||||
|
||||
if operator.value == LogicalOperatorTypeAnd && leftBool == values.False {
|
||||
if left.Type() == core.BooleanType {
|
||||
if left.Type() == types.Boolean {
|
||||
return values.False, nil
|
||||
}
|
||||
|
||||
@@ -98,7 +100,7 @@ func (operator *LogicalOperator) Eval(_ context.Context, left, right core.Value)
|
||||
leftBool := values.ToBoolean(left)
|
||||
|
||||
if operator.value == LogicalOperatorTypeAnd && leftBool == values.False {
|
||||
if left.Type() == core.BooleanType {
|
||||
if left.Type() == types.Boolean {
|
||||
return values.False, nil
|
||||
}
|
||||
|
||||
|
@@ -2,8 +2,10 @@ package operators
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -89,15 +91,15 @@ func Not(left, _ core.Value) core.Value {
|
||||
// Adds numbers
|
||||
// Concats strings
|
||||
func Add(left, right core.Value) core.Value {
|
||||
if left.Type() == core.IntType {
|
||||
if right.Type() == core.IntType {
|
||||
if left.Type() == types.Int {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Int)
|
||||
|
||||
return l + r
|
||||
}
|
||||
|
||||
if right.Type() == core.FloatType {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Float)
|
||||
|
||||
@@ -105,15 +107,15 @@ func Add(left, right core.Value) core.Value {
|
||||
}
|
||||
}
|
||||
|
||||
if left.Type() == core.FloatType {
|
||||
if right.Type() == core.FloatType {
|
||||
if left.Type() == types.Float {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Float)
|
||||
|
||||
return l + r
|
||||
}
|
||||
|
||||
if right.Type() == core.IntType {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Int)
|
||||
|
||||
@@ -125,15 +127,15 @@ func Add(left, right core.Value) core.Value {
|
||||
}
|
||||
|
||||
func Subtract(left, right core.Value) core.Value {
|
||||
if left.Type() == core.IntType {
|
||||
if right.Type() == core.IntType {
|
||||
if left.Type() == types.Int {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Int)
|
||||
|
||||
return l - r
|
||||
}
|
||||
|
||||
if right.Type() == core.FloatType {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Float)
|
||||
|
||||
@@ -141,15 +143,15 @@ func Subtract(left, right core.Value) core.Value {
|
||||
}
|
||||
}
|
||||
|
||||
if left.Type() == core.FloatType {
|
||||
if right.Type() == core.FloatType {
|
||||
if left.Type() == types.Float {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Float)
|
||||
|
||||
return l - r
|
||||
}
|
||||
|
||||
if right.Type() == core.IntType {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Int)
|
||||
|
||||
@@ -161,15 +163,15 @@ func Subtract(left, right core.Value) core.Value {
|
||||
}
|
||||
|
||||
func Multiply(left, right core.Value) core.Value {
|
||||
if left.Type() == core.IntType {
|
||||
if right.Type() == core.IntType {
|
||||
if left.Type() == types.Int {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Int)
|
||||
|
||||
return l * r
|
||||
}
|
||||
|
||||
if right.Type() == core.FloatType {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Float)
|
||||
|
||||
@@ -177,15 +179,15 @@ func Multiply(left, right core.Value) core.Value {
|
||||
}
|
||||
}
|
||||
|
||||
if left.Type() == core.FloatType {
|
||||
if right.Type() == core.FloatType {
|
||||
if left.Type() == types.Float {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Float)
|
||||
|
||||
return l * r
|
||||
}
|
||||
|
||||
if right.Type() == core.IntType {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Int)
|
||||
|
||||
@@ -197,15 +199,15 @@ func Multiply(left, right core.Value) core.Value {
|
||||
}
|
||||
|
||||
func Divide(left, right core.Value) core.Value {
|
||||
if left.Type() == core.IntType {
|
||||
if right.Type() == core.IntType {
|
||||
if left.Type() == types.Int {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Int)
|
||||
|
||||
return l / r
|
||||
}
|
||||
|
||||
if right.Type() == core.FloatType {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Float)
|
||||
|
||||
@@ -213,15 +215,15 @@ func Divide(left, right core.Value) core.Value {
|
||||
}
|
||||
}
|
||||
|
||||
if left.Type() == core.FloatType {
|
||||
if right.Type() == core.FloatType {
|
||||
if left.Type() == types.Float {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Float)
|
||||
|
||||
return l / r
|
||||
}
|
||||
|
||||
if right.Type() == core.IntType {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Int)
|
||||
|
||||
@@ -233,15 +235,15 @@ func Divide(left, right core.Value) core.Value {
|
||||
}
|
||||
|
||||
func Modulus(left, right core.Value) core.Value {
|
||||
if left.Type() == core.IntType {
|
||||
if right.Type() == core.IntType {
|
||||
if left.Type() == types.Int {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Int)
|
||||
|
||||
return l % r
|
||||
}
|
||||
|
||||
if right.Type() == core.FloatType {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Int)
|
||||
r := right.(values.Float)
|
||||
|
||||
@@ -249,15 +251,15 @@ func Modulus(left, right core.Value) core.Value {
|
||||
}
|
||||
}
|
||||
|
||||
if left.Type() == core.FloatType {
|
||||
if right.Type() == core.FloatType {
|
||||
if left.Type() == types.Float {
|
||||
if right.Type() == types.Float {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Float)
|
||||
|
||||
return values.Int(l) % values.Int(r)
|
||||
}
|
||||
|
||||
if right.Type() == core.IntType {
|
||||
if right.Type() == types.Int {
|
||||
l := left.(values.Float)
|
||||
r := right.(values.Int)
|
||||
|
||||
@@ -269,13 +271,13 @@ func Modulus(left, right core.Value) core.Value {
|
||||
}
|
||||
|
||||
func Increment(left, _ core.Value) core.Value {
|
||||
if left.Type() == core.IntType {
|
||||
if left.Type() == types.Int {
|
||||
l := left.(values.Int)
|
||||
|
||||
return l + 1
|
||||
}
|
||||
|
||||
if left.Type() == core.FloatType {
|
||||
if left.Type() == types.Float {
|
||||
l := left.(values.Float)
|
||||
|
||||
return l + 1
|
||||
@@ -285,13 +287,13 @@ func Increment(left, _ core.Value) core.Value {
|
||||
}
|
||||
|
||||
func Decrement(left, _ core.Value) core.Value {
|
||||
if left.Type() == core.IntType {
|
||||
if left.Type() == types.Int {
|
||||
l := left.(values.Int)
|
||||
|
||||
return l - 1
|
||||
}
|
||||
|
||||
if left.Type() == core.FloatType {
|
||||
if left.Type() == types.Float {
|
||||
l := left.(values.Float)
|
||||
|
||||
return l - 1
|
||||
@@ -301,13 +303,13 @@ func Decrement(left, _ core.Value) core.Value {
|
||||
}
|
||||
|
||||
func Negative(value, _ core.Value) core.Value {
|
||||
err := core.ValidateType(value, core.IntType, core.FloatType)
|
||||
err := core.ValidateType(value, types.Int, types.Float)
|
||||
|
||||
if err != nil {
|
||||
return values.ZeroInt
|
||||
}
|
||||
|
||||
if value.Type() == core.IntType {
|
||||
if value.Type() == types.Int {
|
||||
return -value.(values.Int)
|
||||
}
|
||||
|
||||
@@ -315,13 +317,13 @@ func Negative(value, _ core.Value) core.Value {
|
||||
}
|
||||
|
||||
func Positive(value, _ core.Value) core.Value {
|
||||
err := core.ValidateType(value, core.IntType, core.FloatType)
|
||||
err := core.ValidateType(value, types.Int, types.Float)
|
||||
|
||||
if err != nil {
|
||||
return values.ZeroInt
|
||||
}
|
||||
|
||||
if value.Type() == core.IntType {
|
||||
if value.Type() == types.Int {
|
||||
return +value.(values.Int)
|
||||
}
|
||||
|
||||
|
@@ -2,8 +2,10 @@ package operators
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
)
|
||||
|
||||
type RangeOperator struct {
|
||||
@@ -43,13 +45,13 @@ func (operator *RangeOperator) Exec(ctx context.Context, scope *core.Scope) (cor
|
||||
}
|
||||
|
||||
func (operator *RangeOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
|
||||
err := core.ValidateType(left, core.IntType, core.FloatType)
|
||||
err := core.ValidateType(left, types.Int, types.Float)
|
||||
|
||||
if err != nil {
|
||||
return values.None, core.SourceError(operator.src, err)
|
||||
}
|
||||
|
||||
err = core.ValidateType(right, core.IntType, core.FloatType)
|
||||
err = core.ValidateType(right, types.Int, types.Float)
|
||||
|
||||
if err != nil {
|
||||
return values.None, core.SourceError(operator.src, err)
|
||||
@@ -58,13 +60,13 @@ func (operator *RangeOperator) Eval(_ context.Context, left, right core.Value) (
|
||||
var start int
|
||||
var end int
|
||||
|
||||
if left.Type() == core.FloatType {
|
||||
if left.Type() == types.Float {
|
||||
start = int(left.(values.Float))
|
||||
} else {
|
||||
start = int(left.(values.Int))
|
||||
}
|
||||
|
||||
if right.Type() == core.FloatType {
|
||||
if right.Type() == types.Float {
|
||||
end = int(right.(values.Float))
|
||||
} else {
|
||||
end = int(right.(values.Int))
|
||||
|
@@ -2,6 +2,7 @@ package operators
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
)
|
||||
|
Reference in New Issue
Block a user