Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the concept of NULL #70

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ BUILT-IN OPERATORS AND FUNCTIONS

Math: `+ - * / %`

Logic: `< > <= >= <> != = AND OR`
Logic: `< > <= >= <> != = AND OR NULL`

Functions: `IF NOT MIN MAX ROUND ROUNDDOWN ROUNDUP`

Expand Down
7 changes: 4 additions & 3 deletions lib/dentaku/ast/identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ def initialize(token)
end

def value(context={})
v = context[identifier]
v = context.fetch(identifier) do
raise UnboundVariableError.new([identifier])
end

case v
when Node
v.value(context)
when NilClass
raise UnboundVariableError.new([identifier])
else
v
end
Expand Down
14 changes: 12 additions & 2 deletions lib/dentaku/bulk_expression_solver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ def raise_exception_handler
def load_results(&block)
variables_in_resolve_order.each_with_object({}) do |var_name, r|
begin
r[var_name] = calculator.memory[var_name] ||
evaluate!(expressions[var_name], expressions.merge(r))
value_from_memory = calculator.memory[var_name]

if value_from_memory.nil? &&
expressions[var_name].nil? &&
!calculator.memory.has_key?(var_name)
next
end

value = value_from_memory ||
evaluate!(expressions[var_name], expressions.merge(r))

r[var_name] = value
rescue Dentaku::UnboundVariableError, ZeroDivisionError => ex
r[var_name] = block.call(ex)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/dentaku/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def parse
operations.push op_class
end

when :null
output.push AST::Nil.new

when :function
arities.push 0
operations.push function(token)
Expand Down
5 changes: 5 additions & 0 deletions lib/dentaku/token_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def scan(string, last_token=nil)
class << self
def available_scanners
[
:null,
:whitespace,
:numeric,
:double_quoted_string,
Expand Down Expand Up @@ -68,6 +69,10 @@ def whitespace
new(:whitespace, '\s+')
end

def null
new(:null, 'null\b')
end

def numeric
new(:numeric, '(\d+(\.\d+)?|\.\d+)\b', lambda { |raw| raw =~ /\./ ? BigDecimal.new(raw) : raw.to_i })
end
Expand Down
22 changes: 22 additions & 0 deletions spec/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,28 @@
end
end

describe 'explicit NULL' do
it 'can be used in IF statements' do
expect(calculator.evaluate('IF(null, 1, 2)')).to eq(2)
end

it 'can be used in IF statements when passed in' do
expect(calculator.evaluate('IF(foo, 1, 2)', foo: nil)).to eq(2)
end

it 'nil values are carried across middle terms' do
results = calculator.solve!(
choice: 'IF(bar, 1, 2)',
bar: 'foo',
foo: nil)
expect(results).to eq(
choice: 2,
bar: nil,
foo: nil
)
end
end

describe 'case statements' do
it 'handles complex then statements' do
formula = <<-FORMULA
Expand Down
6 changes: 6 additions & 0 deletions spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,10 @@
described_class.new([five, times, minus]).parse
}.to raise_error(Dentaku::ParseError)
end

it "evaluates explicit 'NULL' as a Nil" do
null = Dentaku::Token.new(:null, nil)
node = described_class.new([null]).parse
expect(node.value).to eq(nil)
end
end
2 changes: 1 addition & 1 deletion spec/token_scanner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
end

it 'returns a list of all configured scanners' do
expect(described_class.scanners.length).to eq 13
expect(described_class.scanners.length).to eq 14
end

it 'allows customizing available scanners' do
Expand Down