@helios-lang/compiler
Version:
Helios is a Domain Specific Language that compiles to Plutus-Core (i.e. Cardano on-chain validator scripts). Helios is a non-Haskell alternative to Plutus. With this library you can compile Helios scripts and build Cardano transactions, all you need to bu
152 lines (129 loc) • 4.36 kB
JavaScript
import { makeTypeError } from "@helios-lang/compiler-utils"
import { $ } from "@helios-lang/ir"
import { TAB, ToIRContext } from "../codegen/index.js"
import { GlobalScope } from "../scopes/index.js"
import { BoolType, isDataType, VoidType } from "../typecheck/index.js"
import { EntryPointImpl } from "./EntryPoint.js"
import { ModuleCollection } from "./ModuleCollection.js"
/**
* @import { SourceMappedStringI } from "@helios-lang/ir"
* @import { Definitions, TypeCheckContext } from "../index.js"
* @typedef {import("../typecheck/index.js").DataType} DataType
* @typedef {import("../typecheck/index.js").ScriptTypes} ScriptTypes
* @typedef {import("../typecheck/index.js").Type} Type
* @typedef {import("./EntryPoint.js").EntryPoint} EntryPoint
*/
/**
* @implements {EntryPoint}
*/
export class RedeemerEntryPoint extends EntryPointImpl {
/**
* @param {string} purpose
* @param {ModuleCollection} modules
*/
constructor(purpose, modules) {
super(modules)
this.purpose = purpose
}
/**
* @type {Set<string>}
*/
get requiredParams() {
const ctx = new ToIRContext({ optimize: false, isTestnet: false })
const ir = this.toIRInternal(ctx)
return this.getRequiredParametersInternal(ctx, ir)
}
/**
* @param {TypeCheckContext} ctx
* @param {ScriptTypes} scriptTypes
*/
evalTypes(ctx, scriptTypes) {
const scope = GlobalScope.new({ scriptTypes, currentScript: this.name })
super.evalTypesInternal(ctx, scope)
// check the 'main' function
const main = this.mainFunc
const argTypeNames = main.argTypeNames
const argTypes = main.argTypes
const retType = main.retType
const nArgs = argTypes.length
if (nArgs != 1) {
ctx.errors.type(
main.site,
`expected 1 arg for a ${this.purpose} validator`
)
}
if (argTypeNames[0] != "" && !isDataType(argTypes[0])) {
ctx.errors.type(
main.site,
`illegal redeemer argument type in main: '${argTypes[0].toString()}`
)
}
if (!BoolType.isBaseOf(retType) && !new VoidType().isBaseOf(retType)) {
ctx.errors.type(
main.site,
`illegal return type for main, expected 'Bool' or '()', got '${retType.toString()}'`
)
}
}
/**
* @param {ToIRContext} ctx
* @param {Definitions | undefined} extra
* @returns {SourceMappedStringI}
*/
toIR(ctx, extra = undefined) {
let ir = this.toIRInternal(ctx)
ir = this.wrapEntryPoint(ctx, ir, extra)
ir = $`(__REDEEMER, __CONTEXT) -> {
${ir}
}`
return ir
}
/**
* @returns {string}
*/
toString() {
return `${this.purpose} ${this.name}\n${super.toString()}`
}
/**
* @protected
* @param {ToIRContext} ctx
* @returns {SourceMappedStringI}
*/
toIRInternal(ctx) {
const argTypeNames = this.mainFunc.argTypeNames
const argTypes = this.mainArgTypes
const innerArgNames = [`__REDEEMER`]
const innerArgs = argTypes.map((t, i) => {
// empty path
if (argTypeNames[i] != "") {
if (t.path == "") {
throw new Error("unexpected")
}
return $([
$(`${t.path}__from_data`),
$("("),
$(innerArgNames[i]),
$(")")
])
} else {
// unused arg, 0 is easier to optimize
return $("0")
}
})
let ir = $([$(`${this.mainPath}(`), $(innerArgs).join(", "), $(`)`)])
if (BoolType.isBaseOf(this.mainFunc.retType)) {
ir = $([
$(`${TAB}${TAB}__core__ifThenElse`),
$("(", this.mainRetExprSite),
$(`\n${TAB}${TAB}${TAB}`),
ir,
$(
`,\n${TAB}${TAB}${TAB}() -> {()},\n${TAB}${TAB}${TAB}() -> {__helios__error("validation returned false")}\n${TAB}${TAB})`
),
$("(", this.mainRetExprSite),
$(")")
])
}
return ir
}
}