Skip to content

Commit

Permalink
Compile unary
Browse files Browse the repository at this point in the history
  • Loading branch information
qti3e committed Aug 4, 2019
1 parent 3fd4e15 commit a21d438
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
19 changes: 4 additions & 15 deletions jsc/src/play.ts
Expand Up @@ -3,25 +3,14 @@ import { dump } from "./dump";

function main() {
const source = `
var {
a,
"T": b,
_c: c,
kids: {
_d: d,
...e
},
list: [f, g, ...h],
...i
} = test;
{
var j = 9;
}
void x;
a - -0;
`;

const context = new Compiler();
console.time("Compile");
context.compile(source);
console.timeEnd("Compile");
const ret = context.getCompiledProgram();

console.log(dump(ret.main));
Expand Down
32 changes: 32 additions & 0 deletions jsc/src/visitor.ts
Expand Up @@ -30,6 +30,11 @@ export function visit(writer: Writer, node: estree.Node): void {
jmp1.here();
break;

case "UnaryExpression":
visit(writer, node.argument);
unaryOperator(writer, node.operator);
break;

case "Literal":
switch (node.value) {
case true:
Expand Down Expand Up @@ -146,3 +151,30 @@ function binaryOperator(writer: Writer, operator: estree.BinaryOperator): void {
break;
}
}

function unaryOperator(writer: Writer, operator: estree.UnaryOperator): void {
switch (operator) {
case "-":
writer.write(ByteCode.Neg);
break;
case "!":
writer.write(ByteCode.Not);
break;
case "+":
writer.write(ByteCode.Pos);
break;
case "~":
writer.write(ByteCode.BitNot);
break;
case "delete":
// TODO(qti3e);
writer.write(ByteCode.TODO);
break;
case "typeof":
writer.write(ByteCode.Type);
break;
case "void":
writer.write(ByteCode.Void);
break;
}
}

0 comments on commit a21d438

Please sign in to comment.