Skip to content

Commit 307ebb3

Browse files
committed
Prepare for better record tags in printing
1 parent 76832f3 commit 307ebb3

File tree

12 files changed

+149
-3
lines changed

12 files changed

+149
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

11+
- Better CNS functionality
12+
- Support for tagged forms in Convex Reader
1113
- `dissoc-in`, `update` and `update-in` core functions
1214
- `switch` conditional macro
1315

1416
### Changed
1517

18+
- Class hierarchy refactoring for `convex.core.cpos`
19+
- More GUI updates
1620
- `set!` now allows pending definitions
1721

1822
## [0.7.15] - 2024-09-17

convex-core/src/main/antlr4/convex/core/lang/reader/antlr/Convex.g4

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
grammar Convex;
2+
3+
/* =========================================
4+
* Grammar for Convex Reader
5+
* =========================================
6+
*
7+
* Refers to tokens defined in the lexer at the bottom of this file
8+
*/
29

310
form
411
: quoted
512
| pathSymbol
613
| primary
14+
| taggedForm
715
;
816

917
primary
@@ -12,6 +20,8 @@ primary
1220
| resolve
1321
| atom
1422
;
23+
24+
1525

1626
singleForm: form EOF;
1727

@@ -30,6 +40,10 @@ set : SET_LBR forms RBR;
3040

3141
map : LBR forms RBR;
3242

43+
taggedForm: tag form;
44+
45+
tag: HASH_TAG;
46+
3347
atom
3448
: symbol
3549
| literal

convex-core/src/main/java/convex/core/data/AIndex.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import convex.core.data.type.AType;
88
import convex.core.data.type.Types;
9+
import convex.core.data.util.BlobBuilder;
910

1011
/**
1112
* Abstract base class for Indexes: a sorted radix-tree map of Blobs to Values.
@@ -53,6 +54,12 @@ public Set<Entry<K, V>> entrySet() {
5354
}
5455
return Collections.unmodifiableSet(hs);
5556
}
57+
58+
@Override
59+
public boolean print(BlobBuilder sb, long limit) {
60+
sb.append("#Index ");
61+
return super.print(sb, limit);
62+
}
5663

5764
@Override
5865
public abstract int getRefCount();

convex-core/src/main/java/convex/core/data/ARecord.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import convex.core.cpos.Block;
88
import convex.core.data.type.AType;
99
import convex.core.data.type.Types;
10+
import convex.core.data.util.BlobBuilder;
11+
import convex.core.lang.RT;
1012
import convex.core.lang.RecordFormat;
1113

1214
/**
@@ -198,6 +200,29 @@ public MapEntry<Keyword, ACell> entryAt(long i) {
198200
Keyword k=getFormat().getKeys().get(i);
199201
return getEntry(k);
200202
}
203+
204+
@Override
205+
public boolean print(BlobBuilder sb, long limit) {
206+
AString tag=getType().getTag();
207+
if (tag!=null) {
208+
sb.append(tag);
209+
sb.append(' ');
210+
}
211+
sb.append('{');
212+
long n=count();
213+
RecordFormat format=getFormat();
214+
ACell[] vs=getValuesArray();
215+
for (long i=0; i<n; i++) {
216+
Keyword k=format.getKey(i);
217+
if (!RT.print(sb,k,limit)) return false;
218+
sb.append(' ');
219+
ACell v=vs[(int)i];
220+
if (!RT.print(sb,v,limit)) return false;
221+
if (i<(n-1)) sb.append(',');
222+
}
223+
sb.append('}');
224+
return sb.check(limit);
225+
}
201226

202227
@Override
203228
public MapEntry<Keyword, ACell> getEntry(ACell k) {

convex-core/src/main/java/convex/core/data/Cells.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.lang.reflect.Array;
55
import java.util.function.Consumer;
66

7+
import convex.core.exceptions.ParseException;
78
import convex.core.store.AStore;
89
import convex.core.store.Stores;
910

@@ -260,4 +261,17 @@ public static <T extends ACell> T intern(T value) {
260261
return value;
261262
}
262263

264+
@SuppressWarnings("unchecked")
265+
public static ACell createTagged(Symbol sym, ACell value) throws ParseException {
266+
267+
switch (sym.getName().toString()) {
268+
case "Index":
269+
if (!(value instanceof AHashMap)) throw new ParseException(sym+" tag must be on a map");
270+
Index<ABlobLike<?>,ACell> index= Index.create((AHashMap<ABlobLike<?>,ACell>)value);
271+
if (index==null) throw new ParseException("Invalid Index keys");
272+
return index;
273+
}
274+
return value;
275+
}
276+
263277
}

convex-core/src/main/java/convex/core/data/Index.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,17 @@ public static <R extends AIndex<K, V>, K extends ABlobLike<?>, V extends ACell>
836836
}
837837
return (R) result;
838838
}
839-
840-
839+
840+
@SuppressWarnings("unchecked")
841+
public static <R extends AIndex<K, V>, K extends ABlobLike<?>, V extends ACell> R create(AHashMap<K, V> map) {
842+
Index<K,V> result=(Index<K, V>) EMPTY;
843+
long n=map.count();
844+
for (long i=0; i<n; i++) {
845+
MapEntry<K,V> me=map.entryAt(i);
846+
result=result.assoc(me.getKey(), me.getValue());
847+
if (result==null) return null;
848+
}
849+
return (R) result;
850+
}
841851

842852
}

convex-core/src/main/java/convex/core/data/type/AType.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convex.core.data.type;
22

33
import convex.core.data.ACell;
4+
import convex.core.data.AString;
45

56
/**
67
* Abstract base class for CVM value types
@@ -43,4 +44,12 @@ public abstract class AType {
4344
* @return Java Class representing this Type
4445
*/
4546
public abstract Class<? extends ACell> getJavaClass();
47+
48+
/**
49+
* Gets the tag to be used for printing
50+
* @return Tag string, or null if no tag required
51+
*/
52+
public AString getTag() {
53+
return null;
54+
}
4655
}

convex-core/src/main/java/convex/core/data/type/IndexType.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import convex.core.data.ACell;
44
import convex.core.data.AIndex;
5+
import convex.core.data.AString;
56
import convex.core.data.Index;
7+
import convex.core.data.Strings;
68

79
/**
810
* Type that represents any CVM map
@@ -12,6 +14,8 @@ public class IndexType extends AStandardType<AIndex> {
1214

1315
public static final IndexType INSTANCE = new IndexType();
1416

17+
private static final AString TAG=Strings.create("#Index");
18+
1519
private IndexType() {
1620
super(AIndex.class);
1721
}
@@ -36,5 +40,10 @@ public AIndex implicitCast(ACell a) {
3640
if (a instanceof Index) return (Index)a;
3741
return null;
3842
}
43+
44+
@Override
45+
public AString getTag() {
46+
return TAG;
47+
}
3948

4049
}

convex-core/src/main/java/convex/core/lang/RecordFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Long indexFor(Keyword key) {
5353
* @param i Index of record key
5454
* @return Keyword at the specified index
5555
*/
56-
public Keyword getKey(int i) {
56+
public Keyword getKey(long i) {
5757
return keys.get(i);
5858
}
5959
}

convex-core/src/main/java/convex/core/lang/reader/AntlrReader.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import convex.core.data.AList;
1919
import convex.core.data.Address;
2020
import convex.core.data.Blob;
21+
import convex.core.data.Cells;
2122
import convex.core.data.Keyword;
2223
import convex.core.data.List;
2324
import convex.core.data.Lists;
@@ -286,6 +287,36 @@ public void exitImplicitSymbol(ImplicitSymbolContext ctx) {
286287
if (sym==null) throw new ParseException("Bad implicit symbol: "+s);
287288
push( sym);
288289
}
290+
291+
@Override
292+
public void enterTaggedForm(TaggedFormContext ctx) {
293+
pushList();
294+
}
295+
296+
@Override
297+
public void exitTaggedForm(TaggedFormContext ctx) {
298+
ArrayList<ACell> elements=popList();
299+
if (elements.size()!=2) throw new ParseException("Tagged form tag and form but got:"+ elements);
300+
Symbol sym=(Symbol) elements.get(0);
301+
ACell value=elements.get(1);
302+
303+
ACell result=Cells.createTagged(sym,value);
304+
push(result);
305+
}
306+
307+
@Override
308+
public void enterTag(TagContext ctx) {
309+
// Nothing to do
310+
}
311+
312+
@Override
313+
public void exitTag(TagContext ctx) {
314+
String s=ctx.getText();
315+
s=s.substring(1); // skip leading #
316+
Symbol sym=Symbol.create(s);
317+
if (sym==null) throw new ParseException("Bad tag: #"+s);
318+
push( sym);
319+
}
289320

290321
@Override
291322
public void enterAddress(AddressContext ctx) {
@@ -478,6 +509,8 @@ public void exitPrimary(PrimaryContext ctx) {
478509
// Nothing
479510
}
480511

512+
513+
481514
}
482515

483516
public static ACell read(String s) {

0 commit comments

Comments
 (0)