Module: Voodoo::Validator

Defined in:
lib/voodoo/validator.rb

Overview

Functionality for validating Voodoo code.

Defined Under Namespace

Classes: ValidationError

Constant Summary collapse

BINOPS =

Expressions that take two arguments

[:add, :and, :asr, :bsr, :div, :'get-byte', :'get-word',
:mod, :mul, :or, :rol, :ror, :shl, :shr, :sub, :xor]
VARARG_EXPRS =

Expressions that take zero or more parameters

[:call, :'tail-call']
EXPRS =

Symbols that may occur as the first word of an expression

BINOPS + VARARG_EXPRS + [:not]
STATEMENTS =

Symbols that are a valid start of a statement

[:block, :call, :goto,
:ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne,
:label, :let, :return, :set,
:'set-byte', :'set-word', :'tail-call']
TOP_LEVELS =

Symbols that are valid at top-level

[:align, :byte, :export, :function, :import,
:section, :string, :word] + STATEMENTS
NTH =
['First', 'Second', 'Third']

Class Method Summary collapse

Class Method Details

.assert_at_least_n_params(expr, n) ⇒ Object

Tests that an expression has at least n parameters. Raises a ValidationError if this is not the case.



329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/voodoo/validator.rb', line 329

def assert_at_least_n_params expr, n
  if expr.length <= n
    if n == 1
      raise ValidationError.new \
        "#{expr[0]} should have at least one parameter"
    else
      raise ValidationError.new \
        "#{expr[0]} should have at least #{n} parameters"
    end
  end
  true
end

.assert_n_params(expr, n) ⇒ Object

Tests that an expression has exactly n parameters. Raises a ValidationError if this is not the case.



344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/voodoo/validator.rb', line 344

def assert_n_params expr, n
  if expr.length != n + 1
    if n == 1
      raise ValidationError.new \
        "#{expr[0]} should have exactly one parameter"
    else
      raise ValidationError.new \
        "#{expr[0]} should have exactly #{n} parameters"
    end
  end
  true
end

.assert_params_are_values(expr, ns = nil) ⇒ Object

Tests that parameters to an expression are values (integers, symbols, or at-expressions), and raises ValidationError if this is not the case. If ns is nil (default) all parameters should me values. Alternatively, ns may be a range or array containing the indices of the parameters that should be values.



366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/voodoo/validator.rb', line 366

def assert_params_are_values expr, ns = nil
  if ns == nil
    ns = (1...expr.length)
  end
  ns.each do |i|
    unless int_or_symbol_or_at?(expr[i])
      raise ValidationError.new \
        "#{NTH[i - 1]} parameter to #{expr[0]}" +
        " should be a value (symbol, integer, or at-expression)"
    end
  end
  true
end

.int_or_symbol?(x) ⇒ Boolean

Returns:

  • (Boolean)


380
381
382
# File 'lib/voodoo/validator.rb', line 380

def int_or_symbol? x
  x.kind_of?(::Symbol) || x.kind_of?(::Integer)
end

.int_or_symbol_or_at?(x) ⇒ Boolean

Returns:

  • (Boolean)


384
385
386
387
388
# File 'lib/voodoo/validator.rb', line 384

def int_or_symbol_or_at? x
  int_or_symbol?(x) ||
    (x.respond_to?(:length) && x.length == 2 && x[0] == :'@' &&
     int_or_symbol?(x[1]))
end

.validate_expression(code) ⇒ Object

Validates an expression. Returns true if the expression is valid. Raises ValidationError if the expression is not valid.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/voodoo/validator.rb', line 30

def validate_expression code
  if int_or_symbol_or_at? code
    true
  elsif code.respond_to? :[]
    if BINOPS.member? code[0]
      # binop should have 2 parameters, both of them atomic values
      assert_n_params code, 2
      assert_params_are_values code
    else
      # code[0] is not a binop
      case code[0]
      when :call, :'tail-call'
        # call should have at least 1 parameter
        # and all parameters should be atomic values
        assert_at_least_n_params code, 1
        assert_params_are_values code
      when :'get-byte', :'get-word'
        # Should have exactly 2 parameters, both of which should be values.
        assert_n_params code, 2
        assert_params_are_values code
      when :not
        # should have a single, atomic parameter
        assert_n_params code, 1
        assert_params_are_values code
      else
        raise ValidationError.new("#{code[0].inspect}" +
                                  " cannot start an expression",
                                  code)
      end
    end
  else
    # code is not an atomic value and does not respond to :[]
    raise ValidationError.new("#{code.inspect} is not a valid expression",
                              code)
  end
end

.validate_statement(code) ⇒ Object

Validates a statement. Returns true if the statement is valid. Raises ValidationError if the statement is not valid.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/voodoo/validator.rb', line 70

def validate_statement code
  begin
    case code[0]
    when :block
      code[1..-1].each {|stmt| validate_statement stmt}
      true

    when :call, :'tail-call'
      validate_expression code

    when :goto
      # should have exactly 1 parameter, which should be atomic
      assert_n_params code, 1
      assert_params_are_values code

    when :ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne
      # Should have 2 or 3 parameters.
      # First parameter should be an array (or similar) containing
      # two elements, both atomic.
      # Second parameter should consist of one or more statements.
      # Third parameter, if present, should consist of zero or more
      # statements.
      # let is not allowed as a statement in either parameter, though
      # it can be embedded in a block in either.
      if code.length < 3 || code.length > 4
        raise ValidationError.new("#{code[0]} takes 2 or 3 parameters",
                                  code)
      elsif code[1].length != 2
        raise ValidationError.new("#{code[0]} requires two values to" +
                                  " compare in its first parameter",
                                  code)
      elsif !code[1].all? {|x| int_or_symbol_or_at? x}
        raise ValidationError.new("Values to compare should be values" +
                                  " (symbols, integers, or at-exrpssions)",
                                  code)
      else
        code[2].each do |stmt|
          validate_statement stmt
          if stmt[0] == :let
            raise ValidationError.new("let is not allowed inside " +
                                      code[0].to_s, code)
          end
        end

        if code.length > 3
          code[3].each do |stmt|
            validate_statement stmt
            if stmt[0] == :let
              raise ValidationError.new("let is not allowed inside " +
                                        code[0].to_s, code)
            end
          end
        end

        true
      end

    when :label
      # should have 1 parameter which should be a symbol
      if code.length != 2 || !code[1].kind_of?(::Symbol)
        raise ValidationError.new("label requires a single symbol" +
                                  "as its parameter", code)
      else
        true
      end

    when :let, :set
      # should have at least 2 parameters
      if code.length < 3
        raise ValidationError.new("#{code[0]} requires a symbol" +
                                  " and an expression", code)
      elsif code[1].kind_of? ::Symbol
        # After the symbol, there should be an expression
        expr = code[2..-1]
        if expr.length == 1
          validate_expression expr[0]
        else
          validate_expression expr
        end
      else
        raise ValidationError.new("First parameter to #{code[0]} should be" +
                                    " a symbol", code)
      end

    when :return
      # Should either have no parameters, or a single expression as
      # a parameter.
      case code.length
      when 1
        true
      when 2
        validate_expression code[1]
      else
        validate_expression code[1..-1]
      end

    when :'set-byte', :'set-word'
      # Should have exactly 3 parameters, all of which should be
      # atomic values.
      assert_n_params code, 3
      assert_params_are_values code

    else
      raise ValidationError.new("Not a valid statement: #{code.inspect}",
                                code)
    end

  rescue ValidationError
    # Pass it on
    raise

  rescue Exception => e
    if code.respond_to? :[]
      # Pass on the exception
      raise
    else
      raise ValidationError.new("#{code.inspect} does not respond to" +
                                ":[]", code)
    end

  end
end

.validate_top_level(code) ⇒ Object

Validates a top-level directive. Returns true if the directive is valid. Raises ValidationError if the directive is not valid.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/voodoo/validator.rb', line 196

def validate_top_level code
  begin
    case code[0]
    when :align
      # Should either have no parameter or a single integer parameter
      if code.length == 1 || (code.length == 2 &&
                              code[1].kind_of?(::Integer))
        true
      else
        raise ValidationError.new("align requires either a single" +
                                  " integer parameter, or no parameters",
                                  code)
      end

    when :byte, :word
      # Should have a single integer or symbol parameter
      if code.length != 2 || !int_or_symbol?(code[1])
        raise ValidationError.new("#{code[0]} requires a single" +
                                  " parameter that is either an " +
                                  " integer or a symbol", code)
      else
        true
      end

    when :export, :import
      # Should have at least 1 parameter, and all parameters should
      # be symbols.
      if code.length < 2
        raise ValidationError.new("#{code[0]} requires at least " +
                                  " one parameter", code)
      elsif code[1..-1].all? {|x| x.kind_of? ::Symbol}
        true
      else
        raise ValidationError.new("All parameters to #{code[0]}" +
                                  " should be symbols", code)
      end

    when :function

      # Check that formal parameters have been specified
      if code.length < 2
        raise ValidationError.new("Formal parameters should be" +
                                  " specified for function",
                                  code)
      end

      # Check that all formal parameters are symbols
      code[1].each do |formal|
        unless formal.kind_of? ::Symbol
          raise ValidationError.new("Formal parameter #{formal.inspect}" +
                                    " should be a symbol",
                                    formal)
        end
      end

      # Verify statements
      code[2..-1].each do |stmt|
        if stmt.respond_to?(:[]) && stmt[0] == :function
          raise ValidationError.new("Function definitions are only " +
                                    "valid at top-level", stmt)
        else
          validate_statement stmt
        end
      end

    when :section

      # Check that we have a string or a symbol
      case code.length
      when 1
        raise ValidationError.new("Section name should be specified",
                                  code)
      when 2
        unless code[1].kind_of?(::String) || code[1].kind_of?(::Symbol)
          raise ValidationError.new("Section name should be a string" +
                                    " or a symbol",
                                    code)
        end

      else
        raise ValidationError.new("section directive should have only" +
                                  " a single parameter",
                                  code);
      end

    when :string
      # Should have a single string parameter
      if code.length != 2 || !code[1].kind_of?(::String)
        raise ValidationError.new("string requires a single string" +
                                  " as a parameter", code)
      else
        true
      end

    else
      if STATEMENTS.member? code[0]
        validate_statement code
      else
        raise ValidationError.new("Directive #{code[0]}" +
                                  " not valid at top-level",
                                  code)
      end
    end

  rescue ValidationError
    # Pass it on
    raise

  rescue Exception => e
    if code.respond_to? :[]
      # Pass on the exception
      raise
    else
      raise ValidationError.new("#{code.inspect} does not respond to" +
                                ":[]", code)
    end
  end

  # If we got here, all is well
  true
end