| 1 | import binaryen from './binaryen.js'; |
| 2 | |
| 3 | export default class Compiler { |
| 4 | compile(parsed) { |
| 5 | const module = new binaryen.Module(); |
| 6 | |
| 7 | for (const line of parsed) { |
| 8 | const { firstOperand, operator, secondOperand } = line; |
| 9 | |
| 10 | if (operator === '+') { |
| 11 | module.addFunction( |
| 12 | 'add', // name: string |
| 13 | binaryen.createType([binaryen.i32, binaryen.i32]), // params: Type |
| 14 | binaryen.i32, // results: Type |
| 15 | [binaryen.i32], // vars: Type[] |
| 16 | // body: ExpressionRef |
| 17 | module.block(null, [ |
| 18 | module.local.set( |
| 19 | 2, |
| 20 | module.i32.add( |
| 21 | module.local.get(0, binaryen.i32), |
| 22 | module.local.get(1, binaryen.i32) |
| 23 | ) |
| 24 | ), |
| 25 | module.return(module.local.get(2, binaryen.i32)), |
| 26 | ]) |
| 27 | ); |
| 28 | module.addFunctionExport('add', 'add'); |
| 29 | } else if (operator === '-') { |
| 30 | module.addFunction( |
| 31 | 'subtract', // name: string |
| 32 | binaryen.createType([binaryen.i32, binaryen.i32]), // params: Type |
| 33 | binaryen.i32, // results: Type |
| 34 | [binaryen.i32], // vars: Type[] |
| 35 | // body: ExpressionRef |
| 36 | module.block(null, [ |
| 37 | module.local.set( |
| 38 | 2, |
| 39 | module.i32.sub( |
| 40 | module.local.get(0, binaryen.i32), |
| 41 | module.local.get(1, binaryen.i32) |
| 42 | ) |
| 43 | ), |
| 44 | module.return(module.local.get(2, binaryen.i32)), |
| 45 | ]) |
| 46 | ); |
| 47 | module.addFunctionExport('subtract', 'subtract'); |
| 48 | } else if (operator === '*') { |
| 49 | module.addFunction( |
| 50 | 'multiply', // name: string |
| 51 | binaryen.createType([binaryen.i32, binaryen.i32]), // params: Type |
| 52 | binaryen.i32, // results: Type |
| 53 | [binaryen.i32], // vars: Type[] |
| 54 | // body: ExpressionRef |
| 55 | module.block(null, [ |
| 56 | module.local.set( |
| 57 | 2, |
| 58 | module.i32.mul( |
| 59 | module.local.get(0, binaryen.i32), |
| 60 | module.local.get(1, binaryen.i32) |
nothing calls this directly
no outgoing calls
no test coverage detected