Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Slartibartfass2 committed Dec 4, 2024
1 parent 4914b6c commit 25d8b0d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
12 changes: 12 additions & 0 deletions example1.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
algebra_grades <- list(test = 1.0, exam = 3.0)
algebra_grades$test <- 4.0
grades <- list(algebra = algebra_grades, sports = 1.7)
grades$sports <- 1.0
person <- list(name = "John", grades = grades)
person$name <- "Jane"
result <- person$name

# grades <- list(algebra = 1.3, sports = 1.7)
# grades$algebra <- 1.0
# person <- list(name = "John", grades = grades)
# result <- person$grades$algebra
11 changes: 11 additions & 0 deletions example2.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
person <- list(age = 24, name = "John")
person$is_male <- TRUE

if (length(person) >= 3) {
person$name <- "Jane"
} else {
person$name <- "Lorane"
}

result <- person$name
result
17 changes: 17 additions & 0 deletions src/dataflow/environments/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import { cloneEnvironmentInformation } from './clone';
import type { IdentifierDefinition, InGraphIdentifierDefinition } from './identifier';
import type { ContainerIndex, ContainerIndicesCollection } from '../graph/vertex';

function printDefChanges(newEnvironments: IEnvironment, name: string) {
const defs = newEnvironments.memory.get(name);
console.log('after');
for(const element of defs ?? []) {
const def = element as InGraphIdentifierDefinition;
console.log(def.name);
for(const index of def.indicesCollection ?? []) {
console.log(index.indices, index.isSingleIndex);
}
}
}

function defInEnv(newEnvironments: IEnvironment, name: string, definition: IdentifierDefinition) {
const existing = newEnvironments.memory.get(name);
Expand All @@ -14,6 +25,7 @@ function defInEnv(newEnvironments: IEnvironment, name: string, definition: Ident
const inGraphDefinition = definition as InGraphIdentifierDefinition;
if(existing !== undefined && inGraphDefinition.indicesCollection !== undefined && inGraphDefinition.controlDependencies === undefined) {
newEnvironments.memory.set(name, mergeIndices(existing, inGraphDefinition));
printDefChanges(newEnvironments, name);
return;
}

Expand Down Expand Up @@ -42,6 +54,7 @@ function mergeIndices(existing: IdentifierDefinition[], definition: InGraphIdent
if(existingDef.indicesCollection === undefined) {
continue;
}
// console.log(existingDef);
const newIndicesCollection: ContainerIndicesCollection = [];
for(const indices of existingDef.indicesCollection) {
let newIndices: ContainerIndex[];
Expand All @@ -59,6 +72,7 @@ function mergeIndices(existing: IdentifierDefinition[], definition: InGraphIdent
});
}
}
// console.log('newIndicesCollection', newIndicesCollection);

// if indices are now empty list, don't keep empty definition
if(newIndicesCollection.length > 0) {
Expand All @@ -69,6 +83,7 @@ function mergeIndices(existing: IdentifierDefinition[], definition: InGraphIdent
}
}
}
// console.log('newExistingDefs', newExistingDefs);
// store changed existing definitons and add new one
return [...newExistingDefs, definition];
}
Expand All @@ -79,6 +94,8 @@ function mergeIndices(existing: IdentifierDefinition[], definition: InGraphIdent
*/
export function define(definition: IdentifierDefinition, superAssign: boolean | undefined, environment: REnvironmentInformation): REnvironmentInformation {
const name = definition.name;
// console.log('defining:', name);
// console.log('definition:', definition);
guard(name !== undefined, () => `Name must be defined, but isn't for ${JSON.stringify(definition)}`);
let newEnvironment;
if(superAssign) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { InGraphIdentifierDefinition } from '../../../../../environments/id
import { resolveByName } from '../../../../../environments/resolve-by-name';
import type { ContainerIndex, ContainerIndicesCollection, ContainerParentIndex } from '../../../../../graph/vertex';
import type { RArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument';
import { RoleInParent } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/role';

interface TableAssignmentProcessorMarker {
definitionRootNodes: NodeId[]
Expand Down Expand Up @@ -188,13 +189,30 @@ function processStringBasedAccess<OtherInfo>(
}
}

// console.log('processing string based access of', name, newArgs);
// element of list of list
// accessed: type: RArgument, value: RAccess
// access: type: RArgument, value: RString
// list of list:
// accessed: type: RArgument, value: RSymbol
// access: type: RArgument, value: RString
// element of list:
// accessed: type: RArgument, value: RSymbol
// access: type: RArgument, value: RString

// if value is not symbol but access, resolve access first

const nonEmptyArgs = newArgs.filter(arg => arg !== EmptyArgument);
const accessedArg = nonEmptyArgs.find(arg => arg.info.role === RoleInParent.Accessed); // or just nonEmptyArgs[0] or newArgs[0]
const accessArg = nonEmptyArgs.find(arg => arg.info.role === RoleInParent.IndexAccess); // or just nonEmptyArgs[1] or newArgs[1]
let accessedIndicesCollection: ContainerIndicesCollection;
if(newArgs[0] !== EmptyArgument) {
const accessArg = newArgs[1] === EmptyArgument ? undefined : newArgs[1].lexeme;
const resolvedFirstParameter = resolveByName(newArgs[0].lexeme ?? '', data.environment);
const indicesCollection = resolvedFirstParameter?.flatMap(param => (param as InGraphIdentifierDefinition)?.indicesCollection ?? []);
if(accessArg !== undefined && accessedArg != undefined) {
console.log('accessed', accessedArg);
console.log('access', accessArg);
const resolvedAccessedArg = resolveByName(accessedArg.lexeme ?? '', data.environment);
const indicesCollection = resolvedAccessedArg?.flatMap(param => (param as InGraphIdentifierDefinition)?.indicesCollection ?? []);
for(const indices of indicesCollection ?? []) {
const filteredIndices = indices.indices.filter(index => index.lexeme === accessArg);
const filteredIndices = indices.indices.filter(index => index.lexeme === accessArg.lexeme);
if(filteredIndices.length == 0) {
continue;
}
Expand All @@ -204,6 +222,17 @@ function processStringBasedAccess<OtherInfo>(
isSingleIndex: indices.isSingleIndex
});
}
console.log('---------');
console.log('resolving', resolvedAccessedArg);
console.log('collection');
for(const element of indicesCollection ?? []) {
console.log(element);
}
console.log('accessed');
for(const element of accessedIndicesCollection ?? []) {
console.log(element);
}
console.log('---------');
}

const fnCall = processKnownFunctionCall({ name, args: newArgs, rootId, data, forceArgs: config.forceArgs }, accessedIndicesCollection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ export function markAsAssignment(
if(sourceIds.length === 1) {
// support for tracking indices
indicesCollection = information.graph.getVertex(sourceIds[0])?.indicesCollection;
// if(indicesCollection) {
// const indicesCollectionString = indicesCollection.map(indices =>
// indices.indices.map(index => `{ lexeme: ${index.lexeme}, nodeId: ${index.nodeId} }`).join(', ')
// ).join(', ');
// console.log(`Defining indices ${indicesCollectionString} for ${nodeToDefine.name}`);
// }
}
if(config?.indicesCollection !== undefined) {
indicesCollection = (indicesCollection ?? []).concat(config.indicesCollection);
Expand Down
1 change: 1 addition & 0 deletions src/dataflow/internal/process/functions/call/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export function processAllArguments<OtherInfo>(
// When only a single index is referenced, we don't need to reference the whole object
const resolvedInGraphDef = resolved as InGraphIdentifierDefinition;
const isSingleIndex = resolvedInGraphDef?.indicesCollection?.every((indices) => indices.isSingleIndex);
// TODO
if(!isSingleIndex) {
finalGraph.addEdge(ingoing.nodeId, resolved.nodeId, EdgeType.Reads);
}
Expand Down

0 comments on commit 25d8b0d

Please sign in to comment.