-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
168 additions
and
35 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
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,58 @@ | ||
|
||
class ARQLVM | ||
|
||
def initialize(ns = nil) | ||
@ns = ns | ||
@stack = [] | ||
@current_model = nil | ||
end | ||
|
||
def select_model(model_name) | ||
@stack << @ns.lookup(model_name)[:class_name] | ||
end | ||
|
||
def filter(&filter_expr) | ||
top_elem = @stack.pop | ||
|
||
if top_elem.nil? | ||
raise "Can not apply filter to an empty stack" | ||
elsif top_elem.is_a? Enumerable | ||
@stack << top_elem.find_all(&filter_expr) | ||
#elsif top_elem.is_a? Class and top_elem.ancestors.member? ActiveRecordBaseProxy | ||
#@stack << top_elem.all.find_all(&filter_expr) | ||
else | ||
raise "Can not apply filter to #{top_elem.class}" | ||
end | ||
end | ||
|
||
def push(*elem) | ||
@stack.push *elem | ||
end | ||
|
||
def pop(n = nil) | ||
if n.nil? | ||
@stack.pop | ||
else | ||
@stack.pop(n) | ||
end | ||
end | ||
|
||
def dup | ||
push @stack.last unless @stack.last.nil? | ||
end | ||
|
||
def apply(&function) | ||
n = function.arity | ||
items = pop(n) | ||
push(function.call(*items)) | ||
end | ||
|
||
def empty? | ||
@stack.empty? | ||
end | ||
|
||
def dump_stack! | ||
puts "Stack size: #{@stack.length}" | ||
@stack.each { |item| puts " #{item.class} @ #{item.to_s[0..64]}" } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'ar/ar' | ||
|
||
RSpec.describe ARQLVM do | ||
|
||
context '#initialize' do | ||
it 'should create a new, blank VM' do | ||
vm = ARQLVM.new | ||
end | ||
end | ||
|
||
context '#push' do | ||
it 'should push a new item onto the top of the stack' do | ||
vm = ARQLVM.new | ||
|
||
vm.push :test | ||
expect(vm.pop).to eq(:test) | ||
end | ||
end | ||
|
||
context '#apply' do | ||
it 'should apply the given lambda to the top n elements of the stack' do | ||
vm = ARQLVM.new | ||
|
||
vm.push 1 | ||
vm.push 3 | ||
vm.apply { |a, b| a + b } | ||
expect(vm.pop).to eq(4) | ||
expect(vm.empty?).to be_truthy | ||
end | ||
|
||
it 'should push every element back after evaluation' do | ||
vm = ARQLVM.new | ||
|
||
vm.push 1 | ||
vm.push 3 | ||
vm.push -4 | ||
vm.apply { |a| [ 2, 5 ] } | ||
expect(vm.pop(4)).to eq([1, 3, [ 2, 5 ] ]) | ||
expect(vm.empty?).to be_truthy | ||
end | ||
|
||
it 'should work well with arrays' do | ||
vm = ARQLVM.new | ||
|
||
vm.push 1 | ||
vm.push [ 3, 4, 5 ] | ||
vm.apply { |a| a.map { |b| b + 1 } } | ||
expect(vm.pop(2)).to eq([1, [4, 5, 6]]) | ||
end | ||
end | ||
end |