Skip to content

Commit 3d49a7a

Browse files
committed
Add back line datastructure
1 parent 4ae6957 commit 3d49a7a

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

README.md

+26-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Syntax tools for the
2020

2121
### Data Formats <a name="formats"/>
2222

23-
This module deals with three formats of data.
23+
This module deals with four formats of data.
2424

2525
1. Codes - Immutable space-delimited lists of tokens.
2626

@@ -38,7 +38,28 @@ This module deals with three formats of data.
3838

3939
"ASSERT EQUAL APP VAR two I I"
4040

41-
2. Terms - Immutable array-representations of abstract syntax trees.
41+
2. Lines - Immutable objects with a `code` field and
42+
a `name` field that is either `null` or a string.
43+
Each line is either an assertion or a definition;
44+
assertions have `name = null` and definitions define `name` by their `code`.
45+
46+
**JSON Serializable:** yes
47+
48+
**Examples:**
49+
50+
// a definition
51+
{
52+
"name": "two",
53+
"code": "LAMBDA VAR f LAMBDA VAR x APP VAR f APP VAR x VAR x"
54+
}
55+
56+
// an assertion
57+
{
58+
"name": null,
59+
"code": "EQUAL APP VAR two I I"
60+
}
61+
62+
3. Terms - Immutable array-representations of abstract syntax trees.
4263

4364
**JSON Serializable:** yes
4465

@@ -62,7 +83,7 @@ This module deals with three formats of data.
6283
]
6384
]
6485

65-
3. Trees - Mutable cross-linked abstract syntax trees for easy traversal.
86+
4. Trees - Mutable cross-linked abstract syntax trees for easy traversal.
6687

6788
**JSON Serializable:** no, because of cycles
6889

@@ -91,6 +112,8 @@ Signature:
91112
compiler.parse : string -> term
92113
compiler.load : code -> term // compatible with pomagma analyst
93114
compiler.dump : term -> code // compatible with pomagma analyst
115+
compiler.loadLine : line -> term
116+
compiler.dumpLine : term -> line
94117
compiler.enumerateFresh : int -> string (a variable name)
95118
compiler.substitute : name * term * term -> nil
96119
compiler.parenthesize : term -> term

lib/compiler.js

+35
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ var test = require('./test').suite('compiler');
1111
var parse = serialize.parser();
1212
var print = serialize.printer();
1313

14+
var parseLine = function (line) {
15+
var name = line.name;
16+
var body = parse(line.code);
17+
if (name !== null) {
18+
return DEFINE(VAR(name), body);
19+
} else {
20+
return ASSERT(body);
21+
}
22+
};
23+
24+
var printLine = function (term) {
25+
var token = term[0];
26+
if (token === 'ASSERT') {
27+
return {
28+
token: null,
29+
code: print(term[1])
30+
};
31+
} else if (token === 'DEFINE') {
32+
return {
33+
name: term[1][1],
34+
code: print(term[2])
35+
};
36+
}
37+
};
38+
1439
//----------------------------------------------------------------------------
1540
// Symbols
1641

@@ -1409,6 +1434,16 @@ module.exports = {
14091434
var curryTerm = compile(churchTerm);
14101435
return print(curryTerm);
14111436
},
1437+
loadLine: function (line) {
1438+
var curryTerm = parseLine(line);
1439+
var churchTerm = decompile(curryTerm);
1440+
return churchTerm;
1441+
},
1442+
dumpLine: function (churchTerm) {
1443+
var curryTerm = compile(churchTerm);
1444+
var line = printLine(curryTerm);
1445+
return line;
1446+
},
14121447
enumerateFresh: fresh.enumerate,
14131448
substitute: substitute,
14141449
parenthesize: parenthesize,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "puddle-syntax",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"description": "Syntax tools for the Puddle coding environment",
55
"main": "index.js",
66
"dependencies": {

test/lib/datasets.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ var codes = stats.sampleUnique(grammar.sampleCode, SAMPLE_COUNT);
1212
var curryTerms = _.map(codes, compiler.parse);
1313
var churchTerms = _.map(codes, compiler.load);
1414
var nodes = _.map(churchTerms, tree.load);
15+
var lines = _.map(churchTerms, compiler.dumpLine);
1516

1617
module.exports = {
1718
codes: codes,
1819
curryTerms: curryTerms,
1920
churchTerms: churchTerms,
2021
nodes: nodes,
22+
lines: lines,
2123
};

0 commit comments

Comments
 (0)