Made parser able to report multiple errors per call, using new Voodoo::Parser::Multip...
[voodoo-lang.git] / test / gcd.rb
blob097e65b137c6426cb41d442b2a9b6776f0e7008e
1 # Test case for the code generator API introduced in 0.5.0
3 require 'voodoo'
5 # Instantiate code generator
6 generator = Voodoo::CodeGenerator.get_generator
8 generator.instance_eval do
9   add :code, [:import, :printf], [:export, :main]
11   # Define format string
12   add :data, [:align], [:label, :format], [:string, "gcd(%d, %d): %d\n\x00"]
14   # Define gcd function
15   add :functions, [:align], [:label, :gcd],
16       [:function, [:x, :y],
17        [:iflt, [:x, :y],
18         [[:return, :call, :gcd, :y, :x]]],
19        [:let, :m, :mod, :x, :y],
20        [:ifeq, [:m, 0],
21         [[:return, :y]],
22         [[:'tail-call', :gcd, :y, :m]]]]
24   # Define main
25   add :functions, [:align], [:label, :main]
26   add_function [:argv, :argc],
27                [:let, :x, :call, :gcd, 11, 7],
28                [:call, :printf, :format, 11, 7, :x],
29                [:set, :x, :call, :gcd, 7, 11],
30                [:call, :printf, :format, 7, 11, :x],
31                [:set, :x, :call, :gcd, 33, 27],
32                [:call, :printf, :format, 33, 27, :x],
33                [:return, 0]
34 end
36 # Let code generator determine output file name
37 outfile = generator.output_file_name 'gcd.voo'
38 File.open(outfile, 'w') { |file| generator.write file }