diff --git a/src/runtime/javascript/expr.ts b/src/runtime/javascript/expr.ts new file mode 100644 index 000000000..fcf3bc0f0 --- /dev/null +++ b/src/runtime/javascript/expr.ts @@ -0,0 +1,40 @@ +export class Type { + hypos!: Hypo[] + name!: string + exprs!: Expr[] +} + +export class Hypo { + bind_type!: boolean + var!: string + type!: Type +} + +export class Expr { + dummy!: string +} + +export class ExprAbs extends Expr { + +} +export class ExprApp extends Expr { + +} +export class ExprLit extends Expr { + +} +export class ExprMeta extends Expr { + +} +export class ExprFun extends Expr { + +} +export class ExprVar extends Expr { + +} +export class ExprTyped extends Expr { + +} +export class ExprImplArg extends Expr { + +} diff --git a/src/runtime/javascript/ffi.ts b/src/runtime/javascript/ffi.ts new file mode 100644 index 000000000..015390c8a --- /dev/null +++ b/src/runtime/javascript/ffi.ts @@ -0,0 +1,68 @@ +/* eslint-disable @typescript-eslint/naming-convention */ + +import ffi from 'ffi-napi' +import ref, { Pointer } from 'ref-napi' +import ref_struct from 'ref-struct-di' +const Struct = ref_struct(ref) + +export const PgfExpr = ref.refType(ref.types.void) +export const PgfLiteral = ref.refType(ref.types.void) + +const PgfUnmarshallerVtbl = Struct({ + eabs: ref.refType(ref.types.void), + eapp: ref.refType(ref.types.void), + elit: ref.refType(ref.types.void), + emeta: ref.refType(ref.types.void), + efun: ref.refType(ref.types.void), + evar: ref.refType(ref.types.void), + etyped: ref.refType(ref.types.void), + eimplarg: ref.refType(ref.types.void), + lint: ref.refType(ref.types.void), + lflt: ref.refType(ref.types.void), + lstr: ref.refType(ref.types.void), + dtyp: ref.refType(ref.types.void), + free_ref: ref.refType(ref.types.void) +}) +export const PgfUnmarshaller = Struct({ + vtbl: ref.refType(PgfUnmarshallerVtbl) +}) + +const elit = ffi.Callback(PgfExpr, [PgfUnmarshaller, PgfLiteral], + function (self, lit: Pointer): any { // eslint-disable-line @typescript-eslint/no-unused-vars + console.log('myelit') + console.log(' lit', lit) + return lit.deref() + }) + +const lint = ffi.Callback(PgfLiteral, [PgfUnmarshaller, ref.types.size_t, ref.refType(ref.types.uint)], + function (self, size: number, val: Pointer): any { // eslint-disable-line @typescript-eslint/no-unused-vars + console.log('mylint') + console.log(' size', size) + console.log(' val', val.deref()) + // const x: number = val.deref() + return val + }) + +const free_ref = ffi.Callback(ref.types.void, [PgfUnmarshaller, ref.refType(ref.types.void)], + function (self, x: any): void { // eslint-disable-line @typescript-eslint/no-unused-vars + console.log('free_ref') + }) + +const vtbl = new PgfUnmarshallerVtbl({ + eabs: ref.alloc('void').ref(), + eapp: ref.alloc('void').ref(), + elit: elit as any, + emeta: ref.alloc('void').ref(), + efun: ref.alloc('void').ref(), + evar: ref.alloc('void').ref(), + etyped: ref.alloc('void').ref(), + eimplarg: ref.alloc('void').ref(), + lint: lint as any, + lflt: ref.alloc('void').ref(), + lstr: ref.alloc('void').ref(), + dtyp: ref.alloc('void').ref(), + free_ref: free_ref as any +}) + +export const unmarshaller = new PgfUnmarshaller({ vtbl: vtbl.ref() }) +export const marshaller = {} diff --git a/src/runtime/javascript/index.ts b/src/runtime/javascript/index.ts index a7f192249..8b2079436 100644 --- a/src/runtime/javascript/index.ts +++ b/src/runtime/javascript/index.ts @@ -1,6 +1,9 @@ /* eslint-disable @typescript-eslint/naming-convention */ import errno from './errno' +import { Expr } from './expr' +import { unmarshaller, PgfUnmarshaller, PgfExpr, PgfLiteral } from './ffi' + import ffi from 'ffi-napi' import ref, { Pointer } from 'ref-napi' import ref_struct, { StructObject } from 'ref-struct-di' @@ -40,13 +43,13 @@ const PgfType = ref.types.void // TODO const PgfTypeHypo = ref.types.void // TODO const PgfTypeHypoPtr = ref.refType(PgfTypeHypo) -const PgfExpr = ref.types.void // TODO -const PgfLiteral = ref.types.void // TODO +// const PgfExpr = ref.types.void // TODO +// const PgfLiteral = ref.types.void // TODO const PgfPrintContext = ref.types.void // TODO const PgfPrintContextPtr = ref.refType(PgfPrintContext) -const PgfUnmarshaller = ref.types.void // TODO +// const PgfUnmarshaller = ref.types.void // TODO const PgfUnmarshallerPtr = ref.refType(PgfUnmarshaller) const PgfMarshaller = ref.types.void // TODO @@ -289,6 +292,14 @@ function newNGF (abstract_name: string, path?: string): PGFGrammar { return new PGFGrammar(db, rev) } +function readExpr (str: string): Expr { + const txt = PgfText_FromString(str) + const expr = runtime.pgf_read_expr(txt, unmarshaller.ref()) + console.log('expr', expr) + + return new Expr() +} + // ---------------------------------------------------------------------------- // Exposed library API @@ -297,5 +308,7 @@ export default { readPGF, bootNGF, readNGF, - newNGF + newNGF, + + readExpr }