Ticket #562: test2.pir

File test2.pir, 3.4 KB (added by bsdz, 6 years ago)

PIR demonstrating the general case.

Line 
1.sub '' :main
2    say 'START'
3
4    .local pmc types, ops
5    types = split ' ', 'Integer Float Complex'
6    ops = split ' ', 'add sub mul div'
7   
8    .local pmc i1
9    .local string base_type, derived_type
10    i1 = iter types
11    next_i1:
12        unless i1, end_i1
13        base_type = shift i1
14        derived_type = concat 'Foo', base_type
15
16        $P0 = subclass base_type, derived_type
17       
18        .local pmc i2, cs
19        .local string op 
20        i2 = iter ops
21        next_i2:
22            unless i2, end_i2
23            op = shift i2
24           
25            cs = new 'CodeString'
26            cs.'emit'("print '%0: 10 %1 2 = '", base_type, op)
27            cs.'emit'("$P0 = new '%0'", base_type)
28            cs.'emit'("$P0 = 10")
29            cs.'emit'("%0 $P1, $P0, 2", op)
30            cs.'emit'("say $P1")
31            eval(cs)
32
33            cs = new 'CodeString'
34            cs.'emit'("print '%0: 10 %1 2 = '", derived_type, op)
35            cs.'emit'("$P0 = new '%0'", derived_type)
36            cs.'emit'("$P0 = 10")
37            cs.'emit'("%0 $P1, $P0, 2", op)
38            cs.'emit'("say $P1")
39            eval(cs)
40               
41            goto next_i2
42        end_i2:
43       
44        goto next_i1
45    end_i1:
46   
47    say 'FINISH'
48.end
49
50.sub eval   
51    .param string code
52   
53    .local pmc PIR
54    PIR = compreg 'PIR'
55   
56    .local pmc eh
57    eh = new 'ExceptionHandler'
58    set_addr eh, handler
59    push_eh eh
60   
61    .local pmc cs
62    cs = new ['CodeString']
63    cs.'emit'(".sub '' :anon")
64    cs.'emit'(code)
65    cs.'emit'(".end")
66    $P0 = PIR(cs)
67    $P0() 
68    pop_eh
69    goto end
70   
71    handler:
72        .local pmc ex
73        .local string msg
74        .get_results(ex)
75        msg = ex
76        say ex
77   
78    end:
79.end
80
81=begin output
82
83START
84Integer: 10 add 2 = 12
85FooInteger: 10 add 2 = Multiple Dispatch: No suitable candidate found for 'add_int', with signature 'PIP->P'
86Integer: 10 sub 2 = 8
87FooInteger: 10 sub 2 = Multiple Dispatch: No suitable candidate found for 'subtract_int', with signature 'PIP->P'
88Integer: 10 mul 2 = 20
89FooInteger: 10 mul 2 = Multiple Dispatch: No suitable candidate found for 'multiply_int', with signature 'PIP->P'
90Integer: 10 div 2 = 5
91FooInteger: 10 div 2 = Multiple Dispatch: No suitable candidate found for 'divide_int', with signature 'PIP->P'
92Float: 10 add 2 = 12
93FooFloat: 10 add 2 = Multiple Dispatch: No suitable candidate found for 'add_int', with signature 'PIP->P'
94Float: 10 sub 2 = 8
95FooFloat: 10 sub 2 = Multiple Dispatch: No suitable candidate found for 'subtract_int', with signature 'PIP->P'
96Float: 10 mul 2 = 20
97FooFloat: 10 mul 2 = Multiple Dispatch: No suitable candidate found for 'multiply_int', with signature 'PIP->P'
98Float: 10 div 2 = 5
99FooFloat: 10 div 2 = Multiple Dispatch: No suitable candidate found for 'divide_int', with signature 'PIP->P'
100Complex: 10 add 2 = 12+0i
101FooComplex: 10 add 2 = Multiple Dispatch: No suitable candidate found for 'add_int', with signature 'PIP->P'
102Complex: 10 sub 2 = 8+0i
103FooComplex: 10 sub 2 = Multiple Dispatch: No suitable candidate found for 'subtract_int', with signature 'PIP->P'
104Complex: 10 mul 2 = 20+0i
105FooComplex: 10 mul 2 = Multiple Dispatch: No suitable candidate found for 'multiply_int', with signature 'PIP->P'
106Complex: 10 div 2 = 5+0i
107FooComplex: 10 div 2 = Multiple Dispatch: No suitable candidate found for 'divide_int', with signature 'PIP->P'
108FINISH
109
110=end output
111