Browse Source

Support remaining operators from parser

master
Forest Belton 3 years ago
parent
commit
19d01547fb
3 changed files with 29 additions and 4 deletions
  1. +1
    -0
      example.gby
  2. +2
    -1
      lib/parser.pegjs
  3. +26
    -3
      lib/sm83/realize.ts

+ 1
- 0
example.gby View File

@ -6,3 +6,4 @@ u8 y;
x <- -y + j; x <- -y + j;
z <- w + 7; z <- w + 7;
z <- z << 1;

+ 2
- 1
lib/parser.pegjs View File

@ -34,7 +34,8 @@ BitXorOp = CARET { return "bit_xor" }
BitAndExpr = head:ShiftExpr tail:(op:BitAndOp e:ShiftExpr)* { return assocl(head, tail || []) } BitAndExpr = head:ShiftExpr tail:(op:BitAndOp e:ShiftExpr)* { return assocl(head, tail || []) }
BitAndOp = AMPERSAND { return "bit_and" } BitAndOp = AMPERSAND { return "bit_and" }
ShiftExpr = head:AddExpr tail:(op:ShiftOp e:AddExpr)* { return assocl(head, tail || []) }
ShiftExpr = head:AddExpr op:ShiftOp n:Number { return bexpr(op, head, n) }
/ AddExpr
ShiftOp = LTLT { return "shift_left" } ShiftOp = LTLT { return "shift_left" }
/ GTGT { return "shift_right" } / GTGT { return "shift_right" }

+ 26
- 3
lib/sm83/realize.ts View File

@ -21,18 +21,41 @@ export const realizeUnary = (dest: Loc, op: UnaryOp | BinaryOp, source: Operand)
throw new Error("unexpected form for unary operation") throw new Error("unexpected form for unary operation")
} }
let output: Array<SM83Insn> = []
switch (op) { switch (op) {
case "add": case "add":
return [`ADD ${getSourceName(source)}`] return [`ADD ${getSourceName(source)}`]
case "subtract":
return [`SUB ${getSourceName(source)}`]
case "bit_and":
return [`AND ${getSourceName(source)}`]
case "bit_or":
return [`OR ${getSourceName(source)}`]
case "bit_xor":
return [`XOR ${getSourceName(source)}`]
case "shift_left":
for (let i = 0; i < source; ++i) {
output.push("SLA A")
}
return output
case "shift_right":
for (let i = 0; i < source; ++i) {
output.push("SRL A")
}
return output
case "arith_negate": case "arith_negate":
return ["CPL", "INC A"] return ["CPL", "INC A"]
case "bit_negate": case "bit_negate":
return ["CPL"] return ["CPL"]
default:
throw new Error(`unsupported unary op \`${op}'`)
} }
} }

Loading…
Cancel
Save