1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 20x 15x 76x 38x 15x 15x 15x 15x 15x 15x 15x 15x 9x 9x 9x 5x 20x 20x 5x 4x 8x 8x 15x 24x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 66x 5x 5x 15x | import fnName from "./fnName"; import clauseFromAlts from "./clauseFromAlts"; import { wall, any, zeroOrMore, and, cat, or, ExprClause, mapOf, maybe } from '../core'; import delayed from './delayed'; import match from './match'; import coerceIntoClause from './coerceIntoClause'; import { isStr, isPlainObj, instanceOf } from '../preds'; export class Recursive { constructor (expression) { this.isRecursive = true; this.expression = expression; }; isRecursive: Boolean; expression: any; } export function QuotedParamsMap( map? ) { Object.assign( this, map ); } export function UnquotedParamsMap( map? ) { Object.assign( this, map ); } function Quoted( val ) { this.value = val; } var ParamLabelClause = or( 'str', isStr, 'quoted', instanceOf( Quoted ) ); export function genClauses( headClause ) { var paramItemC = or( 'sExpression', delayed( () => sExprC ), 'quotedParamsMap', delayed( () => QuotedParamsMapC ), 'unquotedParamsMap', delayed( () => UnquotedParamsMapC ), 'optionsObj', isPlainObj, 'recursive', instanceOf( Recursive ) ); var sExprC = wall( cat( 'head', headClause, 'params', or( 'labelled', zeroOrMore( cat( 'label', ParamLabelClause, I 'item', delayed( () => paramItemC ) ) ), 'unlabelled', zeroOrMore( cat( 'item', delayed( () => paramItemC ) ) ) ) ) ); const ParamsMapC = mapOf( any, maybe( or( 'keyList', zeroOrMore( delayed( () => ParamLabelClause ) ), 'singleParam', delayed( () => paramItemC ) ) ) ); var QuotedParamsMapC = and( instanceOf( QuotedParamsMap ), delayed( () => ParamsMapC ) ); var UnquotedParamsMapC = and( instanceOf( UnquotedParamsMap ), delayed( () => ParamsMapC ) ); return [ sExprC, paramItemC ]; } export const [ SExpressionClause, ParamItemClause ] = genClauses( ExprClause ); var singleArgParamGenerator = ( repo, { opts: { enclosedClause } } ) => [ _createSExpr( repo, enclosedClause ) ]; var multipleArgParamGenerator = ( repo, { opts: { named }, exprs } ) => { if ( exprs.length === 0 ) { //empty case return []; } else if ( named ) { const r = exprs.reduce( ( acc, { name, expr } ) => acc.concat( [ new Quoted( name ), _createSExpr( repo, expr ) ] ) , [] ); return r; } else { return exprs.reduce( ( acc, { expr } ) => acc.concat( [ _createSExpr( repo, expr ) ] ), [] ); } }; var sParamsConverters = { 'PRED': ( ) => [ ], 'WALL': ( repo, { opts: { enclosedClause } } ) => [ _createSExpr( repo, enclosedClause ) ], 'AND': ( repo, { opts: { conformedExprs } } ) => conformedExprs.map( clauseFromAlts ).map( ( c ) => _createSExpr( repo, c ) ), 'CAT': multipleArgParamGenerator, 'OR': multipleArgParamGenerator, 'Z_OR_M': singleArgParamGenerator, 'O_OR_M': singleArgParamGenerator, 'Z_OR_O': singleArgParamGenerator, 'COLL_OF': singleArgParamGenerator, 'ANY': () => [], 'MAP_OF': ( repo, { opts } ) => _handleKeyValExprPair( repo, opts ), 'SHAPE': ( repo, { opts: { conformedArgs: { shapeArgs: { optionalFields: { opt = null, optional = null } = {}, requiredFields: { req = null, required = null } = {} } } } } ) => Object.assign( new UnquotedParamsMap(), ( req || required ) ? { required: _fieldDefToFrags( repo, req || required ), } : {}, ( opt || optional ) ? { optional: _fieldDefToFrags( repo, opt || optional ), } : {} ), 'FCLAUSE': ( repo, { opts: { args, ret, fn } } ) => Object.assign( new UnquotedParamsMap(), args ? { args: _createSExpr( repo, args ) } : {}, ret ? { ret: _createSExpr( repo, ret ) } : {}, fn ? { fn: [ `${fnName( fn )}()` ] } : {} ) } I function _fieldDefToFrags( repo, { fieldDefs, keyList } ) { if ( fieldDefs ) { let r = new QuotedParamsMap(); for ( let key in fieldDefs ) { if ( fieldDefs.hasOwnProperty( key ) ) { let val = match( fieldDefs[ key ], { 'keyValExprPair': ( pair ) => _handleKeyValExprPair( repo, pair ), 'valExpressionOnly': ( expr ) => _createSExpr( repo, clauseFromAlts( expr ) ) }, () => { I throw '!g'; } ); Object.assign( r, { [ key ]: val I } ); } } return r; } else if ( keyList ) { return keyList; } else { throw '!w'; } } function _handleKeyValExprPair( repo, { keyExpression, valExpression } ) { return new UnquotedParamsMap( { E'key': _createSExpr( repo, clauseFromAlts( keyExpression ) ), 'val': _createSExpr( repo, clauseFromAlts( valExpression ) ), } ); } function _params( repo, clause ) { const converter = sParamsConverters[ clause.type ]; if ( !converter ) { console.error( clause ); throw new Error( `Unsupported clause type ${clause.type}.` ); } else { const r = converter( repo, clause ); return r; } } function _createSExpr( repo, expr ) { if ( _exists( repo, expr ) ) { return new Recursive( expr ); } let coercedExpr = coerceIntoClause( expr ), realExpr, newRepo = repo; if ( coercedExpr.type === 'DELAYED' || coercedExpr.type === 'CLAUSE_REF' ) { realExpr = coercedExpr.get(); return _createSExpr( repo, realExpr ); } else { realExpr = coercedExpr; newRepo = _addToRepo( repo, coercedExpr ); } var params = _params( newRepo, realExpr ); return [ realExpr ].concat( params ); } function _addToRepo( repo, expr ) { if ( !_exists( repo, expr ) ) { return repo.concat( [ expr ] ); } } function _exists( repo, piece ) { return repo.indexOf( piece ) >= 0; } function sExpression( expr ) { const repo = []; return _createSExpr( repo, expr ); } export default sExpression; |