Skip to content
Kward Search API index

Module: Kward::NativeShell::Arithmetic

Defined in:
lib/kward/shell/native_arithmetic.rb

Overview

Integer arithmetic ASTs preserve short-circuiting and lvalue ownership. Parsing is independent of shell state; evaluation never uses Ruby eval.

Defined Under Namespace

Classes: Error, Evaluator, Parser

Constant Summary collapse

MAX_BITS =
4_096
MAX_DEPTH =
100

Class Method Summary collapse

Class Method Details

.binary(operator, left, right) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kward/shell/native_arithmetic.rb', line 39

def self.binary(operator, left, right)
  value = case operator
  when "+" then left + right
  when "-" then left - right
  when "*" then left * right
  when "/", "%"
    raise Error, "arithmetic division by zero" if right.zero?
    quotient = left.abs / right.abs
    quotient = -quotient if left.negative? != right.negative?
    operator == "/" ? quotient : left - quotient * right
  when "**"
    raise Error, "negative arithmetic exponent" if right.negative?
    raise Error, "arithmetic exponent exceeds result limit" if left.abs > 1 && (left.abs.bit_length - 1) * right > MAX_BITS
    if left == -1
      right.odd? ? -1 : 1
    elsif left == 0
      right.zero? ? 1 : 0
    elsif left == 1
      1
    else
      left**right
    end
  when "<<", ">>"
    raise Error, "arithmetic shift exceeds limit" unless (0..MAX_BITS).cover?(right)
    operator == "<<" ? left << right : left >> right
  when "&" then left & right
  when "|" then left | right
  when "^" then left ^ right
  when "==" then left == right ? 1 : 0
  when "!=" then left != right ? 1 : 0
  when "<" then left < right ? 1 : 0
  when "<=" then left <= right ? 1 : 0
  when ">" then left > right ? 1 : 0
  when ">=" then left >= right ? 1 : 0
  when "," then right
  else raise Error, "unknown arithmetic operator"
  end
  bounded(value)
end

.bounded(value) ⇒ Object

Raises:



30
31
32
33
# File 'lib/kward/shell/native_arithmetic.rb', line 30

def self.bounded(value)
  raise Error, "arithmetic result exceeds #{MAX_BITS} bits" if value.bit_length > MAX_BITS
  value
end

.evaluate(source, assign: nil, &variable) ⇒ Object



35
36
37
# File 'lib/kward/shell/native_arithmetic.rb', line 35

def self.evaluate(source, assign: nil, &variable)
  Evaluator.new(variable, assign).evaluate(Parser.new(source).parse)
end

.integer(text) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kward/shell/native_arithmetic.rb', line 12

def self.integer(text)
  value = text.to_s.strip
  return 0 if value.empty?
  unless value.match?(/\A[+-]?(?:0[xX][0-9a-fA-F]+|0[0-7]*|[1-9][0-9]*)\z/)
    raise Error, "arithmetic requires an integer value"
  end
  negative = value.start_with?("-")
  digits = value.delete_prefix("+").delete_prefix("-")
  base = digits.start_with?("0x", "0X") ? 16 : digits.start_with?("0") ? 8 : 10
  digits = digits[2..] if base == 16
  digits = digits.sub(/\A0+/, "")
  return 0 if digits.empty?
  # More significant digits than bits cannot fit in any supported base.
  # Reject before constructing a huge integer; leading zeros cost no bits.
  raise Error, "arithmetic result exceeds #{MAX_BITS} bits" if digits.length > MAX_BITS
  bounded(Integer(digits, base) * (negative ? -1 : 1))
end