1
0
forked from GitHub/gf-core

JavaScript unmarshalling WIP

This commit is contained in:
John J. Camilleri
2021-10-05 15:33:19 +02:00
parent b7e7319542
commit 1b3a197aac
3 changed files with 125 additions and 4 deletions

View File

@@ -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 {
}

View File

@@ -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>): 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<number>): 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 = {}

View File

@@ -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
}