Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --language option to snippet command #227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { readTablet } from './commands/read';
import { transliterateAssembly } from './commands/transliterate';
import { trimCache } from './commands/trim-cache';
import { TranslateResult, translateTypeScript, RosettaDiagnostic } from './index';
import { TargetLanguage } from './languages';
import { PythonVisitor } from './languages/python';
import { TARGET_LANGUAGES, TargetLanguage } from './languages';
import { VisualizeAstVisitor } from './languages/visualize';
import * as logging from './logging';
import { emitSupportPolicyInformation } from './support';
Expand All @@ -40,10 +39,14 @@ async function main() {
type: 'string',
describe: 'The file to translate (leave out for stdin)',
})
.option('language', {
type: 'string',
describe: 'Language ID to transliterate to',
})
.option('python', {
alias: 'p',
boolean: true,
description: 'Translate snippets to Python',
description: 'Translate snippets to Python. This will override --language option.',
}),
wrapHandler(async (args) => {
const result = translateTypeScript(await makeFileSource(args.FILE ?? '-', 'stdin.ts'), makeVisitor(args));
Expand Down Expand Up @@ -465,9 +468,17 @@ function wrapHandler<A extends { verbose?: number }, R>(handler: (x: A) => Promi
};
}

function makeVisitor(args: { python?: boolean }) {
function makeVisitor(args: { python?: boolean; language?: string }) {
if (args.python) {
return new PythonVisitor();
args.language = 'python';
}
if (args.language !== undefined) {
const lang = args.language.toUpperCase();
const language = Object.entries(TargetLanguage).find(([k]) => k === lang)?.[1];
if (language === undefined) {
throw new Error(`Unknown target language: ${lang}. Expected one of ${Object.keys(TargetLanguage).join(', ')}`);
}
return TARGET_LANGUAGES[language].createVisitor();
}
// Default to visualizing AST, including nodes we don't recognize yet
return new VisualizeAstVisitor();
Expand Down