Hola, arrgéglame la clase ES262AST en el html <!DOCTYPE html> <html lang="en"> <head>…
- User
Hola, arrgéglame la clase ES262AST en el html
// Hay que incluir variables, y luego... // Hay que incluir en el tratamiento de funciones en las expresiones del siguiente árbol, mediante la siguiente estructura:
let functionSet = [ {name: 'acos', func: x => Math.acos(x)}, {name: 'asin', func: x => Math.asin(x)}, {name: 'atan', func: x => Math.atan(x)}, {name: 'cosh', func: x => Math.cosh(x)}, {name: 'sinh', func: x => Math.sinh(x)}, {name: 'atanh', func: x => Math.atanh(x)}, {name: 'cos', func: x => Math.cos(x)}, {name: 'sin', func: x => Math.sin(x)}, {name: 'sins', func: x => Math.sinh(x)}, {name: 'tan', func: x => Math.tan(x)}, {name: 'tanh', func: x => Math.tanh(x)}, {name: 'ln', func: x => Math.log(x)}, {name: 'log', func: (x, y) => Math.log(x) / Math.log(y)}, ];
// Luego, incluir las rutinas o la filosofía de
const descompilarScript = (str) => {
// Sintactical analizer (nexto to where the operator in position ends). // TOKEN COMMENT // Verfica que la primera sentencia de la cadena es un comentario correcto., TESTED 13:54|21/04/2022 function esComentario(line) { return left(line.trim(), 2) === '//' || ((left(line.trim(), 2) === '/*') && sintaxCheck(line, line.indexOf('//') + 2)); } // Extraer comentario en la variable por referencia comentario y devolver el resto... ok function extraerComentario(line, comentario) { comentario = ''; var myLine = ''; myLine = line.trim(); var posicionComentario = sintaxCheck(myLine, 0); comentario = myLine.substring(0, posicionComentario); line = myLine.substr(posicionComentario); return {type: 'comment', content: comentario.trim(), remainder: line}; } // TOKEN STRING // Verfica que la primera instancia de cadena esté bien formada y devuelve su posición final., TESTED 13:54|21/04/2022 function esCadena(line) { var posicion; for (let index = 0; index < 3; index++) { const stringSeparator = ['\'', '`', '"'][index]; if (left(line.trim(), 1) === stringSeparator) { posicion = line.indexOf(stringSeparator, line.indexOf(stringSeparator) + 1); if (posicion !== -1) { return posicion; // eureka } } } return false; } // Extraer una cadena en la variable por referencia comentario y devolver el resto... ok function extraerCadena(line) { var isIt = esCadena(line); return { type: 'string literal', content: line.substr(0, isIt + 1), remainder: line.substr(isIt + 1), }; } // TOKEN PARENTHESIS // Verfica que el primer elemento sea un paréntesis y que esté correctamente cerrado function esParenthesis(line) { return left(line.trim(), 1) === '(' && sintaxCheck(line, line.indexOf('(') !== -1); } // Extraer comentario en la variable por referencia comentario y devolver el resto... ok function extraerParenthesis(line) { line = String(line); var abreEn = line.indexOf('('); var cierraEn = sintaxCheck(line, abreEn); var cont = descompilarScript(line.substring(abreEn + 1, cierraEn - 1)); var resto = line.substring(cierraEn); return {type: 'parenthesis', content: cont, remainder: resto}; } // TOKEN IDENTIFIER // Determinar si es un identificador function esIdentificador(line) { if ((!line) || (line.length === 0) || isNumericAt(line, 0)) { return false; } var posicion = 0; while ((posicion < line.length - 1) && isAlphanumericAt(line, posicion)) { posicion++; } return posicion; } function extraerIdentificador(line) { if (!line || line.length === 0 || isNumericAt(line, 0)) { return false; } var posicion = esIdentificador(line); var name = line.substr(0, posicion); line = line.substring(posicion); return {type: 'identifier', content: name, remainder: line.trim()}; } // TOKEN NUMBER // Determinar si es un identificador function esNumero(line) { if ((!line) || (line.length === 0) || !isNumericAt(line, 0)) { return false; } var posicion = 0; while (posicion < line.length - 1 && isNumericAt(line, posicion)) { posicion++; } return posicion; } function extraerNumero(line) { var posicion = esNumero(line); var value = line.substr(0, posicion); line = line.substring(posicion); return {type: 'number', content: value, remainder: line.trim()}; } // TOKEN FUNCTION (usar este mismo modelo para el while cuando código...) function esFuncion(line) { // Determinar si es una función line = line.trim(); if (comienzaCon(line, 'function')) { var firstParBeg = line.indexOf('('); if (firstParBeg === -1) return false; var firstParEnd = sintaxCheck(line, line.indexOf('(')); if (firstParEnd === -1) return false; var corpusBeg = line.indexOf('{'); if (corpusBeg === -1 || corpusBeg < firstParEnd) return false; var corpusEnd = sintaxCheck(line, line.indexOf('{')); return true; } return false; } function extraerFuncion(line) { var firstParBeg = line.indexOf('('); var firstParEnd = sintaxCheck(line, line.indexOf('(')); var corpusBeg = line.indexOf('{'); if (corpusBeg < firstParEnd) return false; var corpusEnd = sintaxCheck(line, corpusBeg); var resultado = {}; resultado.type = 'function'; resultado.name = line.substring(String('function').length, line.firstParBeg - 1).trim(); resultado.parameters = line.substring(firstParBeg + 1, firstParEnd).split(',').filter((x) => x.trim() !== ''); resultado.content = line.substring(corpusBeg + 1, corpusEnd); resultado.remainder = line.substring(corpusEnd + 1); return resultado; } // TOKEN CLASS DECLARATION // export class FormulaEntity extends GenericEntity implements fulano { // ok tested function obtenerProximoIdentificador(cadena) { if (!cadena) { return ''; } var tmp = String(cadena).trim(); if ((tmp.length === 0) || isNumericAt(tmp, 0)) { return ''; } var posicion = 1; var result = ''; while ((posicion < tmp.length) && isAlphanumericAt(tmp, posicion)) { posicion++; } if (esIdentificador(tmp)) { result = tmp.substring(0, posicion).trim(); } return result; } // recortar por la izquierda a la cadena, con tantos caracteres como aparezcan el caracteres... ok tested function recortarPorlaIzquierda(cadena, caracteres) { if (!cadena || cadena.length === 0 || caracteres > cadena.length) { return ''; } return String(cadena).substring(caracteres); } function esDeclaracionDeClase(line) { var next, tmp = line.trim(); var lista = []; var result = {name: next, exported: false, extends: null, implements: []}; // Parseo rápido de identificadores por espacio antes del primer símbolo next = obtenerProximoIdentificador(tmp); tmp = recortarPorlaIzquierda(tmp, next.length).trim(); while (next !== '' && tmp.trim().length !== 0) { lista.push(next); next = obtenerProximoIdentificador(tmp); tmp = recortarPorlaIzquierda(tmp, next.length).trim(); } var declaraClase = false; var identificadorDesconocido = false; for (let index = 0; index < lista.length - 1; index++) { const element = lista[index]; switch (element) { case 'export': case 'extends': case 'implements': { break; } case 'class': { declaraClase = true; break; } } } if (!esBloque(tmp)) return false; return declaraClase && esBloque(tmp) !== -1 ? true : false; } function extraerDeclaracionDeClase(line) { var next, tmp = line.trim(); var lista = []; var result = {name: next, type: 'class', exported: false, extends: null, implements: []}; // Parseo rápido de identificadores por espacio antes del primer símbolo next = obtenerProximoIdentificador(tmp); tmp = recortarPorlaIzquierda(tmp, next.length).trim(); while (next !== '' && tmp.trim().length !== 0) { lista.push(next); next = obtenerProximoIdentificador(tmp); tmp = recortarPorlaIzquierda(tmp, next.length).trim(); } for (let index = 0; index < lista.length - 1; index++) { const element = lista[index]; switch (element) { case 'export': { result.exported = true; break; } case 'class': { result.name = lista[index + 1]; break; } case 'extends': { result.extends = lista[index + 1]; break; } case 'implements': { result.implements.push(lista[index + 1]); break; } } } result.content = descompilarScript(tmp); if (result.content.length > 0 && result.content[0].type === 'block') { if (!(result.content[0].body instanceof Array)) { result.body = []; } else { result.body = result.content[0].body; } delete result.content; } result.remainder = line.substring(1 + sintaxCheck(line, line.indexOf('{'))); // se agregan los atributos al objeto clase var attributes = []; var downC = 0; let tipoRelacion = ''; let nulabilidad = false; let nullable = false; while (downC < result.body.length - 3) { if (result.body[downC].type === 'decorator' && result.body[downC].content === '@Column') { let bloques = result.body[downC + 1].content; for (let i = 0; i < bloques.length; i++) { if (bloques[i].body && bloques[i].body.some(item => item.content === 'nullable')) { let pos = bloques[i].body.findIndex(item => item.content === 'nullable'); nulabilidad = (pos !== -1); if (nulabilidad) { nullable = bloques[i].body[pos + 2].content; } } } } if (result.body[downC].type === 'decorator' && (result.body[downC].content === '@OneToOne' || result.body[downC].content === '@ManyToOne' || result.body[downC].content === '@ManyToMany' || result.body[downC].content === '@OneToMany')) { tipoRelacion = result.body[downC].content; let bloques = result.body[downC + 1].content; for (let i = 0; i < bloques.length; i++) { if (bloques[i].type === 'block' && bloques[i].body.some(item => item.content === 'nullable')) { let pos = bloques[i].body.findIndex(item => item.content === 'nullable'); nulabilidad = (pos !== -1); if (nulabilidad) { nullable = bloques[i].body[pos + 2].content; } } } } if (result.body[downC].type === 'identifier' && result.body[downC + 1].type === 'symbol' && result.body[downC + 2].type === 'identifier') { let objeto = { type: 'attribute', name: result.body[downC].content, kind: result.body[downC + 2].content, }; if (tipoRelacion !== '') { objeto.relation = tipoRelacion; tipoRelacion = ''; } if (nulabilidad) { objeto.nullable = nullable; } nulabilidad = false; attributes.push(objeto); downC = downC + 3; } else { downC++; } } result.attributes = attributes; // Recorrer el body buscando un token de type "identifier", seguidos de "parenthesis" // Cuando lo encuentre, eso es un método... hasta el token de tipo "block" return result; } // TOKEN CLASS CONSTRUCTOR (que puede estar y a su vez no es un CLASS DECARATION) function esConstructorDeClase(line) { // Determinar si es una función line = line.trim(); if (comienzaCon(line, 'constructor')) { var firstParBeg = line.indexOf('('); if (firstParBeg === false) return false; var firstParEnd = sintaxCheck(line, line.indexOf('(')); if (firstParEnd === false) return false; var corpusBeg = line.indexOf('{'); if (corpusBeg === false || corpusBeg < firstParEnd) return false; var corpusEnd = sintaxCheck(line, line.indexOf('{')); if (corpusEnd === false) return false; return true; } return false; } function extraerConstructorDeClase(line) { var firstParBeg = line.indexOf('('); var firstParEnd = sintaxCheck(line, line.indexOf('(')); var corpusBeg = line.indexOf('{'); if (corpusBeg < firstParEnd) return false; var corpusEnd = sintaxCheck(line, corpusBeg); var resultado = {}; resultado.type = 'constructor'; // pueden aparecer parámetros en blanco... resultado.parameters = line.substring(firstParBeg + 1, firstParEnd - 1).split(',').filter((x) => x.trim() !== ''); resultado.content = descompilarScript(line.substring(corpusBeg + 1, corpusEnd - 1)); resultado.remainder = line.substring(corpusEnd + 1); for (var index = 0; index < resultado.parameters.length; index++) { const element = resultado.parameters[index]; if (element.indexOf(':') !== -1) { // if the perameter is in typescript, pascal, format? Refactorize resultado.parameters[index] = { 'name': element.substring(0, element.indexOf(':')).trim(), 'type': element.substring(element.indexOf(':') + 1).trim(), }; } else { resultado.parameters[index] = {'name': element.trim(), 'type': null}; // as it was in javascript, now and forever. } } return resultado; } // TOKEN RESERVED WORD function esPalabraReservada(line) { // Determinar si es una palabra reservada const palabrasReservadas = ['abstract', 'boolean', 'break', 'byte', 'case', 'catch', 'class', 'const', 'do', 'for', 'function', 'if', 'let', 'return', 'var', 'while', 'char', 'continue', 'default', 'do', 'double', 'else', 'extends', 'false', 'final', 'finally', 'float', 'for', 'implements', 'import', 'int', 'interface', 'long', 'native', 'new', 'null', 'package', 'private', 'protected', 'public', 'short', 'static', 'super', 'switch', 'syncronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'void', 'volatile', 'rest', 'byvalue', 'cast', 'const', 'future', 'generic', 'goto', 'inner', 'operator', 'outer', 'experimental']; var savedLine = line.trim(); for (var i = 0; i < palabrasReservadas.length; i++) { if (left(savedLine, String(palabrasReservadas[i]).length) === palabrasReservadas[i]) { return true; } } return false; } function extraerPalabraReservada(line) { var resultado; resultado = extraerIdentificador(line); resultado.type = 'reserved word'; return resultado; } // TOKEN IMPORT // Extraer la importación en la variable por referencia comentario y devolver el resto... ok function esImportacion(line) { return comienzaCon(line.trim(), 'import'); // fix } // Extraer la importación en la variable por referencia comentario y devolver el resto... ok // recueda que puede darse el caso de: import * from '.'; import identificador1 from '.'; function extraerImportaciones(line) { var importaciones = []; var myLine = ''; myLine = line.trim(); if (esImportacion(line)) { myLine = line.substring(0, line.indexOf(';')); if (!myLine) { throw new Error('La importación no está bien formada o falta el símbolo de ;.'); } var imStr = transformar(myLine, 'import %a from %b', '%a'); if (esBloque(imStr)) { imStr = imStr.substring(1, imStr.length - 1).trim(); } var pathStr = transformar(myLine, 'import %a from %b', '%b'); } line = line.substring(line.indexOf(';') + 1); var resultado = {}; resultado.type = 'import'; resultado.modules = imStr; resultado.path = pathStr; resultado.remainder = line.trim(); return resultado; } // DECORATOR IMPORT here // Determinar si es un identificador function esDecorador(line) { if ((!line) || (line.length === 0) || isNumericAt(line.trim(), 0)) { return false; } line = line.trim(); var posicion = 0; return isEqualAt(line, '@', 0) && isAlphanumericAt(line, 1); } // Extraer la importación en la variable por referencia comentario y devolver el resto... ok function extraerDecorador(line) { var resultado; if (esIdentificador(line)) { resultado = extraerIdentificador(line); } if (resultado) { resultado.type = 'decorator'; } return resultado; } // Es un bloque, verificar si es un bloque... // Determinar si es un identificador function esBloque(line) { if ((!line) || (line.length === 0)) { return false; } line = line.trim(); return (left(line, 1) === '{' && sintaxCheck(line, 0) !== -1); } // Extraer la importación en la variable por referencia comentario y devolver el resto... ok function extraerBloque(line) { var resultado; line = line.trim(); if (esBloque(line)) { var posicionFinBloque = sintaxCheck(line, 0); var contenido = line.substring(1, posicionFinBloque - 1); line = line.substring(posicionFinBloque + 1); resultado = { type: 'block', body: descompilarScript(contenido), remainder: line.substring(sintaxCheck(line, line.indexOf('{')) + 1), }; } return resultado; } // TOKEN OPERATOR function esOperador(line) { // Determinar si es un operador const operadores = ['instanceof', 'typeof', '>>>=', '>>>', '>>=', '<>=', '===', '!==', '<=', '>=', '&&', '||', '++', '--', '+=', '-=', '*=', '/=', '%=', '^=', '&=', '!=', '|=', '&', '|', '^', '', '<', '>', '-', '!', '~', '+', '-', '*', '/', '%', ';', ',', '=>']; var savedLine = line.trim(); for (var i = 0; i < operadores.length; i++) { if (left(savedLine, operadores[i].length) === operadores[i]) { return operadores[i]; } } return false; } function extraerOperador(line) { var sysOp = esOperador(line); var resultado = {}; resultado.type = 'operator'; resultado.content = sysOp; line = line.substring(sysOp.length); resultado.remainder = line; return resultado; } // TOKEN SYMBOL function esSimbolo(line) { // Determinar si es un operador const simbolos = [';', '.', '@', '#', '$', '%', '^', '&', '*', '~', ':', '{', '}', '=', '[', ']', ',', '>', '<']; var savedLine = line.trim(); for (var i = 0; i < simbolos.length; i++) { if (left(savedLine, String(simbolos[i]).length) === simbolos[i]) { return simbolos[i]; } } return false; } function extraerSimbolo(line) { var sysOp = esSimbolo(line); var resultado = {}; resultado.type = 'symbol'; resultado.content = sysOp; line = line.substring(sysOp.length); resultado.remainder = line.trim(); return resultado; } //main if (!str || str === '') return {}; str = String(str).trim(); // Primero let datos = []; // Se procesa al entidad completa... var decorators = []; str = str.trim(); while (str && str !== '') { // alert(JSON.stringify(datos)); // keep for debug purposes... // eliminar espacios if (esBloque(str)) { datos.push(extraerBloque(str)); } else if (esOperador(str)) { datos.push(extraerOperador(str)); } else if (esComentario(str)) { datos.push(extraerComentario(str)); } else if (esImportacion(str)) { datos.push(extraerImportaciones(str)); } else if (esDecorador(str)) { datos.push(extraerDecorador(str)); } else if (esSimbolo(str)) { datos.push(extraerSimbolo(str)); } else if (esFuncion(str)) { datos.push(extraerFuncion(str)); } else if (esConstructorDeClase(str)) { datos.push(extraerConstructorDeClase(str)); } else if (esDeclaracionDeClase(str)) { datos.push(extraerDeclaracionDeClase(str)); // } else if (esPalabraReservada(str)) { // datos.push(extraerPalabraReservada(str)); } else if (esIdentificador(str)) { datos.push(extraerIdentificador(str)); } else if (esCadena(str)) { datos.push(extraerCadena(str)); } else if (esNumero(str)) { datos.push(extraerNumero(str)); } else if (esParenthesis(str)) { datos.push(extraerParenthesis(str)); } else { datos.push({type: 'Unknown token', content: str, remainder: ''}); } // faltan por procesar constantes numéricas, booleanas y de cadena // además de asociarle a los decoradores, el siguiente identificador si existe. // asociarle los paréntesis y los corchetes al identificador anterior., // en los casos donde sea posible, parsear el contenido en profundidad. str = String(datos[datos.length - 1].remainder).trim(); } // Los decoradores deberían meterse en una lista de decoradores y no agregarse a la lista de datos, aunque sí deberían reducir str por el remainder. // Se deben agregar al próximo elemento si no es de paréntesis, o corchetes... // En caso de que termine el ciclo entonces, si quedaron quedaron decoradores sin asignar, es decir, sin otros objetos asociados... // allí sí se agregan 1 x 1 y no de golpe. // Los paréntesis siempre se agregan al elemento anterior, en la propiedad de tipo lista: parenthesis si no existe ninguno, se ponen de primeros... su contenido también puede que se parsee en profundidad. // Los elementos que se encuentran dentro de los corchetes, también deberían parsearse en profundidad., si son complejos. // Al igual que las directivas y prefijos de alcance, experimental, public, private, export., son condiciones lógicas., que se asocian al próximo elemento. // Eliminar los remainders temporales, luego resolver utilizando nua variable global... var lineCounter = 0; while (lineCounter < datos.length) { delete datos[lineCounter].remainder; lineCounter++; } return datos; - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.