Skip to content

Commit

Permalink
lint some
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Apr 12, 2023
1 parent 1e01856 commit d6ddafd
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 33 deletions.
1 change: 0 additions & 1 deletion lib/compress/reduce-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ import {
AST_Sequence,
AST_SimpleStatement,
AST_Symbol,
AST_SymbolBlockDeclaration,
AST_SymbolCatch,
AST_SymbolConst,
AST_SymbolDefun,
Expand Down
2 changes: 1 addition & 1 deletion lib/flow/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function conditional(root_node, world = new World(), condition, body, alt
const [body_exit, body_type] = catch_exit(body, then_world);
const [alt_exit, alt_type] = alternative
? catch_exit(alternative, else_world)
: [null, UnknownType];
: [null, new LiteralType(undefined)];

// Both exit
if (body_exit && alt_exit) throw body_exit.OR(alt_exit);
Expand Down
10 changes: 5 additions & 5 deletions lib/flow/dce.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { TreeTransformer, AST_If, AST_Definitions, AST_VarDef, AST_Number, AST_Conditional, AST_EmptyStatement, AST_SymbolRef } from "../ast.js";
import { TreeTransformer, AST_If, AST_Definitions, AST_Number, AST_Conditional } from "../ast.js";
import { MAP, make_node } from "../utils/index.js";
import { KNOWN_ELSE, KNOWN_THEN } from "./conditionals.js";
import { World } from "./tools.js";

// TODO move to utils?
import { make_sequence, make_statements, to_statement } from "../compress/common.js";
import { make_sequence, make_statements } from "../compress/common.js";

export function flow_drop_dead_code(world = new World(), toplevel) {
let tt = new TreeTransformer((node, descend, in_list) => {
if (node instanceof AST_Conditional) {
if (KNOWN_ELSE.has(node)) {
return node.alternative.transform(tt);
return keep_expression(tt, node, node.alternative);
}
if (KNOWN_THEN.has(node)) {
return node.consequent.transform(tt);
return keep_expression(tt, node, node.consequent);
}
return;
}
Expand Down Expand Up @@ -62,7 +62,7 @@ function keep_expression(tt, orig, node) {
if (!node) node = [];
if (!Array.isArray(node)) node = [node];
node = node.reduce((accum, node) => {
node = node && node.drop_side_effect_free(tt);
node = node && node.transform(tt);
if (node) accum.push(node);
return accum;
}, []);
Expand Down
2 changes: 1 addition & 1 deletion lib/flow/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { LiteralType } from "./types.js";
*/

// CONSTANTS
AST_Number.prototype._flow = function (world = new World()) {
AST_Number.prototype._flow = function (_world = new World()) {
return new LiteralType(this.value);
};

Expand Down
6 changes: 3 additions & 3 deletions lib/flow/flow.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// import { }

import { AST_Defun, AST_Lambda, AST_Node, AST_Toplevel, AST_Var, walk, walk_parent } from "../ast.js";
import { AST_Node, AST_Toplevel } from "../ast.js";

import "./expressions.js";
import "./lambda.js";
import "./statements.js";

import { Exit, analyze_func_or_toplevel_body, NOPE, sequential, World } from "./tools.js";
import { Exit, analyze_func_or_toplevel_body, NOPE, World } from "./tools.js";

/**
* @module lib/flow/flow.js
Expand Down Expand Up @@ -39,7 +39,7 @@ AST_Toplevel.prototype.flow_analysis = function (world = new World()) {
}
};

AST_Node.prototype._flow = function (world = new World()) {
AST_Node.prototype._flow = function (_world = new World()) {
throw NOPE;
};

Expand Down
4 changes: 2 additions & 2 deletions lib/flow/lambda.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {AST_Call, AST_Defun, AST_Return} from "../ast.js";
import {NOPE, World, sequential, Return, analyze_func_or_toplevel_body} from "./tools.js";
import {NOPE, World, Return, analyze_func_or_toplevel_body} from "./tools.js";
import {LiteralType, FunctionType } from "./types.js";

// Defining the defun is done during hoisting (`hoist_defun_decls`)
AST_Defun.prototype._flow = function (world = new World()) {
AST_Defun.prototype._flow = function (_world = new World()) {
return new LiteralType(undefined);
};

Expand Down
4 changes: 2 additions & 2 deletions lib/flow/statements.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AST_BlockStatement, AST_Definitions, AST_EmptyStatement, AST_If, AST_SimpleStatement, AST_Var } from "../ast.js";
import { AST_BlockStatement, AST_Definitions, AST_EmptyStatement, AST_If, AST_SimpleStatement } from "../ast.js";
import { World, sequential } from "./tools.js";
import { conditional } from "./conditionals.js";
import { LiteralType } from "./types.js";
Expand All @@ -13,7 +13,7 @@ AST_BlockStatement.prototype._flow = function (world = new World()) {
return sequential(this.body, world.block_scope_world(this));
};

AST_EmptyStatement.prototype._flow = function (world = new World()) {
AST_EmptyStatement.prototype._flow = function (_world = new World()) {
return new LiteralType(undefined);
};

Expand Down
7 changes: 4 additions & 3 deletions lib/flow/tools.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AST_Const, AST_Definitions, AST_Defun, AST_Lambda, AST_Let, AST_Symbol, AST_Scope, AST_Var, AST_VarDef, walk_parent } from "../ast.js";
import { AST_Const, AST_Definitions, AST_Defun, AST_Lambda, AST_Let, AST_Symbol, AST_Var, walk_parent } from "../ast.js";
import { FunctionType, LiteralType } from "./types.js";
import { map_map_set } from "../utils/index.js";

Expand All @@ -12,7 +12,7 @@ export function sequential(nodes, world) {
}

/** pre-walk to hoist declarations such as var and defun */
export function analyze_func_or_toplevel_body(node_with_body, world = new World(), { allow_return } = {}) {
export function analyze_func_or_toplevel_body(node_with_body, world = new World()) {
const is_function = node_with_body instanceof AST_Lambda;

const block_scopes = new Map();
Expand Down Expand Up @@ -111,7 +111,7 @@ export function catch_exit(node, world) {
}
}

const binding_func_mask = 0b10000000;
const _binding_func_mask = 0b10000000;
const binding_block_mask = 0b01000000;
const binding_const_mask = 0b00100000;

Expand Down Expand Up @@ -392,3 +392,4 @@ export class Return extends Exit {
}

export const NOPE = Symbol("NOPE (flow analysis impossible)");

34 changes: 20 additions & 14 deletions lib/flow/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export class Type {
}

OR(other) {
if (other === UnknownType) return other;
if (other === UnknownType || this.equals(other)) return other;

const all_types = new Set([...this.types, ...other.types]);
return new GenericType([...all_types]);
}

equals(other) {
equals(_other) {
throw new Error("not implemented (Type.equals)");
}

Expand All @@ -34,12 +34,6 @@ export class GenericType extends Type {
super(types);
}

OR(other) {
if (other === UnknownType || this.equals(other)) return other;

return super.OR(other);
}

equals(other) {
return other === this
|| other instanceof GenericType
Expand All @@ -58,12 +52,6 @@ export class LiteralType extends Type {
this.value = value;
}

OR(other) {
if (other === UnknownType || this.equals(other)) return other;

return super.OR(other);
}

equals(other) {
return other === this
|| other instanceof LiteralType
Expand All @@ -83,6 +71,23 @@ export class FunctionType extends Type {
this.parent_world = parent_world;
}

equals(other) {
return other === this
|| other instanceof FunctionType
&& other.node === this.node
&& other.parent_world === this.parent_world;
}

is_truthy() {
return true;
}
}

export class TrickyFunctionType extends Type {
constructor() {
super(["function"]);
}

OR(other) {
if (other === UnknownType || this.equals(other)) return other;

Expand All @@ -105,6 +110,7 @@ export const UnknownType = new (class extends Type {
constructor() { super([]); }

OR(_other) { return this; }
equals(other) { return this === other; }
is_truthy() { return undefined; }
})();

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@
"no-unused-vars": [
"error",
{
"varsIgnorePattern": "^_"
"varsIgnorePattern": "^_",
"argsIgnorePattern": "^_"
}
],
"no-tabs": "error",
Expand Down

0 comments on commit d6ddafd

Please sign in to comment.