module Nisp

Nisp

Public Class Methods

apply(fun, ctx) click to toggle source
# File lib/nisp.rb, line 55
def self.apply(fun, ctx)
  return fun.call(ctx) if fun.is_a? MacroFn

  args = []
  len = ctx[:ast].size
  i = 1
  while i < len
    args[i - 1] = arg(ctx, i)
    i += 1
  end

  ctx[:env].instance_exec(*args, &fun)
end
arg(ctx, index) click to toggle source

Expand an argument @param ctx [Hash] @param indes [Integer] index of the arg in the ast array @return [Object]

# File lib/nisp.rb, line 43
def self.arg(ctx, index)
  run(
    ast: ctx[:ast][index],
    sandbox: ctx[:sandbox],
    env: ctx[:env],

    # private
    parent: ctx,
    index: index
  )
end
run(ctx) click to toggle source

Run nisp @param ctx [Hash] the context to run @return [Object]

# File lib/nisp.rb, line 15
def self.run(ctx)
  sandbox = ctx[:sandbox]
  raise SandboxEmptyError, 'sandbox is missing' if sandbox.nil?

  ast = ctx[:ast]

  if ast.is_a? Array
    return if ast.size.zero?

    action = arg(ctx, 0)

    return apply(action, ctx) if action.is_a? Proc

    unless sandbox.key? action
      raise FunctionUndefinedError, "function '#{action}' is undefined"
    end

    fun = sandbox[action]
    fun.is_a?(Proc) ? apply(fun, ctx) : fun
  else
    ast
  end
end