Skip to content

Commit eb4cc5d

Browse files
authored
Merge pull request #122 from sugarlabs/syntax-tree-snapshot-data-value
Syntax tree: Provide initialisation values in snapshot of data elements
2 parents 03258e7 + 2ce9590 commit eb4cc5d

File tree

9 files changed

+340
-82
lines changed

9 files changed

+340
-82
lines changed

src/@types/specification.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TData } from './data';
1+
import { TData, TDataName } from './data';
22
import {
33
IElementData,
44
IElementExpression,
@@ -20,6 +20,11 @@ export interface IElementSpecificationData {
2020
type: 'Data';
2121
category: string;
2222
prototype: (name: string, label: string) => IElementData<TData>;
23+
values?:
24+
| string[]
25+
| {
26+
types: TDataName[];
27+
};
2328
}
2429

2530
/** Type for the specification entry object for data elements. */
@@ -108,6 +113,11 @@ export interface IElementSpecification {
108113
forbiddenNestInside?: string[];
109114
allowNestInside?: string[] | boolean;
110115
forbidNestInside?: string[] | boolean;
116+
values?:
117+
| string[]
118+
| {
119+
types: TDataName[];
120+
};
111121
}
112122

113123
/** Type for the snapshot of an element's specification. */

src/@types/syntaxTree.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
export interface ITreeSnapshotDataInput {
55
/** Name of the data element. */
66
elementName: string;
7+
/** Value to initialize data element with. */
8+
value?: string;
79
}
810

911
/** Type definition for the snapshot of a data element. */

src/execution/interpreter.spec.ts

Lines changed: 16 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { run } from './interpreter';
22

3-
import { generateFromSnapshot, generateSnapshot, getNode } from '../syntax/tree/syntaxTree';
3+
import { generateFromSnapshot, generateSnapshot } from '../syntax/tree/syntaxTree';
44

55
import { registerElementSpecificationEntries } from '../syntax/specification/specification';
66
import elementSpecification from '../library/specification';
7-
import { getInstance } from '../syntax/warehouse/warehouse';
87

98
// -------------------------------------------------------------------------------------------------
109

@@ -37,9 +36,11 @@ describe('Interpreter', () => {
3736
argMap: {
3837
name: {
3938
elementName: 'value-string',
39+
value: 'a',
4040
},
4141
value: {
4242
elementName: 'value-number',
43+
value: '0',
4344
},
4445
},
4546
},
@@ -48,9 +49,11 @@ describe('Interpreter', () => {
4849
argMap: {
4950
name: {
5051
elementName: 'value-string',
52+
value: 'b',
5153
},
5254
value: {
5355
elementName: 'value-number',
56+
value: '1',
5457
},
5558
},
5659
},
@@ -59,9 +62,11 @@ describe('Interpreter', () => {
5962
argMap: {
6063
name: {
6164
elementName: 'value-string',
65+
value: 'c',
6266
},
6367
value: {
6468
elementName: 'value-number',
69+
value: '1',
6570
},
6671
},
6772
},
@@ -70,6 +75,7 @@ describe('Interpreter', () => {
7075
argMap: {
7176
times: {
7277
elementName: 'value-number',
78+
value: '10',
7379
},
7480
},
7581
scope: [
@@ -78,6 +84,7 @@ describe('Interpreter', () => {
7884
argMap: {
7985
value: {
8086
elementName: 'boxidentifier-number',
87+
value: 'c',
8188
},
8289
},
8390
},
@@ -86,15 +93,18 @@ describe('Interpreter', () => {
8693
argMap: {
8794
name: {
8895
elementName: 'value-string',
96+
value: 'c',
8997
},
9098
value: {
9199
elementName: 'operator-math-plus',
92100
argMap: {
93101
operand1: {
94102
elementName: 'boxidentifier-number',
103+
value: 'a',
95104
},
96105
operand2: {
97106
elementName: 'boxidentifier-number',
107+
value: 'b',
98108
},
99109
},
100110
},
@@ -105,9 +115,11 @@ describe('Interpreter', () => {
105115
argMap: {
106116
name: {
107117
elementName: 'value-string',
118+
value: 'a',
108119
},
109120
value: {
110121
elementName: 'boxidentifier-number',
122+
value: 'b',
111123
},
112124
},
113125
},
@@ -116,9 +128,11 @@ describe('Interpreter', () => {
116128
argMap: {
117129
name: {
118130
elementName: 'value-string',
131+
value: 'b',
119132
},
120133
value: {
121134
elementName: 'boxidentifier-number',
135+
value: 'c',
122136
},
123137
},
124138
},
@@ -132,75 +146,6 @@ describe('Interpreter', () => {
132146
});
133147
const snapshot = generateSnapshot();
134148

135-
getInstance(
136-
// @ts-ignore
137-
getNode(snapshot.process[0].scope[0].argMap['name']['nodeID'])!.instanceID
138-
)!.instance.updateLabel('a');
139-
getInstance(
140-
// @ts-ignore
141-
getNode(snapshot.process[0].scope[0].argMap['value']['nodeID'])!.instanceID
142-
)!.instance.updateLabel('0');
143-
getInstance(
144-
// @ts-ignore
145-
getNode(snapshot.process[0].scope[1].argMap['name']['nodeID'])!.instanceID
146-
)!.instance.updateLabel('b');
147-
getInstance(
148-
// @ts-ignore
149-
getNode(snapshot.process[0].scope[1].argMap['value']['nodeID'])!.instanceID
150-
)!.instance.updateLabel('1');
151-
getInstance(
152-
// @ts-ignore
153-
getNode(snapshot.process[0].scope[2].argMap['name']['nodeID'])!.instanceID
154-
)!.instance.updateLabel('c');
155-
getInstance(
156-
// @ts-ignore
157-
getNode(snapshot.process[0].scope[2].argMap['value']['nodeID'])!.instanceID
158-
)!.instance.updateLabel('1');
159-
getInstance(
160-
// @ts-ignore
161-
getNode(snapshot.process[0].scope[3].argMap['times']['nodeID'])!.instanceID
162-
)!.instance.updateLabel('10');
163-
getInstance(
164-
// @ts-ignore
165-
getNode(snapshot.process[0].scope[3].scope[0].argMap['value']['nodeID'])!.instanceID
166-
)!.instance.updateLabel('c');
167-
getInstance(
168-
// @ts-ignore
169-
getNode(snapshot.process[0].scope[3].scope[1].argMap['name']['nodeID'])!.instanceID
170-
)!.instance.updateLabel('c');
171-
getInstance(
172-
getNode(
173-
// @ts-ignore
174-
snapshot.process[0].scope[3].scope[1].argMap['value']['argMap']['operand1'][
175-
'nodeID'
176-
]
177-
)!.instanceID
178-
)!.instance.updateLabel('a');
179-
getInstance(
180-
getNode(
181-
// @ts-ignore
182-
snapshot.process[0].scope[3].scope[1].argMap['value']['argMap']['operand2'][
183-
'nodeID'
184-
]
185-
)!.instanceID
186-
)!.instance.updateLabel('b');
187-
getInstance(
188-
// @ts-ignore
189-
getNode(snapshot.process[0].scope[3].scope[2].argMap['name']['nodeID'])!.instanceID
190-
)!.instance.updateLabel('a');
191-
getInstance(
192-
// @ts-ignore
193-
getNode(snapshot.process[0].scope[3].scope[2].argMap['value']['nodeID'])!.instanceID
194-
)!.instance.updateLabel('b');
195-
getInstance(
196-
// @ts-ignore
197-
getNode(snapshot.process[0].scope[3].scope[3].argMap['name']['nodeID'])!.instanceID
198-
)!.instance.updateLabel('b');
199-
getInstance(
200-
// @ts-ignore
201-
getNode(snapshot.process[0].scope[3].scope[3].argMap['value']['nodeID'])!.instanceID
202-
)!.instance.updateLabel('c');
203-
204149
const node = snapshot.process[0];
205150
run(node.nodeID);
206151
});

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
getElementCategories,
2222
resetElementSpecificationTable,
2323
getSpecificationSnapshot,
24+
checkValueAssignment,
2425
} from './syntax/specification/specification';
2526

2627
// -- syntax tree ----------------------------------------------------------------------------------
@@ -35,6 +36,7 @@ export {
3536
generateSnapshot,
3637
generateFromSnapshot,
3738
resetSyntaxTree,
39+
assignNodeValue,
3840
} from './syntax/tree/syntaxTree';
3941

4042
// -- warehouse ------------------------------------------------------------------------------------

src/library/specification.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,18 @@ const _elementSpecificationEntries: {
5050
type: 'Data',
5151
category: 'value',
5252
prototype: ElementValueBoolean,
53+
values: {
54+
types: ['boolean'],
55+
},
5356
},
5457
'value-number': {
5558
label: '0',
5659
type: 'Data',
5760
category: 'value',
5861
prototype: ElementValueNumber,
62+
values: {
63+
types: ['number'],
64+
},
5965
},
6066
'value-string': {
6167
label: 'string',

0 commit comments

Comments
 (0)