Syntax tools for the Puddle coding environment
var syntax = require('puddle-syntax');
This module deals with four formats of data.
-
Codes - Immutable space-delimited lists of tokens.
JSON Serializable: yes
Examples:
"I" "VAR x" "APP VAR f VAR x" "DEFINE VAR two LAMBDA VAR f LAMBDA VAR x APP VAR f APP VAR f VAR x" "ASSERT EQUAL APP VAR two I I"
-
Lines - Immutable objects with a
code
field and aname
field that is eithernull
or a string. Each line is either an assertion or a definition; assertions havename = null
and definitions definename
by theircode
.JSON Serializable: yes
Examples:
// a definition { "name": "two", "code": "LAMBDA VAR f LAMBDA VAR x APP VAR f APP VAR x VAR x" } // an assertion { "name": null, "code": "EQUAL APP VAR two I I" }
-
Terms - Immutable array-representations of abstract syntax trees.
JSON Serializable: yes
Examples:
[ "DEFINE", ["VAR", "two"], [ "LAMBDA", ["VAR", "f"], [ "LAMBDA", ["VAR", "x"], [ "APP", ["VAR" "f"], [ "APP", ["VAR" "f"], ["VAR" "x"], ] ] ] ] ] [ "ASSERT", [ "EQUAL", ["APP", ["VAR", "two"], "I"], "I" ] ]
-
Trees - Mutable cross-linked abstract syntax trees for easy traversal.
JSON Serializable: no, because of cycles
Examples:
{"name": "I", "above": null, "below": []} {"name": "VAR", "varName": "x", "above": null, "below": []} var fx = { "name": "APP", "above": null, "below": [ {"name": "VAR", "varName": "f", "below": [], "above": fx}, {"name": "VAR", "varName": "x", "below": [], "above": fx} ] };
Signature:
compiler.symbols : object(string | function) (constructors for terms)
compiler.load : code -> term
compiler.dump : term -> code
compiler.loadLine : line -> term
compiler.dumpLine : term -> line
compiler.print : term -> string
compiler.enumerateFresh : int -> string (a variable name)
compiler.substitute : name * term -> term
compiler.parenthesize : term -> term
compiler.fold : /\f: (string * any -> t). term -> t
Examples:
compiler.load("APP VAR f VAR x");
// = ["APP", ["VAR", "f"], ["VAR", "x"]]
compiler.dump(["APP", ["VAR", "f"], ["VAR", "x"]]);
// = "APP VAR f VAR x"
Signature:
pretty : term -> string
Examples:
pretty("LAMBDA VAR x APP VAR f VAR x");
// = "\ x f x"
Signature:
tree.load : term -> node
tree.dump : node -> term
getRoot : node -> node
getLocals : node -> Array of strings (variable names)
getFresh : node -> string (a variable name)
Examples:
tree.load(["VAR", "x"]);
// = {"name": "VAR", "varName": "x", "above": null, "below": []}
tree.dump({"name": "VAR", "varName": "x", "above": null, "below": []});
// = ["VAR", "x"]
Copyright 2013-2014 Fritz Obermeyer.
Puddle is licensed under the MIT license.