UNPKG

@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

100 lines (90 loc) 2.51 kB
import { $ } from "@helios-lang/ir" import { ToIRContext } from "../codegen/index.js" import { Scope } from "../scopes/index.js" import { BoolType, ByteArrayType, DataEntity, IntType, RealType, StringType } from "../typecheck/index.js" import { Expr } from "./Expr.js" /** * @import { BoolLiteral, ByteArrayLiteral, IntLiteral, RealLiteral, StringLiteral } from "@helios-lang/compiler-utils" * @import { SourceMappedStringI } from "@helios-lang/ir" * @import { TypeCheckContext } from "../index.js" * @typedef {import("../typecheck/index.js").DataType} DataType * @typedef {import("../typecheck/index.js").EvalEntity} EvalEntity */ /** * @typedef {(IntLiteral | RealLiteral | BoolLiteral | ByteArrayLiteral | StringLiteral)} PrimitiveLiteral */ /** * Literal expression class (wraps literal tokens) */ export class PrimitiveLiteralExpr extends Expr { /** * @private * @readonly * @type {PrimitiveLiteral} */ _primitive /** * @param {PrimitiveLiteral} primitive */ constructor(primitive) { super(primitive.site) this._primitive = primitive } /** * @type {DataType} */ get type() { if (this._primitive.kind == "int") { return IntType } else if (this._primitive.kind == "real") { return RealType } else if (this._primitive.kind == "bool") { return BoolType } else if (this._primitive.kind == "string") { return StringType } else if (this._primitive.kind == "bytes") { return ByteArrayType } else { throw new Error("unhandled primitive type") } } /** * @param {TypeCheckContext} _ctx * @param {Scope} _scope * @returns {EvalEntity} */ evalInternal(_ctx, _scope) { return new DataEntity(this.type) } /** * @returns {boolean} */ isLiteral() { return true } /** * @param {ToIRContext} _ctx * @returns {SourceMappedStringI} */ toIR(_ctx) { if (this._primitive.kind == "real") { return $(this._primitive.value.toString(), this._primitive.site) } else { // all literals except RealLiteral can be reused in their string-form in the IR return $(this._primitive.toString(), this._primitive.site) } } /** * @returns {string} */ toString() { return this._primitive.toString() } }