├── .gitignore ├── .prettierrc.js ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── package.json ├── src ├── ast.ts ├── eval.ts ├── index.ts ├── parser.ts ├── test │ └── test.ts └── utils.ts ├── tsconfig.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | semi: true, 3 | trailingComma: 'all', 4 | singleQuote: true, 5 | bracketSpacing: false, 6 | arrowParens: 'always', 7 | }; 8 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true, 3 | "typescript.tsdk": "node_modules\\typescript\\lib", 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 judehunter 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ts-asm 2 | 3 | Read the story: https://judehunter.dev/blog/assembly-interpreter-in-typescripts-type-system 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "prettier": "^2.7.1", 4 | "typescript": "^4.8.4" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/ast.ts: -------------------------------------------------------------------------------- 1 | export type Register = 2 | | 'r0' 3 | | 'r1' 4 | | 'r2' 5 | | 'r3' 6 | | 'r4' 7 | | 'r5' 8 | | 'r6' 9 | | 'r7' 10 | | 'sp' 11 | | 'lr' 12 | | 'pc'; 13 | 14 | export type Immediate = string; 15 | 16 | export type AddIInstr< 17 | Rd extends Register, 18 | Rn extends Register, 19 | imm extends Immediate, 20 | > = { 21 | type: 'AddI'; 22 | Rd: Rd; 23 | Rn: Rn; 24 | imm: imm; 25 | }; 26 | 27 | export type AddRInstr< 28 | Rd extends Register, 29 | Rn extends Register, 30 | Rm extends Register, 31 | > = { 32 | type: 'AddR'; 33 | Rd: Rd; 34 | Rn: Rn; 35 | Rm: Rm; 36 | }; 37 | 38 | export type SubIInstr< 39 | Rd extends Register, 40 | Rn extends Register, 41 | imm extends Immediate, 42 | > = { 43 | type: 'SubI'; 44 | Rd: Rd; 45 | Rn: Rn; 46 | imm: imm; 47 | }; 48 | 49 | export type SubRInstr< 50 | Rd extends Register, 51 | Rn extends Register, 52 | Rm extends Register, 53 | > = { 54 | type: 'SubR'; 55 | Rd: Rd; 56 | Rn: Rn; 57 | Rm: Rm; 58 | }; 59 | 60 | export type MovIInstr = { 61 | type: 'MovI'; 62 | Rd: Rd; 63 | imm: imm; 64 | }; 65 | 66 | export type MovRInstr = { 67 | type: 'MovR'; 68 | Rd: Rd; 69 | Rm: Rm; 70 | }; 71 | 72 | export type LabelInstr = { 73 | type: 'Label'; 74 | name: name; 75 | }; 76 | 77 | export type _BranchInstr