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

Remove all warnings when running spec and install tasks #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ begin
gemspec.email = "[email protected]"
gemspec.homepage = "http://github.com/aarongough/flea"
gemspec.authors = ["Aaron Gough"]
gemspec.license = "MIT"
gemspec.rdoc_options << '--line-numbers' << '--inline-source'
gemspec.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
gemspec.add_dependency "sexpistol"
gemspec.add_development_dependency "any-spec"
gemspec.add_dependency "sexpistol", "= 0.0.7"
gemspec.add_development_dependency "any-spec", "= 0.1.0"
gemspec.add_development_dependency "rspec", "= 3.3.0"
gemspec.executables << 'flea'
end
rescue LoadError
Expand All @@ -33,4 +35,4 @@ RSpec::Core::RakeTask.new do |t|
t.rspec_opts = "-c"
t.fail_on_error = false
t.verbose = false
end
end
25 changes: 13 additions & 12 deletions lib/flea/standard_library/lambda.scm
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
(define lambda
(define lambda
(native_function "
Proc.new() do |arguments, interpreter|
formals = arguments[0]
body = arguments.slice(1, arguments.length)

if formals.is_a? Array
# detect if any formal names have been used more than once
error_message = 'Formal {FORMAL} declared more than once'
formals.each_index do |x|
error_message = 'Formal {FORMAL} declared more than once'
formals.each_index do |x|
tmp = formals.dup
tmp.delete_at(x)
raise(error_message.gsub('{FORMAL}', formals[x])) if tmp.include? formals[x]

raise(error_message.gsub('{FORMAL}', formals[x].to_s)) if tmp.include? formals[x]
end
end

sub_env = Flea::Environment.new(interpreter.current_environment)

execute_body = Proc.new() do |body, environment, interpreter|
interpreter.current_environment = environment
result = nil
Expand All @@ -24,8 +25,8 @@
end
interpreter.current_environment = environment.parent
result
end
end

if formals.is_a?(Array) && formals.include?(:'.')
Proc.new() do |arguments, interpreter|
args = arguments.dup
Expand All @@ -37,15 +38,15 @@
sub_env.define(list_formal, args)
execute_body.call(body, sub_env, interpreter)
end

elsif formals.is_a? Array
Proc.new() do |arguments, interpreter|
formals.each_index do |i|
sub_env.define(formals[i], interpreter.evaluate(arguments[i]))
end
execute_body.call(body, sub_env, interpreter)
end

elsif formals.is_a? Symbol
Proc.new() do |arguments, interpreter|
arguments = arguments.map {|x| interpreter.evaluate(x) }
Expand All @@ -54,4 +55,4 @@
end
end
end
"))
"))
4 changes: 2 additions & 2 deletions lib/flea/standard_library/set.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(native_function "
Proc.new() do |arguments, interpreter|
if( interpreter.current_environment.find(arguments[0]) == nil)
raise 'Cannot set unbound variable ' + arguments[0]
raise 'Cannot set unbound variable ' + arguments[0].to_s
end
interpreter.current_environment.define(arguments[0], arguments[1])
end
"))
"))
100 changes: 49 additions & 51 deletions spec/flea/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,111 @@

describe "Flea" do
describe "::Environment" do

include Flea


describe ".new" do
it "should return an environment object" do
Environment.new.should be_an Environment
expect(Flea::Environment.new).to be_a Flea::Environment
end

it "should return an environment object with no parent" do
environment = Environment.new
environment.parent.should be_nil
environment = Flea::Environment.new
expect(environment.parent).to be_nil
end

it "should return an environment object with the specified parent" do
parent_environment = mock("Environment")
environment = Environment.new(parent_environment)
environment.parent.should be parent_environment
parent_environment = double("Flea::Environment")
environment = Flea::Environment.new(parent_environment)
expect(environment.parent).to be parent_environment
end

it "should add base variables for #t and #f" do
environment = Environment.new
environment.should have_variable :"#t"
environment.should have_variable :"#f"
environment.find(:"#t").should be_true
environment.find(:"#f").should be_false
environment = Flea::Environment.new
expect(environment).to have_variable :"#t"
expect(environment).to have_variable :"#f"
expect(environment.find(:"#t")).to be true
expect(environment.find(:"#f")).to be false
end
end

describe "#has_variable?" do
context "without a parent" do
before :each do
@environment = Environment.new
@environment = Flea::Environment.new
end

it "should return false if the variable is not set in the current environment" do
@environment.should_not have_variable :test
expect(@environment).not_to have_variable :test
end

it "should return true if the variable is set in the current environment" do
@environment.define(:test, 1)
@environment.should have_variable :test
expect(@environment).to have_variable :test
end
end

context "with a parent" do
before :each do
@parent_environment = Environment.new
@environment = Environment.new(@parent_environment)
@parent_environment = Flea::Environment.new
@environment = Flea::Environment.new(@parent_environment)
end

it "should return false if the variable is not set in the parent environment" do
@environment.should_not have_variable :test
expect(@environment).to_not have_variable :test
end

it "should return true if the variable is set in the parent environment" do
@parent_environment.define(:test, 1)
@environment.should have_variable :test
expect(@environment).to have_variable :test
end
end
end

describe "#define" do
it "should set a variable to the supplied value" do
env = Environment.new
env = Flea::Environment.new
result = env.define(:test, 1)
env.should have_variable :test
env.find(:test).should == 1
result.should == 1
expect(env).to have_variable :test
expect(env.find(:test)).to be == 1
expect(result).to be == 1
end
end

describe "#find" do
context "without a parent" do
before :each do
@environment = Environment.new
@environment = Flea::Environment.new
@environment.define(:test, 1)
end

it "should find a variable in the current environment" do
@environment.find(:test).should == 1
expect(@environment.find(:test)).to be == 1
end

it "should return nil when variable is not set" do
@environment.find(:fake).should be_nil
expect(@environment.find(:fake)).to be_nil
end
end

context "with a parent" do
before :each do
@parent_environment = Environment.new
@environment = Environment.new(@parent_environment)
@parent_environment = Flea::Environment.new
@environment = Flea::Environment.new(@parent_environment)
@parent_environment.define(:test, 1)
end

it "should find a variable in the parent environment" do
@environment.find(:test).should == 1
expect(@environment.find(:test)).to be == 1
end

it "should return a variable from the current environment if it is set in both current and parent" do
@environment.define(:test, 5)
@environment.find(:test).should == 5
expect(@environment.find(:test)).to be == 5
end

it "should return nil when variable is not set in the current or parent environment" do
@environment.find(:fake).should be_nil
expect(@environment.find(:fake)).to be_nil
end
end
end
end
end
end
70 changes: 34 additions & 36 deletions spec/flea/interpreter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,85 +1,83 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe "Flea" do
describe "::Interpreter" do

include Flea

describe "Flea::Interpreter" do

describe "#new" do
it "should return an Interpreter" do
Interpreter.new.should be_an Interpreter
expect(Flea::Interpreter.new).to be_a Flea::Interpreter
end

it "should allow setting the base environment to use" do
environment = mock("Environment")
interpreter = Interpreter.new(
environment = double("Flea::Environment")
interpreter = Flea::Interpreter.new(
:base_environment => environment,
:load_standard_library => false
)
interpreter.base_environment.should be environment
expect(interpreter.base_environment).to be environment
end
end

describe ".run" do
it "should run a program and return the last output from the program" do
interpreter = Interpreter.new
interpreter = Flea::Interpreter.new
result = interpreter.run("(define test 1)")
result.should == 1
interpreter.base_environment.should have_variable :test
expect(result).to be == 1
expect(interpreter.base_environment).to have_variable :test
end
end

describe ".parse" do
it "should return an abstract syntax tree representing the supplied program" do
ast = Interpreter.new.parse("(define test 1)")
ast.should == [[:define, :test, 1]]
ast = Flea::Interpreter.new.parse("(define test 1)")
expect(ast).to be == [[:define, :test, 1]]
end
end

describe ".evaluate" do
before :each do
@environment = mock("Environment")
@interpreter = Interpreter.new(
:base_environment => @environment,
@environment = double("Environment")
@interpreter = Flea::Interpreter.new(
:base_environment => @environment,
:load_standard_library => false
)
end

it "should return the value of a variable" do
@environment.should_receive(:find).with(:test).and_return(1)
expect(@environment).to receive(:find).with(:test).and_return(1)
result = @interpreter.evaluate(:test)
result.should == 1
expect(result).to be == 1
end

it "should define a variable in the current environment" do
@environment.should_receive(:define).with(:test, 1).and_return(1)
expect(@environment).to receive(:define).with(:test, 1).and_return(1)
result = @interpreter.evaluate([:define, :test, 1])
result.should == 1
expect(result).to be == 1
end

it "should create a native function" do
result = @interpreter.evaluate([:native_function, "
Proc.new() do |arguments, interpreter|
1
end
"])
result.should be_a Proc
result.call.should == 1
expect(result).to be_a Proc
expect(result.call).to be == 1
end

it "should call a native function" do
@environment.should_receive(:find).with(:foo).and_return(Proc.new {|a,b| "bar"})
expect(@environment).to receive(:find).with(:foo).and_return(Proc.new {|a,b| "bar"})
result = @interpreter.evaluate([:foo, 1, 2, 3])
result.should == "bar"
expect(result).to be == "bar"
end

[1, 1.0, "string"].each do |literal|
it "should return literal '#{literal}' as is" do
result = @interpreter.evaluate(literal)
result.should be literal
expect(result).to be literal
end
end
end

end
end
end
Loading