-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
167 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ def value(context = nil) | |
end | ||
|
||
def true? | ||
false | ||
bool | ||
end | ||
|
||
def ! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,30 @@ | ||
module Keisan | ||
module AST | ||
class LogicalOperator < Operator | ||
def evaluate(context = nil) | ||
context ||= Context.new | ||
children[0].evaluate(context).send(operator, children[1].evaluate(context)) | ||
end | ||
|
||
def simplify(context = nil) | ||
context ||= Context.new | ||
children[0].simplify(context).send(operator, children[1].simplify(context)) | ||
end | ||
|
||
def value(context=nil) | ||
context ||= Context.new | ||
children[0].value(context).send(value_operator, children[1].value(context)) | ||
end | ||
|
||
protected | ||
|
||
def value_operator | ||
raise Exceptions::NotImplementedError.new | ||
end | ||
|
||
def operator | ||
raise Exceptions::NotImplementedError.new | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe "Logical operators" do | ||
let(:calculator) { Keisan::Calculator.new } | ||
|
||
describe "evaluate" do | ||
it "leaves as logical comparison when cannot fully evaluate operands" do | ||
expect(Keisan::AST.parse("1 == x").evaluate).to be_a(Keisan::AST::LogicalEqual) | ||
end | ||
|
||
it "evaluates to a boolean for complex operands" do | ||
calculator.evaluate("a = [1, 2, 3]") | ||
calculator.evaluate("i = 1") | ||
calculator.evaluate("x = 2") | ||
expect(calculator.evaluate("a[i] == x")).to be true | ||
end | ||
end | ||
|
||
describe "simplify" do | ||
it "short-circuits if possible" do | ||
expect(calculator.simplify("true || (x = 1)").value).to be true | ||
expect(calculator.simplify("x")).to be_a(Keisan::AST::Variable) | ||
|
||
expect(calculator.simplify("false && (x = 1)").value).to be false | ||
expect(calculator.simplify("x")).to be_a(Keisan::AST::Variable) | ||
|
||
expect(calculator.simplify("false || (x = 1)").value).to be 1 | ||
expect(calculator.simplify("x").value).to eq 1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters