Skip to content
Snippets Groups Projects
Commit 693a3f5f authored by David Sehnal's avatar David Sehnal
Browse files

wip mol-script, execute parsed/transpiled query

parent 6e17f5bb
No related branches found
No related tags found
No related merge requests found
...@@ -8,12 +8,6 @@ import Expression from './expression' ...@@ -8,12 +8,6 @@ import Expression from './expression'
const { isLiteral, isSymbol, isArgumentsArray } = Expression; const { isLiteral, isSymbol, isArgumentsArray } = Expression;
export default function format(e: Expression) {
const writer = new Writer();
_format(e, writer);
return writer.getStr();
}
class Writer { class Writer {
private value: string[] = []; private value: string[] = [];
private currentLineLength = 0; private currentLineLength = 0;
...@@ -128,4 +122,10 @@ function _format(e: Expression, writer: Writer) { ...@@ -128,4 +122,10 @@ function _format(e: Expression, writer: Writer) {
_format(e.args[a], writer); _format(e.args[a], writer);
} }
writer.pop(); writer.pop();
} }
\ No newline at end of file
export function formatMolScript(e: Expression) {
const writer = new Writer();
_format(e, writer);
return writer.getStr();
}
...@@ -20,7 +20,7 @@ namespace Expression { ...@@ -20,7 +20,7 @@ namespace Expression {
export function isArgumentsArray(e?: Arguments): e is Expression[] { return !!e && Array.isArray(e); } export function isArgumentsArray(e?: Arguments): e is Expression[] { return !!e && Array.isArray(e); }
export function isArgumentsMap(e?: Arguments): e is { [name: string]: Expression } { return !!e && !Array.isArray(e); } export function isArgumentsMap(e?: Arguments): e is { [name: string]: Expression } { return !!e && !Array.isArray(e); }
export function isLiteral(e: Expression): e is Expression.Literal { return !isApply(e); } export function isLiteral(e: Expression): e is Expression.Literal { return !isApply(e) && !isSymbol(e); }
export function isApply(e: Expression): e is Expression.Apply { return !!e && !!(e as Expression.Apply).head && typeof e === 'object'; } export function isApply(e: Expression): e is Expression.Apply { return !!e && !!(e as Expression.Apply).head && typeof e === 'object'; }
export function isSymbol(e: Expression): e is Expression.Symbol { return !!e && typeof (e as any).name === 'string' } export function isSymbol(e: Expression): e is Expression.Symbol { return !!e && typeof (e as any).name === 'string' }
} }
......
...@@ -134,8 +134,10 @@ function _compile(ctx: QueryCompilerCtx, expression: Expression): CompiledQueryF ...@@ -134,8 +134,10 @@ function _compile(ctx: QueryCompilerCtx, expression: Expression): CompiledQueryF
} }
if (Expression.isSymbol(expression)) { if (Expression.isSymbol(expression)) {
// TODO: check for "nullary symbols" and automatically apply them? const runtime = ctx.table.getRuntime(expression.name);
return CompiledQueryFn.Const(expression.name); if (!runtime) return CompiledQueryFn.Const(expression.name);
return runtime.compile(ctx);
} }
if (!Expression.isSymbol(expression.head)) { if (!Expression.isSymbol(expression.head)) {
...@@ -143,7 +145,6 @@ function _compile(ctx: QueryCompilerCtx, expression: Expression): CompiledQueryF ...@@ -143,7 +145,6 @@ function _compile(ctx: QueryCompilerCtx, expression: Expression): CompiledQueryF
} }
const compiler = ctx.table.getRuntime(expression.head.name); const compiler = ctx.table.getRuntime(expression.head.name);
if (!compiler) { if (!compiler) {
throw new Error(`Symbol '${expression.head.name}' is not implemented.`); throw new Error(`Symbol '${expression.head.name}' is not implemented.`);
} }
......
This diff is collapsed.
...@@ -6,6 +6,8 @@ import { CustomPropSymbol } from 'mol-script/language/symbol'; ...@@ -6,6 +6,8 @@ import { CustomPropSymbol } from 'mol-script/language/symbol';
import Type from 'mol-script/language/type'; import Type from 'mol-script/language/type';
import { parseMolScript } from 'mol-script/language/parser'; import { parseMolScript } from 'mol-script/language/parser';
import * as util from 'util' import * as util from 'util'
import { transpileMolScript } from 'mol-script/script/mol-script/symbols';
import { formatMolScript } from 'mol-script/language/expression-formatter';
// import Examples from 'mol-script/script/mol-script/examples' // import Examples from 'mol-script/script/mol-script/examples'
// import { parseMolScript } from 'mol-script/script/mol-script/parser' // import { parseMolScript } from 'mol-script/script/mol-script/parser'
...@@ -15,14 +17,25 @@ import * as util from 'util' ...@@ -15,14 +17,25 @@ import * as util from 'util'
// const expr = parseMolScript(e.value)[0]; // const expr = parseMolScript(e.value)[0];
// console.log(e.name, util.inspect(expr, true, 10, true)); // console.log(e.name, util.inspect(expr, true, 10, true));
// } // }
// const exprs = parseMolScript(`(sel.atom.atom-groups
// :residue-test (= atom.auth_comp_id ALA)
// ;; ho ho ho
// :atom-test (set.has { _C _N } atom.el)) ; comm
// ;; this is a comment
// ((hi) (ho))`);
const exprs = parseMolScript(`(sel.atom.atom-groups const exprs = parseMolScript(`(sel.atom.atom-groups
:residue-test (= atom.auth_comp_id ALA) :residue-test (= atom.label_comp_id REA)
;; ho ho ho :atom-test (= atom.el _C))`);
:atom-test (set.has { _C _N } atom.el)) ; comm
;; this is a comment const tsp = transpileMolScript(exprs[0]);
((hi) (ho))`);
//console.log(util.inspect(exprs, true, 10, true));
console.log(util.inspect(exprs, true, 10, true)); console.log(util.inspect(tsp, true, 10, true));
console.log(formatMolScript);
console.log(formatMolScript(tsp));
// //console.log(expr); // //console.log(expr);
const expr = MolScriptBuilder.core.math.add([1, 2, 3]); const expr = MolScriptBuilder.core.math.add([1, 2, 3]);
...@@ -49,7 +62,7 @@ export async function testQ() { ...@@ -49,7 +62,7 @@ export async function testQ() {
const frame = await readCifFile('e:/test/quick/1cbs_updated.cif'); const frame = await readCifFile('e:/test/quick/1cbs_updated.cif');
const { structure } = await getModelsAndStructure(frame); const { structure } = await getModelsAndStructure(frame);
const expr = MolScriptBuilder.struct.generator.atomGroups({ let expr = MolScriptBuilder.struct.generator.atomGroups({
'atom-test': MolScriptBuilder.core.rel.eq([ 'atom-test': MolScriptBuilder.core.rel.eq([
MolScriptBuilder.struct.atomProperty.core.elementSymbol(), MolScriptBuilder.struct.atomProperty.core.elementSymbol(),
MolScriptBuilder.es('C') MolScriptBuilder.es('C')
...@@ -61,10 +74,12 @@ export async function testQ() { ...@@ -61,10 +74,12 @@ export async function testQ() {
'residue-test': MolScriptBuilder.core.rel.inRange([CustomProp.symbols.residueIndex.symbol(), 1, 5]) 'residue-test': MolScriptBuilder.core.rel.inRange([CustomProp.symbols.residueIndex.symbol(), 1, 5])
}); });
expr = tsp;
const compiled = compile<StructureQuery>(expr); const compiled = compile<StructureQuery>(expr);
const result = compiled(new QueryContext(structure)); const result = compiled(new QueryContext(structure));
console.log(result); console.log(result);
} }
// testQ(); testQ();
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment