Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type-safety for Grecha tag #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions js/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function parse_primary(lexer) {
var expr = parse_expr(lexer);
token = lexer.next();
if (token !== ')') {
throw new Error("Expected ')' but got '" + token + "'");
throw new Error("Expected ')' but got '".concat(token, "'"));
}
return expr;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ function parse_primary(lexer) {
next_token = lexer.next();
}
if (next_token !== ')') {
throw Error("Expected ')' but got '" + next_token + "'");
throw Error("Expected ')' but got '".concat(next_token, "'"));
}
return {
"kind": "funcall",
Expand Down Expand Up @@ -172,7 +172,7 @@ function compile_expr(src) {
if (token !== null) {
console.log(typeof (token));
console.log(token);
throw new Error("Unexpected token '" + token + "'");
throw new Error("Unexpected token '".concat(token, "'"));
}
return result;
}
Expand All @@ -189,7 +189,7 @@ function run_expr(expr, user_context) {
if (user_context.vars && value in user_context.vars) {
return user_context.vars[value];
}
throw new Error("Unknown variable '" + value + "'");
throw new Error("Unknown variable '".concat(value, "'"));
}
else {
return number;
Expand All @@ -200,24 +200,24 @@ function run_expr(expr, user_context) {
if (unary_op.op in UNARY_OPS) {
return UNARY_OPS[unary_op.op](run_expr(unary_op.operand, user_context));
}
throw new Error("Unknown unary operator '" + unary_op.op + "'");
throw new Error("Unknown unary operator '".concat(unary_op.op, "'"));
}
case 'binary_op': {
var binary_op = expr.payload;
if (binary_op.op in BINARY_OPS) {
return BINARY_OPS[binary_op.op].func(run_expr(binary_op.lhs, user_context), run_expr(binary_op.rhs, user_context));
}
throw new Error("Unknown binary operator '" + binary_op.op + "'");
throw new Error("Unknown binary operator '".concat(binary_op.op, "'"));
}
case 'funcall': {
var funcall = expr.payload;
if (user_context.funcs && funcall.name in user_context.funcs) {
return (_a = user_context.funcs)[funcall.name].apply(_a, funcall.args.map(function (arg) { return run_expr(arg, user_context); }));
}
throw new Error("Unknown function '" + funcall.name + "'");
throw new Error("Unknown function '".concat(funcall.name, "'"));
}
default: {
throw new Error("Unexpected AST node '" + expr.kind + "'");
throw new Error("Unexpected AST node '".concat(expr.kind, "'"));
}
}
}
35 changes: 19 additions & 16 deletions js/grecha.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
function tag(name) {
Expand All @@ -13,7 +17,7 @@ function tag(name) {
var result = document.createElement(name);
for (var _a = 0, children_1 = children; _a < children_1.length; _a++) {
var child = children_1[_a];
if (typeof (child) === 'string') {
if (typeof child === 'string') {
result.appendChild(document.createTextNode(child));
}
else {
Expand All @@ -35,63 +39,63 @@ function canvas() {
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["canvas"], children));
return tag.apply(void 0, __spreadArray(["canvas"], children, false));
}
function h1() {
var children = [];
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["h1"], children));
return tag.apply(void 0, __spreadArray(["h1"], children, false));
}
function h2() {
var children = [];
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["h2"], children));
return tag.apply(void 0, __spreadArray(["h2"], children, false));
}
function h3() {
var children = [];
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["h3"], children));
return tag.apply(void 0, __spreadArray(["h3"], children, false));
}
function p() {
var children = [];
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["p"], children));
return tag.apply(void 0, __spreadArray(["p"], children, false));
}
function a() {
var children = [];
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["a"], children));
return tag.apply(void 0, __spreadArray(["a"], children, false));
}
function div() {
var children = [];
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["div"], children));
return tag.apply(void 0, __spreadArray(["div"], children, false));
}
function span() {
var children = [];
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["span"], children));
return tag.apply(void 0, __spreadArray(["span"], children, false));
}
function select() {
var children = [];
for (var _i = 0; _i < arguments.length; _i++) {
children[_i] = arguments[_i];
}
return tag.apply(void 0, __spreadArray(["select"], children));
return tag.apply(void 0, __spreadArray(["select"], children, false));
}
function img(src) {
return tag("img").att$("src", src);
Expand All @@ -117,8 +121,7 @@ function router(routes) {
result.appendChild(routes[hashLocation]);
return result;
}
;
syncHash();
window.addEventListener("hashchange", syncHash);
window.addEventListener('hashchange', syncHash);
return result;
}
42 changes: 21 additions & 21 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function compileShaderSource(gl, source, shaderType) {
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error("Could not compile " + shaderTypeToString() + " shader: " + gl.getShaderInfoLog(shader));
throw new Error("Could not compile ".concat(shaderTypeToString(), " shader: ").concat(gl.getShaderInfoLog(shader)));
}
return shader;
}
Expand All @@ -43,7 +43,7 @@ function linkShaderProgram(gl, shaders, vertexAttribs) {
}
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw new Error("Could not link shader program: " + gl.getProgramInfoLog(program));
throw new Error("Could not link shader program: ".concat(gl.getProgramInfoLog(program)));
}
return program;
}
Expand Down Expand Up @@ -77,36 +77,36 @@ function loadFilterProgram(gl, filter, vertexAttribs) {
var paramsInputs = {};
var _loop_1 = function (paramName) {
if (paramName in uniforms) {
throw new Error("Redefinition of existing uniform parameter " + paramName);
throw new Error("Redefinition of existing uniform parameter ".concat(paramName));
}
switch (filter.params[paramName].type) {
case "float":
{
var valuePreview_1 = span(filter.params[paramName].init.toString());
var valueInput = input("range");
if (filter.params[paramName].min !== undefined) {
valueInput.att$("min", filter.params[paramName].min);
valueInput.att$("min", filter.params[paramName].min.toString());
}
if (filter.params[paramName].max !== undefined) {
valueInput.att$("max", filter.params[paramName].max);
valueInput.att$("max", filter.params[paramName].max.toString());
}
if (filter.params[paramName].step !== undefined) {
valueInput.att$("step", filter.params[paramName].step);
valueInput.att$("step", filter.params[paramName].step.toString());
}
if (filter.params[paramName].init !== undefined) {
valueInput.att$("value", filter.params[paramName].init);
valueInput.att$("value", filter.params[paramName].init.toString());
}
paramsInputs[paramName] = valueInput;
valueInput.oninput = function () {
valuePreview_1.innerText = this.value;
valueInput.oninput = function (e) {
valuePreview_1.innerText = e.currentTarget.value;
paramsPanel.dispatchEvent(new CustomEvent("paramsChanged"));
};
var label = (_a = filter.params[paramName].label) !== null && _a !== void 0 ? _a : paramName;
paramsPanel.appendChild(div(span(label + ": "), valuePreview_1, div(valueInput)));
paramsPanel.appendChild(div(span("".concat(label, ": ")), valuePreview_1, div(valueInput)));
}
break;
default: {
throw new Error("Filter parameters do not support type " + filter.params[paramName].type);
throw new Error("Filter parameters do not support type ".concat(filter.params[paramName].type));
}
}
uniforms[paramName] = gl.getUniformLocation(id, paramName);
Expand Down Expand Up @@ -136,7 +136,7 @@ function ImageSelector() {
var imageInput = input("file");
var imagePreview = img("img/tsodinClown.png")
.att$("class", "widget-element")
.att$("width", CANVAS_WIDTH);
.att$("width", String(CANVAS_WIDTH));
var root = div(div(imageInput).att$("class", "widget-element"), imagePreview).att$("class", "widget");
root.selectedImage$ = function () {
return imagePreview;
Expand All @@ -155,7 +155,7 @@ function ImageSelector() {
};
root.updateFiles$ = function (files) {
imageInput.files = files;
imageInput.onchange();
imageInput.dispatchEvent(new UIEvent("change", { view: window, bubbles: true }));
};
imagePreview.addEventListener('load', function () {
root.dispatchEvent(new CustomEvent("imageSelected", {
Expand All @@ -168,8 +168,8 @@ function ImageSelector() {
imageInput.value = '';
this.src = 'img/error.png';
});
imageInput.onchange = function () {
imagePreview.src = URL.createObjectURL(this.files[0]);
imageInput.onchange = function (e) {
imagePreview.src = URL.createObjectURL(e.currentTarget.files[0]);
};
return root;
}
Expand All @@ -196,15 +196,15 @@ function FilterList() {
if (e.deltaY > 0) {
root.selectedIndex = Math.min(root.selectedIndex + 1, root.length - 1);
}
root.onchange();
root.dispatchEvent(new UIEvent("change", { view: window, bubbles: true }));
});
return root;
}
function FilterSelector() {
var filterList_ = FilterList();
var filterPreview = canvas()
.att$("width", CANVAS_WIDTH)
.att$("height", CANVAS_HEIGHT);
.att$("width", String(CANVAS_WIDTH))
.att$("height", String(CANVAS_HEIGHT));
var root = div(div("Filter: ", filterList_)
.att$("class", "widget-element"), filterPreview.att$("class", "widget-element")).att$("class", "widget");
var gl = filterPreview.getContext("webgl", { antialias: false, alpha: false });
Expand Down Expand Up @@ -348,7 +348,7 @@ function FilterSelector() {
delay: dt * 1000,
dispose: 2,
});
renderProgress.style.width = (t / duration) * 50 + "%";
renderProgress.style.width = "".concat((t / duration) * 50, "%");
t += dt;
}
gif.on('finished', function (blob) {
Expand All @@ -360,7 +360,7 @@ function FilterSelector() {
renderSpinner.style.display = "none";
});
gif.on('progress', function (p) {
renderProgress.style.width = 50 + p * 50 + "%";
renderProgress.style.width = "".concat(50 + p * 50, "%");
});
gif.render();
return gif;
Expand Down Expand Up @@ -416,6 +416,6 @@ window.onload = function () {
gif.abort();
}
var fileName = imageSelector.selectedFileName$();
gif = filterSelector.render$(fileName + ".gif");
gif = filterSelector.render$("".concat(fileName, ".gif"));
};
};
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"homepage": "https://github.com/tsoding/emoteJAM#readme",
"devDependencies": {
"@types/gif.js": "^0.2.1",
"typescript": "^4.3.2"
"typescript": "^4.5.4"
}
}
2 changes: 1 addition & 1 deletion ts/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,4 @@ function run_expr(expr: Expr, user_context: UserContext = {}): number {
throw new Error(`Unexpected AST node '${expr.kind}'`);
}
}
}
}
Loading