@harmoniclabs/pebble 0.1.3 → 0.1.4-dev0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/IR/IRNodes/IRNative/IRNativeTag.d.ts +2 -1
- package/dist/IR/IRNodes/IRNative/IRNativeTag.js +2 -0
- package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.d.ts +1 -0
- package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.js +19 -0
- package/dist/ast/nodes/expr/litteral/LitNamedObjExpr.d.ts +5 -1
- package/dist/ast/nodes/expr/litteral/LitNamedObjExpr.js +5 -1
- package/dist/compiler/AstCompiler/internal/exprs/_compileLitteralExpr.js +22 -2
- package/dist/compiler/AstCompiler/utils/getPropAccessReturnType.js +9 -0
- package/dist/compiler/SourceTypeMap.js +13 -0
- package/dist/compiler/TirCompiler/expressify/expressifyVars.js +52 -5
- package/dist/compiler/tir/expressions/TirNativeFunc.d.ts +1 -0
- package/dist/compiler/tir/expressions/TirNativeFunc.js +8 -0
- package/dist/parser/Parser.js +17 -0
- package/package.json +1 -1
|
@@ -134,6 +134,7 @@ export declare enum IRNativeTag {
|
|
|
134
134
|
_getCredentialsHash = -44,
|
|
135
135
|
_findSopOptional = -45,
|
|
136
136
|
_increment = -46,
|
|
137
|
-
_decrement = -47
|
|
137
|
+
_decrement = -47,
|
|
138
|
+
_lookupLinearMap = -48
|
|
138
139
|
}
|
|
139
140
|
export declare function nativeTagToString(nativeTag: IRNativeTag): string;
|
|
@@ -158,6 +158,7 @@ export var IRNativeTag;
|
|
|
158
158
|
IRNativeTag[IRNativeTag["_findSopOptional"] = -45] = "_findSopOptional";
|
|
159
159
|
IRNativeTag[IRNativeTag["_increment"] = -46] = "_increment";
|
|
160
160
|
IRNativeTag[IRNativeTag["_decrement"] = -47] = "_decrement";
|
|
161
|
+
IRNativeTag[IRNativeTag["_lookupLinearMap"] = -48] = "_lookupLinearMap";
|
|
161
162
|
})(IRNativeTag || (IRNativeTag = {}));
|
|
162
163
|
Object.freeze(IRNativeTag);
|
|
163
164
|
export function nativeTagToString(nativeTag) {
|
|
@@ -203,6 +204,7 @@ export function nativeTagToString(nativeTag) {
|
|
|
203
204
|
case IRNativeTag._getCredentialsHash: return "getCredentialHash";
|
|
204
205
|
case IRNativeTag._increment: return "increment";
|
|
205
206
|
case IRNativeTag._decrement: return "decrement";
|
|
207
|
+
case IRNativeTag._lookupLinearMap: return "lookupLinearMap";
|
|
206
208
|
default: return "";
|
|
207
209
|
}
|
|
208
210
|
}
|
|
@@ -24,6 +24,7 @@ export declare const hoisted_length: IRHoisted;
|
|
|
24
24
|
export declare const hoisted_dropList: IRHoisted;
|
|
25
25
|
export declare const hoisted_sizeofPositiveInt: IRHoisted;
|
|
26
26
|
export declare const hoisted_findSopOptional: IRHoisted;
|
|
27
|
+
export declare const hoisted_lookupLinearMap: IRHoisted;
|
|
27
28
|
export declare const hoisted_mkFindDataOptional: IRHoisted;
|
|
28
29
|
export declare const hoisted_strictAnd: IRHoisted;
|
|
29
30
|
export declare const hoisted_strictOr: IRHoisted;
|
|
@@ -128,6 +128,24 @@ new IRConstr(1, [new IRVar(findSop_head)]), // Some{ head }
|
|
|
128
128
|
// else => self(tail)
|
|
129
129
|
_ir_apps(new IRSelfCall(findSop_self), _ir_apps(IRNative.tailList, new IRVar(findSop_list))))))))));
|
|
130
130
|
hoisted_findSopOptional.hash;
|
|
131
|
+
// hoisted _lookupLinearMap
|
|
132
|
+
// (key: data) -> (map: list<pair<data,data>>) -> SopOptional<data>
|
|
133
|
+
const lookup_key = Symbol("lookup_key");
|
|
134
|
+
const lookup_self = Symbol("lookup_self");
|
|
135
|
+
const lookup_map = Symbol("lookup_map");
|
|
136
|
+
const lookup_head = Symbol("lookup_head");
|
|
137
|
+
export const hoisted_lookupLinearMap = new IRHoisted(new IRFunc([lookup_key], new IRRecursive(lookup_self, new IRFunc([lookup_map], _ir_lazyChooseList(new IRVar(lookup_map),
|
|
138
|
+
// case nil => None
|
|
139
|
+
new IRConstr(1, []),
|
|
140
|
+
// case cons
|
|
141
|
+
new IRFunc([lookup_head], _ir_lazyIfThenElse(_ir_apps(IRNative.equalsData, _ir_apps(IRNative.fstPair, new IRVar(lookup_head)), new IRVar(lookup_key)),
|
|
142
|
+
// then => Some(sndPair(head))
|
|
143
|
+
new IRConstr(1, [
|
|
144
|
+
_ir_apps(IRNative.sndPair, new IRVar(lookup_head))
|
|
145
|
+
]),
|
|
146
|
+
// else => self(tailList(map))
|
|
147
|
+
_ir_apps(new IRSelfCall(lookup_self), _ir_apps(IRNative.tailList, new IRVar(lookup_map))))))))));
|
|
148
|
+
hoisted_lookupLinearMap.hash;
|
|
131
149
|
// hoisted _mkFindDataOptional
|
|
132
150
|
const mkFind_elemToData = Symbol("elemToData");
|
|
133
151
|
const mkFind_pred = Symbol("predicate");
|
|
@@ -265,6 +283,7 @@ export function nativeToIR(native) {
|
|
|
265
283
|
// case IRNativeTag._equalPairData: ;
|
|
266
284
|
case IRNativeTag._mkEqualsList: return hoisted_mkEqualsList.clone();
|
|
267
285
|
case IRNativeTag._negateInt: return hoisted_negateInteger.clone();
|
|
286
|
+
case IRNativeTag._lookupLinearMap: return hoisted_lookupLinearMap.clone();
|
|
268
287
|
// case IRNativeTag._mkEqualsList: return hoisted_mkEqualsList.clone();
|
|
269
288
|
default:
|
|
270
289
|
throw new Error("unknown (negative) native calling 'nativeToIR'; " +
|
|
@@ -8,5 +8,9 @@ export declare class LitNamedObjExpr implements HasSourceRange, ILibObjExpr {
|
|
|
8
8
|
readonly fieldNames: Identifier[];
|
|
9
9
|
readonly values: PebbleExpr[];
|
|
10
10
|
readonly range: SourceRange;
|
|
11
|
-
|
|
11
|
+
/** When using `Type.Constructor{ ... }` syntax, this is the type name */
|
|
12
|
+
readonly typeName: Identifier | undefined;
|
|
13
|
+
constructor(name: Identifier, fieldNames: Identifier[], values: PebbleExpr[], range: SourceRange,
|
|
14
|
+
/** When using `Type.Constructor{ ... }` syntax, this is the type name */
|
|
15
|
+
typeName?: Identifier | undefined);
|
|
12
16
|
}
|
|
@@ -3,10 +3,14 @@ export class LitNamedObjExpr {
|
|
|
3
3
|
fieldNames;
|
|
4
4
|
values;
|
|
5
5
|
range;
|
|
6
|
-
|
|
6
|
+
typeName;
|
|
7
|
+
constructor(name, fieldNames, values, range,
|
|
8
|
+
/** When using `Type.Constructor{ ... }` syntax, this is the type name */
|
|
9
|
+
typeName = undefined) {
|
|
7
10
|
this.name = name;
|
|
8
11
|
this.fieldNames = fieldNames;
|
|
9
12
|
this.values = values;
|
|
10
13
|
this.range = range;
|
|
14
|
+
this.typeName = typeName;
|
|
11
15
|
}
|
|
12
16
|
}
|
|
@@ -93,7 +93,23 @@ export function _compileLitteralNamedObjExpr(ctx, expr, typeHint) {
|
|
|
93
93
|
else
|
|
94
94
|
typeHint = undefined;
|
|
95
95
|
const constructorName = expr.name.text;
|
|
96
|
-
|
|
96
|
+
let inferredStructType = undefined;
|
|
97
|
+
// Type.Constructor{ ... } syntax — resolve type directly
|
|
98
|
+
if (expr.typeName) {
|
|
99
|
+
const possibleTypes = ctx.scope.resolveType(expr.typeName.text);
|
|
100
|
+
if (possibleTypes) {
|
|
101
|
+
const dataTirName = possibleTypes.dataTirName;
|
|
102
|
+
const sopTirName = possibleTypes.sopTirName;
|
|
103
|
+
inferredStructType = getStructType((dataTirName ? ctx.program.types.get(dataTirName) : undefined)
|
|
104
|
+
?? ctx.program.types.get(sopTirName));
|
|
105
|
+
}
|
|
106
|
+
if (!inferredStructType) {
|
|
107
|
+
return ctx.error(DiagnosticCode._0_is_not_defined, expr.typeName.range, expr.typeName.text);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
inferredStructType = getStructType(ctx.scope.inferStructTypeFromConstructorName(constructorName)?.structType);
|
|
112
|
+
}
|
|
97
113
|
if (!inferredStructType) {
|
|
98
114
|
return ctx.error(DiagnosticCode._0_is_not_defined, expr.name.range, constructorName);
|
|
99
115
|
}
|
|
@@ -105,7 +121,11 @@ export function _compileLitteralNamedObjExpr(ctx, expr, typeHint) {
|
|
|
105
121
|
structType = inferredStructType;
|
|
106
122
|
if (!isTirType(typeHint))
|
|
107
123
|
typeHint = structType ?? inferredStructType;
|
|
108
|
-
const
|
|
124
|
+
const constructorIndex = structType?.constructors.findIndex(c => c.name === constructorName) ?? -1;
|
|
125
|
+
if (constructorIndex < 0) {
|
|
126
|
+
return ctx.error(DiagnosticCode._0_is_not_defined, expr.name.range, constructorName);
|
|
127
|
+
}
|
|
128
|
+
const fieldValues = __commonCompileStructFieldValues(ctx, expr, typeHint, structType, constructorIndex);
|
|
109
129
|
if (!Array.isArray(fieldValues))
|
|
110
130
|
return undefined;
|
|
111
131
|
typeHint = getUnaliased(typeHint);
|
|
@@ -108,6 +108,15 @@ function getStructPropAccessReturnType(ctx, structType, propName) {
|
|
|
108
108
|
if (field)
|
|
109
109
|
return field.type;
|
|
110
110
|
}
|
|
111
|
+
// constructor accessor methods for multi-constructor data structs
|
|
112
|
+
// e.g. ExtendedInteger.finite() returns int (the field of the Finite constructor)
|
|
113
|
+
if (structType.constructors.length > 1) {
|
|
114
|
+
const lowerPropName = propName.toLowerCase();
|
|
115
|
+
const ctor = structType.constructors.find(c => c.name.toLowerCase() === lowerPropName);
|
|
116
|
+
if (ctor && ctor.fields.length === 1) {
|
|
117
|
+
return new TirFuncT([], ctor.fields[0].type);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
111
120
|
return findPropInImpls(ctx, structType.methodNamesPtr, propName);
|
|
112
121
|
}
|
|
113
122
|
function findPropInImpls(ctx, methodsNamesPtr, propName) {
|
|
@@ -143,6 +143,19 @@ export class SourceTypeMap {
|
|
|
143
143
|
result.push({ name: field.name, type: field.type, kind: "field" });
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
+
// constructor accessor methods for multi-constructor structs
|
|
147
|
+
// e.g. ExtendedInteger.finite() returns int
|
|
148
|
+
if (type.constructors.length > 1) {
|
|
149
|
+
for (const ctor of type.constructors) {
|
|
150
|
+
if (ctor.fields.length === 1) {
|
|
151
|
+
result.push({
|
|
152
|
+
name: ctor.name.toLowerCase(),
|
|
153
|
+
type: new TirFuncT([], ctor.fields[0].type),
|
|
154
|
+
kind: "method"
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
146
159
|
// methods
|
|
147
160
|
result.push(...this.methodsFromMap(type.methodNamesPtr));
|
|
148
161
|
return result;
|
|
@@ -14,7 +14,7 @@ import { TirLitUndefExpr } from "../../tir/expressions/litteral/TirLitUndefExpr.
|
|
|
14
14
|
import { TirLitVoidExpr } from "../../tir/expressions/litteral/TirLitVoidExpr.js";
|
|
15
15
|
import { TirAssertAndContinueExpr } from "../../tir/expressions/TirAssertAndContinueExpr.js";
|
|
16
16
|
import { TirCallExpr } from "../../tir/expressions/TirCallExpr.js";
|
|
17
|
-
import { TirCaseExpr, TirCaseMatcher } from "../../tir/expressions/TirCaseExpr.js";
|
|
17
|
+
import { TirCaseExpr, TirCaseMatcher, TirWildcardCaseMatcher } from "../../tir/expressions/TirCaseExpr.js";
|
|
18
18
|
import { TirElemAccessExpr } from "../../tir/expressions/TirElemAccessExpr.js";
|
|
19
19
|
import { TirFailExpr } from "../../tir/expressions/TirFailExpr.js";
|
|
20
20
|
import { TirFromDataExpr } from "../../tir/expressions/TirFromDataExpr.js";
|
|
@@ -42,7 +42,8 @@ import { TirArrayLikeDeconstr } from "../../tir/statements/TirVarDecl/TirArrayLi
|
|
|
42
42
|
import { TirNamedDeconstructVarDecl } from "../../tir/statements/TirVarDecl/TirNamedDeconstructVarDecl.js";
|
|
43
43
|
import { TirSimpleVarDecl } from "../../tir/statements/TirVarDecl/TirSimpleVarDecl.js";
|
|
44
44
|
import { TirAliasType } from "../../tir/types/TirAliasType.js";
|
|
45
|
-
import { TirFuncT, TirListT } from "../../tir/types/TirNativeType/index.js";
|
|
45
|
+
import { TirFuncT, TirListT, TirPairDataT } from "../../tir/types/TirNativeType/index.js";
|
|
46
|
+
import { TirLinearMapT } from "../../tir/types/TirNativeType/native/linearMap.js";
|
|
46
47
|
import { TirDataStructType, TirSoPStructType } from "../../tir/types/TirStructType.js";
|
|
47
48
|
import { getListTypeArg } from "../../tir/types/utils/getListTypeArg.js";
|
|
48
49
|
import { getUnaliased } from "../../tir/types/utils/getUnaliased.js";
|
|
@@ -67,9 +68,6 @@ export function expressifyVars(ctx, expr) {
|
|
|
67
68
|
|| expr instanceof TirLitTrueExpr
|
|
68
69
|
|| expr instanceof TirLitFalseExpr
|
|
69
70
|
|| expr instanceof TirLitThisExpr
|
|
70
|
-
|| expr instanceof TirLitArrExpr
|
|
71
|
-
|| expr instanceof TirLitObjExpr
|
|
72
|
-
|| expr instanceof TirLitNamedObjExpr
|
|
73
71
|
|| expr instanceof TirLitStrExpr
|
|
74
72
|
|| expr instanceof TirLitIntExpr
|
|
75
73
|
|| expr instanceof TirLitHexBytesExpr
|
|
@@ -77,6 +75,19 @@ export function expressifyVars(ctx, expr) {
|
|
|
77
75
|
// hoisted expressions are necessarily closed, so no external variables
|
|
78
76
|
|| expr instanceof TirHoistedExpr)
|
|
79
77
|
return expr;
|
|
78
|
+
if (expr instanceof TirLitObjExpr
|
|
79
|
+
|| expr instanceof TirLitNamedObjExpr) {
|
|
80
|
+
for (let i = 0; i < expr.values.length; i++) {
|
|
81
|
+
expr.values[i] = expressifyVars(ctx, expr.values[i]);
|
|
82
|
+
}
|
|
83
|
+
return expr;
|
|
84
|
+
}
|
|
85
|
+
if (expr instanceof TirLitArrExpr) {
|
|
86
|
+
for (let i = 0; i < expr.elems.length; i++) {
|
|
87
|
+
expr.elems[i] = expressifyVars(ctx, expr.elems[i]);
|
|
88
|
+
}
|
|
89
|
+
return expr;
|
|
90
|
+
}
|
|
80
91
|
// every property access must be replaced with a variable access (or similar)
|
|
81
92
|
// that is either letted/hoisted/nativeFunc/varAccess expression
|
|
82
93
|
//
|
|
@@ -374,6 +385,29 @@ function expressifyMethodCall(ctx, methodCall) {
|
|
|
374
385
|
}
|
|
375
386
|
if (objectType instanceof TirDataStructType
|
|
376
387
|
|| objectType instanceof TirSoPStructType) {
|
|
388
|
+
// constructor accessor methods for multi-constructor structs
|
|
389
|
+
// e.g. ExtendedInteger.finite() asserts Finite and returns int
|
|
390
|
+
if (objectType.constructors.length > 1 && methodCall.args.length === 0) {
|
|
391
|
+
const lowerMethodName = methodName.toLowerCase();
|
|
392
|
+
const ctor = objectType.constructors.find(c => c.name.toLowerCase() === lowerMethodName);
|
|
393
|
+
if (ctor && ctor.fields.length === 1) {
|
|
394
|
+
const fName = ctor.fields[0].name;
|
|
395
|
+
const fType = ctor.fields[0].type;
|
|
396
|
+
const exprRange = SourceRange.join(methodIdentifierProp.range, methodCall.range.atEnd());
|
|
397
|
+
return new TirCaseExpr(objectExpr, [
|
|
398
|
+
new TirCaseMatcher(new TirNamedDeconstructVarDecl(ctor.name, new Map([
|
|
399
|
+
[fName, new TirSimpleVarDecl(fName, fType, undefined, true, exprRange)]
|
|
400
|
+
]), undefined, objectExpr.type, undefined, true, exprRange), new TirVariableAccessExpr({
|
|
401
|
+
variableInfos: {
|
|
402
|
+
name: fName,
|
|
403
|
+
type: fType,
|
|
404
|
+
isConstant: true,
|
|
405
|
+
},
|
|
406
|
+
isDefinedOutsideFuncScope: false,
|
|
407
|
+
}, exprRange), exprRange)
|
|
408
|
+
], new TirWildcardCaseMatcher(new TirFailExpr(undefined, fType, exprRange), exprRange), fType, exprRange);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
377
411
|
const structMethods = objectType.methodNamesPtr;
|
|
378
412
|
const tirMethodName = structMethods.get(methodName);
|
|
379
413
|
if (!tirMethodName)
|
|
@@ -388,6 +422,19 @@ function expressifyMethodCall(ctx, methodCall) {
|
|
|
388
422
|
if (result)
|
|
389
423
|
return result;
|
|
390
424
|
}
|
|
425
|
+
if (objectType instanceof TirLinearMapT) {
|
|
426
|
+
const exprRange = SourceRange.join(methodIdentifierProp.range, methodCall.range.atEnd());
|
|
427
|
+
if (methodName === "lookup") {
|
|
428
|
+
if (methodCall.args.length !== 1)
|
|
429
|
+
throw new Error(`Method 'lookup' of type 'LinearMap' takes 1 argument, ${methodCall.args.length} provided`);
|
|
430
|
+
return new TirCallExpr(TirNativeFunc._lookupLinearMap(objectType.keyTypeArg, objectType.valTypeArg), [methodCall.args[0], objectExpr], methodCall.type, exprRange);
|
|
431
|
+
}
|
|
432
|
+
// LinearMap is List<PairData> at UPLC level; delegate list methods
|
|
433
|
+
const listType = new TirListT(new TirPairDataT());
|
|
434
|
+
const result = expressifyListMethodCall(ctx, objectExpr, methodCall, methodName, listType, exprRange);
|
|
435
|
+
if (result)
|
|
436
|
+
return result;
|
|
437
|
+
}
|
|
391
438
|
throw new Error(`not implemented::expressifyMethodCall for type '${objectType.toString()}' (method name: '${methodName}')`);
|
|
392
439
|
// const tsEnsureExhautstiveCheck: never = objectType;
|
|
393
440
|
throw new Error(`Cannot call method '${methodName}' on non-struct type '${objectType.toString()}'`);
|
|
@@ -85,6 +85,7 @@ export declare class TirNativeFunc implements ITirExpr {
|
|
|
85
85
|
static _foldr(elemT: TirType, returnT: TirType): TirNativeFunc;
|
|
86
86
|
static _foldl(elemT: TirType, returnT: TirType): TirNativeFunc;
|
|
87
87
|
static _findSopOptional(elems_t: TirType): TirNativeFunc;
|
|
88
|
+
static _lookupLinearMap(keyT: TirType, valT: TirType): TirNativeFunc;
|
|
88
89
|
static _length(elemT: TirType): TirNativeFunc;
|
|
89
90
|
static _some(elemT: TirType): TirNativeFunc;
|
|
90
91
|
static _every(elemT: TirType): TirNativeFunc;
|
|
@@ -684,6 +684,14 @@ export class TirNativeFunc {
|
|
|
684
684
|
new TirListT(elems_t)
|
|
685
685
|
], new TirSopOptT(elems_t)));
|
|
686
686
|
}
|
|
687
|
+
static _lookupLinearMap(keyT, valT) {
|
|
688
|
+
return new TirNativeFunc(IRNativeTag._lookupLinearMap, new TirFuncT([
|
|
689
|
+
// key: data
|
|
690
|
+
data_t,
|
|
691
|
+
// map: list<pair<data,data>>
|
|
692
|
+
new TirLinearMapT(keyT, valT)
|
|
693
|
+
], new TirSopOptT(valT)));
|
|
694
|
+
}
|
|
687
695
|
static _length(elemT) {
|
|
688
696
|
return new TirNativeFunc(IRNativeTag._length, new TirFuncT([
|
|
689
697
|
// list
|
package/dist/parser/Parser.js
CHANGED
|
@@ -1491,6 +1491,23 @@ export class Parser extends DiagnosticEmitter {
|
|
|
1491
1491
|
}
|
|
1492
1492
|
return new LitNamedObjExpr(identifier, litObjExpr.fieldNames, litObjExpr.values, SourceRange.join(identifier.range, litObjExpr.range));
|
|
1493
1493
|
}
|
|
1494
|
+
// LitNamedObjExpr with type qualifier
|
|
1495
|
+
// eg: `Type.Constructor{ a: 1, b: 2 }`
|
|
1496
|
+
if (tn.peek() === Token.Dot) {
|
|
1497
|
+
const savedState = tn.mark();
|
|
1498
|
+
tn.next(); // consume '.'
|
|
1499
|
+
if (tn.skipIdentifier(IdentifierHandling.Always)) {
|
|
1500
|
+
const ctorIdentifier = new Identifier(tn.readIdentifier(), tn.range());
|
|
1501
|
+
if (tn.peek() === Token.OpenBrace) {
|
|
1502
|
+
const litObjExpr = this.parseExprStart();
|
|
1503
|
+
if (litObjExpr instanceof LitObjExpr) {
|
|
1504
|
+
return new LitNamedObjExpr(ctorIdentifier, litObjExpr.fieldNames, litObjExpr.values, SourceRange.join(identifier.range, litObjExpr.range), identifier // typeName
|
|
1505
|
+
);
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
tn.reset(savedState);
|
|
1510
|
+
}
|
|
1494
1511
|
// param => ...
|
|
1495
1512
|
if (tn.peek() === Token.FatArrow
|
|
1496
1513
|
// && !tn.isNextTokenOnNewLine() // original impl had this, not sure why
|
package/package.json
CHANGED