A "high-level" language for the Gameboy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

  1. import type { Loc } from "./loc"
  2. import type { AbsInsn2, AbsInsn3, Operand } from "./insn"
  3. export const prettyPrintBlock = <A extends AbsInsn2 | AbsInsn3>(block: Array<A>): string => {
  4. return block.map(prettyPrintInsn).join("\n")
  5. }
  6. export const prettyPrintInsn = (ssa: AbsInsn2 | AbsInsn3): string => {
  7. let output = ""
  8. switch (ssa.type) {
  9. case 'copy':
  10. output = `${ppLoc(ssa.dest)} = ${ppOp(ssa.source)}`
  11. break
  12. case 'unary':
  13. output = `${ppLoc(ssa.dest)} = ${ssa.op}(${ppOp(ssa.source)})`
  14. break
  15. case 'binary':
  16. output = `${ppLoc(ssa.dest)} = ${ssa.op}(${ppOp(ssa.source)}, ${ppOp(ssa.source1)})`
  17. break
  18. case 'goto':
  19. output = `goto(${ssa.dest})`
  20. break
  21. case 'label':
  22. output = `${ssa.dest}:`
  23. break
  24. }
  25. return output
  26. }
  27. const ppOp = (op: Operand): string =>
  28. typeof op === "number"
  29. ? op.toString()
  30. : ppLoc(op)
  31. const ppLoc = (loc: Loc): string => ({
  32. 'register': `%${loc.name}`,
  33. 'temporary': `#${loc.name}`,
  34. 'variable': loc.name,
  35. }[loc.type])