Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1/** @import { AST, Scope } from '#compiler' */2/** @import * as ESTree from 'estree' */3import { walk } from 'zimmerframe';4import * as b from '#compiler/builders';5 6/**7 * Gets the left-most identifier of a member expression or identifier.8 * @param {ESTree.MemberExpression | ESTree.Identifier} expression9 * @returns {ESTree.Identifier | null}10 */11export function object(expression) {12 while (expression.type === 'MemberExpression') {13 expression = /** @type {ESTree.MemberExpression | ESTree.Identifier} */ (expression.object);14 }15 16 if (expression.type !== 'Identifier') {17 return null;18 }19 20 return expression;21}22 23/**24 * Returns true if the attribute contains a single static text node.25 * @param {AST.Attribute} attribute26 * @returns {attribute is AST.Attribute & { value: [AST.Text] }}27 */28export function is_text_attribute(attribute) {29 return (30 Array.isArray(attribute.value) &&31 attribute.value.length === 1 &&32 attribute.value[0].type === 'Text'33 );34}35 36/**37 * Returns true if the attribute contains a single expression node.38 * In Svelte 5, this also includes a single expression node wrapped in an array.39 * TODO change that in a future version40 * @param {AST.Attribute} attribute41 * @returns {attribute is AST.Attribute & { value: [AST.ExpressionTag] | AST.ExpressionTag }}42 */43export function is_expression_attribute(attribute) {44 return (45 (attribute.value !== true && !Array.isArray(attribute.value)) ||46 (Array.isArray(attribute.value) &&47 attribute.value.length === 1 &&48 attribute.value[0].type === 'ExpressionTag')49 );50}51 52/**53 * Returns the single attribute expression node.54 * In Svelte 5, this also includes a single expression node wrapped in an array.55 * TODO change that in a future version56 * @param { AST.Attribute & { value: [AST.ExpressionTag] | AST.ExpressionTag }} attribute57 * @returns {ESTree.Expression}58 */59export function get_attribute_expression(attribute) {60 return Array.isArray(attribute.value)61 ? /** @type {AST.ExpressionTag} */ (attribute.value[0]).expression62 : attribute.value.expression;63}64 65/**66 * Returns the expression chunks of an attribute value67 * @param {AST.Attribute['value']} value68 * @returns {Array<AST.Text | AST.ExpressionTag>}69 */70export function get_attribute_chunks(value) {71 return Array.isArray(value) ? value : typeof value === 'boolean' ? [] : [value];72}73 74/**75 * Returns true if the attribute starts with `on` and contains a single expression node.76 * @param {AST.Attribute} attribute77 * @returns {attribute is AST.Attribute & { value: [AST.ExpressionTag] | AST.ExpressionTag }}78 */79export function is_event_attribute(attribute) {80 return is_expression_attribute(attribute) && attribute.name.startsWith('on');81}82 83/**84 * Extracts all identifiers and member expressions from a pattern.85 * @param {ESTree.Pattern} pattern86 * @param {Array<ESTree.Identifier | ESTree.MemberExpression>} [nodes]87 * @returns {Array<ESTree.Identifier | ESTree.MemberExpression>}88 */89export function unwrap_pattern(pattern, nodes = []) {90 switch (pattern.type) {91 case 'Identifier':92 nodes.push(pattern);93 break;94 95 case 'MemberExpression':96 // member expressions can be part of an assignment pattern, but not a binding pattern97 // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#binding_and_assignment98 nodes.push(pattern);99 break;100 101 case 'ObjectPattern':102 for (const prop of pattern.properties) {103 if (prop.type === 'RestElement') {104 unwrap_pattern(prop.argument, nodes);105 } else {106 unwrap_pattern(prop.value, nodes);107 }108 }109 110 break;111 112 case 'ArrayPattern':113 for (const element of pattern.elements) {114 if (element) unwrap_pattern(element, nodes);115 }116 117 break;118 119 case 'RestElement':120 unwrap_pattern(pattern.argument, nodes);121 break;122 123 case 'AssignmentPattern':124 unwrap_pattern(pattern.left, nodes);125 break;126 }127 128 return nodes;129}130 131/**132 * Extracts all identifiers from a pattern.133 * @param {ESTree.Pattern} pattern134 * @returns {ESTree.Identifier[]}135 */136export function extract_identifiers(pattern) {137 return unwrap_pattern(pattern, []).filter((node) => node.type === 'Identifier');138}139 140/**141 * Extracts all identifiers and a stringified keypath from an expression.142 * TODO replace this with `expression.dependencies`143 * @param {ESTree.Expression} expr144 * @returns {[keypath: string, ids: ESTree.Identifier[]]}145 */146export function extract_all_identifiers_from_expression(expr) {147 /** @type {ESTree.Identifier[]} */148 let nodes = [];149 /** @type {string[]} */150 let keypath = [];151 152 walk(153 expr,154 {},155 {156 Identifier(node, { path }) {157 const parent = path.at(-1);158 if (parent?.type !== 'MemberExpression' || parent.property !== node || parent.computed) {159 nodes.push(node);160 }161 162 if (parent?.type === 'MemberExpression' && parent.computed && parent.property === node) {163 keypath.push(`[${node.name}]`);164 } else {165 keypath.push(node.name);166 }167 },168 Literal(node, { path }) {169 const value = typeof node.value === 'string' ? `"${node.value}"` : String(node.value);170 const parent = path.at(-1);171 if (parent?.type === 'MemberExpression' && parent.computed && parent.property === node) {172 keypath.push(`[${value}]`);173 } else {174 keypath.push(value);175 }176 },177 ThisExpression(_, { next }) {178 keypath.push('this');179 next();180 }181 }182 );183 184 return [keypath.join('.'), nodes];185}186 187/**188 * Extracts all leaf identifiers from a destructuring expression.189 * @param {ESTree.Identifier | ESTree.ObjectExpression | ESTree.ArrayExpression} node190 * @param {ESTree.Identifier[]} [nodes]191 * @returns192 */193export function extract_identifiers_from_destructuring(node, nodes = []) {194 // TODO This isn't complete, but it should be enough for our purposes195 switch (node.type) {196 case 'Identifier':197 nodes.push(node);198 break;199 200 case 'ObjectExpression':201 for (const prop of node.properties) {202 if (prop.type === 'Property') {203 extract_identifiers_from_destructuring(/** @type {any} */ (prop.value), nodes);204 } else {205 extract_identifiers_from_destructuring(/** @type {any} */ (prop.argument), nodes);206 }207 }208 209 break;210 211 case 'ArrayExpression':212 for (const element of node.elements) {213 if (element) extract_identifiers_from_destructuring(/** @type {any} */ (element), nodes);214 }215 216 break;217 }218 219 return nodes;220}221 222/**223 * Represents the path of a destructured assignment from either a declaration224 * or assignment expression. For example, given `const { foo: { bar: baz } } = quux`,225 * the path of `baz` is `foo.bar`226 * @typedef {Object} DestructuredAssignment227 * @property {ESTree.Identifier | ESTree.MemberExpression} node The node the destructuring path end in. Can be a member expression only for assignment expressions228 * @property {boolean} is_rest `true` if this is a `...rest` destructuring229 * @property {boolean} has_default_value `true` if this has a fallback value like `const { foo = 'bar } = ..`230 * @property {ESTree.Expression} expression The value of the current path231 * This will be a call expression if a rest element or default is involved — e.g. `const { foo: { bar: baz = 42 }, ...rest } = quux` — since we can't represent `baz` or `rest` purely as a path232 * Will be an await expression in case of an async default value (`const { foo = await bar } = ...`)233 * @property {ESTree.Expression} update_expression Like `expression` but without default values.234 */235 236/**237 * Extracts all destructured assignments from a pattern.238 * For each `id` in the returned `inserts`, make sure to adjust the `name`.239 * @param {ESTree.Node} param240 * @param {ESTree.Expression} initial241 * @returns {{ inserts: Array<{ id: ESTree.Identifier, value: ESTree.Expression }>, paths: DestructuredAssignment[] }}242 */243export function extract_paths(param, initial) {244 /**245 * When dealing with array destructuring patterns (`let [a, b, c] = $derived(blah())`)246 * we need an intermediate declaration that creates an array, since `blah()` could247 * return a non-array-like iterator248 * @type {Array<{ id: ESTree.Identifier, value: ESTree.Expression }>}249 */250 const inserts = [];251 252 /** @type {DestructuredAssignment[]} */253 const paths = [];254 255 _extract_paths(paths, inserts, param, initial, initial, false);256 257 return { inserts, paths };258}259 260/**261 * @param {DestructuredAssignment[]} paths262 * @param {Array<{ id: ESTree.Identifier, value: ESTree.Expression }>} inserts263 * @param {ESTree.Node} param264 * @param {ESTree.Expression} expression265 * @param {ESTree.Expression} update_expression266 * @param {boolean} has_default_value267 * @returns {DestructuredAssignment[]}268 */269function _extract_paths(paths, inserts, param, expression, update_expression, has_default_value) {270 switch (param.type) {271 case 'Identifier':272 case 'MemberExpression':273 paths.push({274 node: param,275 is_rest: false,276 has_default_value,277 expression,278 update_expression279 });280 break;281 282 case 'ObjectPattern':283 for (const prop of param.properties) {284 if (prop.type === 'RestElement') {285 /** @type {ESTree.Expression[]} */286 const props = [];287 288 for (const p of param.properties) {289 if (p.type === 'Property' && p.key.type !== 'PrivateIdentifier') {290 if (p.key.type === 'Identifier' && !p.computed) {291 props.push(b.literal(p.key.name));292 } else if (p.key.type === 'Literal') {293 props.push(b.literal(String(p.key.value)));294 } else {295 props.push(b.call('String', p.key));296 }297 }298 }299 300 const rest_expression = b.call('$.exclude_from_object', expression, b.array(props));301 302 if (prop.argument.type === 'Identifier') {303 paths.push({304 node: prop.argument,305 is_rest: true,306 has_default_value,307 expression: rest_expression,308 update_expression: rest_expression309 });310 } else {311 _extract_paths(312 paths,313 inserts,314 prop.argument,315 rest_expression,316 rest_expression,317 has_default_value318 );319 }320 } else {321 const object_expression = b.member(322 expression,323 prop.key,324 prop.computed || prop.key.type !== 'Identifier'325 );326 327 _extract_paths(328 paths,329 inserts,330 prop.value,331 object_expression,332 object_expression,333 has_default_value334 );335 }336 }337 338 break;339 340 case 'ArrayPattern': {341 // we create an intermediate declaration to convert iterables to arrays if necessary.342 // the consumer is responsible for setting the name of the identifier343 const id = b.id('#');344 345 const value = b.call(346 '$.to_array',347 expression,348 param.elements.at(-1)?.type === 'RestElement' ? undefined : b.literal(param.elements.length)349 );350 351 inserts.push({ id, value });352 353 for (let i = 0; i < param.elements.length; i += 1) {354 const element = param.elements[i];355 if (element) {356 if (element.type === 'RestElement') {357 const rest_expression = b.call(b.member(id, 'slice'), b.literal(i));358 359 if (element.argument.type === 'Identifier') {360 paths.push({361 node: element.argument,362 is_rest: true,363 has_default_value,364 expression: rest_expression,365 update_expression: rest_expression366 });367 } else {368 _extract_paths(369 paths,370 inserts,371 element.argument,372 rest_expression,373 rest_expression,374 has_default_value375 );376 }377 } else {378 const array_expression = b.member(id, b.literal(i), true);379 380 _extract_paths(381 paths,382 inserts,383 element,384 array_expression,385 array_expression,386 has_default_value387 );388 }389 }390 }391 392 break;393 }394 395 case 'AssignmentPattern': {396 const fallback_expression = build_fallback(expression, param.right);397 398 if (param.left.type === 'Identifier') {399 paths.push({400 node: param.left,401 is_rest: false,402 has_default_value: true,403 expression: fallback_expression,404 update_expression405 });406 } else {407 _extract_paths(paths, inserts, param.left, fallback_expression, update_expression, true);408 }409 410 break;411 }412 }413 414 return paths;415}416 417/**418 * Like `path.at(x)`, but skips over `TSNonNullExpression` and `TSAsExpression` nodes and eases assertions a bit419 * by removing the `| undefined` from the resulting type.420 *421 * @template {AST.SvelteNode} T422 * @param {T[]} path423 * @param {number} at424 */425export function get_parent(path, at) {426 let node = path.at(at);427 // @ts-expect-error428 if (node.type === 'TSNonNullExpression' || node.type === 'TSAsExpression') {429 return /** @type {T} */ (path.at(at < 0 ? at - 1 : at + 1));430 }431 return /** @type {T} */ (node);432}433 434/**435 * Returns `true` if the expression is an identifier, a literal, a function expression,436 * or a logical expression that only contains simple expressions. Used to determine whether437 * something needs to be treated as though accessing it could have side-effects (i.e.438 * reading signals prematurely)439 * @param {ESTree.Expression} node440 * @returns {boolean}441 */442export function is_simple_expression(node) {443 if (444 node.type === 'Literal' ||445 node.type === 'Identifier' ||446 node.type === 'ArrowFunctionExpression' ||447 node.type === 'FunctionExpression'448 ) {449 return true;450 }451 452 if (node.type === 'ConditionalExpression') {453 return (454 is_simple_expression(node.test) &&455 is_simple_expression(node.consequent) &&456 is_simple_expression(node.alternate)457 );458 }459 460 if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {461 return (462 node.left.type !== 'PrivateIdentifier' &&463 is_simple_expression(node.left) &&464 is_simple_expression(node.right)465 );466 }467 468 return false;469}470 471/**472 * @template {ESTree.SimpleCallExpression | ESTree.MemberExpression} T473 * @param {ESTree.ChainExpression & { expression : T } | T} node474 * @returns {T}475 */476export function unwrap_optional(node) {477 return node.type === 'ChainExpression' ? node.expression : node;478}479 480/**481 * @param {ESTree.Expression | ESTree.Pattern} expression482 * @returns {boolean}483 */484export function is_expression_async(expression) {485 switch (expression.type) {486 case 'AwaitExpression': {487 return true;488 }489 case 'ArrayPattern': {490 return expression.elements.some((element) => element && is_expression_async(element));491 }492 case 'ArrayExpression': {493 return expression.elements.some((element) => {494 if (!element) {495 return false;496 } else if (element.type === 'SpreadElement') {497 return is_expression_async(element.argument);498 } else {499 return is_expression_async(element);500 }501 });502 }503 case 'AssignmentPattern':504 case 'AssignmentExpression':505 case 'BinaryExpression':506 case 'LogicalExpression': {507 return (508 (expression.left.type !== 'PrivateIdentifier' && is_expression_async(expression.left)) ||509 is_expression_async(expression.right)510 );511 }512 case 'CallExpression':513 case 'NewExpression': {514 return (515 (expression.callee.type !== 'Super' && is_expression_async(expression.callee)) ||516 expression.arguments.some((element) => {517 if (element.type === 'SpreadElement') {518 return is_expression_async(element.argument);519 } else {520 return is_expression_async(element);521 }522 })523 );524 }525 case 'ChainExpression': {526 return is_expression_async(expression.expression);527 }528 case 'ConditionalExpression': {529 return (530 is_expression_async(expression.test) ||531 is_expression_async(expression.alternate) ||532 is_expression_async(expression.consequent)533 );534 }535 case 'ImportExpression': {536 return is_expression_async(expression.source);537 }538 case 'MemberExpression': {539 return (540 (expression.object.type !== 'Super' && is_expression_async(expression.object)) ||541 (expression.property.type !== 'PrivateIdentifier' &&542 is_expression_async(expression.property))543 );544 }545 case 'ObjectPattern':546 case 'ObjectExpression': {547 return expression.properties.some((property) => {548 if (property.type === 'SpreadElement') {549 return is_expression_async(property.argument);550 } else if (property.type === 'Property') {551 return (552 (property.key.type !== 'PrivateIdentifier' && is_expression_async(property.key)) ||553 is_expression_async(property.value)554 );555 }556 });557 }558 case 'RestElement': {559 return is_expression_async(expression.argument);560 }561 case 'SequenceExpression':562 case 'TemplateLiteral': {563 return expression.expressions.some((subexpression) => is_expression_async(subexpression));564 }565 case 'TaggedTemplateExpression': {566 return is_expression_async(expression.tag) || is_expression_async(expression.quasi);567 }568 case 'UnaryExpression':569 case 'UpdateExpression': {570 return is_expression_async(expression.argument);571 }572 case 'YieldExpression': {573 return expression.argument ? is_expression_async(expression.argument) : false;574 }575 default:576 return false;577 }578}579 580/**581 *582 * @param {ESTree.Expression} expression583 * @param {ESTree.Expression} fallback584 */585export function build_fallback(expression, fallback) {586 if (is_simple_expression(fallback)) {587 return b.call('$.fallback', expression, fallback);588 }589 590 if (fallback.type === 'AwaitExpression' && is_simple_expression(fallback.argument)) {591 return b.await(b.call('$.fallback', expression, fallback.argument));592 }593 594 return is_expression_async(fallback)595 ? b.await(b.call('$.fallback', expression, b.thunk(fallback, true), b.true))596 : b.call('$.fallback', expression, b.thunk(fallback), b.true);597}598 599/**600 * @param {ESTree.AssignmentOperator} operator601 * @param {ESTree.Identifier | ESTree.MemberExpression} left602 * @param {ESTree.Expression} right603 */604export function build_assignment_value(operator, left, right) {605 return operator === '='606 ? right607 : // turn something like x += 1 into x = x + 1608 ['||=', '&&=', '??='].includes(operator)609 ? b.logical(/** @type {ESTree.LogicalOperator} */ (operator.slice(0, -1)), left, right)610 : b.binary(/** @type {ESTree.BinaryOperator} */ (operator.slice(0, -1)), left, right);611}612 613/**614 * @param {ESTree.Node} node615 */616export function has_await_expression(node) {617 let has_await = false;618 619 walk(node, null, {620 AwaitExpression(_node, context) {621 has_await = true;622 context.stop();623 },624 // don't traverse into these625 FunctionDeclaration() {},626 FunctionExpression() {},627 ArrowFunctionExpression() {}628 });629 630 return has_await;631}632 633/**634 * Turns `await ...` to `(await $.save(...))()`635 * @param {ESTree.Expression} expression636 */637export function save(expression) {638 return b.call(b.await(b.call('$.save', expression)));639}640 