Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mover funciones de definitions.ts a wollok-ts #162

Closed
fdodino opened this issue Apr 2, 2024 · 1 comment
Closed

Mover funciones de definitions.ts a wollok-ts #162

fdodino opened this issue Apr 2, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request severity: 1 - minor Low priority issue

Comments

@fdodino
Copy link
Contributor

fdodino commented Apr 2, 2024

Hoy tenemos toda esta funcionalidad que parece que podría migrarse toda a wollok-ts:

// WOLLOK-TS: hablar con Nahue/Ivo, para mí desde acá para abajo todo se podria migrar a wollok-ts
export const getNodeDefinition = (environment: Environment) => (node: Node): Node[] => {
  try {
    return match(node)(
      when(Reference)(node => definedOrEmpty(referenceDefinition(node))),
      when(Send)(sendDefinitions(environment)),
      when(Super)(node => definedOrEmpty(superMethodDefinition(node))),
      when(Self)(node => definedOrEmpty(node.ancestors.find(is(Module))))
    )
  } catch (error) {
    logger.error(`✘ Error in getNodeDefinition: ${error}`, error)
    return [node]
  }
}

function referenceDefinition(ref: Reference<Node>): Node | undefined {
  return ref.target
}


const sendDefinitions = (environment: Environment) => (send: Send): Method[] => {
  try {
    return match(send.receiver)(
      when(Reference)(node => {
        const target = node.target
        return target && is(Singleton)(target) ?
          definedOrEmpty(target.lookupMethod(send.message, send.args.length))
          : allMethodDefinitions(environment, send)
      }),
      when(New)(node => definedOrEmpty(node.instantiated.target?.lookupMethod(send.message, send.args.length))),
      when(Self)(_ => moduleFinderWithBackup(environment, send)(
        (module) => definedOrEmpty(module.lookupMethod(send.message, send.args.length))
      )),
    )
  } catch (error) {
    logger.error(`✘ Send definitions error: ${error}`, error)
    return allMethodDefinitions(environment, send)
  }
}

function superMethodDefinition(superNode: Super): Method | undefined {
  const currentMethod = superNode.ancestors.find(is(Method))!
  const module = superNode.ancestors.find(is(Module))
  return module ? module.lookupMethod(currentMethod.name, superNode.args.length, { lookupStartFQN: module.fullyQualifiedName }) : undefined
}

function allMethodDefinitions(environment: Environment, send: Send): Method[] {
  const arity = send.args.length
  const name = send.message
  return environment.descendants.filter(n =>
    is(Method)(n) &&
    n.name === name &&
    n.parameters.length === arity
  ) as Method[]
}


// UTILS
const moduleFinderWithBackup = (environment: Environment, send: Send) => (methodFinder: (module: Module) => Method[]) => {
  const module = send.ancestors.find(is(Module))
  return module ? methodFinder(module) : allMethodDefinitions(environment, send)
}

function definedOrEmpty<T>(value: T | undefined): T[] {
  return value ? [value] : []
}
@fdodino fdodino added enhancement New feature or request severity: 1 - minor Low priority issue labels Apr 2, 2024
@fdodino
Copy link
Contributor Author

fdodino commented May 4, 2024

Update, se migraron algunas, quedó pendiente ésto:

// TODO: terminar de migrar a wollok-ts estas 4 definiciones
export const getNodeDefinition = (environment: Environment) => (node: Node): Node[] => {
  try {
    return match(node)(
      when(Reference)(node => definedOrEmpty(node.target)),
      when(Send)(sendDefinitions(environment)),
      when(Super)(node => definedOrEmpty(superMethodDefinition(node))),
      when(Self)(node => definedOrEmpty(getParentModule(node)))
    )
  } catch {
    return [node]
  }
}

const superMethodDefinition = (superNode: Super): Method | undefined => {
  const currentMethod = superNode.ancestors.find(is(Method))!
  const module = getParentModule(superNode)
  return module ? module.lookupMethod(currentMethod.name, superNode.args.length, { lookupStartFQN: module.fullyQualifiedName }) : undefined
}

const getParentModule = (node: Node) => node.ancestors.find(is(Module))

const definedOrEmpty = <T>(value: T | undefined): T[] => value ? [value] : []

fdodino added a commit to uqbar-project/wollok-ts that referenced this issue Oct 6, 2024
@fdodino fdodino self-assigned this Oct 6, 2024
fdodino added a commit that referenced this issue Oct 7, 2024
fdodino added a commit that referenced this issue Oct 9, 2024
fdodino added a commit that referenced this issue Oct 9, 2024
* Fix #128

* Fix #162

* Undo local dependency

* Undo local dependency

* Fix #186

* add game port number configuration

* i18n command message

* i18n extension texts

* 📝 Update contributors list

* Updating yarn.lock

* Updating wollok-ts dependency

* Fix yarn.lock & tests

---------

Co-authored-by: Publishing Bot <action@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request severity: 1 - minor Low priority issue
Projects
None yet
Development

No branches or pull requests

1 participant