Files
str/src/vm/commands/infile.ts

30 lines
964 B
TypeScript
Raw Normal View History

import {Command, ParseContext, processPath, StrTerm, wrapString} from "./command.js";
2025-11-10 23:54:20 -06:00
import {LexInput} from "../lexer.js";
import {StrVM} from "../vm.js";
import {Awaitable} from "../../util/types.js";
import fs from "node:fs/promises";
2025-11-10 23:54:20 -06:00
export class InFile extends Command<{ path: StrTerm }> {
isParseCandidate(token: LexInput): boolean {
return this.isKeyword(token, 'infile')
}
async attemptParse(context: ParseContext): Promise<{ path: StrTerm }> {
return { path: await context.popTerm() }
2025-11-10 23:54:20 -06:00
}
getDisplayName(): string {
return 'infile'
}
execute(vm: StrVM, data: { path: StrTerm }): Awaitable<StrVM> {
return vm.replaceContextMatchingTerm(ctx => ({
override: async () => {
const path = processPath(ctx.resolveString(data.path))
const content = await fs.readFile(path, 'utf8')
return wrapString(content)
}
}))
}
2025-11-10 23:54:20 -06:00
}