Class: Heist::Runtime::Syntax

Inherits:
Function
  • Object
show all
Defined in:
lib/heist/runtime/callable/syntax.rb

Overview

The Syntax class is used to model built-in special forms. Syntax functions are universally implemented in Ruby; user-defined special forms are represented using Macro. Syntax is very simple: its body is a Ruby block that accepts a Scope and a Cons list of the expression following the special form, and the Syntax class does not automatically evaluate any parameters. It is up to the Ruby code implementing the syntax to decide what to evaluate. For example, here’s a couple of implementations for Scheme’s (if) and (set!).

env = Scope.new

# e.g. (set! x (+ 9 4))
env['set!'] = Syntax.new(env) do |scope, cells|
  value = Heist.evaluate(cells.cdr.car, scope)
  scope.set!(cells.car, value)
end

# e.g. (if (> 6 3) 'yes 'no)
env['if'] = Syntax.new(env) do |scope, cells|
  which = Heist.evaluate(cells.car, scope) ? cells.cdr : cells.cdr.cdr
  Heist.evaluate(which.car, scope)
end

Direct Known Subclasses

Macro

Instance Attribute Summary

Attributes inherited from Function

#body, #name

Instance Method Summary collapse

Methods inherited from Function

#apply, #initialize, #lazy?, #primitive?

Constructor Details

This class inherits a constructor from Heist::Runtime::Function

Instance Method Details

#call(scope, cells) ⇒ Object

Calls the Ruby implementation of the Syntax and returns the result.



31
32
33
# File 'lib/heist/runtime/callable/syntax.rb', line 31

def call(scope, cells)
  @body.call(scope, cells)
end

#to_sObject Also known as: inspect

Returns a string placeholder for the Syntax, containing its name if it has one.



37
38
39
# File 'lib/heist/runtime/callable/syntax.rb', line 37

def to_s
  "#<syntax:#{ @name }>"
end