Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions TellC/tests/fixtures/operators.tell
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
say 6 & 3
say 6 | 3
say 6 ^ 3
say ~5
say 1 << 3 + 1
say -8 >> 2
say 1 + 2 << 2 & 15 | 1
say 2 equal to 2
say 2 is equal to 3
say true && !false
say false || true
say false and (1 << 63 = 0)
say true or (1 << 63 = 0)
try:
say 1 << -1
if something goes wrong:
say "invalid shift"
end try
try:
say 1 << 53
if something goes wrong:
say "shift out of range"
end try
6 changes: 6 additions & 0 deletions native/internal/tell/bytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,8 @@ func foldBytecodeExpression(expr expression) expression {
switch value.operator {
case "not":
result = !truthy(literal)
case "~":
result, err = bitwiseNot(literal)
case "+", "-":
var number float64
number, err = numberValue(literal)
Expand Down Expand Up @@ -1505,6 +1507,10 @@ func executeBytecodeCode(r *runtime, instructions []BytecodeInstruction, functio
case "UNARY":
if instruction.Name == "not" {
stack = append(stack, !truthy(value))
} else if instruction.Name == "~" {
var result any
result, err = bitwiseNot(value)
if err == nil { stack = append(stack, result) }
} else {
var number float64
number, err = numberValue(value)
Expand Down
41 changes: 39 additions & 2 deletions native/internal/tell/expression_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (p *expressionParser) parsePrecedence(min int) (expression, error) {
break
}
operatorToken := p.take()
if op == "=" { op = "==" }
next := prec + 1
if op == "**" {
next = prec
Expand Down Expand Up @@ -169,8 +170,8 @@ func (p *expressionParser) parsePrefix() (expression, error) {
switch t.kind {
case "number", "string", "true", "false", "nothing":
value = literalExpression{token: t}
case "-", "+", "not":
operand, err := p.parsePrecedence(7)
case "-", "+", "not", "~":
operand, err := p.parsePrecedence(10)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -453,6 +454,11 @@ func (r *runtime) evalExpression(expr expression, e *env) (any, error) {
if x.operator == "not" {
return !truthy(value), nil
}
if x.operator == "~" {
result, err := bitwiseNot(value)
if err != nil { return nil, withSourceLocation(err, r.filename, x.line, x.column) }
return result, nil
}
n, err := numberValue(value)
if err != nil {
return nil, withSourceLocation(err, r.filename, x.line, x.column)
Expand Down Expand Up @@ -718,7 +724,38 @@ func indexValue(value, index any) (any, error) {
}
}

func bitwiseInteger(value any) (int64, error) {
n, err := numberValue(value)
if err != nil || math.IsNaN(n) || math.IsInf(n, 0) || math.Trunc(n) != n || n < -9007199254740991 || n > 9007199254740991 {
return 0, fail("bitwise operators require integers within the exact numeric range")
}
return int64(n), nil
}

func bitwiseNot(value any) (any, error) {
n, err := bitwiseInteger(value)
if err != nil { return nil, err }
result := ^n
if result < -9007199254740991 || result > 9007199254740991 { return nil, fail("bitwise result exceeds exact numeric range") }
return float64(result), nil
}

func applyBinary(op string, a, b any) (any, error) {
if op == "&" || op == "|" || op == "^" || op == "<<" || op == ">>" {
left, err := bitwiseInteger(a); if err != nil { return nil, err }
right, err := bitwiseInteger(b); if err != nil { return nil, err }
var result int64
switch op {
case "&": result = left & right
case "|": result = left | right
case "^": result = left ^ right
case "<<", ">>":
if right < 0 || right >= 64 { return nil, fail("shift count must be between 0 and 63") }
if op == "<<" { result = left << uint(right); if (result >> uint(right)) != left { return nil, fail("bitwise shift overflow") } } else { result = left >> uint(right) }
}
if result < -9007199254740991 || result > 9007199254740991 { return nil, fail("bitwise result exceeds exact numeric range") }
return float64(result), nil
}
if op == "and" || op == "or" {
return truthy(b), nil
}
Expand Down
26 changes: 19 additions & 7 deletions native/internal/tell/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var languagePhrases = []languagePhrase{
{"is at most", "<=", "<="},
{"is not in", "notin", "notin"},
{"is equal to", "==", "=="},
{"equal to", "==", "=="},
{"isn't", "!=", "!="},
{"is not", "!=", "!="},
{"is below", "<", "<"},
Expand Down Expand Up @@ -206,13 +207,16 @@ func lex(s string) ([]token, error) {
}
if i+1 < len(s) {
two := s[i : i+2]
if two == ">=" || two == "<=" || two == "==" || two == "!=" || two == "**" || two == "//" {
appendToken(two, two, start, i+2)
if two == "&&" || two == "||" || two == "<<" || two == ">>" || two == ">=" || two == "<=" || two == "==" || two == "!=" || two == "**" || two == "//" {
kind := two
if two == "&&" { kind = "and" }; if two == "||" { kind = "or" }
appendToken(kind, two, start, i+2)
i += 2
continue
}
}
if strings.ContainsRune("+-*/%<>()[]{}.,:", c) {
if c == '!' { appendToken("not", "!", start, start+size); i += size; continue }
if strings.ContainsRune("+-*/%<>()[]{}.,:~&|^", c) {
appendToken(string(c), string(c), start, start+size)
i += size
continue
Expand All @@ -235,14 +239,22 @@ func precedence(op string) int {
return 1
case "and":
return 2
case "==", "!=", "<", ">", "<=", ">=", "in", "notin":
case "=", "==", "!=", "<", ">", "<=", ">=", "in", "notin":
return 3
case "+", "-":
case "|":
return 4
case "*", "/", "//", "%":
case "^":
return 5
case "**":
case "&":
return 6
case "<<", ">>":
return 7
case "+", "-":
return 8
case "*", "/", "//", "%":
return 9
case "**":
return 11
}
return 0
}
Expand Down
43 changes: 43 additions & 0 deletions native/internal/tell/operators_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tell

import (
"bytes"
"os"
"strings"
"testing"
)

func TestSharedOperators(t *testing.T) {
source, err := os.ReadFile("../../../TellC/tests/fixtures/operators.tell")
if err != nil {
t.Fatal(err)
}
const want = "2\n7\n5\n-6\n16\n-2\n13\ntrue\nfalse\ntrue\ntrue\nfalse\ntrue\ninvalid shift\nshift out of range\n"
var out bytes.Buffer
if err := RunWithIO(string(source), "operators.tell", strings.NewReader(""), &out); err != nil {
t.Fatal(err)
}
if out.String() != want {
t.Fatalf("interpreter = %q, want %q", out.String(), want)
}
if got := runCompiled(t, string(source)); got != want {
t.Fatalf("bytecode = %q, want %q", got, want)
}
}

func TestInvalidBitwiseOperands(t *testing.T) {
for _, source := range []string{"say 1 << -1", "say 1 << 64", "say 1.5 & 1", "say 1 << 53", "say ~9007199254740991"} {
t.Run(source, func(t *testing.T) {
if err := RunWithIO(source, "invalid.tell", strings.NewReader(""), &bytes.Buffer{}); err == nil {
t.Fatal("expected error")
}
artifact, err := Compile(source, "invalid.tell")
if err != nil {
return
}
if err = RunBytecode(artifact, "invalid.tbc", strings.NewReader(""), &bytes.Buffer{}); err == nil {
t.Fatal("expected bytecode error")
}
})
}
}
Loading
Loading