Skip to content

Commit

Permalink
version bump + change in coq-lsp dependency. also can potentially add…
Browse files Browse the repository at this point in the history
… color, but for now the color dictionary is just set to white
  • Loading branch information
bhaktishh committed Jul 11, 2024
1 parent 91e552b commit 1c26936
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Install [here](https://marketplace.visualstudio.com/items?itemName=inQWIRE.vizx).

To render manually, use command `Render Expressions with ViZX`.
To render manually, use command `Render Expressions with ZXViz`.
To render automatically, use command `Activate ZXViz automatic rendering`. To stop rendering automatically, use command `Deactivate ZXViz automatic rendering`.

## Requirements
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vizx",
"displayName": "ViZX",
"description": "Visualizer for the ZX calculus",
"version": "0.1.3",
"version": "0.1.4",
"repository": "https://github.com/inQWIRE/ViZX/",
"publisher": "inQWIRE",
"engines": {
Expand Down
26 changes: 26 additions & 0 deletions src/constants/variableconsts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,29 @@ export function setCanvasWidthHeight(wh: [number, number]) {
},
};
}

export let COLOR_DICT: string[] = ["#FFFFFF"];
// export let COLOR_DICT : string[] = [
// "#d0d6e4",
// "#cfb77f",
// "#a48758",
// "#b5bccd",
// "#8b92a2"
// ];

export function updateColorDict(newDict: Array<string>) {
console.log("pre update in update: ", COLOR_DICT);
if (COLOR_DICT !== undefined) {
while (COLOR_DICT.pop() !== undefined) {}
}
let w = JSON.parse(JSON.stringify(newDict.pop()));
while (w !== undefined) {
COLOR_DICT.push(JSON.parse(JSON.stringify(w)));
w = newDict.pop();
}
console.log("post update in update: ", COLOR_DICT);
}

export function getColorDictLength() {
return COLOR_DICT.length;
}
6 changes: 6 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export function activate(context: vscode.ExtensionContext) {
renderCallback(context, expr)
);
context.subscriptions.push(disposable);
let coqLspApi = vscode.extensions.getExtension("ejgallego.coq-lsp")!.exports;
let hook = coqLspApi.onUserGoals((goals: any) =>
vscode.commands.executeCommand("vizx.lspRender", goals)
);

disposable = vscode.commands.registerCommand("vizx.activateRendering", () => {
vscode.window.showInformationMessage(
"Automatic rendering is now turned on."
Expand All @@ -76,6 +81,7 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.showInformationMessage(
"Automatic rendering is now turned off."
);
hook.dispose();
}
);

Expand Down
2 changes: 2 additions & 0 deletions src/parsing/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ export interface ASTStack extends ASTNode {
kind: "stack";
left: ASTNode;
right: ASTNode;
index: number;
}

export interface ASTCompose extends ASTNode {
kind: "compose";
left: ASTNode;
right: ASTNode;
index: number;
}

export interface ASTNStack extends ASTNode {
Expand Down
24 changes: 22 additions & 2 deletions src/parsing/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ let _ = require("lodash");

import * as ast from "./ast";
import * as lex from "./lexer";
import * as v from "../constants/variableconsts";
import { lexerPrettyPrinter } from "./lexer";

type Token = psec.Token<lex.TokenKind>;
let index = 0;

function incrIndex() {
console.log("incrementing index: ", index + 1);
console.log("length of dict at incr index: ", v.getColorDictLength());
let r = index;
index = ++index % v.getColorDictLength();
return r;
}

/*********************** NUMBERS ******************************/

Expand Down Expand Up @@ -441,10 +451,20 @@ function applyStackCompose(
// console.log('calling applyStackCompose');
switch (args[0].kind) {
case lex.TokenKind.Compose: {
return { kind: "compose", left: l, right: args[1] } as ast.ASTCompose;
return {
kind: "compose",
left: l,
right: args[1],
index: incrIndex(),
} as ast.ASTCompose;
}
case lex.TokenKind.Stack: {
return { kind: "stack", left: l, right: args[1] } as ast.ASTStack;
return {
kind: "stack",
left: l,
right: args[1],
index: incrIndex(),
} as ast.ASTStack;
}
default: {
// throw new Error(`Unknown compose: ${args[0].text}`);
Expand Down
6 changes: 3 additions & 3 deletions src/parsing/sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function addSizesHappyRobot(node: ast.ASTNode): ast.ASTNode {
break;
}
}
console.log("happy robot! ", node);
// console.log("happy robot! ", node);
return node;
}
export function addSizes(node: ast.ASTNode): ast.ASTNode {
Expand Down Expand Up @@ -276,9 +276,9 @@ export function addSizes(node: ast.ASTNode): ast.ASTNode {
throw new Error(`Unknown kind: ${node.kind}`);
}
}
console.log("before happy robot: ", node);
// console.log("before happy robot: ", node);
node = addSizesHappyRobot(node);
console.log("after happy robot: ", node);
// console.log("after happy robot: ", node);
return node;
}

Expand Down
20 changes: 14 additions & 6 deletions src/rendering/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
SMALL_TEXT,
ARIAL_FONT,
REALLY_SMALL_TEXT,
COLOR_DICT,
} from "../constants/variableconsts";
import { findCenter, findLeftCenter, findRightCenter } from "../parsing/coords";
import { quad } from "../constants/types";
Expand Down Expand Up @@ -173,18 +174,18 @@ function drawNWireNode(node: ast.ASTNode) {

function drawStackNode(node: ast.ASTNode) {
let stack = <ast.ASTStack>node;
drawBoundary(node.boundary!, STACK_DASH, COLOR_DICT[stack.index]);
draw(stack.left);
draw(stack.right);
drawBoundary(node.boundary!, STACK_DASH);
}

function drawNStackNode(node: ast.ASTNode) {
let nstack = <ast.ASTNStack>node;
draw(nstack.node);
let label_bound = JSON.parse(JSON.stringify(nstack.boundary!));
label_bound.tr.x = label_bound.tl.x + FUNC_ARG_SIZE;
label_bound.br.x = label_bound.bl.x + FUNC_ARG_SIZE;
drawBoundary(label_bound, FUNCTION_DASH);
draw(nstack.node);
let bound = JSON.parse(JSON.stringify(nstack.boundary!));
bound.tl.x += FUNC_ARG_SIZE;
bound.bl.x += FUNC_ARG_SIZE;
Expand All @@ -196,11 +197,11 @@ function drawNStackNode(node: ast.ASTNode) {

function drawNStack1Node(node: ast.ASTNode) {
let nstack = <ast.ASTNStack>node;
draw(nstack.node);
let label_bound = JSON.parse(JSON.stringify(nstack.boundary!));
label_bound.tr.x = label_bound.tl.x + FUNC_ARG_SIZE;
label_bound.br.x = label_bound.bl.x + FUNC_ARG_SIZE;
drawBoundary(label_bound, FUNCTION_DASH);
draw(nstack.node);
let bound = JSON.parse(JSON.stringify(nstack.boundary!));
bound.tl.x += FUNC_ARG_SIZE;
bound.bl.x += FUNC_ARG_SIZE;
Expand All @@ -210,21 +211,28 @@ function drawNStack1Node(node: ast.ASTNode) {
ctx.fillText(nstack.n.expr.concat(N_STACK_1_OP), cent.x, cent.y);
}

function drawBoundary(boundary: quad, dash?: [number, number]) {
function drawBoundary(
boundary: quad,
dash?: [number, number],
color: string = white
) {
if (dash !== undefined) {
ctx.setLineDash(dash);
} else {
ctx.setLineDash([]);
}
ctx.lineWidth = LINE_WIDTH;
ctx.strokeStyle = black;
console.log("setting fill style in draw boundary, ", color);
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(boundary.tl.x, boundary.tl.y);
ctx.lineTo(boundary.tr.x, boundary.tr.y);
ctx.lineTo(boundary.br.x, boundary.br.y);
ctx.lineTo(boundary.bl.x, boundary.bl.y);
ctx.closePath();
ctx.stroke();
ctx.fill();
return;
}

Expand All @@ -250,15 +258,15 @@ function drawFuncBoundary(boundary: quad) {

function drawComposeNode(node: ast.ASTNode) {
let compose = <ast.ASTCompose>node;
drawBoundary(node.boundary!, COMPOSE_DASH, COLOR_DICT[compose.index]);
draw(compose.left);
draw(compose.right);
drawBoundary(node.boundary!, COMPOSE_DASH);
}

function drawCastNode(node: ast.ASTNode) {
let cast = <ast.ASTCast>node;
draw(cast.node);
drawBoundary(cast.boundary!, CAST_DASH);
draw(cast.node);
let lc = findLeftCenter(cast.boundary!);
let rc = findRightCenter(cast.boundary!);
ctx.save();
Expand Down

0 comments on commit 1c26936

Please sign in to comment.