From 317881b75c8aec1efb0f147b83b0c1204b3d5cf7 Mon Sep 17 00:00:00 2001 From: Chris Martens Date: Wed, 7 Aug 2024 19:26:32 -0400 Subject: [PATCH 1/4] term to string function --- src/client.ts | 2 +- src/termoutput.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 8c86202..bc82ae3 100644 --- a/src/client.ts +++ b/src/client.ts @@ -32,7 +32,7 @@ import { termToData, } from './termoutput.js'; export type { Term, Fact, InputTerm, InputFact, JsonData } from './termoutput.js'; -export { dataToTerm, termToData } from './termoutput.js'; +export { dataToTerm, termToData, termToString } from './termoutput.js'; export type { Issue, Stats }; export type { SourcePosition, SourceLocation } from './parsing/source-location.js'; diff --git a/src/termoutput.ts b/src/termoutput.ts index e284737..d00c8b3 100644 --- a/src/termoutput.ts +++ b/src/termoutput.ts @@ -51,3 +51,22 @@ export function termToData(tm: InputTerm): Data { } return hide({ type: 'int', value: BigInt(tm) }); } + +export function termToString(t: Term) { + let s = "" + if (t.name) { + s += t.name + } + if (t.args) { + s = "(" + s + for(const t2 of t.args) + s += " " + termToString(t2) + s += ")" + } + if(!t.name && !t.args) { + // it's a base type like int or string that js already knows how to print + return t + } + return s +} + From 5257a1ada7d9af99b3e10302e6cd1a33b72aa3b4 Mon Sep 17 00:00:00 2001 From: Chris Martens Date: Wed, 7 Aug 2024 19:38:27 -0400 Subject: [PATCH 2/4] prettier --- src/termoutput.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/termoutput.ts b/src/termoutput.ts index d00c8b3..d961a6c 100644 --- a/src/termoutput.ts +++ b/src/termoutput.ts @@ -53,20 +53,18 @@ export function termToData(tm: InputTerm): Data { } export function termToString(t: Term) { - let s = "" + let s = ''; if (t.name) { - s += t.name + s += t.name; } if (t.args) { - s = "(" + s - for(const t2 of t.args) - s += " " + termToString(t2) - s += ")" + s = '(' + s; + for (const t2 of t.args) s += ' ' + termToString(t2); + s += ')'; } - if(!t.name && !t.args) { + if (!t.name && !t.args) { // it's a base type like int or string that js already knows how to print - return t + return t; } - return s + return s; } - From b12c6ec7243501ea33c7c91fcf609034234253c3 Mon Sep 17 00:00:00 2001 From: Rob Simmons Date: Thu, 8 Aug 2024 22:34:35 -0400 Subject: [PATCH 3/4] Revise term output function --- src/termoutput.test.ts | 16 ++++++++++++++++ src/termoutput.ts | 27 +++++++++++---------------- 2 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 src/termoutput.test.ts diff --git a/src/termoutput.test.ts b/src/termoutput.test.ts new file mode 100644 index 0000000..b7b7b50 --- /dev/null +++ b/src/termoutput.test.ts @@ -0,0 +1,16 @@ +import { test, expect } from 'vitest'; +import { dataToTerm, termToData, termToString } from './termoutput.js'; + +test('termToString', () => { + expect(termToString(dataToTerm(termToData(null)))).toBe('()'); + expect(termToString(dataToTerm(termToData(-12)))).toBe('-12'); + expect(termToString(dataToTerm(termToData(12n)))).toBe('12'); + expect(termToString(dataToTerm(termToData(false)))).toBe('bool#false'); + expect(termToString(dataToTerm(termToData('hello world')))).toBe('"hello world"'); + expect( + termToString( + dataToTerm(termToData({ name: 's', args: [{ name: 's', args: [{ name: 'z' }] }] })), + ), + ).toBe('s (s z)'); + expect(termToString(dataToTerm(termToData({ name: 'const', args: [] })))).toBe('const'); +}); diff --git a/src/termoutput.ts b/src/termoutput.ts index d961a6c..57ab32f 100644 --- a/src/termoutput.ts +++ b/src/termoutput.ts @@ -1,4 +1,4 @@ -import { Data, TRIVIAL, expose, hide } from './datastructures/data.js'; +import { Data, TRIVIAL, escapeString, expose, hide } from './datastructures/data.js'; export type Term = | null // Trivial type () @@ -52,19 +52,14 @@ export function termToData(tm: InputTerm): Data { return hide({ type: 'int', value: BigInt(tm) }); } -export function termToString(t: Term) { - let s = ''; - if (t.name) { - s += t.name; - } - if (t.args) { - s = '(' + s; - for (const t2 of t.args) s += ' ' + termToString(t2); - s += ')'; - } - if (!t.name && !t.args) { - // it's a base type like int or string that js already knows how to print - return t; - } - return s; +export function termToString(tm: Term, parens = false): String { + if (tm === null) return '()'; + if (typeof tm === 'boolean') return `bool#${tm}`; + if (typeof tm === 'string') return `"${escapeString(tm)}"`; + if (typeof tm === 'bigint') return `${tm}`; + if (tm.name === null) return `ref#${tm.value}`; + if (!tm.args) return tm.name; + const tmStr = `${tm.name} ${tm.args.map((arg) => termToString(arg, true)).join('')}`; + if (!parens) return tmStr; + return `(${tmStr})`; } From b47040775fe4e98de274aaa69b9c646e232ac65b Mon Sep 17 00:00:00 2001 From: Rob Simmons Date: Thu, 8 Aug 2024 22:38:47 -0400 Subject: [PATCH 4/4] Make eslint happy --- src/termoutput.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/termoutput.ts b/src/termoutput.ts index 57ab32f..1173fe9 100644 --- a/src/termoutput.ts +++ b/src/termoutput.ts @@ -52,7 +52,7 @@ export function termToData(tm: InputTerm): Data { return hide({ type: 'int', value: BigInt(tm) }); } -export function termToString(tm: Term, parens = false): String { +export function termToString(tm: Term, parens = false): string { if (tm === null) return '()'; if (typeof tm === 'boolean') return `bool#${tm}`; if (typeof tm === 'string') return `"${escapeString(tm)}"`;