Skip to content

Commit

Permalink
Bad state WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Mar 10, 2024
1 parent 0eff0a2 commit fde928f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"files.trimTrailingWhitespace": true
"files.trimTrailingWhitespace": true,
"typescript.preferences.quoteStyle": "single",
"typescript.format.semicolons": "remove"
}
3 changes: 2 additions & 1 deletion src/commands/extrasGame.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Interpreter, RuntimeObject } from 'wollok-ts'
import { Interpreter } from 'wollok-ts/dist/interpreter/interpreter'
import { RuntimeObject } from 'wollok-ts'

const { round } = Math

Expand Down
25 changes: 22 additions & 3 deletions src/commands/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@ import http from 'http'
import logger from 'loglevel'
import { CompleterResult, Interface, createInterface as Repl } from 'readline'
import { Server, Socket } from 'socket.io'
import { Entity, Environment, Evaluation, Import, Interpreter, link, notEmpty, Package, Reference, Sentence, WollokException, parse, TO_STRING_METHOD, RuntimeObject } from 'wollok-ts'
import { Entity, Environment, Evaluation, Import, link, notEmpty, Package, Reference, Sentence, WollokException, parse, TO_STRING_METHOD, RuntimeObject, LocalScope, List, Name, Node, Field, Parameter } from 'wollok-ts'
import { ParseError } from 'wollok-ts/dist/parser'
import natives from 'wollok-ts/dist/wre/wre.natives'
import { getDataDiagram } from '../services/diagram-generator'
import { buildEnvironmentForProject, failureDescription, getFQN, publicPath, successDescription, valueDescription, validateEnvironment, handleError, ENTER, serverError, stackTrace, replIcon } from '../utils'
import { logger as fileLogger } from '../logger'
import { TimeMeasurer } from '../time-measurer'
import { linkSentenceWithPackage } from 'wollok-ts/dist/linker'
import { Interpreter } from 'wollok-ts/dist/interpreter/interpreter'

export const REPL = 'REPL'

// TODO:
// - autocomplete piola

// This is a fake linking, TS should give us a better API
export function linkSentence<S extends Sentence>(newSentence: S, environment: Environment): void {
const { scope } = replNode(environment)
scope.register(...scopeContribution(newSentence))
newSentence.reduce((parentScope, node) => {
const localScope = new LocalScope(parentScope, ...scopeContribution(node))
Object.assign(node, { scope: localScope, environment })
return localScope
}, scope)
}

const scopeContribution = (contributor: Node): List<[Name, Node]> => {
if (canBeReferenced(contributor))
return contributor.name ? [[contributor.name, contributor]] : []
return []
}
const canBeReferenced = (node: Node): node is Entity | Field | Parameter => node.is(Entity) || node.is(Field) || node.is(Parameter)
// ========================================================================================================================

export type Options = {
project: string
skipValidations: boolean,
Expand Down Expand Up @@ -201,7 +220,7 @@ export function interprete(interpreter: Interpreter, line: string): string {

if (sentenceOrImport.is(Sentence)) {
const environment = interpreter.evaluation.environment
linkSentenceWithPackage(sentenceOrImport, environment, replNode(environment))
linkSentence(sentenceOrImport, environment)
const unlinkedNode = [sentenceOrImport, ...sentenceOrImport.descendants].find(_ => _.is(Reference) && !_.target)

if (unlinkedNode) {
Expand Down
5 changes: 3 additions & 2 deletions src/services/diagram-generator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ElementDefinition } from 'cytoscape'
import { BOOLEAN_MODULE, CLOSURE_MODULE, DATE_MODULE, DICTIONARY_MODULE, Entity, getImportedEntities, InnerValue, Interpreter, KEYWORDS, LIST_MODULE, NUMBER_MODULE, Package, PAIR_MODULE, RANGE_MODULE, RuntimeObject, STRING_MODULE, TO_STRING_METHOD, WOLLOK_BASE_PACKAGE } from 'wollok-ts'
import { BOOLEAN_MODULE, CLOSURE_MODULE, DATE_MODULE, DICTIONARY_MODULE, Entity, InnerValue, KEYWORDS, LIST_MODULE, NUMBER_MODULE, Package, PAIR_MODULE, RANGE_MODULE, RuntimeObject, STRING_MODULE, TO_STRING_METHOD, WOLLOK_BASE_PACKAGE } from 'wollok-ts'
import { Interpreter } from 'wollok-ts/dist/interpreter/interpreter'
import { REPL, replNode } from '../commands/repl'
import { isREPLConstant } from '../utils'

type objectType = 'literal' | 'object' | 'null'

export function getDataDiagram(interpreter: Interpreter, rootFQN?: Package): ElementDefinition[] {
const environment = interpreter.evaluation.environment
const importedFromConsole = getImportedEntities(interpreter, replNode(environment), rootFQN)
const importedFromConsole = (replNode(environment) ?? rootFQN!).allScopedEntities()
const currentFrame = interpreter.evaluation.currentFrame
const objects = new Map(Array.from(currentFrame.locals.keys()).map((name) => [name, currentFrame.get(name)]))

Expand Down
3 changes: 1 addition & 2 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { buildEnvironmentForProject, failureDescription, getFQN, handleError, pr
import chaiAsPromised from 'chai-as-promised'
import chai from 'chai'
import { spyCalledWithSubstring } from './assertions'
import { Problem, WOLLOK_EXTRA_STACK_TRACE_HEADER, validate } from 'wollok-ts'
import { List } from 'wollok-ts/dist/extensions'
import { Problem, WOLLOK_EXTRA_STACK_TRACE_HEADER, validate, List } from 'wollok-ts'

describe('build & validating environment', () => {

Expand Down

0 comments on commit fde928f

Please sign in to comment.