class Scope def initialize(parent = {}) @parent = parent @symbols = {} end def [](name) @symbols[name] || @parent[name] end def []=(name, value) @symbols[name] = value end def merge(scope) @symbols.merge!(scope) end def override_scope(scope) scope.merge!(@symbols) @symbols = scope end def retrieve_scope @symbols end def define(name, &block) self[name] = Function.new(&block) end 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 define('+') {|a,b| a + b } define('-') {|a,b| a - b } define('*') {|a,b| a * b } define('/') {|a,b| a / b } define('=') {|a,b| a == b } define('sum') {|*args| args.flatten.reduce(:+) } end end