Skip to content

Commit

Permalink
- use isDataType() instead of the more verbose new DefaultTypeClass()…
Browse files Browse the repository at this point in the history
….isImplementedBy(...)

- type-utils lib upgrade
- TupleType TypeSchema added
  • Loading branch information
christianschmitz committed Aug 19, 2024
1 parent 54a4388 commit 417a63a
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 35 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helios-lang/compiler",
"version": "0.17.0-43",
"version": "0.17.0-44",
"description": "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 build 100% client-side dApps for Cardano.",
"main": "src/index.js",
"types": "types/index.d.ts",
Expand Down Expand Up @@ -54,7 +54,7 @@
"@helios-lang/codec-utils": "^0.1.32",
"@helios-lang/compiler-utils": "^0.1.53",
"@helios-lang/ir": "^0.1.26",
"@helios-lang/type-utils": "^0.1.20",
"@helios-lang/type-utils": "^0.1.21",
"@helios-lang/uplc": "^0.1.35"
}
}
7 changes: 2 additions & 5 deletions src/program/DatumRedeemerEntryPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { None } from "@helios-lang/type-utils"
import { UplcProgramV2 } from "@helios-lang/uplc"
import { TAB, ToIRContext } from "../codegen/index.js"
import { GlobalScope } from "../scopes/index.js"
import { BoolType, DefaultTypeClass } from "../typecheck/index.js"
import { BoolType, isDataType } from "../typecheck/index.js"
import { EntryPointImpl } from "./EntryPoint.js"
import { ModuleCollection } from "./ModuleCollection.js"

Expand Down Expand Up @@ -102,10 +102,7 @@ export class DatumRedeemerEntryPoint extends EntryPointImpl {
}

for (let i = 0; i < nArgs; i++) {
if (
argTypeNames[i] != "" &&
!new DefaultTypeClass().isImplementedBy(argTypes[i])
) {
if (argTypeNames[i] != "" && !isDataType(argTypes[i])) {
throw CompilerError.type(
main.site,
`illegal type for arg ${i + 1} in main ${i == nArgs - 2 ? "(datum) " : i == nArgs - 3 ? "(redeemer) " : ""}: '${argTypes[i].toString()}`
Expand Down
10 changes: 3 additions & 7 deletions src/program/GenericEntryPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { $, SourceMappedString } from "@helios-lang/ir"
import { None, expectSome } from "@helios-lang/type-utils"
import { TAB, ToIRContext } from "../codegen/index.js"
import { GlobalScope } from "../scopes/index.js"
import { DefaultTypeClass } from "../typecheck/index.js"
import { isDataType } from "../typecheck/index.js"
import { EntryPointImpl } from "./EntryPoint.js"
import { ModuleCollection } from "./ModuleCollection.js"

Expand Down Expand Up @@ -60,19 +60,15 @@ export class GenericEntryPoint extends EntryPointImpl {
const retType = main.retType

argTypeNames.forEach((argTypeName, i) => {
if (
argTypeName != "" &&
!new DefaultTypeClass().isImplementedBy(argTypes[i])
) {
if (argTypeName != "" && !isDataType(argTypes[i])) {
throw CompilerError.type(
main.site,
`illegal argument type in main: '${argTypes[i].toString()}`
)
}
})

// TODO: support tuple return values ?
if (!new DefaultTypeClass().isImplementedBy(retType)) {
if (!isDataType(retType)) {
throw CompilerError.type(
main.site,
`illegal return type for main: '${retType.toString()}'`
Expand Down
10 changes: 5 additions & 5 deletions src/program/Program.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ErrorCollector, Source, Word } from "@helios-lang/compiler-utils"
import { ErrorCollector, Source } from "@helios-lang/compiler-utils"
import {
$,
DEFAULT_PARSE_OPTIONS,
SourceMappedString,
compile as compileIR
Expand All @@ -12,10 +11,11 @@ import {
FuncStatement,
StructStatement
} from "../statements/index.js"
import { isDataType } from "../typecheck/index.js"
import { newEntryPoint } from "./newEntryPoint.js"
import { Module } from "./Module.js"
import { UserFunc } from "./UserFunc.js"
import { ModuleCollection } from "./ModuleCollection.js"
import { UserFunc } from "./UserFunc.js"

/**
* @typedef {import("@helios-lang/compiler-utils").Site} Site
Expand Down Expand Up @@ -156,8 +156,8 @@ export class Program {

// make sure all arg types and return type are compatible and that the function doesn't have any typeparameters
if (
fn.argTypes.every((a) => !!a.asDataType) &&
!!fn.retType.asDataType &&
fn.argTypes.every((a) => isDataType(a)) &&
isDataType(fn.retType) &&
!fn.typeParameters.hasParameters()
) {
const filteredImportedModules =
Expand Down
34 changes: 33 additions & 1 deletion src/program/Program.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { strictEqual, throws } from "node:assert"
import { deepEqual, strictEqual, throws } from "node:assert"
import { describe, it } from "node:test"
import { removeWhitespace } from "@helios-lang/codec-utils"
import { Program } from "./Program.js"
Expand Down Expand Up @@ -190,4 +190,36 @@ describe(Program.name, () => {
fn.mainFunc
fn.toIR({})
})

it("tuple type schema ok", () => {
const src = `testing m
func my_func() -> (Int, Int) {
(0, 0)
}
func main() -> Int {
0
}`

const program = new Program(src)

const fns = program.userFunctions["m"]

deepEqual(Object.keys(fns), ["my_func", "main"])

deepEqual(fns["my_func"].mainFunc.retType.asDataType?.toSchema(), {
kind: "tuple",
itemTypes: [
{
kind: "internal",
name: "Int"
},
{
kind: "internal",
name: "Int"
}
]
})
})
})
7 changes: 2 additions & 5 deletions src/program/RedeemerEntryPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { $, SourceMappedString } from "@helios-lang/ir"
import { None } from "@helios-lang/type-utils"
import { TAB, ToIRContext } from "../codegen/index.js"
import { GlobalScope } from "../scopes/index.js"
import { BoolType, DefaultTypeClass } from "../typecheck/index.js"
import { BoolType, isDataType } from "../typecheck/index.js"
import { EntryPointImpl } from "./EntryPoint.js"
import { ModuleCollection } from "./ModuleCollection.js"

Expand Down Expand Up @@ -58,10 +58,7 @@ export class RedeemerEntryPoint extends EntryPointImpl {
throw CompilerError.type(main.site, "expected 1 arg for main")
}

if (
argTypeNames[0] != "" &&
!new DefaultTypeClass().isImplementedBy(argTypes[0])
) {
if (argTypeNames[0] != "" && !isDataType(argTypes[0])) {
throw CompilerError.type(
main.site,
`illegal redeemer argument type in main: '${argTypes[0].toString()}`
Expand Down
2 changes: 1 addition & 1 deletion src/program/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.17.0-43"
export const VERSION = "0.17.0-44"
17 changes: 17 additions & 0 deletions src/typecheck/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,26 @@ export function TupleType$(itemTypes, isAllDataTypes = null) {
return isDataType(it)
})

/**
* @type {GenericTypeProps}
*/
const props = {
name: `(${itemTypes.map((it) => it.toString()).join(", ")})`,
path: `__helios__tuple[${itemTypes.map((it) => (it.asDataType ? it.asDataType.path : "__helios__func")).join("@")}]`,
genTypeSchema: (self) => {
if (isData) {
return {
kind: "tuple",
itemTypes: itemTypes.map((it) =>
expectSome(it.asDataType).toSchema()
)
}
} else {
throw new Error(
`TypeSchema not available for ${self.toString()}`
)
}
},
genInstanceMembers: (self) => {
const members = isData ? genCommonInstanceMembers(self) : {}

Expand Down

0 comments on commit 417a63a

Please sign in to comment.