Get marshalling of integers working

This commit is contained in:
John J. Camilleri
2021-10-07 12:54:02 +02:00
parent e33d881ce8
commit db66144c25
6 changed files with 82 additions and 31 deletions

View File

@@ -11,7 +11,16 @@ export class Hypo {
}
export class Expr {
dummy!: string
constructor (n?: number) {
if (n != null) {
return new ExprLit(n)
}
}
toString (): string {
// TODO call showExpr
return this.toString()
}
}
export class ExprAbs extends Expr {
@@ -21,7 +30,16 @@ export class ExprApp extends Expr {
}
export class ExprLit extends Expr {
lit: Literal
constructor (l: Literal | number | string) {
super()
if (l instanceof Literal) this.lit = l
else this.lit = new Literal(l)
}
toString (): string {
return this.lit.toString()
}
}
export class ExprMeta extends Expr {
@@ -38,3 +56,14 @@ export class ExprTyped extends Expr {
export class ExprImplArg extends Expr {
}
export class Literal {
val: number | string
constructor (v: number | string) {
this.val = v
}
toString (): string {
return this.val.toString()
}
}

View File

@@ -1,12 +1,13 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { ExprLit, Literal } from './expr'
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)
export const PgfExpr = ref.refType(ref.types.Object)
export const PgfLiteral = ref.refType(ref.types.Object)
const PgfUnmarshallerVtbl = Struct({
eabs: ref.refType(ref.types.void),
@@ -28,24 +29,25 @@ export const PgfUnmarshaller = Struct({
})
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()
function (self, lit: Pointer<any>): Pointer<ExprLit> {
const litObj = lit.deref() as Literal
const obj = new ExprLit(litObj)
const buf = ref.alloc(ref.types.Object) as Pointer<ExprLit>
ref.writeObject(buf, 0, obj)
return buf
})
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
function (self, size: number, val: Pointer<number>): Pointer<Literal> {
const obj = new Literal(val.deref())
const buf = ref.alloc(ref.types.Object) as Pointer<Literal>
ref.writeObject(buf, 0, obj)
return buf
})
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')
function (self, x: any): void {
// console.log('free_ref')
})
const vtbl = new PgfUnmarshallerVtbl({

View File

@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import errno from './errno'
import { Expr } from './expr'
import { Expr, ExprLit } from './expr'
import { unmarshaller, PgfUnmarshaller, PgfExpr, PgfLiteral } from './ffi'
import ffi from 'ffi-napi'
@@ -185,7 +185,7 @@ export class PGFGrammar {
getCategories (): string[] {
const cats: string[] = []
const callback = ffi.Callback(ref.types.void, [PgfItorPtr, PgfTextPtr, voidPtr, PgfExnPtr],
function (self: Pointer<any>, key: Pointer<any>, value: Pointer<any>, err: Pointer<any>) { // eslint-disable-line @typescript-eslint/no-unused-vars
function (self: Pointer<any>, key: Pointer<any>, value: Pointer<any>, err: Pointer<any>) {
const k = PgfText_AsString(key)
cats.push(k)
})
@@ -198,7 +198,7 @@ export class PGFGrammar {
getFunctions (): string[] {
const funs: string[] = []
const callback = ffi.Callback(ref.types.void, [PgfItorPtr, PgfTextPtr, voidPtr, PgfExnPtr],
function (self: Pointer<any>, key: Pointer<any>, value: Pointer<any>, err: Pointer<any>) { // eslint-disable-line @typescript-eslint/no-unused-vars
function (self: Pointer<any>, key: Pointer<any>, value: Pointer<any>, err: Pointer<any>) {
const k = PgfText_AsString(key)
funs.push(k)
})
@@ -244,7 +244,7 @@ export class PGFGrammar {
const catname = PgfText_FromString(cat)
const funs: string[] = []
const callback = ffi.Callback(ref.types.void, [PgfItorPtr, PgfTextPtr, voidPtr, PgfExnPtr],
function (self: Pointer<any>, key: Pointer<any>, value: Pointer<any>, err: Pointer<any>) { // eslint-disable-line @typescript-eslint/no-unused-vars
function (self: Pointer<any>, key: Pointer<any>, value: Pointer<any>, err: Pointer<any>) {
const k = PgfText_AsString(key)
funs.push(k)
})
@@ -295,9 +295,7 @@ function newNGF (abstract_name: string, path?: string): PGFGrammar {
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()
return ref.readObject(expr) as Expr
}
// ----------------------------------------------------------------------------
@@ -310,5 +308,8 @@ export default {
readNGF,
newNGF,
readExpr
readExpr,
Expr,
ExprLit
}

View File

@@ -206,3 +206,29 @@ describe('abstract syntax', () => {
})
})
})
// ----------------------------------------------------------------------------
describe('expressions', () => {
test('small integer', () => {
const e1 = PGF.readExpr('123')
const e2 = new PGF.ExprLit(123)
const e3 = new PGF.ExprLit(456)
expect(e1).toEqual(e2)
expect(e1).not.toEqual(e3)
})
test.skip('negative integer', () => {
const e1 = PGF.readExpr('-123')
const e2 = new PGF.ExprLit(-123)
const e3 = new PGF.ExprLit(-456)
expect(e1).toEqual(e2)
expect(e1).not.toEqual(e3)
})
// test.only('big integer', () => {
// const e1 = PGF.readExpr('774763251095801167872')
// const e2 = new PGF.ExprLit(BigInt(774763251095801167872))
// expect(e1).toEqual(e2)
// })
})

View File

@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "ES2015",
"module": "commonjs",
"rootDir": "./",
"moduleResolution": "node",

View File

@@ -1,7 +0,0 @@
# Project moved
The GF TypeScript runtime has been moved to the repository:
<https://github.com/GrammaticalFramework/gf-typescript>
If you are looking for an updated version of the JavaScript runtime,
you should also look there.