Skip to content

Commit

Permalink
Add the concept of NULL
Browse files Browse the repository at this point in the history
Allow NULL to be referenced in formulas, which will be evaluated as nil.
  • Loading branch information
tristil committed Feb 17, 2016
1 parent 0ce7c4a commit 959339c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
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
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
6 changes: 6 additions & 0 deletions spec/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@
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
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

0 comments on commit 959339c

Please sign in to comment.