Skip to content

Commit

Permalink
Update parser.js
Browse files Browse the repository at this point in the history
  • Loading branch information
franck403 authored Oct 8, 2024
1 parent c824a84 commit 7b776be
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,22 @@ class ChatterScript {
if (line.startsWith('on ')) {
const eventName = line.split(' ')[1].replace(':', '');
this.events[eventName] = [];
this.log.push(`Event handler for '${eventName}' registered.`);
}

// Handle class definitions
else if (line.startsWith('class ')) {
const className = line.split(' ')[1].replace(':', '');
this.classes[className] = {};
currentClass = className;
this.log.push(`Class '${className}' defined.`);
}

// Handle function definitions inside classes
else if (line.startsWith('function ') && currentClass) {
const funcName = line.split(' ')[1].replace(':', '');
this.classes[currentClass][funcName] = (args) => {
this.log.push(`Executing function ${funcName} in class ${currentClass} with args: ${JSON.stringify(args)}`);
const name = args[0] || "there"; // Default name if no argument is provided
this.log.push(`Bot replies: Hello, ${name}!`);
};
this.log.push(`Function '${funcName}' defined in class '${currentClass}'.`);
}

// Handle replies and messages
Expand All @@ -61,9 +59,8 @@ class ChatterScript {

// Allow calls to defined functions and methods
else if (this.isFunctionCall(line)) {
const methodName = line.split('(')[0].trim();
const args = this.extractArgs(line);
this.callFunction(methodName, args);
this.callFunction(line, args);
}
}
}
Expand Down Expand Up @@ -92,17 +89,13 @@ class ChatterScript {
// Check if the class and method exist
if (className in this.classes && methodName in this.classes[className]) {
this.classes[className][methodName](args); // Call the function with arguments
this.log.push(`Called method ${className}.${methodName} with args: ${JSON.stringify(args)}`);
} else {
this.log.push(`Error: Method ${methodName} not found in class ${className}`);
}
}
}

triggerEvent(eventName, data) {
if (this.events[eventName]) {
this.events[eventName].forEach(callback => callback(data));
this.log.push(`Event '${eventName}' triggered with data: ${JSON.stringify(data)}`);
}
}

Expand Down

0 comments on commit 7b776be

Please sign in to comment.