Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions scad-to-manifold/cli.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=cli.d.ts.map
1 change: 1 addition & 0 deletions scad-to-manifold/cli.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions scad-to-manifold/cli.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scad-to-manifold/cli.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions scad-to-manifold/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Lexer, Parser, ErrorCollector, CodeFile } from 'openscad-parser';
import { OpenScadCompiler } from './compiler.js';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import fs from 'node:fs';
import path from 'node:path';

const argv = yargs(hideBin(process.argv))
.option('input', {
alias: 'i',
description: 'Input OpenSCAD file',
type: 'string',
demandOption: true
})
.option('output', {
alias: 'o',
description: 'Output JavaScript file',
type: 'string'
})
.help()
.argv;

const run = async () => {
const { input, output } = await argv;

if (!fs.existsSync(input)) {
console.error(`Error: Input file ${input} does not exist.`);
process.exit(1);
}

const scadCode = fs.readFileSync(input, 'utf-8');
const outPath = output || input.replace('.scad', '.js');

const codeFile = new CodeFile(path.basename(input), scadCode);
const errorCollector = new ErrorCollector();
const lexer = new Lexer(codeFile, errorCollector);
const tokens = lexer.scan();
const parser = new Parser(codeFile, tokens, errorCollector);
const ast = parser.parse();

if (errorCollector.hasErrors()) {
console.error("Errors encountered during parsing:");
errorCollector.errors.forEach(e => console.error(e.message));
process.exit(1);
}

const compiler = new OpenScadCompiler();
const result = compiler.compile(ast);

fs.writeFileSync(outPath, result);
console.log(`Success: Compiled ${input} to ${outPath}`);
console.log(`To run the output, make sure to install 'manifold-3d' and import 'main'.`);
};

run().catch(err => {
console.error("Execution failed:", err);
process.exit(1);
});
48 changes: 48 additions & 0 deletions scad-to-manifold/compiler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { type ASTVisitor, type ScadFile, type ModuleInstantiationStmt, type LiteralExpr, type VectorExpr, type BinaryOpExpr, type UnaryOpExpr, type AssignmentNode, type GroupingExpr, type TernaryExpr, type FunctionCallExpr, LookupExpr, type IfElseStatement, type BlockStmt, type ErrorNode, type NoopStmt, type RangeExpr, type ArrayLookupExpr, type ModuleDeclarationStmt, type FunctionDeclarationStmt, type MemberLookupExpr, type LetExpr, type AssertExpr, type EchoExpr, type LcIfExpr, type LcEachExpr, type LcForExpr, type LcForCExpr, type LcLetExpr, type AnonymousFunctionExpr, type UseStmt, type IncludeStmt } from 'openscad-parser';
export declare enum AssignmentNodeRole {
VARIABLE_DECLARATION = 0,
ARGUMENT_DECLARATION = 1,
ARGUMENT_ASSIGNMENT = 2
}
export declare class OpenScadCompiler implements ASTVisitor<string> {
private indentLevel;
private resultsStack;
private declarations;
constructor();
private indent;
private get currentResults();
compile(ast: ScadFile): string;
private getRuntimeHelpers;
visitScadFile(n: ScadFile): string;
visitModuleInstantiationStmt(n: ModuleInstantiationStmt): string;
visitAssignmentNode(n: AssignmentNode): string;
visitLiteralExpr(n: LiteralExpr<any>): string;
visitVectorExpr(n: VectorExpr): string;
visitLookupExpr(n: LookupExpr): string;
visitBinaryOpExpr(n: BinaryOpExpr): string;
visitUnaryOpExpr(n: UnaryOpExpr): string;
visitBlockStmt(n: BlockStmt): string;
visitIfElseStatement(n: IfElseStatement): string;
visitGroupingExpr(n: GroupingExpr): string;
visitFunctionCallExpr(n: FunctionCallExpr): string;
visitRangeExpr(n: RangeExpr): string;
visitArrayLookupExpr(n: ArrayLookupExpr): string;
visitModuleDeclarationStmt(n: ModuleDeclarationStmt): string;
visitFunctionDeclarationStmt(n: FunctionDeclarationStmt): string;
visitTernaryExpr(n: TernaryExpr): string;
visitMemberLookupExpr(n: MemberLookupExpr): string;
visitLetExpr(n: LetExpr): string;
visitAssertExpr(n: AssertExpr): string;
visitEchoExpr(n: EchoExpr): string;
visitLcIfExpr(n: LcIfExpr): string;
visitLcEachExpr(n: LcEachExpr): string;
visitLcForExpr(n: LcForExpr): string;
visitLcForCExpr(n: LcForCExpr): string;
visitLcLetExpr(n: LcLetExpr): string;
visitAnonymousFunctionExpr(n: AnonymousFunctionExpr): string;
visitUseStmt(n: UseStmt): string;
visitIncludeStmt(n: IncludeStmt): string;
visitNoopStmt(n: NoopStmt): string;
visitErrorNode(n: ErrorNode): string;
}
//# sourceMappingURL=compiler.d.ts.map
1 change: 1 addition & 0 deletions scad-to-manifold/compiler.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading