Skip to content

Commit

Permalink
fix(pointfree-lang): update NodeType handling
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 8, 2019
1 parent b198c19 commit 227be4b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions packages/pointfree-lang/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IObjectOf } from "@thi.ng/api/api";
import { IObjectOf } from "@thi.ng/api";
import * as pf from "@thi.ng/pointfree";

export interface ASTNode {
Expand Down Expand Up @@ -34,7 +34,7 @@ export const enum NodeType {
/**
* Reverse lookup for `NodeType` enums
*/
export const __NodeType = (<any>exports).NodeType;
// export const __NodeType = (<any>exports).NodeType;

export const ALIASES: IObjectOf<pf.StackFn> = {
"?drop": pf.dropif,
Expand Down
60 changes: 30 additions & 30 deletions packages/pointfree-lang/src/grammar.pegjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
const __NodeType = require("./api").__NodeType;

// const __NodeType = {};
// __NodeType[__NodeType["SYM"] = 1] = "SYM";
// __NodeType[__NodeType["WORD"] = 2] = "WORD";
// __NodeType[__NodeType["VAR_DEREF"] = 3] = "VAR_DEREF";
// __NodeType[__NodeType["VAR_STORE"] = 4] = "VAR_STORE";
// __NodeType[__NodeType["NIL"] = 5] = "NIL";
// __NodeType[__NodeType["NUMBER"] = 6] = "NUMBER";
// __NodeType[__NodeType["BOOLEAN"] = 7] = "BOOLEAN";
// __NodeType[__NodeType["STRING"] = 8] = "STRING";
// __NodeType[__NodeType["ARRAY"] = 9] = "ARRAY";
// __NodeType[__NodeType["OBJ"] = 10] = "OBJ";
// __NodeType[__NodeType["COMMENT"] = 11] = "COMMENT";
// __NodeType[__NodeType["STACK_COMMENT"] = 12] = "STACK_COMMENT";
// const __NodeType = require("./api").__NodeType;

const NodeType = {};
NodeType[NodeType["SYM"] = 1] = "SYM";
NodeType[NodeType["WORD"] = 2] = "WORD";
NodeType[NodeType["VAR_DEREF"] = 3] = "VAR_DEREF";
NodeType[NodeType["VAR_STORE"] = 4] = "VAR_STORE";
NodeType[NodeType["NIL"] = 5] = "NIL";
NodeType[NodeType["NUMBER"] = 6] = "NUMBER";
NodeType[NodeType["BOOLEAN"] = 7] = "BOOLEAN";
NodeType[NodeType["STRING"] = 8] = "STRING";
NodeType[NodeType["ARRAY"] = 9] = "ARRAY";
NodeType[NodeType["OBJ"] = 10] = "OBJ";
NodeType[NodeType["COMMENT"] = 11] = "COMMENT";
NodeType[NodeType["STACK_COMMENT"] = 12] = "STACK_COMMENT";

const ast = (node) => {
const loc = location().start;
Expand Down Expand Up @@ -42,7 +42,7 @@ NonWordExpr

Word
= ":" __ id:Sym locals:LocalVars? body:NonWordExpr+ ";" {
return { type: __NodeType.WORD, id: id.id, locals, body};
return { type: NodeType.WORD, id: id.id, locals, body};
}

LocalVars
Expand All @@ -55,12 +55,12 @@ SymList

Array
= "[" body:NonWordExpr* "]" {
return { type: __NodeType.ARRAY, body };
return { type: NodeType.ARRAY, body };
}

Obj
= "{" _ body:ObjPair* "}" {
return { type: __NodeType.OBJ, body };
return { type: NodeType.OBJ, body };
}

ObjPair
Expand Down Expand Up @@ -92,17 +92,17 @@ Atom

Nil
= "nil" {
return {type: __NodeType.NIL, body: null};
return {type: NodeType.NIL, body: null};
}

Boolean
= $("T" / "F") {
return {type: __NodeType.BOOLEAN, body: text() == "T"};
return {type: NodeType.BOOLEAN, body: text() == "T"};
}

Sym
= id:$((Alpha / SymChars) (AlphaNum / SymChars)*) {
return {type: __NodeType.SYM, id};
return {type: NodeType.SYM, id};
}

SymChars
Expand All @@ -114,35 +114,35 @@ Var

VarDeref
= "@" id:Sym {
return {type: __NodeType.VAR_DEREF, id: id.id}
return {type: NodeType.VAR_DEREF, id: id.id}
}

VarStore
= id:Sym "!" {
return {type: __NodeType.VAR_STORE, id: id.id}
return {type: NodeType.VAR_STORE, id: id.id}
}

LitQuote
= "'" body:NonWordExpr {
return {type: __NodeType.ARRAY, body: [body]};
return {type: NodeType.ARRAY, body: [body]};
}

Comment
= "("+ body:$(!")" .)* ")" {
return body.indexOf("--") > 0 ?
{
type: __NodeType.STACK_COMMENT,
type: NodeType.STACK_COMMENT,
body: body.split("--").map(x => x.trim())
} :
{
type: __NodeType.COMMENT,
type: NodeType.COMMENT,
body: body.trim()
};
}

String
= "\"" body:$(!"\"" .)* "\"" {
return {type: __NodeType.STRING, body };
return {type: NodeType.STRING, body };
}

Number
Expand All @@ -154,12 +154,12 @@ Sign = [-+]

Binary
= "0b" n:$[01]+ {
return {type: __NodeType.NUMBER, radix: 2, body: parseInt(n, 2)};
return {type: NodeType.NUMBER, radix: 2, body: parseInt(n, 2)};
}

Hex
= "0x" n:$[0-9a-fA-F]+ {
return {type: __NodeType.NUMBER, radix: 16, body: parseInt(n, 16)};
return {type: NodeType.NUMBER, radix: 16, body: parseInt(n, 16)};
}

Int
Expand All @@ -170,7 +170,7 @@ Uint

Decimal
= Int ("." Uint?)? ("e" Int)? {
return {type: __NodeType.NUMBER, body: parseFloat(text())};
return {type: NodeType.NUMBER, body: parseFloat(text())};
}

AlphaNum
Expand Down
3 changes: 1 addition & 2 deletions packages/pointfree-lang/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { illegalArgs, illegalState } from "@thi.ng/errors";
import * as pf from "@thi.ng/pointfree";

import {
__NodeType,
ALIASES,
ASTNode,
NodeType,
Expand Down Expand Up @@ -186,7 +185,7 @@ const endvar = (id: string) => (ctx: pf.StackContext) => {
* @param state
*/
const visit = (node: ASTNode, ctx: pf.StackContext, state: VisitorState) => {
DEBUG && console.log("visit", __NodeType[node.type], node, ctx[0].toString());
DEBUG && console.log("visit", node.type, node, ctx[0].toString());
switch (node.type) {
case NodeType.SYM:
return visitSym(node, ctx, state);
Expand Down
3 changes: 2 additions & 1 deletion packages/pointfree-lang/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"outDir": ".",
"module": "es6",
"target": "es6"
"target": "es6",
"preserveConstEnums": false
},
"include": [
"./src/**/*.ts"
Expand Down

0 comments on commit 227be4b

Please sign in to comment.