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.

60 lines
1.4 KiB

  1. export enum LocType {
  2. REGISTER = "register",
  3. TEMPORARY = "temporary",
  4. VARIABLE = "variable",
  5. }
  6. export class Loc {
  7. type: LocType
  8. name: string
  9. constructor(type: LocType, name: string) {
  10. this.type = type
  11. this.name = name
  12. }
  13. reduce<A>(
  14. vari: (name: string) => A,
  15. temp: (name: string) => A,
  16. reg: (name: string) => A,
  17. ): A {
  18. switch (this.type) {
  19. case LocType.VARIABLE:
  20. return vari(this.name)
  21. case LocType.TEMPORARY:
  22. return temp(this.name)
  23. case LocType.REGISTER:
  24. return reg(this.name)
  25. }
  26. }
  27. ppName(): string {
  28. return this.reduce(
  29. (name: string) => name,
  30. (name: string) => '#' + name,
  31. (name: string) => '%' + name,
  32. )
  33. }
  34. asmName(): string {
  35. return this.reduce(
  36. (name: string) => `(${name})`,
  37. (_name: string) => {
  38. throw new Error("temporaries do not exist in assembly!")
  39. },
  40. (name: string) => name.toUpperCase(),
  41. )
  42. }
  43. static vari(name: string): Loc {
  44. return new Loc(LocType.VARIABLE, name)
  45. }
  46. static temp(name: string): Loc {
  47. return new Loc(LocType.TEMPORARY, name)
  48. }
  49. static reg(name: string): Loc {
  50. return new Loc(LocType.REGISTER, name)
  51. }
  52. }