Skip to content

Commit

Permalink
refactor: Update AWST nodes to include AssertExpression and make use …
Browse files Browse the repository at this point in the history
…of it in place of intrinsic op
  • Loading branch information
tristanmenzel committed Nov 27, 2024
1 parent d4e8546 commit bfd9ee4
Show file tree
Hide file tree
Showing 46 changed files with 7,049 additions and 7,803 deletions.
16 changes: 6 additions & 10 deletions src/awst/intrinsic-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,19 @@ export const intrinsicFactory = {
})
},
err({ sourceLocation, comment }: { sourceLocation: SourceLocation; comment: string | null }) {
return nodeFactory.intrinsicCall({
opCode: 'err',
return nodeFactory.assertExpression({
condition: null,
sourceLocation,
stackArgs: [],
immediates: [],
wtype: wtypes.voidWType,
comment,
errorMessage: comment,
})
},
assert({ sourceLocation, comment, condition }: { sourceLocation: SourceLocation; comment: string | null; condition: Expression }) {
return nodeFactory.intrinsicCall({
opCode: 'assert',
return nodeFactory.assertExpression({
sourceLocation,
stackArgs: [condition],
immediates: [],
condition,
wtype: wtypes.voidWType,
comment,
errorMessage: comment,
})
},
bytesLen({ value, sourceLocation }: { value: awst.Expression; sourceLocation: SourceLocation }) {
Expand Down
7 changes: 0 additions & 7 deletions src/awst/node-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
Copy,
ExpressionStatement,
IntegerConstant,
IntrinsicCall,
MethodDocumentation,
Not,
NumericComparisonExpression,
Expand Down Expand Up @@ -213,12 +212,6 @@ const explicitNodeFactory = {
returns: props?.returns ?? null,
})
},
intrinsicCall(props: Omit<Props<IntrinsicCall>, 'comment'> & { comment?: string | null }) {
return new IntrinsicCall({
...props,
comment: props.comment ?? null,
})
},
copy({ value, sourceLocation }: { value: Expression; sourceLocation: SourceLocation }) {
return new Copy({
value,
Expand Down
17 changes: 15 additions & 2 deletions src/awst/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ export class ReturnStatement extends Statement {
return visitor.visitReturnStatement(this)
}
}
export class AssertExpression extends Expression {
constructor(props: Props<AssertExpression>) {
super(props)
this.condition = props.condition
this.errorMessage = props.errorMessage
this.wtype = props.wtype
}
condition: Expression | null
errorMessage: string | null
accept<T>(visitor: ExpressionVisitor<T>): T {
return visitor.visitAssertExpression(this)
}
}
export class IntegerConstant extends Expression {
constructor(props: Props<IntegerConstant>) {
super(props)
Expand Down Expand Up @@ -316,12 +329,10 @@ export class IntrinsicCall extends Expression {
this.opCode = props.opCode
this.immediates = props.immediates
this.stackArgs = props.stackArgs
this.comment = props.comment
}
opCode: string
immediates: Array<string | bigint>
stackArgs: Array<Expression>
comment: string | null
accept<T>(visitor: ExpressionVisitor<T>): T {
return visitor.visitIntrinsicCall(this)
}
Expand Down Expand Up @@ -1309,6 +1320,7 @@ export const concreteNodes = {
loopExit: LoopExit,
loopContinue: LoopContinue,
returnStatement: ReturnStatement,
assertExpression: AssertExpression,
integerConstant: IntegerConstant,
decimalConstant: DecimalConstant,
boolConstant: BoolConstant,
Expand Down Expand Up @@ -1393,6 +1405,7 @@ export const concreteNodes = {
bigUIntConstant: IntegerConstant,
} as const
export interface ExpressionVisitor<T> {
visitAssertExpression(expression: AssertExpression): T
visitIntegerConstant(expression: IntegerConstant): T
visitDecimalConstant(expression: DecimalConstant): T
visitBoolConstant(expression: BoolConstant): T
Expand Down
18 changes: 17 additions & 1 deletion src/awst/to-code-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { TodoError } from '../errors'
import { logger } from '../logger'
import { uint8ArrayToBase32, uint8ArrayToUtf8 } from '../util'
import type { ContractReference } from './models'
import type { AppStorageDefinition, ContractMemberNodeVisitor, Emit, ExpressionVisitor, RootNodeVisitor, StatementVisitor } from './nodes'
import type {
AppStorageDefinition,
AssertExpression,
ContractMemberNodeVisitor,
Emit,
ExpressionVisitor,
RootNodeVisitor,
StatementVisitor,
} from './nodes'
import * as nodes from './nodes'
import { AppStorageKind, BytesEncoding, ContractMethodTarget, InstanceMethodTarget, InstanceSuperMethodTarget, SubroutineID } from './nodes'
import { SymbolToNumber } from './util'
Expand Down Expand Up @@ -331,6 +339,14 @@ export class ToCodeVisitor
visitLogicSignature(moduleStatement: nodes.LogicSignature): string[] {
return ['', `logicsig ${moduleStatement.id} {`, ...indent(moduleStatement.program.body.accept(this)), '}']
}
visitAssertExpression(expression: AssertExpression): string {
return [
expression.condition ? 'assert(' : 'err(',
expression.condition?.accept(this) ?? '',
expression.errorMessage ? `, comment=${expression.errorMessage}` : '',
')',
].join('')
}

private currentContract: ContractReference[] = []
visitContract(c: nodes.Contract): string[] {
Expand Down
3 changes: 1 addition & 2 deletions tests/approvals/out/abi-decorators/abi-decorators.awst.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@
"immediates": [
"ApplicationID"
],
"stack_args": [],
"comment": null
"stack_args": []
}
}
},
Expand Down
Loading

0 comments on commit bfd9ee4

Please sign in to comment.