-
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.
Merge pull request #61 from project-eutopia/puts
Support for `puts` in expressions
- Loading branch information
Showing
6 changed files
with
80 additions
and
3 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
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,23 @@ | ||
module Keisan | ||
module Functions | ||
class Puts < Function | ||
def initialize | ||
super("puts", 1) | ||
end | ||
|
||
def value(ast_function, context = nil) | ||
evaluate(ast_function, context) | ||
end | ||
|
||
def evaluate(ast_function, context = nil) | ||
validate_arguments!(ast_function.children.count) | ||
puts ast_function.children.first.evaluate(context).to_s | ||
Keisan::AST::Null.new | ||
end | ||
|
||
def simplify(ast_function, context = nil) | ||
evaluate(ast_function, context) | ||
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,40 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe Keisan::Functions::Puts do | ||
let(:calculator) { Keisan::Calculator.new } | ||
|
||
it "outputs to STDOUT" do | ||
expect { calculator.evaluate("puts 123") }.to output("123\n").to_stdout | ||
expect { calculator.evaluate("puts(x**2 + 1)") }.to output("(x**2)+1\n").to_stdout | ||
calculator.evaluate("x = 2") | ||
expect { calculator.evaluate("puts(x**2 + 1)") }.to output("5\n").to_stdout | ||
end | ||
|
||
describe "evaluate" do | ||
it "returns null" do | ||
expect { | ||
ast = calculator.ast("puts x = 5") | ||
expect(ast.evaluate).to eq Keisan::AST::Null.new | ||
}.to output("5\n").to_stdout | ||
end | ||
|
||
it "does evaluation of arguments" do | ||
expect { calculator.evaluate("puts(x = 12)") }.to output("12\n").to_stdout | ||
expect(calculator.evaluate("x")).to eq 12 | ||
end | ||
end | ||
|
||
describe "#value and #simplify call evaluate" do | ||
it "value calls evaluate" do | ||
expect_any_instance_of(Keisan::Functions::Puts).to receive(:evaluate).and_return(Keisan::AST::Null.new) | ||
ast = calculator.ast("puts x = 5") | ||
expect(ast.value).to eq Keisan::AST::Null.new | ||
end | ||
|
||
it "simplify calls evaluate" do | ||
expect_any_instance_of(Keisan::Functions::Puts).to receive(:evaluate).and_return(Keisan::AST::Null.new) | ||
ast = calculator.ast("puts x = 5") | ||
expect(ast.simplify).to eq Keisan::AST::Null.new | ||
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