Skip to content

Commit b509c83

Browse files
committed
refactor: semantically port TS to AffineScript
1 parent ccb3b04 commit b509c83

3 files changed

Lines changed: 56 additions & 68 deletions

File tree

bindings/deno/bunsenite.affine

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
// SPDX-License-Identifier: MPL-2.0
2-
// Ported via Harvard Engine mechanical processor
2+
// Ported via Harvard Engine (Semantic pass)
33

44
module bunsenite;
55

6-
// TODO: Complete semantic implementation
7-
8-
/* === ORIGINAL TYPESCRIPT CONTEXT ===
96
// SPDX-License-Identifier: MPL-2.0
107
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
118
// Bunsenite Deno FFI Bindings
@@ -16,18 +13,18 @@ module bunsenite;
1613
//
1714
// Usage:
1815
// import { parseNickel, validateNickel } from "./bunsenite.ts";
19-
// const result = parseNickel('{ foo = 42 }', "config.ncl");
16+
// let result = parseNickel('{ foo = 42 }', "config.ncl");
2017
// console.log(result);
2118

2219
// Detect library path based on platform
23-
function getLibraryPath(): string {
24-
const platform = Deno.build.os;
25-
const libName = platform === "windows" ? "bunsenite.dll"
20+
fn getLibraryPath(): string {
21+
let platform = Deno.build.os;
22+
let libName = platform === "windows" ? "bunsenite.dll"
2623
: platform === "darwin" ? "libbunsenite.dylib"
2724
: "libbunsenite.so";
2825

2926
// Try common locations
30-
const paths = [
27+
let paths = [
3128
`../../target/release/${libName}`,
3229
`./target/release/${libName}`,
3330
`./${libName}`,
@@ -49,7 +46,7 @@ function getLibraryPath(): string {
4946

5047
// FFI symbol definitions
5148
// These match the C ABI exported by the Zig layer
52-
const symbols = {
49+
let symbols = {
5350
// Parse Nickel string to JSON
5451
// char* parse_nickel(const char* source, const char* name)
5552
parse_nickel: {
@@ -96,27 +93,27 @@ const symbols = {
9693
// Load the native library
9794
let lib: Deno.DynamicLibrary<typeof symbols> | null = null;
9895

99-
function getLib(): Deno.DynamicLibrary<typeof symbols> {
96+
fn getLib(): Deno.DynamicLibrary<typeof symbols> {
10097
if (!lib) {
101-
const libPath = getLibraryPath();
98+
let libPath = getLibraryPath();
10299
lib = Deno.dlopen(libPath, symbols);
103100
}
104101
return lib;
105102
}
106103

107104
// Helper: Convert JS string to C string (null-terminated)
108-
function toCString(str: string): Uint8Array {
109-
const encoder = new TextEncoder();
110-
const encoded = encoder.encode(str + "\0");
105+
fn toCString(str: string): Uint8Array {
106+
let encoder = new TextEncoder();
107+
let encoded = encoder.encode(str + "\0");
111108
return encoded;
112109
}
113110

114111
// Helper: Convert C string pointer to JS string
115-
function fromCString(ptr: Deno.UnsafePointer): string {
112+
fn fromCString(ptr: Deno.UnsafePointer): string {
116113
if (!ptr) {
117114
throw new Error("Null pointer received from C");
118115
}
119-
const view = new Deno.UnsafePointerView(ptr);
116+
let view = new Deno.UnsafePointerView(ptr);
120117
return view.getCString();
121118
}
122119

@@ -130,17 +127,17 @@ function fromCString(ptr: Deno.UnsafePointer): string {
130127
*
131128
* @example
132129
* ```typescript
133-
* const config = parseNickel('{ name = "example", port = 8080 }', "config.ncl");
130+
* let config = parseNickel('{ name = "example", port = 8080 }', "config.ncl");
134131
* console.log(config.port); // 8080
135132
* ```
136133
*/
137-
export function parseNickel(source: string, name: string): unknown {
138-
const library = getLib();
134+
fn parseNickel(source: string, name: string): unknown {
135+
let library = getLib();
139136

140-
const sourceBytes = toCString(source);
141-
const nameBytes = toCString(name);
137+
let sourceBytes = toCString(source);
138+
let nameBytes = toCString(name);
142139

143-
const resultPtr = library.symbols.parse_nickel(
140+
let resultPtr = library.symbols.parse_nickel(
144141
sourceBytes,
145142
nameBytes,
146143
) as Deno.UnsafePointer;
@@ -150,7 +147,7 @@ export function parseNickel(source: string, name: string): unknown {
150147
}
151148

152149
try {
153-
const jsonString = fromCString(resultPtr);
150+
let jsonString = fromCString(resultPtr);
154151
return JSON.parse(jsonString);
155152
} finally {
156153
// Free the string allocated by Rust
@@ -176,13 +173,13 @@ export function parseNickel(source: string, name: string): unknown {
176173
* }
177174
* ```
178175
*/
179-
export function validateNickel(source: string, name: string): boolean {
180-
const library = getLib();
176+
fn validateNickel(source: string, name: string): boolean {
177+
let library = getLib();
181178

182-
const sourceBytes = toCString(source);
183-
const nameBytes = toCString(name);
179+
let sourceBytes = toCString(source);
180+
let nameBytes = toCString(name);
184181

185-
const result = library.symbols.validate_nickel(
182+
let result = library.symbols.validate_nickel(
186183
sourceBytes,
187184
nameBytes,
188185
);
@@ -204,9 +201,9 @@ export function validateNickel(source: string, name: string): boolean {
204201
* console.log("Bunsenite version:", getVersion());
205202
* ```
206203
*/
207-
export function getVersion(): string {
208-
const library = getLib();
209-
const ptr = library.symbols.version() as Deno.UnsafePointer;
204+
fn getVersion(): string {
205+
let library = getLib();
206+
let ptr = library.symbols.version() as Deno.UnsafePointer;
210207
return fromCString(ptr);
211208
}
212209

@@ -220,9 +217,9 @@ export function getVersion(): string {
220217
* console.log("RSR tier:", getRSRTier());
221218
* ```
222219
*/
223-
export function getRSRTier(): string {
224-
const library = getLib();
225-
const ptr = library.symbols.rsr_tier() as Deno.UnsafePointer;
220+
fn getRSRTier(): string {
221+
let library = getLib();
222+
let ptr = library.symbols.rsr_tier() as Deno.UnsafePointer;
226223
return fromCString(ptr);
227224
}
228225

@@ -236,8 +233,8 @@ export function getRSRTier(): string {
236233
* console.log("TPCF perimeter:", getTPCFPerimeter());
237234
* ```
238235
*/
239-
export function getTPCFPerimeter(): number {
240-
const library = getLib();
236+
fn getTPCFPerimeter(): number {
237+
let library = getLib();
241238
return library.symbols.tpcf_perimeter();
242239
}
243240

@@ -250,12 +247,12 @@ export function getTPCFPerimeter(): number {
250247
*
251248
* @example
252249
* ```typescript
253-
* const config = await parseFile("./config.ncl");
250+
* let config = await parseFile("./config.ncl");
254251
* console.log(config);
255252
* ```
256253
*/
257-
export async function parseFile(path: string): Promise<unknown> {
258-
const source = await Deno.readTextFile(path);
254+
async fn parseFile(path: string): unknown {
255+
let source = await Deno.readTextFile(path);
259256
return parseNickel(source, path);
260257
}
261258

@@ -276,8 +273,8 @@ export async function parseFile(path: string): Promise<unknown> {
276273
* }
277274
* ```
278275
*/
279-
export async function validateFile(path: string): Promise<boolean> {
280-
const source = await Deno.readTextFile(path);
276+
async fn validateFile(path: string): boolean {
277+
let source = await Deno.readTextFile(path);
281278
return validateNickel(source, path);
282279
}
283280

@@ -290,10 +287,10 @@ globalThis.addEventListener("unload", () => {
290287
});
291288

292289
// Export type definitions
293-
export type BunseniteConfig = Record<string, unknown>;
290+
struct BunseniteConfig { Record<string, unknown>;
294291

295-
// Re-export for convenience
296-
export default {
292+
// Re-for convenience
293+
default {
297294
parseNickel,
298295
validateNickel,
299296
parseFile,
@@ -303,4 +300,3 @@ export default {
303300
getTPCFPerimeter,
304301
};
305302

306-
==================================== */

bindings/deno/example.affine

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
// SPDX-License-Identifier: MPL-2.0
2-
// Ported via Harvard Engine mechanical processor
2+
// Ported via Harvard Engine (Semantic pass)
33

44
module example;
55

6-
// TODO: Complete semantic implementation
7-
8-
/* === ORIGINAL TYPESCRIPT CONTEXT ===
96
// SPDX-License-Identifier: MPL-2.0
107
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
118
#!/usr/bin/env deno run --allow-ffi --allow-read
@@ -33,7 +30,7 @@ console.log("");
3330

3431
// Example 1: Parse simple inline config
3532
console.log("Example 1: Parse inline config");
36-
const config1 = parseNickel(
33+
let config1 = parseNickel(
3734
`{
3835
name = "deno-example",
3936
version = "1.0.0",
@@ -46,7 +43,7 @@ console.log("");
4643

4744
// Example 2: Parse with computations
4845
console.log("Example 2: Parse with computations");
49-
const config2 = parseNickel(
46+
let config2 = parseNickel(
5047
`{
5148
base_port = 8000,
5249
api_port = base_port + 80,
@@ -81,11 +78,11 @@ console.log("");
8178
// Example 5: Parse file (if it exists)
8279
console.log("Example 5: Parse file");
8380
try {
84-
const config = await parseFile("../../examples/config.ncl");
81+
let config = await parseFile("../../examples/config.ncl");
8582
console.log("Parsed config from file:");
86-
console.log(` Name: ${(config as any).name}`);
87-
console.log(` Version: ${(config as any).version}`);
88-
console.log(` Server port: ${(config as any).server.port}`);
83+
console.log(` Name: ${(config as unknown).name}`);
84+
console.log(` Version: ${(config as unknown).version}`);
85+
console.log(` Server port: ${(config as unknown).server.port}`);
8986
} catch (e) {
9087
console.log(`Could not parse file: ${e.message}`);
9188
console.log("(This is expected if bunsenite hasn't been built yet)");
@@ -94,7 +91,7 @@ console.log("");
9491

9592
// Example 6: Advanced features
9693
console.log("Example 6: Advanced features");
97-
const config6 = parseNickel(
94+
let config6 = parseNickel(
9895
`{
9996
# Comments work!
10097
app_name = "bunsenite",
@@ -118,4 +115,3 @@ console.log("Advanced config:", JSON.stringify(config6, null, 2));
118115

119116
console.log("\n✓ All examples completed successfully!");
120117

121-
==================================== */
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
// SPDX-License-Identifier: MPL-2.0
2-
// Ported via Harvard Engine mechanical processor
2+
// Ported via Harvard Engine (Semantic pass)
33

44
module bunsenite.d;
55

6-
// TODO: Complete semantic implementation
7-
8-
/* === ORIGINAL TYPESCRIPT CONTEXT ===
96
// SPDX-License-Identifier: MPL-2.0
107
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
118
// TypeScript type definitions for bunsenite
@@ -17,32 +14,31 @@ module bunsenite.d;
1714
* @param name - The name of the file (for error messages)
1815
* @returns The parsed configuration as a JSON string, or null on error
1916
*/
20-
export function parse_nickel(source: string, name: string): string | null;
17+
fn parse_nickel(source: string, name: string): string | null;
2118

2219
/**
2320
* Validate a Nickel configuration without evaluating it
2421
* @param source - The Nickel source code to validate
2522
* @param name - The name of the file (for error messages)
2623
* @returns 0 if valid, non-zero on error
2724
*/
28-
export function validate_nickel(source: string, name: string): number;
25+
fn validate_nickel(source: string, name: string): number;
2926

3027
/**
3128
* Get the library version
3229
* @returns The version string (e.g., "1.0.0")
3330
*/
34-
export function version(): string;
31+
fn version(): string;
3532

3633
/**
3734
* Get the RSR compliance tier
3835
* @returns The RSR tier (e.g., "bronze")
3936
*/
40-
export function rsr_tier(): string;
37+
fn rsr_tier(): string;
4138

4239
/**
4340
* Get the TPCF perimeter assignment
4441
* @returns The perimeter number (e.g., 3)
4542
*/
46-
export function tpcf_perimeter(): number;
43+
fn tpcf_perimeter(): number;
4744

48-
==================================== */

0 commit comments

Comments
 (0)