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

Working on mol-script

parent 422dd10a
No related branches found
No related tags found
No related merge requests found
// TODO: compilation step from lisp AST
\ No newline at end of file
...@@ -5,34 +5,36 @@ ...@@ -5,34 +5,36 @@
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
// TODO: add "lisp AST" which is then compiled to mol-script
import { MonadicParser as P } from 'mol-util/monadic-parser' import { MonadicParser as P } from 'mol-util/monadic-parser'
import Parser from '../parser' import Parser from '../parser'
import Expression from '../../expression' import Expression from '../../expression'
import { SymbolMap, MolScriptSymbol } from './symbols' // import { MolScriptSymbol } from './symbols'
import B from '../../builder' import B from '../../builder'
const ws = P.regexp(/[\n\r\s]*/) const ws = P.regexp(/[\n\r\s]*/)
function getSymbolExpression(s: MolScriptSymbol, args?: any) { // function getSymbolExpression(s: MolScriptSymbol, args?: any) {
switch (s.kind) { // switch (s.kind) {
case 'alias': return args ? Expression.Apply(s.symbol.id, args) : Expression.Apply(s.symbol.id); // case 'alias': return args ? Expression.Apply(s.symbol.id, args) : Expression.Apply(s.symbol.id);
case 'macro': return s.translate(args); // case 'macro': return s.translate(args);
} // }
} // }
namespace Language { namespace Language {
const Expr = P.lazy(() => P.seq(Symb, ArgList, NamedArgList)); const Expr = P.lazy(() => P.seq(Identifier, ArgList, NamedArgList));
const Arg: P<Expression> = P.lazy(() => P.seq( const Arg: P<Expression> = P.lazy(() => P.seq(
P.lookahead(P.regexp(/[^:]/)), P.lookahead(P.test(ch => ch !== ':')),
P.alt( P.alt(
// order matters // order matters
AtomName, AtomName,
ElementSymbol, ElementSymbol,
Bool, Bool,
Num, Num,
Str, Identifier,
QuotedStr, QuotedStr,
ListSymbol, ListSymbol,
SetSymbol, SetSymbol,
...@@ -51,25 +53,11 @@ namespace Language { ...@@ -51,25 +53,11 @@ namespace Language {
return namedArgs return namedArgs
}); });
const Symb = P.regexp(/[^\s'`,@()\[\]{}';:]+/) // /[a-zA-Z_-][a-zA-Z0-9_.-]+/) const Identifier = P.regexp(/[^\s'`,@()\[\]{}';:]+/)
.map(x => { //.map(id => Expression.Apply(B.core.type.identifier.id, void 0))
const s = SymbolMap[x]; .desc('identifier')
if (!s) {
throw new Error(`'${x}': unknown symbol.`);
}
return s;
})
.desc('symbol');
const Str = P.regexp(/[a-zA-Z_-]+[a-zA-Z0-9_.-]*/).map(x => {
const s = SymbolMap[x];
if (s) return getSymbolExpression(s);
return x;
}).desc('string');
const QuotedStr = P.string('`') const QuotedStr = P.regexp(/[^`]*/).trim(P.string('`'))
.then(P.regexp(/[^`]*/))
.skip(P.string('`'))
.desc('quoted-string'); .desc('quoted-string');
const Num = P.regexp(/-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?/) const Num = P.regexp(/-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?/)
...@@ -101,26 +89,28 @@ namespace Language { ...@@ -101,26 +89,28 @@ namespace Language {
// '.e' => struct.type.atomName(e) // '.e' => struct.type.atomName(e)
const AtomName = P.string('.') const AtomName = P.string('.')
.then(P.alt(Str, QuotedStr, Num)) .then(P.alt(Identifier, QuotedStr, Num))
.map(v => B.atomName('' + v)) .map(v => B.atomName('' + v))
.desc('identifier'); .desc('identifier');
const List = Expr const List = Expr
.wrap(P.string('('), P.string(')')) .wrap(P.string('('), P.string(')'))
.map(x => { .map(x => {
const array: any[] = x[1]; // const array: any[] = x[1];
const named: any = x[2]; // const named: any = x[2];
if (named && Object.keys(named).length) { return 0 as any;
if (array) {
for (let i = 0; i < array.length; i++) named[i] = array[i]; // if (named && Object.keys(named).length) {
} // if (array) {
return getSymbolExpression(x[0], named); // for (let i = 0; i < array.length; i++) named[i] = array[i];
} else if (array && array.length) { // }
return getSymbolExpression(x[0], x[1]); // return getSymbolExpression(x[0], named);
} else { // } else if (array && array.length) {
return getSymbolExpression(x[0]) // return getSymbolExpression(x[0], x[1]);
} // } else {
// return getSymbolExpression(x[0])
// }
}) })
.desc('list'); .desc('list');
......
File moved
...@@ -13,7 +13,6 @@ namespace Type { ...@@ -13,7 +13,6 @@ namespace Type {
export interface Variable<T> { kind: 'variable', name: string, type: Type, isConstraint: boolean, '@type': any } export interface Variable<T> { kind: 'variable', name: string, type: Type, isConstraint: boolean, '@type': any }
export interface AnyValue { kind: 'any-value', '@type': any } export interface AnyValue { kind: 'any-value', '@type': any }
export interface Value<T> { kind: 'value', namespace: string, name: string, parent?: Value<any>, '@type': T } export interface Value<T> { kind: 'value', namespace: string, name: string, parent?: Value<any>, '@type': T }
export interface Identifier { kind: 'identifier', '@type': string }
export interface Container<T> { kind: 'container', namespace: string, name: string, alias?: string, child: Type, '@type': T } export interface Container<T> { kind: 'container', namespace: string, name: string, alias?: string, child: Type, '@type': T }
export interface Union<T> { kind: 'union', types: Type[], '@type': T } export interface Union<T> { kind: 'union', types: Type[], '@type': T }
export interface OneOf<T> { kind: 'oneof', type: Value<T>, namespace: string, name: string, values: { [v: string]: boolean | undefined }, '@type': T } export interface OneOf<T> { kind: 'oneof', type: Value<T>, namespace: string, name: string, values: { [v: string]: boolean | undefined }, '@type': T }
......
...@@ -299,14 +299,15 @@ export namespace MonadicParser { ...@@ -299,14 +299,15 @@ export namespace MonadicParser {
export function string(str: string) { export function string(str: string) {
const expected = `'${str}'`; const expected = `'${str}'`;
if (str.length === 1) {
const code = str.charCodeAt(0);
return new MonadicParser((input, i) => input.charCodeAt(i) === code ? makeSuccess(i + 1, str) : makeFailure(i, expected));
}
return new MonadicParser((input, i) => { return new MonadicParser((input, i) => {
const j = i + str.length; const j = i + str.length;
const head = input.slice(i, j); if (input.slice(i, j) === str) return makeSuccess(j, str);
if (head === str) { else return makeFailure(i, expected);
return makeSuccess(j, head);
} else {
return makeFailure(i, expected);
}
}); });
} }
...@@ -472,7 +473,6 @@ function seqPick(idx: number, ...parsers: MonadicParser<any>[]): MonadicParser<a ...@@ -472,7 +473,6 @@ function seqPick(idx: number, ...parsers: MonadicParser<any>[]): MonadicParser<a
}); });
} }
function makeSuccess<T>(index: number, value: T): MonadicParser.Success<T> { function makeSuccess<T>(index: number, value: T): MonadicParser.Success<T> {
return { status: true, index, value }; return { status: true, index, value };
} }
...@@ -515,20 +515,11 @@ function formatGot(input: string, error: MonadicParser.ParseFailure) { ...@@ -515,20 +515,11 @@ function formatGot(input: string, error: MonadicParser.ParseFailure) {
} }
const prefix = i > 0 ? '\'...' : '\''; const prefix = i > 0 ? '\'...' : '\'';
const suffix = input.length - i > 12 ? '...\'' : '\''; const suffix = input.length - i > 12 ? '...\'' : '\'';
return ( return ` at line ${index.line} column ${index.column}, got ${prefix}${input.slice(i, i + 12)}${suffix}`;
' at line ' +
index.line +
' column ' +
index.column +
', got ' +
prefix +
input.slice(i, i + 12) +
suffix
);
} }
function formatError(input: string, error: MonadicParser.ParseFailure) { function formatError(input: string, error: MonadicParser.ParseFailure) {
return 'expected ' + formatExpected(error.expected) + formatGot(input, error); return `expected ${formatExpected(error.expected)}${formatGot(input, error)}`;
} }
function unsafeUnion(xs: string[], ys: string[]) { function unsafeUnion(xs: string[], ys: string[]) {
......
import Examples from 'mol-script/parsers/mol-script/examples' import Examples from 'mol-script/script/mol-script/examples'
import transpile from 'mol-script/parsers/mol-script/parser' import parse from 'mol-script/script/mol-script/parser'
const expr = transpile(Examples[Examples.length - 1].value); const expr = parse(Examples[Examples.length - 1].value);
console.log(expr); console.log(expr);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment