Skip to content

Commit

Permalink
Merge pull request #261 from uqbar-project/fix-link-new-sentence
Browse files Browse the repository at this point in the history
Fix link new sentence
  • Loading branch information
PalumboN authored Aug 17, 2024
2 parents 679cade + fe05120 commit 6e7e032
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 33 deletions.
53 changes: 37 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ export default (newPackages: List<Package>, baseEnvironment?: Environment): Envi
return environment
}

export function linkSentenceInNode<S extends Sentence>(newSentence: S, parentNode: Node): void {
const { scope } = parentNode
export function linkSentenceInNode<S extends Sentence>(newSentence: S, context: Node): void {
const { scope, environment } = context
// Register top contributions into context's scope
scope.register(...scopeContribution(newSentence))
newSentence.reduce((parentScope: Scope, node: Node) => {
// Create scopes for sub-nodes and link nodes (chain)
newSentence.reduce((parentScope: Scope, node: Node, parent?: Node) => {
const localScope = new LocalScope(parentScope, ...scopeContribution(node))
Object.assign(node, { scope: localScope, environment: parentNode.environment })
Object.assign(node, { id: uuid(), scope: localScope, environment, parent: parent ?? context })
return localScope
}, scope)
}
45 changes: 32 additions & 13 deletions test/linker.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { expect, should, use } from 'chai'
import { GAME_MODULE, OBJECT_MODULE } from '../src'
import { getPotentiallyUninitializedLazy } from '../src/decorators'
import link, { canBeReferenced, linkSentenceInNode } from '../src/linker'
import { Body, Class, Closure, Describe, Field, Import, Method, Mixin, NamedArgument, Package, Parameter, ParameterizedType, Reference, Return, Singleton, Test, Variable } from '../src/model'
import { linkerAssertions } from './assertions'
import { Evaluation, GAME_MODULE, Interpreter, OBJECT_MODULE, WRENatives } from '../src'
import { Body, Class, Closure, Describe, Environment, Field, Import, Method, Mixin, NamedArgument, Node, Package, Parameter, ParameterizedType, Reference, Return, Sentence, Singleton, Test, Variable } from '../src/model'
import * as parse from '../src/parser'
import { WREEnvironment, environmentWithEntities } from './utils'
import { linkerAssertions } from './assertions'
import { environmentWithEntities, WREEnvironment } from './utils'

should()
use(linkerAssertions)
Expand Down Expand Up @@ -837,15 +838,33 @@ describe('can be referenced', () => {

describe('link sentence in node', () => {
const replPackage = new Package({ name: 'repl' })
const environment = link([replPackage], WREEnvironment)
new Interpreter(Evaluation.build(environment, WRENatives))

it('should link a new sentence for an existing node', () => {
const replScope = environment.getNodeByFQN('repl').scope
replScope.localContributions().should.be.empty
const aAssignmentSentence = parse.Variable.tryParse('const a = 4')
linkSentenceInNode(aAssignmentSentence, environment.getNodeByFQN('repl'))
const [variableName] = replScope.localContributions()[0]
let environment: Environment
let repl: Node
let newSentence: Sentence

beforeEach(() => {
environment = link([replPackage], WREEnvironment)
repl = environment.getNodeByFQN('repl')
newSentence = parse.Variable.tryParse('const a = 4')
})

it('should link new nodes', () => {
newSentence.id?.should.be.undefined
getPotentiallyUninitializedLazy(newSentence, 'parent')?.should.be.undefined
getPotentiallyUninitializedLazy(newSentence, 'environment')?.should.be.undefined

linkSentenceInNode(newSentence, repl)
newSentence.id.should.be.ok
newSentence.environment.should.be.eq(environment)
newSentence.parent.should.be.eq(repl)
newSentence.children[0].parent.should.be.eq(newSentence)
})

it('should add new contributions to context scope', () => {
repl.scope.localContributions().should.be.empty

linkSentenceInNode(newSentence, repl)
const [variableName] = repl.scope.localContributions()[0]
variableName.should.be.equal('a')
})

Expand Down

0 comments on commit 6e7e032

Please sign in to comment.