Skip to content

Commit

Permalink
pmatch.sop test
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed May 22, 2024
1 parent 147c102 commit 63b24d6
Show file tree
Hide file tree
Showing 28 changed files with 1,024 additions and 551 deletions.
8 changes: 4 additions & 4 deletions packages/onchain/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/onchain/src/IR/IRNodes/IRCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { BaseIRMetadata } from "./BaseIRMetadata";
import { mapArrayLike } from "./utils/mapArrayLike";
import { isIRTerm } from "../utils";
import { makeArrayLikeProxy } from "./utils/makeArrayLikeProxy";
import { MutArrayLike } from "../utils/MutArrayLike";

export interface IRCaseMeta extends BaseIRMetadata {}

export class IRCase
implements Cloneable<IRCase>, IHash, IIRParent, ToJson
{
constrTerm!: IRTerm;
continuations!: ArrayLike<IRTerm>;
continuations!: MutArrayLike<IRTerm>;

readonly hash!: Uint8Array;
markHashAsInvalid!: () => void;
Expand Down
3 changes: 2 additions & 1 deletion packages/onchain/src/IR/IRNodes/IRConstr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import { positiveIntAsBytes } from "../utils/positiveIntAsBytes";
import { mapArrayLike } from "./utils/mapArrayLike";
import { isIRTerm } from "../utils";
import { makeArrayLikeProxy } from "./utils/makeArrayLikeProxy";
import { MutArrayLike } from "../utils/MutArrayLike";

export interface IRConstrMeta extends BaseIRMetadata {}

export class IRConstr
implements Cloneable<IRConstr>, IHash, IIRParent, ToJson
{
readonly index!: bigint;
fields!: ArrayLike<IRTerm>;
fields!: MutArrayLike<IRTerm>;

readonly hash!: Uint8Array;
markHashAsInvalid!: () => void;
Expand Down
13 changes: 13 additions & 0 deletions packages/onchain/src/IR/IRNodes/IRLetted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { _getMinUnboundDbn } from "../toUPLC/subRoutines/handleLetted/groupByScope";
import { IRConstr } from "./IRConstr";
import { IRCase } from "./IRCase";


export type LettedSetEntry = {
Expand Down Expand Up @@ -477,6 +479,17 @@ export function getLettedTerms( irTerm: IRTerm, options?: Partial<GetLettedTerms
continue;
}

if( t instanceof IRConstr )
{
stack.push( ...Array.from( t.fields ) );
continue;
}
if( t instanceof IRCase )
{
stack.push( t.constrTerm, ...Array.from( t.continuations ) );
continue;
}

if( t instanceof IRFunc )
{
stack.push( t.body );
Expand Down
44 changes: 42 additions & 2 deletions packages/onchain/src/IR/toUPLC/_internal/_irToUplc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { showIR } from "../../utils/showIR";
import { IRError } from "../../IRNodes/IRError";
import { IRForced } from "../../IRNodes/IRForced";
import { IRDelayed } from "../../IRNodes/IRDelayed";
import { UPLCTerm, UPLCVar, Lambda, Application, UPLCConst, Builtin, ErrorUPLC, Force, Delay } from "@harmoniclabs/uplc";
import { UPLCTerm, UPLCVar, Lambda, Application, UPLCConst, Builtin, ErrorUPLC, Force, Delay, Constr, Case } from "@harmoniclabs/uplc";
import { IRConstr } from "../../IRNodes/IRConstr";
import { IRCase } from "../../IRNodes/IRCase";

export type RawSrcMap = { [node_index: number]: string };

Expand Down Expand Up @@ -130,7 +132,45 @@ export function _irToUplc(
max_idx
};
}

if( ir instanceof IRConstr )
{
const fields = Array.from( ir.fields )
.slice( 1 )
.reduce(
( accum, field, i ) => {
accum.push( _irToUplc( field, srcmap, accum[ i ].max_idx + 1 ) );
return accum;
},
[ _irToUplc( ir.fields[0], srcmap, node_index ) ]
)
return {
term: new Constr(
ir.index,
fields.map(({ term }) => term )
),
max_idx: fields[fields.length - 1].max_idx
};
}
if( ir instanceof IRCase )
{
const { term: constrTerm, max_idx } = _irToUplc( ir.constrTerm, srcmap, node_index );
const conts = Array.from( ir.continuations )
.slice( 1 )
.reduce(
( accum, cont, i ) => {
accum.push( _irToUplc( cont, srcmap, accum[ i ].max_idx + 1 ) );
return accum;
},
[ _irToUplc( ir.continuations[0], srcmap, max_idx + 1 ) ]
)
return {
term: new Case(
constrTerm,
conts.map(({ term }) => term )
),
max_idx: conts[conts.length - 1].max_idx
};
}
throw new Error("unknown IR term calling '_irToUplc'")
}

Expand Down
68 changes: 66 additions & 2 deletions packages/onchain/src/IR/toUPLC/_internal/_modifyChildFromTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { IRLetted } from "../../IRNodes/IRLetted";
import { IRTerm } from "../../IRTerm";
import { IRParentTerm } from "../../utils/isIRParentTerm";
import { isIRTerm, prettyIRJsonStr } from "../../utils";
import { IRConstr } from "../../IRNodes/IRConstr";
import { IRCase } from "../../IRNodes/IRCase";

/**
*
Expand Down Expand Up @@ -35,9 +37,10 @@ export function _modifyChildFromTo( parent: IRParentTerm | undefined, currentChi
)
{
// we are modifying the child
// so we remove the reference
// so we remove parent the reference
currentChild.parent = undefined;
}

if( parent instanceof IRApp )
{
// DO NO USE **ONLY** SHALLOW EQUALITY
Expand Down Expand Up @@ -68,7 +71,68 @@ export function _modifyChildFromTo( parent: IRParentTerm | undefined, currentChi
);
}


return;
}
else if( parent instanceof IRConstr )
{
let foundChild = false;
for( let i = 0; i < parent.fields.length; i++ )
{
const field = parent.fields[i];
if( field === currentChild )
{
parent.fields[i] = newChild;
foundChild = true;
break
}
}
if( foundChild ) return;
const currChildHash = currentChild instanceof Uint8Array ? currentChild : currentChild.hash;
for( let i = 0; i < parent.fields.length; i++ )
{
const field = parent.fields[i];
if( uint8ArrayEq( field.hash, currChildHash ) )
{
parent.fields[i] = newChild;
break
}
}
return;
}
else if( parent instanceof IRCase )
{
if( parent.constrTerm === currentChild )
{
parent.constrTerm = newChild;
return;
}
let foundChild = false;
for( let i = 0; i < parent.continuations.length; i++ )
{
const field = parent.continuations[i];
if( field === currentChild )
{
parent.continuations[i] = newChild;
foundChild = true;
break
}
}
if( foundChild ) return;
const currChildHash = currentChild instanceof Uint8Array ? currentChild : currentChild.hash;
if( uint8ArrayEq( currChildHash, parent.constrTerm.hash ) )
{
parent.constrTerm = newChild;
return;
}
for( let i = 0; i < parent.continuations.length; i++ )
{
const field = parent.continuations[i];
if( uint8ArrayEq( field.hash, currChildHash ) )
{
parent.continuations[i] = newChild;
break
}
}
return;
}

Expand Down
26 changes: 24 additions & 2 deletions packages/onchain/src/IR/toUPLC/_internal/iterTree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IRApp } from "../../IRNodes/IRApp";
import { IRCase } from "../../IRNodes/IRCase";
import { IRConstr } from "../../IRNodes/IRConstr";
import { IRDelayed } from "../../IRNodes/IRDelayed";
import { IRForced } from "../../IRNodes/IRForced";
import { IRFunc } from "../../IRNodes/IRFunc";
Expand All @@ -20,7 +22,7 @@ export function iterTree(
{
const { term: t, dbn } = stack.pop() as { term: IRTerm, dbn: number };

if( has_shouldSkipNode && shouldSkipNode( t, dbn ) ) continue;
if( has_shouldSkipNode && shouldSkipNode( t, dbn ) ) continue;

const termParent = t.parent;
const negDbn = t instanceof IRFunc ? t.arity : 0;
Expand All @@ -29,7 +31,7 @@ export function iterTree(

if( modifiedParent && termParent !== undefined )
{
if( stack.length > 0 && stack[ stack.length - 1 ].isIRAppArg )
while( stack.length > 0 && stack[ stack.length - 1 ].isIRAppArg )
{
// there is an extra node
stack.pop();
Expand Down Expand Up @@ -64,6 +66,26 @@ export function iterTree(
stack.push({ term: t.body, dbn: dbn + t.arity });
continue;
}

if( t instanceof IRCase )
{
stack.push(
{ term: t.constrTerm, dbn },
...Array.from( t.continuations ).map( cont =>
({ term: cont, dbn, isIRAppArg: true })
)
);
continue;
}
if( t instanceof IRConstr )
{
stack.push(
...Array.from( t.fields ).map( f =>
({ term: f, dbn, isIRAppArg: true })
)
);
continue;
}

if( t instanceof IRHoisted )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IRApp } from "../../IRNodes/IRApp";
import { IRCase } from "../../IRNodes/IRCase";
import { IRConstr } from "../../IRNodes/IRConstr";
import { IRDelayed } from "../../IRNodes/IRDelayed";
import { IRForced } from "../../IRNodes/IRForced";
import { IRFunc } from "../../IRNodes/IRFunc";
Expand Down Expand Up @@ -33,6 +35,39 @@ export function markRecursiveHoistsAsForced( _term: IRTerm ): void
);
continue;
}
if( t instanceof IRConstr )
{
// must push the arg first and then the fucntion
// so that we can check if the function is the Z combinator before the arg is processed
stack.push(
...Array.from( t.fields )
.map( (f, i) => ({
term: f,
isIRAppArg: i > 0,
isInRecursiveTerm
}))
);
continue;
}
if( t instanceof IRCase )
{
// must push the arg first and then the fucntion
// so that we can check if the function is the Z combinator before the arg is processed
stack.push(
{
term: t.constrTerm,
isIRAppArg: false,
isInRecursiveTerm
},
...Array.from( t.continuations )
.map( cont => ({
term: cont,
isIRAppArg: true,
isInRecursiveTerm
}))
);
continue;
}

if(
t instanceof IRNative &&
Expand Down Expand Up @@ -85,7 +120,7 @@ export function markRecursiveHoistsAsForced( _term: IRTerm ): void
stack.push({ term: t.value, isInRecursiveTerm });
continue;
}
else // if( t instanceof IRHoisted )
else if( t instanceof IRHoisted )
{
stack.push({ term: t.hoisted, isInRecursiveTerm });
continue;
Expand Down
4 changes: 4 additions & 0 deletions packages/onchain/src/IR/utils/MutArrayLike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface MutArrayLike<T> {
readonly length: number;
[n: number]: T;
}
4 changes: 4 additions & 0 deletions packages/onchain/src/IR/utils/showIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { IRError } from "../IRNodes/IRError";
import { IRFunc } from "../IRNodes/IRFunc";
import { termTypeToString } from "../../pluts/type_system/utils";
import { showUPLCConstValue } from "@harmoniclabs/uplc";
import { IRConstr } from "../IRNodes/IRConstr";
import { IRCase } from "../IRNodes/IRCase";

const vars = "abcdefghilmopqrstuvzwxyjkABCDEFGHILJMNOPQRSTUVZWXYJK".split('');

Expand Down Expand Up @@ -125,6 +127,8 @@ export function showIR( _ir: IRTerm )
function _loop( ir: IRTerm, dbn: number ): string
{
if( ir instanceof IRApp ) return `[${_loop(ir.fn, dbn)} ${_loop(ir.arg, dbn)}]`;
if( ir instanceof IRConstr ) return `(constr ${ir.index} ${Array.from( ir.fields ).map( f => _loop( f, dbn )).join(" ")})`;
if( ir instanceof IRCase ) return `(case ${_loop( ir.constrTerm, dbn ) } ${Array.from( ir.continuations ).map( f => _loop( f, dbn )).join(" ")})`;
if( ir instanceof IRNative ) return `(native ${nativeTagToString(ir.tag)})`;
if( ir instanceof IRLetted )
{
Expand Down
1 change: 1 addition & 0 deletions packages/onchain/src/pluts/PTypes/PSoP/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./psop";
1 change: 1 addition & 0 deletions packages/onchain/src/pluts/PTypes/PSoP/psop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { IRConstr } from "../../../IR/IRNodes/IRConstr";
*/
class _PSop extends PType
{
protected _sop: never;
protected constructor()
{
super();
Expand Down
1 change: 1 addition & 0 deletions packages/onchain/src/pluts/PTypes/PStruct/pstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { IRApp } from "../../../IR/IRNodes/IRApp";
*/
class _PStruct extends PData
{
protected _struct: never;
protected constructor()
{
super();
Expand Down
1 change: 1 addition & 0 deletions packages/onchain/src/pluts/PTypes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./PStruct";
export * from "./PSoP";
export * from "./PAlias";
export * from "./PData";
export * from "./PFn";
Expand Down
Loading

0 comments on commit 63b24d6

Please sign in to comment.