lib/scope.rb in expressive-0.0.2 vs lib/scope.rb in expressive-0.0.3
- old
+ new
@@ -23,31 +23,52 @@
def retrieve_scope
@symbols
end
- def define(name, &block)
- self[name] = Function.new(&block)
+ def define(name, &block)
+ self[name] = Function.new(&block)
end
- def syntax(name, &block)
- self[name] = Syntax.new(&block)
+ def syntax(name, &block)
+ self[name] = Syntax.new(&block)
end
-
end
class TopLevel < Scope
def initialize
super
syntax('set') do |scope, cells|
scope[cells.first.text_value] = cells[1].eval(scope)
end
+ syntax('post') do |scope, cells|
+ uri = cells.shift.text_value.gsub('"', '')
+ payload = {}
+ cells.each do |key|
+ payload[key.text_value] = scope[key.text_value]
+ end
+ begin
+ response = RestClient.post uri, payload
+ scope = scope.merge(response) if response.is_a?(Hash)
+ rescue RestClient::Exception => e
+ scope['_errors'] = e.message
+ end
+ scope
+ end
+
define('+') {|a,b| a.to_f + b.to_f }
define('-') {|a,b| a.to_f - b.to_f }
define('*') {|a,b| a.to_f * b.to_f }
define('/') {|a,b| a.to_f / b.to_f }
define('=') {|a,b| a == b }
- define('sum') {|*args| args.flatten.map(&:to_f).reduce(:+) }
+ define('>') {|a,b| a.to_f > b.to_f }
+ define('<') {|a,b| a.to_f < b.to_f }
+ define('>=') {|a,b| a.to_f >= b.to_f }
+ define('<=') {|a,b| a.to_f <= b.to_f }
+ define('and') {|a,b| !!a && !!b }
+ define('or') {|a,b| !!a || !!b }
+ define('sum') { |*args| args.flatten.map(&:to_f).reduce(:+) }
+ define('if') { |*args| args.compact!; args[0] ? args[1] : args[2] }
end
end