Class: Finitio::System

Inherits:
Object
  • Object
show all
Defined in:
lib/finitio/system.rb

Overview

A System is a collection of named Finitio types.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types = {}, imports = []) ⇒ System

Returns a new instance of System.



7
8
9
10
# File 'lib/finitio/system.rb', line 7

def initialize(types = {}, imports = [])
  @types = types
  @imports = imports
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



12
13
14
# File 'lib/finitio/system.rb', line 12

def types
  @types
end

Instance Method Details

#add_import(system) ⇒ Object



15
16
17
# File 'lib/finitio/system.rb', line 15

def add_import(system)
  @imports << system
end

#add_type(type, name = nil, metadata = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/finitio/system.rb', line 19

def add_type(type, name = nil,  = nil)
  type = factory.type(type, name, )

  if @types.has_key?(type.name)
    raise Error, "Duplicate type name `#{type.name}`"
  end

  @types[type.name] = type
end

#check_and_warn(logger = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/finitio/system.rb', line 99

def check_and_warn(logger = nil)
  logger ||= begin
    require 'logger'
    Logger.new(STDERR)
  end
  each_type do |t|
    next unless t.named?
    each_import do |i|
      next unless found = i.get_type(t.name)
      if found == t
        logger.info "Duplicate type def `#{t.name}`"
        break
      else
        logger.warn "Type erasure `#{t.name}`"
        break
      end
    end
  end
  self
end

#dress(*args, &bl) ⇒ Object

Raises:



81
82
83
84
# File 'lib/finitio/system.rb', line 81

def dress(*args, &bl)
  raise Error, "No main type." unless main
  main.dress(*args, &bl)
end

#dup(types = @types.dup, imports = @imports.dup) ⇒ Object



95
96
97
# File 'lib/finitio/system.rb', line 95

def dup(types = @types.dup, imports = @imports.dup)
  System.new(types, imports)
end

#factoryObject



71
72
73
# File 'lib/finitio/system.rb', line 71

def factory
  @factory ||= TypeFactory.new
end

#fetch(name, with_imports = true, &bl) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/finitio/system.rb', line 50

def fetch(name, with_imports = true, &bl)
  if with_imports
    @types.fetch(name) do
      fetch_on_imports(name, &bl)
    end
  else
    @types.fetch(name, &bl)
  end
end

#fetch_on_imports(name, imports = @imports, &bl) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/finitio/system.rb', line 60

def fetch_on_imports(name, imports = @imports, &bl)
  if imports.empty?
    raise KeyError, %Q{key not found: "#{name}"} unless bl
    bl.call(name)
  else
    imports.first.fetch(name, false) do
      fetch_on_imports(name, imports[1..-1], &bl)
    end
  end
end

#get_type(name) ⇒ Object Also known as: []



39
40
41
42
43
# File 'lib/finitio/system.rb', line 39

def get_type(name)
  fetch(name){|_|
    fetch(name.to_s){ nil }
  }
end

#inspectObject



91
92
93
# File 'lib/finitio/system.rb', line 91

def inspect
  @types.each_pair.map{|k,v| "#{k} = #{v}" }.join("\n")
end

#mainObject



46
47
48
# File 'lib/finitio/system.rb', line 46

def main
  self['Main']
end

#parse(source) ⇒ Object



86
87
88
89
# File 'lib/finitio/system.rb', line 86

def parse(source)
  require_relative "syntax"
  Syntax.compile(source, self.dup)
end