Skip to content

Commit 8ce97f3

Browse files
committed
Fix problem in closure
1 parent 8a572b0 commit 8ce97f3

File tree

15 files changed

+407
-1005
lines changed

15 files changed

+407
-1005
lines changed

dist/easycoder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4321,7 +4321,7 @@ const EasyCoder_Browser = {
43214321
}
43224322
const command = program[program.pc];
43234323
let state = program.getValue(command.state);
4324-
if (!state) {
4324+
if (state != ``) {
43254325
state = `{"script":"${program.script}"}`;
43264326
}
43274327
let title = program.getValue(command.title);

ecr/command.h

Lines changed: 16 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ class Command {
5151
public:
5252

5353
///////////////////////////////////////////////////////////////////////
54-
// Show the element list in detail
54+
// Show an element list in detail
5555
void showElements(ElementArray* elements, int indent) {
56-
// printf("\n----------\n");
5756
for (int m = 0; m < indent; m++) {
5857
print(" ");
5958
}
@@ -81,18 +80,6 @@ class Command {
8180
print("\n");
8281
}
8382

84-
///////////////////////////////////////////////////////////////////////
85-
// Get the element array
86-
// ElementArray* getElements() {
87-
// return elements;
88-
// }
89-
90-
///////////////////////////////////////////////////////////////////////
91-
// Get the size (the number of elements in the array and list combined)
92-
// int getSize() {
93-
// return elements->getSize();
94-
// }
95-
9683
///////////////////////////////////////////////////////////////////////
9784
// Get a specified Element.
9885
Element* get(ElementArray* elements, int n) {
@@ -458,6 +445,20 @@ class Command {
458445
return false;
459446
}
460447

448+
// ///////////////////////////////////////////////////////////////////////
449+
// // Find a label
450+
// Symbol* getLabel(const char* name) {
451+
// for (int n = 0; n < elements->getSize(); n++) {
452+
// Element* element = elements->get(n);
453+
// if (element == nullptr) {
454+
// continue;
455+
// // } else if (element->is(name)) {
456+
// // return value->getText();
457+
// }
458+
// }
459+
// return nullptr;
460+
// }
461+
461462
///////////////////////////////////////////////////////////////////////
462463
// Find the code for a named value property
463464
const char* getCommandPropertyCode(ElementArray* elements, const char* key) {
@@ -497,12 +498,6 @@ class Command {
497498
return value->getTextValue();
498499
}
499500

500-
///////////////////////////////////////////////////////////////////////
501-
// Flatten the element array
502-
// void flatten() {
503-
// elements->flatten();
504-
// }
505-
506501
///////////////////////////////////////////////////////////////////////
507502
// Print all the elements in the command
508503
void dump() {
@@ -526,102 +521,4 @@ class Command {
526521
noDomainValueTypes = nullptr;
527522
delete[] noDomainValueMap;
528523
}
529-
};
530-
531-
// CommandArray is a memory-efficient class for managing arrays of commands.
532-
class CommandArray {
533-
534-
private:
535-
int size = 0; // the number of items
536-
Command** array = nullptr; // the array of items
537-
LinkedList* list; // A list to hold new data items as they are added
538-
539-
public:
540-
541-
///////////////////////////////////////////////////////////////////////
542-
// Get the size (the number of elements in the array and list combined)
543-
int getSize() {
544-
return this->size + list->getSize();
545-
}
546-
547-
///////////////////////////////////////////////////////////////////////
548-
// Get a specified item.
549-
// If the index is greater than the array size but not greater than
550-
// the combined size of array and list, return the item from the list.
551-
Command* get(int n) {
552-
if (n < size) {
553-
return array[n];
554-
}
555-
else if (n < size + list->getSize()) {
556-
return (Command*)list->get(n - size);
557-
}
558-
return nullptr;
559-
}
560-
561-
///////////////////////////////////////////////////////////////////////
562-
// Add a command. This goes into the linked list.
563-
void add(Command* command) {
564-
list->add(command);
565-
}
566-
567-
///////////////////////////////////////////////////////////////////////
568-
// Flatten this item by creating a single array to hold all the data.
569-
void flatten() {
570-
Command** oldArray = array;
571-
int oldSize = size;
572-
// Create a new array big enough for the old array and the list
573-
int total = oldSize + list->getSize();
574-
if (total > 0) {
575-
array = new Command*[total];
576-
// Copy the old array to the new
577-
size = 0;
578-
while (size < oldSize) {
579-
array[size] = oldArray[size];
580-
size++;
581-
}
582-
if (oldArray != nullptr) {
583-
delete[] oldArray;
584-
}
585-
// Copy the list to the new array
586-
int n = 0;
587-
while (n < list->getSize()) {
588-
array[size++] = (Command*)list->get(n++);
589-
}
590-
list->clear();
591-
}
592-
}
593-
594-
///////////////////////////////////////////////////////////////////////
595-
// Provide info about the object
596-
void info() {
597-
print("CommandArray: list size=%d, array size=%d\n", list->getSize(), size);
598-
}
599-
600-
///////////////////////////////////////////////////////////////////////
601-
// Print all the values in the array
602-
void dump() {
603-
for (int n = 0; n < size; n++) {
604-
print("Command %d: ", n);
605-
get(n)->dump();
606-
}
607-
print("\n");
608-
}
609-
610-
///////////////////////////////////////////////////////////////////////
611-
// Default constructor
612-
CommandArray() {
613-
list = new LinkedList();
614-
}
615-
616-
///////////////////////////////////////////////////////////////////////
617-
// Destructor
618-
~CommandArray() {
619-
delete array;
620-
array = nullptr;
621-
delete list;
622-
list = nullptr;
623-
#if DESTROY
624-
print("CommandArray: Delete %s\n", name);
625-
#endif
626-
}
627-
};
524+
};

ecr/condition.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class Condition {
2020
this->negate = negate;
2121
}
2222

23+
bool isNegate() {
24+
return negate;
25+
}
26+
2327
void addValue(RuntimeValue* value) {
2428
values->add(value);
2529
}

ecr/core-conditions.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,43 @@ class CoreConditions {
5555
///////////////////////////////////////////////////////////////////////
5656
// Run a condition. All necessary information is passed in
5757
bool run(Condition* condition, Functions* functions) {
58+
bool result = false;
5859
RuntimeValue* value1;
5960
RuntimeValue* value2;
6061
const char* type = condition->getType();
61-
if (strcmp(type, "is") == 0) {
62-
value1 = condition->getValue(0);
62+
value1 = condition->getValue(0);
63+
// 'none', 'list', 'object', 'even', 'odd', 'empty'
64+
if (strcmp(type, "string") == 0) {
65+
result = (value1->getType() == TEXT_VALUE);
66+
}
67+
else if (strcmp(type, "numeric") == 0) {
68+
result = (value1->getType() == INT_VALUE);
69+
}
70+
else if (strcmp(type, "boolean") == 0) {
71+
result = (value1->getType() == BOOL_VALUE);
72+
}
73+
else if (strcmp(type, "even") == 0) {
74+
result = (value1->getIntValue() % 2 == 0);
75+
}
76+
else if (strcmp(type, "odd") == 0) {
77+
result = (value1->getIntValue() % 2 == 1);
78+
}
79+
else if (strcmp(type, "empty") == 0) {
80+
result = (strlen(value1->getTextValue()) == 0);
81+
}
82+
else if (strcmp(type, "is") == 0) {
6383
value2 = condition->getValue(1);
64-
return (value1->getIntValue() == value2->getIntValue());
84+
result = (value1->getIntValue() == value2->getIntValue());
6585
}
66-
if (strcmp(type, "greater") == 0) {
67-
value1 = condition->getValue(0);
86+
else if (strcmp(type, "greater") == 0) {
6887
value2 = condition->getValue(1);
69-
return (value1->getIntValue() > value2->getIntValue());
88+
result = (value1->getIntValue() > value2->getIntValue());
7089
}
71-
if (strcmp(type, "less") == 0) {
72-
value1 = condition->getValue(0);
90+
else if (strcmp(type, "less") == 0) {
7391
value2 = condition->getValue(1);
74-
return (value1->getIntValue() < value2->getIntValue());
92+
result = (value1->getIntValue() < value2->getIntValue());
7593
}
76-
return false;
94+
return condition->isNegate() ? !result : result;
7795
}
7896

7997
///////////////////////////////////////////////////////////////////////

ecr/core-keywords.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ class CoreKeywords {
147147
case EXIT:
148148
return FINISHED;
149149
// case FILE:
150-
// case FORK:
150+
case FORK: {
151+
runtime->getThreads()->add(new Thread(0, next));
152+
Text* name = runtime->getCommandProperty(elements, "fork");
153+
Symbol* label = runtime->getLabel(name->getText());
154+
return next;
155+
}
151156
// case GET:
152157
// case GOSUB:
153158
// case GO:
@@ -208,7 +213,10 @@ class CoreKeywords {
208213
// case SCRIPT:
209214
case SET: {
210215
Text* type = runtime->getParameter(elements, "type");
211-
if (type->is("elements")) {
216+
if (type->is("set")) {
217+
Symbol* target = runtime->getSymbol(elements, "target");
218+
target->setBoolValue(true);
219+
} else if (type->is("elements")) {
212220
Symbol* symbol = runtime->getCommand()->getSymbol(elements, "name");
213221
runtimeValue = runtime->getRuntimeValue(elements, "value");
214222
symbol->setElements(runtimeValue->getIntValue());

ecr/run.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Run {
1010
SymbolArray* symbols;
1111
Functions* functions;
1212
ElementArray* currentElements;
13+
int codeSize;
1314
// Domain-specific
1415
CoreKeywords* coreKeywords;
1516
CoreValues* coreValues;
@@ -25,6 +26,7 @@ class Run {
2526
runtime->setKeyArray(keyArray);
2627
runtime->setCommands(commands);
2728
runtime->setSymbols(symbols);
29+
runtime->setCodeSize(codeSize);
2830
// Set the value handlers for each domain
2931
runtime->setCoreValues(coreValues);
3032
}
@@ -35,7 +37,7 @@ class Run {
3537
// Run a sequence of commands from the current program counter
3638
int runFrom(Runtime* runtime, int pc) {
3739
do {
38-
// print("Command %d\n", pc);
40+
print("Command %d\n", pc);
3941
currentElements = commands[pc];
4042
Command* command = new Command(functions);
4143
command->setKeyArray(keyArray);
@@ -183,10 +185,11 @@ class Run {
183185
// Create a "functions" object
184186
functions = new Functions(keyArray, symbols);
185187

186-
int codeSize = codeArray->getSize();
188+
codeSize = codeArray->getSize();
187189
// Create the array of commands and a list of initial keywords
188190
commands = new ElementArray*[codeSize];
189191
for (int n = 0; n < codeSize; n++) {
192+
print("Parse line %d\n", n);
190193
Text* codeLine = codeArray->get(n);
191194
TextArray* ta = split(codeLine, ',');
192195

@@ -199,6 +202,7 @@ class Run {
199202
Command* command = new Command(functions);
200203

201204
// Get the domain - item 1 in the command
205+
elements->dump();
202206
int domainIndex = command->getElementCode(elements, 1);
203207
// Get the keyword - item 2 in the command
204208
Text* tt = keyArray->get(command->getElementCode(elements, 2));

ecr/runtime.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Runtime {
1919
KeywordArray* keywordArray;
2020
// The command that is to be executed
2121
Command* command;
22+
// The number of code lines
23+
int codeSize;
2224
// The current program counter
2325
int pc;
2426
// The values in the 'core' domain
@@ -44,6 +46,8 @@ class Runtime {
4446
KeywordArray* getKeywordArray() { return keywordArray; }
4547
void setCommand(Command* arg) { command = arg; }
4648
Command* getCommand() { return command; }
49+
void setCodeSize(int arg) { codeSize = arg; }
50+
int getCodeSize() { return codeSize; }
4751
void setPC(int arg) { pc = arg; }
4852
int getPC() { return pc; }
4953
// Domain-specific
@@ -99,6 +103,35 @@ class Runtime {
99103
Text* getValueProperty(ElementArray* elements, const char* key) {
100104
return functions->getValueProperty(elements, key);
101105
}
106+
107+
///////////////////////////////////////////////////////////////////////
108+
// Find the code for a named value property
109+
Text* getCommandProperty(ElementArray* elements, const char* key) {
110+
return command->getCommandProperty(elements, key);
111+
}
112+
113+
///////////////////////////////////////////////////////////////////////
114+
// Find a label
115+
Symbol* getLabel(const char* name) {
116+
for (int n = 0; n < codeSize; n++) {
117+
ElementArray* elements = commands[n];
118+
command->showElements(elements, 0);
119+
const char* keyword = command->getCommandPropertyCode(elements, "keyword");
120+
if (keyword == nullptr) {
121+
continue;
122+
}
123+
if (strcmp(keyword, "label") == 0) {
124+
RuntimeValue* value = getRuntimeValue(elements, "name");
125+
if (value->getTextValue() != nullptr) {
126+
Symbol* symbol = getSymbol(elements, name);
127+
if (symbol != nullptr) {
128+
return symbol;
129+
}
130+
}
131+
}
132+
}
133+
return nullptr;
134+
}
102135

103136
///////////////////////////////////////////////////////////////////////
104137
// Get the line number

0 commit comments

Comments
 (0)