Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Nov 18, 2023
1 parent 63a43c8 commit a754f54
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 46 deletions.
2 changes: 2 additions & 0 deletions generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ export function loadHashmapNode<X>(slice: Slice, n: number, loadX: (slice: Slice
let slice2 = slice.loadRef().beginParse();
return {
kind: 'HashmapNode_hmn_fork',
n: n - 1,
left: loadHashmap<X>(slice1, n, loadX),
right: loadHashmap<X>(slice2, n, loadX)
};
Expand Down Expand Up @@ -724,6 +725,7 @@ export function loadSame<X>(slice: Slice, loadX: (slice: Slice) => X): Same<X> {
z = slice.loadUint(32);
return {
kind: 'Same_t',
Y: Y - 1,
y: y,
z: z
};
Expand Down
98 changes: 52 additions & 46 deletions tests/my.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,38 +599,58 @@ function getCondition(conditions: Array<BinaryExpression>): Expression {
}
}

function fillConstructor(declaration: Declaration, constructor: TLBConstructor) {
declaration.combinator.args.forEach(element => {
let parameter: TLBParameter | undefined = undefined;
if (element instanceof NameExpr) {
if (constructor.implicitFields.has(element.name)) {
let variable: TLBVariable;
if (constructor.implicitFields.get(element.name) == 'Type') {
variable = {negated: false, const: false, type: 'Type', name: element.name}
function fillConstructors(declarations: Declaration[], tlbCode: TLBCode) {
declarations.forEach(declaration => {
let tlbType: TLBType | undefined = tlbCode.types.get(declaration.combinator.name);
if (tlbType == undefined) {
tlbType = {name: declaration.combinator.name, constructors: []}
}
tlbType.constructors.push({declaration: declaration, parameters: [], parametersMap: new Map<string, TLBParameter>(), implicitFields: new Map<string, string>(), name: declaration.constructorDef.name});
tlbCode.types.set(tlbType.name, tlbType);
})

tlbCode.types.forEach((tlbType: TLBType, combinatorName: string) => {
tlbType.constructors.forEach(constructor => {

constructor.declaration?.fields.forEach(field => {
if (field instanceof FieldBuiltinDef) {
constructor.implicitFields.set(field.name, field.type);
}
else {
variable = {negated: false, const: false, type: '#', name: element.name}
})

constructor.declaration.combinator.args.forEach(element => {
let parameter: TLBParameter | undefined = undefined;
if (element instanceof NameExpr) {
if (constructor.implicitFields.has(element.name)) {
let variable: TLBVariable;
if (constructor.implicitFields.get(element.name) == 'Type') {
variable = {negated: false, const: false, type: 'Type', name: element.name}
}
else {
variable = {negated: false, const: false, type: '#', name: element.name}
}
parameter = {variable: variable, expression: tIdentifier(element.name)};
}
else {
throw new Error('Field not known before using (should be tagged as implicit): ' + element)
}
} else if (element instanceof MathExpr) {
let derivedExpr = deriveMathExpression(element);
parameter = {variable: {negated: false, const: false, type: '#', name: derivedExpr.name}, expression: derivedExpr.derived};
} else if (element instanceof NegateExpr && element.expr instanceof MathExpr) {
let derivedExpr = deriveMathExpression(element.expr);
parameter = {variable: {negated: true, const: false, type: '#', name: derivedExpr.name}, expression: derivedExpr.derived};
} else if (element instanceof NumberExpr) {
parameter = {variable: {negated: false, const: true, type: '#', name: 'n'}, expression: tNumericLiteral(element.num)}
} else {
// throw new Error('Cannot identify combinator arg: ' + element)
}
parameter = {variable: variable, expression: tIdentifier(element.name)};
}
else {
throw new Error('Field not known before using (should be tagged as implicit): ' + element)
}
} else if (element instanceof MathExpr) {
let derivedExpr = deriveMathExpression(element);
parameter = {variable: {negated: false, const: false, type: '#', name: derivedExpr.name}, expression: derivedExpr.derived};
} else if (element instanceof NegateExpr && element.expr instanceof MathExpr) {
let derivedExpr = deriveMathExpression(element.expr);
parameter = {variable: {negated: true, const: false, type: '#', name: derivedExpr.name}, expression: derivedExpr.derived};
} else if (element instanceof NumberExpr) {
parameter = {variable: {negated: false, const: true, type: '#', name: 'n'}, expression: tNumericLiteral(element.num)}
} else {
// throw new Error('Cannot identify combinator arg: ' + element)
}
if (parameter) {
constructor.parameters.push(parameter);
constructor.parametersMap.set(parameter.variable.name, parameter);
}
if (parameter) {
constructor.parameters.push(parameter);
constructor.parametersMap.set(parameter.variable.name, parameter);
}
});
});
});
}

Expand Down Expand Up @@ -670,15 +690,8 @@ describe('parsing into intermediate representation using grammar', () => {


let tlbCode: TLBCode = {types: new Map<string, TLBType>()}

tree.declarations.forEach(declaration => {
let tlbType: TLBType | undefined = tlbCode.types.get(declaration.combinator.name);
if (tlbType == undefined) {
tlbType = {name: declaration.combinator.name, constructors: []}
}
tlbType.constructors.push({declaration: declaration, parameters: [], parametersMap: new Map<string, TLBParameter>(), implicitFields: new Map<string, string>(), name: declaration.constructorDef.name});
tlbCode.types.set(tlbType.name, tlbType);
})

fillConstructors(tree.declarations, tlbCode);

tlbCode.types.forEach((tlbType: TLBType, combinatorName: string) => {
let variableCombinatorName = combinatorName.charAt(0).toLowerCase() + combinatorName.slice(1)
Expand Down Expand Up @@ -720,14 +733,7 @@ describe('parsing into intermediate representation using grammar', () => {
let tagBitLen = tag?.length - 1;
let tagBinary = '0b' + tag.slice(1);

declaration?.fields.forEach(field => {
if (field instanceof FieldBuiltinDef) {
constructor.implicitFields.set(field.name, field.type);
}
})

if (structTypeParametersExpr.typeParameters.length == 0) {
fillConstructor(declaration, constructor)
structTypeParametersExpr = getTypeParametersExpression(constructor.parameters);
}

Expand Down

0 comments on commit a754f54

Please sign in to comment.