optional BraceExp ReaderExpDef

This commit is contained in:
2024-12-23 17:30:35 -06:00
parent 22c8ce4247
commit e36fb991c6
3 changed files with 29 additions and 4 deletions

View File

@@ -666,6 +666,8 @@ class Helpers {
CallExp(recurse(func), evalUnquoteLists(callArgs, innerRunAtCompileTime).map(recurse));
case ListExp(elements):
ListExp(evalUnquoteLists(elements, innerRunAtCompileTime).map(recurse));
case BraceExp(elements):
BraceExp(evalUnquoteLists(elements, innerRunAtCompileTime).map(recurse));
case TypedExp(type, innerExp):
TypedExp(type, recurse(innerExp));
case FieldExp(field, innerExp, safe):
@@ -764,6 +766,8 @@ class Helpers {
CallExp(func(f), Lambda.map(args, func));
case ListExp(args):
ListExp(Lambda.map(args, func));
case BraceExp(args):
BraceExp(Lambda.map(args, func));
case TypedExp(type, exp):
TypedExp(type, func(exp));
case MetaExp(meta, exp):

View File

@@ -31,10 +31,16 @@ typedef HasReadTables = {
identAliases:Map<String,ReaderExpDef>
};
typedef ReadTableOptions = {
// When this is false (the default), {<...>} always becomes (begin <...>) instead of BraceExp(<...>)
?keepBraceExps:Bool
};
@:allow(kiss.Helpers)
class Reader {
// The built-in readtable
public static function builtins() {
public static function builtins(?options:ReadTableOptions) {
if (options == null) options = {};
var readTable:ReadTable = [];
readTable["("] = (stream, k) -> {
@@ -49,9 +55,13 @@ class Reader {
readTable["[::"] = (stream, k) -> ListEatingExp(_readExpArray(stream, "]", k));
readTable["..."] = (stream, k) -> ListRestExp(nextToken(stream, "name for list-eating rest exp", true));
// Provides a nice syntactic sugar for (if... {<then block...>} {<else block...>}),
// and also handles string interpolation cases like "${exp}moreString":
readTable["{"] = (stream:Stream, k) -> CallExp(Symbol("begin").withPos(stream.position()), _readExpArray(stream, "}", k));
if (options.keepBraceExps) {
readTable["{"] = (stream, k) -> BraceExp(_readExpArray(stream, "}", k));
} else {
// Provides a nice syntactic sugar for (if... {<then block...>} {<else block...>}),
// and also handles string interpolation cases like "${exp}moreString":
readTable["{"] = (stream:Stream, k) -> CallExp(Symbol("begin").withPos(stream.position()), _readExpArray(stream, "}", k));
}
readTable["<>["] = (stream, k) -> TypeParams(_readExpArray(stream, "]", k));
@@ -671,6 +681,16 @@ class Reader {
].join(" ");
str += ']';
str;
case BraceExp(exps):
// {e1 e2 e3}
var str = '{';
str += [
for (exp in exps) {
exp.def.toString();
}
].join(" ");
str += '}';
str;
case TypeParams(exps):
// <>[T1 T2 T3]
var str = '<>[';

View File

@@ -10,6 +10,7 @@ typedef ReaderExp = {
enum ReaderExpDef {
CallExp(func:ReaderExp, args:Array<ReaderExp>); // (f a1 a2...)
ListExp(exps:Array<ReaderExp>); // [v1 v2 v3]
BraceExp(exps:Array<ReaderExp>); // {e1 e2 e3}
StrExp(s:String); // "literal"
Symbol(name:String); // s
RawHaxe(code:String); // #| haxeCode() |# // deprecated!