@@ -85,68 +85,78 @@ pub fn (mut g Gen) get_wasm_type(typ_ ast.Type) wasm.ValType {
8585 g.w_error ("get_wasm_type: unreachable type '${* g .table .sym (typ )}' ${ts .info }" )
8686}
8787
88+ pub fn (mut g Gen) infix_param_type (typ ast.Type, op token.Kind) {
89+ match typ {
90+ ast.string_type {
91+ g.handle_string_operation (op)
92+ }
93+ else {
94+ eprintln (* g.table.sym (typ))
95+ panic ('unimplemented infix operation for type' )
96+ }
97+ }
98+ }
99+
88100pub fn (mut g Gen) infix_from_typ (typ ast.Type, op token.Kind) {
89101 if g.is_param_type (typ) {
90- eprintln (* g.table.sym (typ))
91- panic ('unimplemented' )
102+ g.infix_param_type (typ, op)
103+ } else {
104+ g.infix_numeric_type (typ, op)
92105 }
106+ }
93107
108+ fn (mut g Gen) infix_numeric_type (typ ast.Type, op token.Kind) {
94109 wasm_typ := g.as_numtype (g.get_wasm_type (typ))
95110
111+ // This adds two tiers of comparaison but is a lot cleaner
96112 match op {
97- .plus {
98- g.func.add (wasm_typ)
99- }
100- .minus {
101- g.func.sub (wasm_typ)
102- }
103- .mul {
104- g.func.mul (wasm_typ)
105- }
106- .mod {
107- g.func.rem (wasm_typ, typ.is_signed ())
108- }
109- .div {
110- g.func.div (wasm_typ, typ.is_signed ())
111- }
112- .eq {
113- g.func.eq (wasm_typ)
114- }
115- .ne {
116- g.func.ne (wasm_typ)
117- }
118- .gt {
119- g.func.gt (wasm_typ, typ.is_signed ())
120- }
121- .lt {
122- g.func.lt (wasm_typ, typ.is_signed ())
123- }
124- .ge {
125- g.func.ge (wasm_typ, typ.is_signed ())
126- }
127- .le {
128- g.func.le (wasm_typ, typ.is_signed ())
113+ .plus, .minus, .mul, .div, .mod {
114+ g.emit_arithmetic_op (wasm_typ, typ, op)
129115 }
130- .xor {
131- g.func. b_xor (wasm_typ)
116+ .eq, .ne, .gt, .lt, .ge, .le {
117+ g.emit_comparison_op (wasm_typ, typ, op )
132118 }
133- .pipe {
134- g.func.b_or (wasm_typ)
135- }
136- .amp {
137- g.func.b_and (wasm_typ)
138- }
139- .left_shift {
140- g.func.b_shl (wasm_typ)
141- }
142- .right_shift {
143- g.func.b_shr (wasm_typ, true )
144- }
145- .unsigned_right_shift {
146- g.func.b_shr (wasm_typ, false )
119+ .xor, .pipe, .amp, .left_shift, .right_shift, .unsigned_right_shift {
120+ g.emit_bitwise_op (wasm_typ, typ, op)
147121 }
148122 else {
149123 g.w_error ('bad infix: op `${op }`' )
150124 }
151125 }
152126}
127+
128+ fn (mut g Gen) emit_arithmetic_op (wasm_typ wasm.NumType, typ ast.Type, op token.Kind) {
129+ match op {
130+ .plus { g.func.add (wasm_typ) }
131+ .minus { g.func.sub (wasm_typ) }
132+ .mul { g.func.mul (wasm_typ) }
133+ .div { g.func.div (wasm_typ, typ.is_signed ()) }
134+ .mod { g.func.rem (wasm_typ, typ.is_signed ()) }
135+ else { g.w_error ('invalid aritmetic op: `${op }`' ) }
136+ }
137+ }
138+
139+ fn (mut g Gen) emit_comparison_op (wasm_typ wasm.NumType, typ ast.Type, op token.Kind) {
140+ is_signed := typ.is_signed ()
141+ match op {
142+ .eq { g.func.eq (wasm_typ) }
143+ .ne { g.func.ne (wasm_typ) }
144+ .gt { g.func.gt (wasm_typ, is_signed) }
145+ .lt { g.func.lt (wasm_typ, is_signed) }
146+ .ge { g.func.ge (wasm_typ, is_signed) }
147+ .le { g.func.le (wasm_typ, is_signed) }
148+ else { g.w_error ('invalid comparison op: `${op }`' ) }
149+ }
150+ }
151+
152+ fn (mut g Gen) emit_bitwise_op (wasm_typ wasm.NumType, typ ast.Type, op token.Kind) {
153+ match op {
154+ .xor { g.func.b_xor (wasm_typ) }
155+ .pipe { g.func.b_or (wasm_typ) }
156+ .amp { g.func.b_and (wasm_typ) }
157+ .left_shift { g.func.b_shl (wasm_typ) }
158+ .right_shift { g.func.b_shr (wasm_typ, true ) }
159+ .unsigned_right_shift { g.func.b_shr (wasm_typ, false ) }
160+ else { g.w_error ('invalid bitwise op: `${op }`' ) }
161+ }
162+ }
0 commit comments