Skip to content

Commit

Permalink
feat(pointfree-lang): update grammar, aliases, ASTNode, NodeType
Browse files Browse the repository at this point in the history
- add VAR_DEREF_IMM node type (immediate/non-defered var deref)
- add node source location info
- add VarDerefImmediate and NonWordExpr grammar rules
- add more aliases for built-ins
  • Loading branch information
postspectacular committed Apr 1, 2018
1 parent 92d2d68 commit ee684c7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
17 changes: 17 additions & 0 deletions packages/pointfree-lang/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import * as pf from "@thi.ng/pointfree";
export interface ASTNode {
type: NodeType;
body: any;
loc: [number, number];
id?: string;
}

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

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

VAR_DEREF,
VAR_DEREF_IMM,
VAR_STORE,

NIL,
Expand Down Expand Up @@ -47,6 +54,16 @@ export const ALIASES: IObjectOf<pf.StackFn> = {
"v-": pf.vsub,
"v*": pf.vmul,
"v/": pf.vdiv,
"=": pf.eq,
"!=": pf.gteq,
"<=": pf.lteq,
">=": pf.gteq,
"<": pf.lteq,
">": pf.gteq,
"pos?": pf.ispos,
"neg?": pf.isneg,
"nil?": pf.isnull,
"zero?": pf.iszero,
".": pf.print,
".s": pf.printds,
".r": pf.printrs,
Expand Down
58 changes: 38 additions & 20 deletions packages/pointfree-lang/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,35 @@
// NodeType[NodeType["WORD"] = 2] = "WORD";
// NodeType[NodeType["QUOT"] = 3] = "QUOT";
// NodeType[NodeType["VAR_DEREF"] = 4] = "VAR_DEREF";
// NodeType[NodeType["VAR_STORE"] = 5] = "VAR_STORE";
// NodeType[NodeType["NIL"] = 6] = "NIL";
// NodeType[NodeType["NUMBER"] = 7] = "NUMBER";
// NodeType[NodeType["BOOLEAN"] = 8] = "BOOLEAN";
// NodeType[NodeType["STRING"] = 9] = "STRING";
// NodeType[NodeType["MAP"] = 10] = "MAP";
// NodeType[NodeType["SET"] = 11] = "SET";
// NodeType[NodeType["COMMENT"] = 12] = "COMMENT";
// NodeType[NodeType["STACK_COMMENT"] = 13] = "STACK_COMMENT";
// 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";

const ast = (node) => {
// const loc = location().start;
// node.loc = [loc.offset, loc.line, loc.column];
const loc = location().start;
node.loc = [loc.line, loc.column];
return node;
};
}

Root
= exrp:Expr*
= expr:Expr*

Expr
= _ expr:( Word / NonWordExpr ) _ {
return ast(expr);
}

NonWordExpr
= _ expr:(
Word
/ Quot
Quot
/ LitQuote
/ Var
/ Comment
Expand All @@ -39,17 +44,17 @@ Expr
) _ { return ast(expr); }

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

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

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

Expand All @@ -62,13 +67,20 @@ MapPair
= k:MapKey v:MapVal { return [ k, v ]; }

MapKey
= k:(String / Sym / Number / VarDeref) ":" { return k; }
= k:(
String
/ Sym
/ Number
/ VarDerefImmediate
/ VarDeref
) ":" { return k; }

MapVal
= _ val:(
Atom
/ Quot
/ LitQuote
/ VarDerefImmediate
/ VarDeref
/ Map
// / Set
Expand Down Expand Up @@ -108,9 +120,15 @@ SymChars
= [*?$%&/\|~<>=._+\-]

Var
= VarDeref
= VarDerefImmediate
/ 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 @@ -122,7 +140,7 @@ VarStore
}

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

Expand Down

0 comments on commit ee684c7

Please sign in to comment.