Skip to content

Commit

Permalink
refactor(pointfree-lang): fix #256 replace enum w/ type alias
Browse files Browse the repository at this point in the history
- replace NodeType enum w/ type alias
- update grammar & compiler
  • Loading branch information
postspectacular committed Dec 22, 2020
1 parent 8f00375 commit 7ae9e27
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 66 deletions.
30 changes: 13 additions & 17 deletions packages/pointfree-lang/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ export interface WordMeta {
arities?: number;
}

export enum NodeType {
SYM = 1,
WORD,

VAR_DEREF,
VAR_STORE,

NIL,
NUMBER,
BOOLEAN,
STRING,
ARRAY,
OBJ,

COMMENT,
STACK_COMMENT,
}
export type NodeType =
| "sym"
| "word"
| "var_deref"
| "var_store"
| "nil"
| "number"
| "boolean"
| "string"
| "array"
| "obj"
| "comment"
| "stack_comment";

export const ALIASES: IObjectOf<pf.StackFn> = {
"?drop": pf.dropif,
Expand Down
46 changes: 16 additions & 30 deletions packages/pointfree-lang/src/grammar.pegjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
{
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;
node.loc = [loc.line, loc.column];
Expand Down Expand Up @@ -41,7 +27,7 @@ NonWordExpr

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

LocalVars
Expand All @@ -54,12 +40,12 @@ SymList

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

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

ObjPair
Expand Down Expand Up @@ -91,17 +77,17 @@ Atom

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

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

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

SymChars
Expand All @@ -113,43 +99,43 @@ Var

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

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

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

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

LineComment
= "//" body:$(!"\n" .)* "\n" {
return {
type: NodeType.COMMENT,
type: "comment",
body: body.trim()
};
}

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

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

Binary
= "0b" n:$[01]+ {
return {type: NodeType.NUMBER, radix: 2, body: parseInt(n, 2)};
return {type: "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: "number", radix: 16, body: parseInt(n, 16)};
}

Int
Expand All @@ -177,7 +163,7 @@ Uint

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

AlphaNum
Expand Down
38 changes: 19 additions & 19 deletions packages/pointfree-lang/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fn, Fn2, FnU, ILogger, IObjectOf, NULL_LOGGER } from "@thi.ng/api";
import { illegalArgs, illegalState } from "@thi.ng/errors";
import * as pf from "@thi.ng/pointfree";
import { ALIASES, ASTNode, NodeType, VisitorState } from "./api";
import { ALIASES, ASTNode, VisitorState } from "./api";
import { parse, SyntaxError } from "./parser";

export let LOGGER = NULL_LOGGER;
Expand Down Expand Up @@ -62,15 +62,15 @@ const resolveVar = (node: ASTNode, ctx: pf.StackContext) => {
*/
const resolveNode = (node: ASTNode, ctx: pf.StackContext): any => {
switch (node.type) {
case NodeType.SYM:
case "sym":
return resolveSym(node, ctx);
case NodeType.VAR_DEREF:
case "var_deref":
return resolveVar(node, ctx);
case NodeType.VAR_STORE:
case "var_store":
return storevar(node.id!);
case NodeType.ARRAY:
case "array":
return resolveArray(node, ctx);
case NodeType.OBJ:
case "obj":
return resolveObject(node, ctx);
default:
return node.body;
Expand Down Expand Up @@ -100,7 +100,7 @@ const resolveArray = (node: ASTNode, ctx: pf.StackContext) => {
const resolveObject = (node: ASTNode, ctx: pf.StackContext) => {
const res: any = {};
for (let [k, v] of node.body) {
res[k.type === NodeType.SYM ? k.id : resolveNode(k, ctx)] = resolveNode(
res[k.type === "sym" ? k.id : resolveNode(k, ctx)] = resolveNode(
v,
ctx
);
Expand Down Expand Up @@ -181,27 +181,27 @@ const endvar = (id: string): FnU<pf.StackContext> => (ctx) => {
* @param state -
*/
const visit = (node: ASTNode, ctx: pf.StackContext, state: VisitorState) => {
LOGGER.fine("visit", NodeType[node.type], node, ctx[0].toString());
LOGGER.fine("visit", node.type, node, ctx[0].toString());
switch (node.type) {
case NodeType.SYM:
case "sym":
return visitSym(node, ctx, state);
case NodeType.NUMBER:
case NodeType.BOOLEAN:
case NodeType.STRING:
case NodeType.NIL:
case "number":
case "boolean":
case "string":
case "nil":
ctx[0].push(node.body);
return ctx;
case NodeType.ARRAY:
case "array":
return visitArray(node, ctx, state);
case NodeType.OBJ:
case "obj":
return visitObject(node, ctx, state);
case NodeType.VAR_DEREF:
case "var_deref":
return visitDeref(node, ctx, state);
case NodeType.VAR_STORE:
case "var_store":
return visitStore(node, ctx, state);
case NodeType.WORD:
case "word":
return visitWord(node, ctx, state);
case NodeType.STACK_COMMENT:
case "stack_comment":
visitStackComment(node, state);
default:
LOGGER.fine("skipping node...");
Expand Down

0 comments on commit 7ae9e27

Please sign in to comment.