UNPKG

awv3

Version:
94 lines (83 loc) 3.29 kB
// lowlevel commands are those that map 1-to-1 to ClassCad constructions import * as THREE from 'three'; import Ccfuturef from '../ccfuturef'; import Ccref from '../ccref'; import * as ast from './ast'; function toAst(x) { if (x === null) return new ast.Pass(); switch (typeof x) { case 'undefined': case 'boolean': return new ast.NameConstant(x); case 'number': return new ast.Num(x); case 'string': return new ast.Str(x); } if (x instanceof Array) return new ast.List(x.map(toAst)); else if (x instanceof THREE.Vector3) return new ast.Vector(x.toArray().map(toAst)); else if (x instanceof Ccref) return RealToId(x.id); else if (x instanceof Ccfuturef) return x.command; else if (x instanceof ast.Ast) return x; let e = new TypeError('Unsupported value to convert to ClassCad type'); e.value = x; throw e; } export function Assign(target, value) { return new ast.Assign(toAst(target), toAst(value)); } export function Call(fn, ...args) { return new ast.Call(toAst(fn), args.map(toAst)); } export function Member(value, attr) { return new ast.Attribute(toAst(value), attr); } export function Return(value) { return new ast.Return(toAst(value)); } // return a Module AST node that can be directly executed // commands can be a mix of literal values, expression and statement AST nodes export function Sequence(...commands) { if (commands[0] instanceof ast.Module) return commands[0]; return new ast.Module( commands.map(toAst).map((command, i) => { if (command instanceof ast.Stmt) return command; else if (i === commands.length - 1) return new ast.Return(command); else return new ast.Expression(command); }), ); } let variableCounter = 0; export function Variable(basename = 't') { return new ast.NameVariable(basename + String((variableCounter++))); } function cc(name) { return (...args) => new ast.Call(new ast.Name(name), args.map(toAst)); } const interop = '_C.SketcherCloudInterop.'; const obj = '_O.OBJ_'; const cadh = 'CADH_'; export const AddPoint = cc(interop + 'AddPoint'), AddLine = cc(interop + 'AddLine'), AddArc = cc(interop + 'AddArc'), AddCircle = cc(interop + 'AddCircle'), AddConstraint = cc(interop + 'AddConstraint'), RemovePoint = cc(interop + 'RemovePoint'), RemoveLine = cc(interop + 'RemoveLine'), RemoveArc = cc(interop + 'RemoveArc'), RemoveCircle = cc(interop + 'RemoveCircle'), RemoveConstraint = cc(interop + 'RemoveConstraint'), UpdatePoint = cc(interop + 'UpdatePoint'), UpdateLine = cc(interop + 'UpdateLine'), UpdateArc = cc(interop + 'UpdateArc'), UpdateCircle = cc(interop + 'UpdateCircle'), UpdateDimensions = cc(interop + 'UpdateDimensions'), SketchAppendNewObj = cc(interop + 'SketchAppendNewObj'), ReplaceEntityInConstraints = cc(interop + 'ReplaceEntityInConstraints'), CopyObjects = cc(interop + 'CopyObjects'), GetOrigin = cc(interop + 'GetOrigin'), MoveObjects = cc(interop + 'MoveObjects'), SolveConstraints = cc(interop + 'SolveConstraints'), IdToReal = cc(cadh + 'IdToReal'), RealToId = cc(cadh + 'RealToId'), Recalc = cc(obj + 'Recalc');