From 19d01547fb67bbdbc31d8cb1c205158979b171da Mon Sep 17 00:00:00 2001 From: Forest Belton Date: Fri, 17 Sep 2021 23:30:50 -0400 Subject: [PATCH] Support remaining operators from parser --- example.gby | 1 + lib/parser.pegjs | 3 ++- lib/sm83/realize.ts | 29 ++++++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/example.gby b/example.gby index 7068d90..ba7531b 100644 --- a/example.gby +++ b/example.gby @@ -6,3 +6,4 @@ u8 y; x <- -y + j; z <- w + 7; +z <- z << 1; diff --git a/lib/parser.pegjs b/lib/parser.pegjs index 3338def..85ca7b5 100644 --- a/lib/parser.pegjs +++ b/lib/parser.pegjs @@ -34,7 +34,8 @@ BitXorOp = CARET { return "bit_xor" } BitAndExpr = head:ShiftExpr tail:(op:BitAndOp e:ShiftExpr)* { return assocl(head, tail || []) } 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" } / GTGT { return "shift_right" } diff --git a/lib/sm83/realize.ts b/lib/sm83/realize.ts index c06d3f8..0f2fadd 100644 --- a/lib/sm83/realize.ts +++ b/lib/sm83/realize.ts @@ -21,18 +21,41 @@ export const realizeUnary = (dest: Loc, op: UnaryOp | BinaryOp, source: Operand) throw new Error("unexpected form for unary operation") } + let output: Array = [] + switch (op) { case "add": 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": return ["CPL", "INC A"] case "bit_negate": return ["CPL"] - - default: - throw new Error(`unsupported unary op \`${op}'`) } }