Skip to content

Commit

Permalink
feat(pointfree-lang): overhaul visitor quote/array & map handling, gr…
Browse files Browse the repository at this point in the history
…ammar

- revert / remove NodeType.VAR_DEREF_IMM
- add resolveNode, resolveArray, resolveMap
- update resolveVar to use hasOwnProperty() check
- simplify VisitorState and handling
- add source location handling (for improved error msg)
- update aliases
  • Loading branch information
postspectacular committed Apr 3, 2018
1 parent 2101e92 commit 769e84d
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 162 deletions.
9 changes: 3 additions & 6 deletions packages/pointfree-lang/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ export interface ASTNode {
}

export interface VisitorState {
quote: boolean;
word: boolean;
}

export enum NodeType {
SYM = 1,
WORD,
QUOT,

VAR_DEREF,
VAR_DEREF_IMM,
VAR_STORE,

NIL,
NUMBER,
BOOLEAN,
STRING,
ARRAY,
MAP,
SET,

COMMENT,
STACK_COMMENT,
Expand All @@ -48,8 +45,6 @@ export const ALIASES: IObjectOf<pf.StackFn> = {
"-": pf.sub,
"*": pf.mul,
"/": pf.div,
"1+": pf.inc,
"1-": pf.dec,
"v+": pf.vadd,
"v-": pf.vsub,
"v*": pf.vmul,
Expand All @@ -64,6 +59,8 @@ export const ALIASES: IObjectOf<pf.StackFn> = {
"neg?": pf.isneg,
"nil?": pf.isnull,
"zero?": pf.iszero,
"pi": pf.push(Math.PI),
"tau": pf.push(2 * Math.PI),
".": pf.print,
".s": pf.printds,
".r": pf.printrs,
Expand Down
86 changes: 36 additions & 50 deletions packages/pointfree-lang/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
// const NodeType = {};
// NodeType[NodeType["SYM"] = 1] = "SYM";
// NodeType[NodeType["WORD"] = 2] = "WORD";
// NodeType[NodeType["QUOT"] = 3] = "QUOT";
// NodeType[NodeType["VAR_DEREF"] = 4] = "VAR_DEREF";
// NodeType[NodeType["VAR_DEREF_IMM"] = 5] = "VAR_DEREF_IMM";
// NodeType[NodeType["VAR_STORE"] = 6] = "VAR_STORE";
// NodeType[NodeType["NIL"] = 7] = "NIL";
// NodeType[NodeType["NUMBER"] = 8] = "NUMBER";
// NodeType[NodeType["BOOLEAN"] = 9] = "BOOLEAN";
// NodeType[NodeType["STRING"] = 10] = "STRING";
// NodeType[NodeType["MAP"] = 11] = "MAP";
// NodeType[NodeType["SET"] = 12] = "SET";
// NodeType[NodeType["COMMENT"] = 13] = "COMMENT";
// NodeType[NodeType["STACK_COMMENT"] = 14] = "STACK_COMMENT";
// 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["MAP"] = 10] = "MAP";
// NodeType[NodeType["COMMENT"] = 11] = "COMMENT";
// NodeType[NodeType["STACK_COMMENT"] = 12] = "STACK_COMMENT";

const ast = (node) => {
const loc = location().start;
Expand All @@ -34,28 +32,22 @@ Expr

NonWordExpr
= _ expr:(
Quot
/ LitQuote
LitQuote
/ Var
/ Comment
/ Atom
/ Array
/ Map
// / Set
/ Atom
) _ { return ast(expr); }

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

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

Set
= "#{" body:NonWordExpr* "}" {
return { type: NodeType.SET, body };
return { type: NodeType.ARRAY, body };
}

Map
Expand All @@ -69,29 +61,26 @@ MapPair
MapKey
= k:(
String
/ Sym
/ Number
/ VarDerefImmediate
/ VarDeref
) ":" { return k; }
/ Sym
) ":" { return ast(k); }

MapVal
= _ val:(
Atom
/ Quot
/ LitQuote
/ VarDerefImmediate
/ VarDeref
/ Array
/ Map
// / Set
) _ { return val; }
) _ { return ast(val); }

Atom
= String
/ Sym
/ Number
/ Boolean
/ Nil
/ Sym

Nil
= "nil" {
Expand All @@ -104,31 +93,23 @@ Boolean
}

Sym
= id:$(SymInit SymRest*) {
= id:$(SymV1) {
return {type: NodeType.SYM, id};
}

SymInit
= Alpha
/ SymChars
SymV1
= (Alpha / SymChars) (AlphaNum / SymChars / [.])*

SymRest
= AlphaNum
/ SymChars
SymV2
= Digit (AlphaNum / SymChars)+

SymChars
= [*?$%&/\|~<>=._+\-]
= [*?$%&/\|~<>=_+\-]

Var
= VarDerefImmediate
/ VarDeref
= VarDeref
/ VarStore

VarDerefImmediate
= "@@" id:Sym {
return {type: NodeType.VAR_DEREF_IMM, id: id.id}
}

VarDeref
= "@" id:Sym {
return {type: NodeType.VAR_DEREF, id: id.id}
Expand All @@ -141,16 +122,20 @@ VarStore

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

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

String
Expand All @@ -169,6 +154,7 @@ Binary
= "0b" n:$[01]+ {
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)};
Expand Down
Loading

0 comments on commit 769e84d

Please sign in to comment.