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

export enum LocType {
REGISTER = "register",
TEMPORARY = "temporary",
VARIABLE = "variable",
}
export class Loc {
type: LocType
name: string
constructor(type: LocType, name: string) {
this.type = type
this.name = name
}
reduce<A>(
vari: (name: string) => A,
temp: (name: string) => A,
reg: (name: string) => A,
): A {
switch (this.type) {
case LocType.VARIABLE:
return vari(this.name)
case LocType.TEMPORARY:
return temp(this.name)
case LocType.REGISTER:
return reg(this.name)
}
}
ppName(): string {
return this.reduce(
(name: string) => name,
(name: string) => '#' + name,
(name: string) => '%' + name,
)
}
asmName(): string {
return this.reduce(
(name: string) => `(${name})`,
(_name: string) => {
throw new Error("temporaries do not exist in assembly!")
},
(name: string) => name.toUpperCase(),
)
}
static vari(name: string): Loc {
return new Loc(LocType.VARIABLE, name)
}
static temp(name: string): Loc {
return new Loc(LocType.TEMPORARY, name)
}
static reg(name: string): Loc {
return new Loc(LocType.REGISTER, name)
}
}