@harmoniclabs/pebble 0.1.4-dev1 → 0.1.4

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.
@@ -351,12 +351,12 @@ new IRDelayed(IRConst.int(0)),
351
351
  // token map cons
352
352
  new IRDelayed(_ir_let(_ir_apps(IRNative.headList, new IRVar(amount_tokenMap)), pairDataTokenSym => new IRForced(_ir_apps(IRNative.strictIfThenElse,
353
353
  // isTokenName( fst pairDataToken as bytes )
354
- _ir_apps(new IRVar(amount_isTokenName), _ir_apps(IRNative.unBData, _ir_apps(IRNative.fstPair, new IRVar(pairDataTokenSym))),
354
+ _ir_apps(new IRVar(amount_isTokenName), _ir_apps(IRNative.unBData, _ir_apps(IRNative.fstPair, new IRVar(pairDataTokenSym)))),
355
355
  // then: return amount
356
356
  new IRDelayed(_ir_apps(IRNative.unIData, _ir_apps(IRNative.sndPair, new IRVar(pairDataTokenSym)))),
357
357
  // else: recurse tail
358
358
  new IRDelayed(_ir_apps(new IRSelfCall(amount_tokenNameLoop), _ir_apps(IRNative.tailList, new IRVar(amount_tokenMap) // tokenMap list
359
- )))))))))))),
359
+ ))))))))))),
360
360
  // pass token map (snd pairData)
361
361
  _ir_apps(IRNative.unMapData, _ir_apps(IRNative.sndPair, new IRVar(pairDataSym)))))),
362
362
  // else: recurse policyLoop on tail value list
@@ -231,8 +231,8 @@ export class AstCompiler extends DiagnosticEmitter {
231
231
  console.log( msg.toString() );
232
232
  //*/
233
233
  }
234
- // return this.program;
235
- throw new Error("AstCompiler.compile: failed with " + nDiags + " diagnostic messages; first message: " + fstErrorMsg);
234
+ return this.program;
235
+ // throw new Error("AstCompiler.compile: failed with " + nDiags + " diagnostic messages; first message: " + fstErrorMsg );
236
236
  }
237
237
  const mainFuncExpr = this.program.functions.get(this.program.contractTirFuncName);
238
238
  if (this.program.contractTirFuncName === "" || !mainFuncExpr) {
@@ -73,7 +73,7 @@ export class Compiler extends DiagnosticEmitter {
73
73
  while (msg = this.diagnostics.shift()) {
74
74
  this.io.stdout.write(msg.toString() + "\n");
75
75
  }
76
- throw new Error("compilation failed with " + nDiags + " diagnostic messages; first message: " + fstErrorMsg);
76
+ // throw new Error("compilation failed with " + nDiags + " diagnostic messages; first message: " + fstErrorMsg );
77
77
  }
78
78
  return this._compileBackend(cfg, program);
79
79
  }
@@ -36,7 +36,8 @@ import { TirUnaryMinus } from "../../tir/expressions/unary/TirUnaryMinus.js";
36
36
  import { TirUnaryPlus } from "../../tir/expressions/unary/TirUnaryPlus.js";
37
37
  import { isTirUnaryPrefixExpr } from "../../tir/expressions/unary/TirUnaryPrefixExpr.js";
38
38
  import { TirUnaryTilde } from "../../tir/expressions/unary/TirUnaryTilde.js";
39
- import { bool_t } from "../../tir/program/stdScope/stdScope.js";
39
+ import { bool_t, bytes_t, int_t, valueAmountOfName } from "../../tir/program/stdScope/stdScope.js";
40
+ import { IRNativeTag } from "../../../IR/IRNodes/IRNative/IRNativeTag.js";
40
41
  import { TirReturnStmt } from "../../tir/statements/TirReturnStmt.js";
41
42
  import { TirArrayLikeDeconstr } from "../../tir/statements/TirVarDecl/TirArrayLikeDeconstr.js";
42
43
  import { TirNamedDeconstructVarDecl } from "../../tir/statements/TirVarDecl/TirNamedDeconstructVarDecl.js";
@@ -378,10 +379,23 @@ function expressifyMethodCall(ctx, methodCall) {
378
379
  objectType = objectType.aliased;
379
380
  continue;
380
381
  }
382
+ const callRange = SourceRange.join(methodIdentifierProp.range, methodCall.range.atEnd());
383
+ // fast-path: `Value.amountOf(policy, name)` compiles directly to
384
+ // `_amountOfValue(eqByteString(policy))(value)(eqByteString(name))`
385
+ // skipping the wrapper function indirection.
386
+ if (tirMethodName === valueAmountOfName
387
+ && methodCall.args.length === 2) {
388
+ const [policyArg, nameArg] = methodCall.args;
389
+ const bytesToBoolT = new TirFuncT([bytes_t], bool_t);
390
+ const isPolicy = new TirLettedExpr("value_amountOf_isPolicy", new TirCallExpr(TirNativeFunc.equalsByteString, [policyArg], bytesToBoolT, policyArg.range), policyArg.range);
391
+ const isTokenName = new TirLettedExpr("value_amountOf_isTokenName", new TirCallExpr(TirNativeFunc.equalsByteString, [nameArg], bytesToBoolT, nameArg.range), nameArg.range);
392
+ const amountOfValueNative = new TirNativeFunc(IRNativeTag._amountOfValue, new TirFuncT([bytesToBoolT, objectExpr.type, bytesToBoolT], int_t));
393
+ return new TirCallExpr(amountOfValueNative, [isPolicy, objectExpr, isTokenName], int_t, callRange);
394
+ }
381
395
  const funcExpr = ctx.program.functions.get(tirMethodName);
382
396
  if (!funcExpr)
383
397
  throw new Error(`Definition of method '${methodName}' on type '${objectType.toString()}' is missing.`);
384
- return new TirCallExpr(funcExpr, [objectExpr, ...methodCall.args], methodCall.type, SourceRange.join(methodIdentifierProp.range, methodCall.range.atEnd()));
398
+ return new TirCallExpr(funcExpr, [objectExpr, ...methodCall.args], methodCall.type, callRange);
385
399
  }
386
400
  if (objectType instanceof TirDataStructType
387
401
  || objectType instanceof TirSoPStructType) {
@@ -52,7 +52,8 @@ function addExtension(path, endsWithSlash) {
52
52
  return path + extension;
53
53
  }
54
54
  export function isAbsolutePath(path) {
55
- return path.startsWith(PATH_DELIMITER);
55
+ // Support both Unix (/) and Windows (C:/, D:/) absolute paths
56
+ return path.startsWith(PATH_DELIMITER) || /^[A-Za-z]:[\\/]/.test(path);
56
57
  }
57
58
  export function getEnvRelativePath(filePath, projectRoot) {
58
59
  if (!(typeof filePath === 'string' &&
@@ -11,5 +11,7 @@ export declare const string_t: TirStringT;
11
11
  export declare const bytes_t: TirBytesT;
12
12
  export declare const bool_t: TirBoolT;
13
13
  export declare const data_t: TirDataT;
14
+ export declare const valueLovelacesName: string;
15
+ export declare const valueAmountOfName: string;
14
16
  export declare function populateStdScope(program: TypedProgram): void;
15
17
  export declare function populatePreludeScope(program: TypedProgram): void;
@@ -15,12 +15,17 @@ import { SourceRange } from "../../../../ast/Source/SourceRange.js";
15
15
  import { TirInlineClosedIR } from "../../expressions/TirInlineClosedIR.js";
16
16
  import { TirFuncT } from "../../types/TirNativeType/index.js";
17
17
  import { IRNative } from "../../../../IR/IRNodes/IRNative/index.js";
18
+ import { IRFunc } from "../../../../IR/IRNodes/IRFunc.js";
19
+ import { IRVar } from "../../../../IR/IRNodes/IRVar.js";
20
+ import { _ir_apps } from "../../../../IR/IRNodes/IRApp.js";
18
21
  export const void_t = new TirVoidT();
19
22
  export const int_t = new TirIntT();
20
23
  export const string_t = new TirStringT();
21
24
  export const bytes_t = new TirBytesT();
22
25
  export const bool_t = new TirBoolT();
23
26
  export const data_t = new TirDataT();
27
+ export const valueLovelacesName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "sortedValueLovelaces";
28
+ export const valueAmountOfName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "amountOfValue";
24
29
  export function populateStdScope(program) {
25
30
  const stdScope = program.stdScope;
26
31
  function _defineStdUnambigous(t) {
@@ -544,14 +549,35 @@ export function populatePreludeScope(program) {
544
549
  const map_policyId_map_tokenName_int_t = program.getAppliedGeneric(TirLinearMapT.toTirTypeKey(), [policyId_t, map_tokenName_int_t]);
545
550
  if (!map_policyId_map_tokenName_int_t)
546
551
  throw new Error("expected map_policyId_map_tokenName_int_t");
547
- const valueLovelacesName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "sortedValueLovelaces";
548
552
  const value_t = _defineUnambigousAlias("Value", map_policyId_map_tokenName_int_t, new Map([
549
553
  [
550
554
  "lovelaces",
551
555
  valueLovelacesName
552
556
  ],
557
+ [
558
+ "amountOf",
559
+ valueAmountOfName
560
+ ],
553
561
  ]));
554
562
  preludeScope.program.functions.set(valueLovelacesName, new TirInlineClosedIR(new TirFuncT([value_t], int_t), (ctx) => IRNative._sortedValueLovelaces, SourceRange.unknown));
563
+ // Value.amountOf( policy: PolicyId, name: bytes ): int
564
+ // The IR native _amountOfValue is curried as: (isPolicy)(value)(isTokenName) => int
565
+ // where isPolicy and isTokenName are equality-check predicates.
566
+ // This wrapper adapts (self, policy, tokenName) => _amountOfValue(p => equalsByteString(p, policy))(self)(tn => equalsByteString(tn, tokenName))
567
+ preludeScope.program.functions.set(valueAmountOfName, new TirInlineClosedIR(new TirFuncT([value_t, policyId_t, bytes_t], int_t), (ctx) => {
568
+ const self = Symbol("amtOf_self");
569
+ const policy = Symbol("amtOf_policy");
570
+ const tokenName = Symbol("amtOf_tokenName");
571
+ const p = Symbol("amtOf_p");
572
+ const tn = Symbol("amtOf_tn");
573
+ return new IRFunc([self, policy, tokenName], _ir_apps(_ir_apps(_ir_apps(IRNative._amountOfValue,
574
+ // isPolicy predicate: \p -> equalsByteString(p, policy)
575
+ _ir_apps(IRNative.equalsByteString, new IRVar(policy))),
576
+ // value (self)
577
+ new IRVar(self)),
578
+ // isTokenName predicate: \tn -> equalsByteString(tn, tokenName)
579
+ _ir_apps(IRNative.equalsByteString, new IRVar(tokenName))));
580
+ }, SourceRange.unknown));
555
581
  /* // TODO
556
582
  untagged struct FlatValueEntry {
557
583
  policy: PolicyId,
@@ -2,7 +2,7 @@ import { SourceRange } from "../ast/Source/SourceRange.js";
2
2
  import { DiagnosticEmitter } from "../diagnostics/DiagnosticEmitter.js";
3
3
  import { DiagnosticCode } from "../diagnostics/diagnosticMessages.generated.js";
4
4
  import { assert } from "../utils/assert.js";
5
- import { isHighSurrogate, combineSurrogates, isIdentifierStart, numCodeUnits, isIdentifierPart, isWhiteSpace, isLineBreak, isDecimal, isOctal, isHexBase } from "../utils/text.js";
5
+ import { isHighSurrogate, combineSurrogates, isIdentifierStart, numCodeUnits, isIdentifierPart, isWhiteSpace, isLineBreak, isDecimal, isOctal, isHexBase, isHexOrDecimal } from "../utils/text.js";
6
6
  import { IdentifierHandling } from "./IdentifierHandling.js";
7
7
  import { OnNewLine } from "./OnNewLine.js";
8
8
  import { Token } from "./Token.js";
@@ -926,7 +926,7 @@ export class Tokenizer extends DiagnosticEmitter {
926
926
  let length = 0;
927
927
  while (pos < end) {
928
928
  let c = text.charCodeAt(pos);
929
- if (isHexBase(c)) {
929
+ if (isHexOrDecimal(c)) {
930
930
  length++;
931
931
  pos++;
932
932
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harmoniclabs/pebble",
3
- "version": "0.1.4-dev1",
3
+ "version": "0.1.4",
4
4
  "description": "A simple, yet rock solid, functional language with an imperative bias, targeting UPLC",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -63,6 +63,7 @@
63
63
  "devDependencies": {
64
64
  "@babel/preset-env": "^7.18.6",
65
65
  "@babel/preset-typescript": "^7.18.6",
66
+ "@harmoniclabs/buildooor": "^0.2.6",
66
67
  "@types/jest": "^28.1.4",
67
68
  "@types/node": "^18.14.6",
68
69
  "jest": "^29.4.3",