Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 225 additions & 0 deletions example/wasm/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*--------------------------------------------------------------------------

ParseBox

The MIT License (MIT)

Copyright (c) 2024-2026 Haydn Paterson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

---------------------------------------------------------------------------*/

export function test(math: Math) {
const a = math.$add(1, 2)
const b = math.$sub(1, 2)
const c = math.$mul(1, 2)
const d = math.$div(1, 2)

const expr = math.$add( // (1 - 2) + (3 + ((4 * 5) / 6))
math.$sub(1, 2),
math.$add(3,
math.$div(
math.$mul(4, 5),
6
)
)
)
}

type Math = Wat<`(module
(func $add (param $x f32) (param $y f32) (result f32)
local.get $x
local.get $y
f32.add
)
(func $sub (param $x f32) (param $y f32) (result f32)
local.get $x
local.get $y
f32.sub
)
(func $mul (param $x f32) (param $y f32) (result f32)
local.get $x
local.get $y
f32.mul
)
(func $div (param $x f32) (param $y f32) (result f32)
local.get $x
local.get $y
f32.div
)
)`>

import { Static } from '@sinclair/parsebox'

// ---------------------------------------------------
// Wat: Parse
// ---------------------------------------------------
export type Wat<Code extends string,
Parsed extends unknown = Static.Parse<Module, Code>[0],
Ast extends TModule = Parsed extends TModule ? Parsed : TModule<[]>,
Exports extends Record<PropertyKey, unknown> = GetExports<Ast>
> = Exports

// ---------------------------------------------------
// Query: Exports
// ---------------------------------------------------
type GetExportsParams<Params extends TParam[], Result extends unknown[] = []> = (
Params extends [infer _ extends TParam, ...infer Right extends TParam[]]
? GetExportsParams<Right, [...Result, number]> // i32, i64, f32, f64
: Result
)
type GetExportsFunc<Func extends TFunc,
Params extends unknown[] = GetExportsParams<Func['params']>,
Return extends unknown = number, // single return type
Result extends Record<PropertyKey, unknown> = {
[K in Func['name']]: (...args: [...Params]) => Return
}
> = Result
type GetExportsQuery<Funcs extends TFunc[], Result extends Record<PropertyKey, unknown> = {}> = (
Funcs extends [infer Left extends TFunc, ...infer Right extends TFunc[]]
? GetExportsQuery<Right, Result & GetExportsFunc<Left>>
: {[K in keyof Result]: Result[K]}
)
type GetExports<Module extends TModule> = GetExportsQuery<Module['funcs']>

// ---------------------------------------------------
// NativeType
// ---------------------------------------------------
type NativeType = Static.Union<[
Static.Const<'i32'>,
Static.Const<'i64'>,
Static.Const<'f32'>,
Static.Const<'i64'>
]>
// ---------------------------------------------------
// Param
// ---------------------------------------------------
interface TParam<Name extends string = string, Type extends string = string> {
kind: 'Param'
name: Name
type: Type
}
interface ParamMapping extends Static.IMapping {
output: this['input'] extends ['(', 'param', infer Name extends string, infer Type extends string, ')']
? TParam<Name, Type>
: never
}
type Param = Static.Tuple<[
Static.Const<'('>,
Static.Const<'param'>,
Static.Ident,
NativeType,
Static.Const<')'>
], ParamMapping>
type Params = Static.Array<Param>
// ---------------------------------------------------
// Result
// ---------------------------------------------------
interface TResult<Type extends string = string> {
kind: 'Result'
type: Type
}
interface ResultMapping extends Static.IMapping {
output: this['input'] extends ['(', 'result', infer Type extends string, ')']
? TResult<Type>
: never
}
type Result = Static.Tuple<[
Static.Const<'('>,
Static.Const<'result'>,
NativeType,
Static.Const<')'>,
], ResultMapping>
// ---------------------------------------------------
// Instruction
// ---------------------------------------------------
type Oprand = Static.Union<[
Static.Ident,
Static.Number
]>
interface TInstruction<
Namespace extends string = string,
Operator extends string = string,
Operand extends string[] = string[]
> {
kind: 'Instruction'
namespace: Namespace
operator: Operator
operand: Operand
}
interface InstructionMapping extends Static.IMapping {
output: this['input'] extends [infer Namespace extends string, '.', infer Operator extends string, infer Operand extends string[]]
? TInstruction<Namespace, Operator, Operand>
: never
}
type Instruction = Static.Tuple<[
Static.Union<[NativeType, Static.Const<'local'>]>,
Static.Const<'.'>,
Static.Ident,
Static.Optional<Oprand>
], InstructionMapping>
type Instructions = Static.Array<Instruction>
// ---------------------------------------------------
// Func
// ---------------------------------------------------
interface TFunc<
Name extends string = string,
Params extends TParam[] = TParam[],
Result extends TResult = TResult,
Instructions extends TInstruction[] = TInstruction[]> {
kind: 'Func'
name: Name
params: Params
result: Result
instructions: Instructions
}
interface FuncMapping extends Static.IMapping {
output: this['input'] extends ['(', 'func', infer Name extends string, infer Params extends TParam[], infer Result extends TResult, infer Instructions extends TInstruction[], ')']
? TFunc<Name, Params, Result, Instructions>
: never
}
type Func = Static.Tuple<[
Static.Const<'('>,
Static.Const<'func'>,
Static.Ident,
Params,
Result,
Instructions,
Static.Const<')'>
], FuncMapping>
type Funcs = Static.Array<Func>
// ---------------------------------------------------
// Module
// ---------------------------------------------------
interface TModule<Funcs extends TFunc[] = TFunc[]> {
kind: 'Module'
funcs: Funcs
}
interface ModuleMapping extends Static.IMapping {
output: this['input'] extends ['(', 'module', infer Funcs extends TFunc[], ')']
? TModule<Funcs>
: never
}
type Module = Static.Tuple<[
Static.Const<'('>,
Static.Const<'module'>,
Funcs,
Static.Const<')'>
], ModuleMapping>
10 changes: 9 additions & 1 deletion src/build/common/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ function FromTuple(parser: Runtime.ITuple): string {
function FromUnion(parser: Runtime.IUnion): string {
return parser.parsers.map((parser) => `${FromParser(parser)}`).join(' | ')
}
function FromUnsignedInteger(parser: Runtime.IUnsignedInteger): string {
return `<UnsignedInteger>`
}
function FromUnsignedNumber(parser: Runtime.IUnsignedNumber): string {
return `<UnsignedNumber>`
}
function FromUntil_1(parser: Runtime.IUntil_1): string {
return `string`
}
Expand All @@ -79,15 +85,17 @@ function FromParser(parser: Runtime.IParser): string {
Runtime.IsArray(parser) ? FromArray(parser) :
Runtime.IsBigInt(parser) ? FromBigInt(parser) :
Runtime.IsConst(parser) ? FromConst(parser) :
Runtime.IsInteger(parser) ? FromInteger(parser) :
Runtime.IsIdent(parser) ? FromIdent(parser) :
Runtime.IsInteger(parser) ? FromInteger(parser) :
Runtime.IsNumber(parser) ? FromNumber(parser) :
Runtime.IsOptional(parser) ? FromOptional(parser) :
Runtime.IsRef(parser) ? FromRef(parser) :
Runtime.IsRest(parser) ? FromRest(parser) :
Runtime.IsString(parser) ? FromString(parser) :
Runtime.IsTuple(parser) ? FromTuple(parser) :
Runtime.IsUnion(parser) ? FromUnion(parser) :
Runtime.IsUnsignedInteger(parser) ? FromUnsignedInteger(parser) :
Runtime.IsUnsignedNumber(parser) ? FromUnsignedNumber(parser) :
Runtime.IsUntil_1(parser) ? FromUntil_1(parser) :
Runtime.IsUntil(parser) ? FromUntil(parser) :
Unreachable(parser)
Expand Down
22 changes: 15 additions & 7 deletions src/build/common/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ function InferIdent(parser: Runtime.IIdent) {
function InferInteger(parser: Runtime.IInteger) {
return `string`
}
function InferNumber(parser: Runtime.INumber) {
return `string`
}
function InferOptional(parser: Runtime.IParser) {
return `([${Infer(parser)}] | [])`
}
function InferUnion(parsers: Runtime.IParser[]): string {
return [...new Set(parsers.map((parser) => Infer(parser)))].join(' | ')
}
function InferString(parser: Runtime.IString) {
return `string`
}
Expand All @@ -62,12 +62,18 @@ function InferRef(parser: Runtime.IRef) {
function InferRest(parser: Runtime.IRest) {
return `string`
}
function InferNumber(parser: Runtime.INumber) {
return `string`
}
function InferTuple(parsers: Runtime.IParser[]): string {
return `[${parsers.map(() => 'unknown').join(', ')}]`
}
function InferUnion(parsers: Runtime.IParser[]): string {
return [...new Set(parsers.map((parser) => Infer(parser)))].join(' | ')
}
function InferUnsignedInteger(parser: Runtime.IUnsignedInteger) {
return `number`
}
function InferUnsignedNumber(parser: Runtime.IUnsignedNumber) {
return `number`
}
function InferUntil_1(parser: Runtime.IUntil_1) {
return `string`
}
Expand All @@ -79,15 +85,17 @@ export function Infer(parser: Runtime.IParser): string {
Runtime.IsArray(parser) ? InferArray(parser.parser) :
Runtime.IsBigInt(parser) ? InferBigInt(parser) :
Runtime.IsConst(parser) ? InferConst(parser) :
Runtime.IsInteger(parser) ? InferInteger(parser) :
Runtime.IsIdent(parser) ? InferIdent(parser) :
Runtime.IsInteger(parser) ? InferInteger(parser) :
Runtime.IsNumber(parser) ? InferNumber(parser) :
Runtime.IsOptional(parser) ? InferOptional(parser.parser) :
Runtime.IsRef(parser) ? InferRef(parser) :
Runtime.IsRest(parser) ? InferRest(parser) :
Runtime.IsString(parser) ? InferString(parser) :
Runtime.IsTuple(parser) ? InferTuple(parser.parsers) :
Runtime.IsUnion(parser) ? InferUnion(parser.parsers) :
Runtime.IsUnsignedInteger(parser) ? InferUnsignedInteger(parser) :
Runtime.IsUnsignedNumber(parser) ? InferUnsignedNumber(parser) :
Runtime.IsUntil_1(parser) ? InferUntil_1(parser) :
Runtime.IsUntil(parser) ? InferUntil(parser) :
Unreachable(parser)
Expand Down
14 changes: 14 additions & 0 deletions src/build/runtime/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ function FromUnion(name: string, parsers: Runtime.IParser[]): string {
return parsers.length === 0 ? '[]' : parsers.reduceRight((result, right) => `If(${FromParser(name, right)}, ([_0, input]) => [_0, input], () => ${result})`, '[]')
}
// ------------------------------------------------------------------
// UnsignedInteger
// ------------------------------------------------------------------
function FromUnsignedInteger(name: string): string {
return `Token.UnsignedInteger(input)`
}
// ------------------------------------------------------------------
// UnsignedNumber
// ------------------------------------------------------------------
function FromUnsignedNumber(name: string): string {
return `Token.UnsignedNumber(input)`
}
// ------------------------------------------------------------------
// Until_1
// ------------------------------------------------------------------
function FromUntil_1(name: string, end: string[]): string {
Expand Down Expand Up @@ -152,6 +164,8 @@ function FromParser(name: string, parser: Runtime.IParser): string {
Runtime.IsString(parser) ? FromString(name, parser.quotes) :
Runtime.IsTuple(parser) ? FromTuple(name, parser.parsers) :
Runtime.IsUnion(parser) ? FromUnion(name, parser.parsers) :
Runtime.IsUnsignedInteger(parser) ? FromUnsignedInteger(name) :
Runtime.IsUnsignedNumber(parser) ? FromUnsignedNumber(name) :
Runtime.IsUntil_1(parser) ? FromUntil_1(name, parser.end) :
Runtime.IsUntil(parser) ? FromUntil(name, parser.end) :
Unreachable(parser)
Expand Down
16 changes: 15 additions & 1 deletion src/build/static/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ function FromUnion(name: string, parsers: Runtime.IParser[]): string {
return parsers.length === 0 ? '[]' : `(${parsers.reduceRight((result, right) => `${FromParser(name, right)} extends [infer _0, infer Input extends string] ? [_0, Input] : ${result}`, '[]')})`
}
// ------------------------------------------------------------------
// UnsignedInteger
// ------------------------------------------------------------------
function FromUnsignedInteger(name: string): string {
return `Token.TUnsignedInteger<Input>`
}
// ------------------------------------------------------------------
// UnsignedNumber
// ------------------------------------------------------------------
function FromUnsignedNumber(name: string): string {
return `Token.TUnsignedNumber<Input>`
}
// ------------------------------------------------------------------
// Until_1
// ------------------------------------------------------------------
function FromUntil_1(name: string, end: string[]): string {
Expand All @@ -142,15 +154,17 @@ function FromParser(name: string, parser: Runtime.IParser): string {
Runtime.IsArray(parser) ? FromArray(name, parser.parser) :
Runtime.IsBigInt(parser) ? FromBigInt(name) :
Runtime.IsConst(parser) ? FromConst(name, parser.const) :
Runtime.IsInteger(parser) ? FromInteger(name) :
Runtime.IsIdent(parser) ? FromIdent(name) :
Runtime.IsInteger(parser) ? FromInteger(name) :
Runtime.IsNumber(parser) ? FromNumber(name) :
Runtime.IsOptional(parser) ? FromOptional(name, parser) :
Runtime.IsRef(parser) ? FromRef(name, parser.ref) :
Runtime.IsRest(parser) ? FromRest(name) :
Runtime.IsString(parser) ? FromString(name, parser.quotes) :
Runtime.IsTuple(parser) ? FromTuple(name, parser.parsers) :
Runtime.IsUnion(parser) ? FromUnion(name, parser.parsers) :
Runtime.IsUnsignedInteger(parser) ? FromUnsignedInteger(name) :
Runtime.IsUnsignedNumber(parser) ? FromUnsignedNumber(name) :
Runtime.IsUntil_1(parser) ? FromUntil_1(name, parser.end) :
Runtime.IsUntil(parser) ? FromUntil(name, parser.end) :
Unreachable(parser)
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export { Module } from './module.ts'
export { type INumber, IsNumber, Number } from './number.ts'
export { type IOptional, IsOptional, Optional } from './optional.ts'
export { Parse } from './parse.ts'
export { type IParser, type IProperties, type IMapping, As, Identity } from './parser.ts'
export { type IParser, type IProperties, type IMapping, Identity } from './parser.ts'
export { type IRef, Ref, IsRef, } from './ref.ts'
export { type IRest, Rest, IsRest, } from './rest.ts'
export { type IString, IsString, String } from './string.ts'
export { type ITuple, IsTuple, Tuple } from './tuple.ts'
export { type IUnion, IsUnion, Union } from './union.ts'
export { type IUnsignedInteger, IsUnsignedInteger, UnsignedInteger } from './unsigned_integer.ts'
export { type IUnsignedNumber, IsUnsignedNumber, UnsignedNumber } from './unsigned_number.ts'
export { type IUntil_1, IsUntil_1, Until_1 } from './until_1.ts'
export { type IUntil, IsUntil, Until } from './until.ts'
Loading
Loading