Module: Liquor::Runtime

Defined in:
lib/liquor/runtime.rb

Defined Under Namespace

Classes: DummyExternal

Class Method Summary collapse

Class Method Details

.add!(left, left_loc, right, right_loc) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/liquor/runtime.rb', line 103

def self.add!(left, left_loc, right, right_loc)
  if left.is_a? Integer
    right = integer! right, right_loc
  elsif left.is_a? String
    right = string! right, right_loc
  elsif indexable?(left)
    left  = left.to_a
    right = tuple! right, right_loc
  else
    return soft_error("Integer, String, Tuple or indexable External value expected, #{pretty_type(left)} found", :null, left_loc)
  end

  left + right
end

.capture_diagnosticsObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/liquor/runtime.rb', line 50

def self.capture_diagnostics
  old_diagnostics = @diagnostics
  @diagnostics    = []

  yield

  @diagnostics
ensure
  @diagnostics    = old_diagnostics
end

.default_value_of(type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/liquor/runtime.rb', line 37

def self.default_value_of(type)
  case type
  when :null;     nil
  when :boolean;  false
  when :integer;  0
  when :string;   ""
  when :tuple;    []
  when :external; DummyExternal.new
  end
end

.deprecation(message, loc) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/liquor/runtime.rb', line 85

def self.deprecation(message, loc)
  error = Liquor::Deprecation.new(message, loc)
  error.set_backtrace(caller(2))

  if @fatal_deprecations
    raise error
  elsif @diagnostics
    @diagnostics << error
  end
end

.external!(value, loc) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/liquor/runtime.rb', line 149

def self.external!(value, loc)
  unless value.is_a? External
    return soft_error("External value expected, #{pretty_type(value)} found", :external, loc)
  end

  value
end

.indexable?(value) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'lib/liquor/runtime.rb', line 96

def self.indexable?(value)
  value.is_a?(Array) ||
    value.is_a?(External) &&
    value.class.liquor_exports &&
    value.class.liquor_exports.include?(:[])
end

.integer!(value, loc) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/liquor/runtime.rb', line 118

def self.integer!(value, loc)
  unless value.is_a? Integer
    return soft_error("Integer value expected, #{pretty_type(value)} found", :integer, loc)
  end

  value
end

.interp!(value, loc) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/liquor/runtime.rb', line 157

def self.interp!(value, loc)
  unless value.is_a?(String) ||
         value.is_a?(Integer) ||
         value.nil?
    return soft_error("String or Null value expected, #{pretty_type(value)} found", :string, loc)
  end

  value.to_s
end

.pretty_type(value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/liquor/runtime.rb', line 25

def self.pretty_type(value)
  case value
  when nil;         "Null"
  when true, false; "Boolean"
  when Integer;     "Integer"
  when String;      "String"
  when Array;       "Tuple"
  when External;    indexable?(value) ? "IndexableExternal" : "External"
  else              "_Foreign<#{value.class}>"
  end
end

.soft_error(klass = TypeError, message, expectation, loc) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/liquor/runtime.rb', line 72

def self.soft_error(klass=TypeError, message, expectation, loc)
  error = klass.new(message, loc)

  if @diagnostics.nil?
    raise error
  else
    error.set_backtrace(caller(2))
    @diagnostics << error

    default_value_of expectation
  end
end

.string!(value, loc) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/liquor/runtime.rb', line 126

def self.string!(value, loc)
  if value.is_a? String
    value
  elsif value.is_a? Integer
    value.to_s
  else
    return soft_error("String value expected, #{pretty_type(value)} found", :string, loc)
  end
end

.tuple!(value, loc) ⇒ Object

Proper indexable external API: .[](index) => element fetch .size => element count .to_a => converts to Array



141
142
143
144
145
146
147
# File 'lib/liquor/runtime.rb', line 141

def self.tuple!(value, loc)
  unless indexable?(value)
    return soft_error("Tuple or indexable External value expected, #{pretty_type(value)} found", :tuple, loc)
  end

  value
end

.type(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/liquor/runtime.rb', line 13

def self.type(value)
  case value
  when nil;         :null
  when true, false; :boolean
  when Integer;     :integer
  when String;      :string
  when Array;       :tuple
  when External;    :external
  else;             :invalid
  end
end

.with_fatal_deprecationsObject



63
64
65
66
67
68
69
70
# File 'lib/liquor/runtime.rb', line 63

def self.with_fatal_deprecations
  old_fatal_deprecations = @fatal_deprecations
  @fatal_deprecations    = true

  yield
ensure
  @fatal_deprecations    = old_fatal_deprecations
end