From b4805f355a20d028f2e07c656e02126053dcc28b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Bo=C4=8Fa?= Date: Wed, 17 Jan 2024 21:07:56 +0100 Subject: [PATCH] feat(agent): Added agent property value update functionality --- src/interpreter/interpreter.ts | 4 ++++ src/parser/parser-util.ts | 2 +- src/runtime/runtime.ts | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/interpreter/interpreter.ts b/src/interpreter/interpreter.ts index b85e2b6..c9337c3 100644 --- a/src/interpreter/interpreter.ts +++ b/src/interpreter/interpreter.ts @@ -180,4 +180,8 @@ export class Interpreter { return heightFunction; } + + public updateAgentValue(agentIndex: number, propertyIdentifier: string, value: number): void { + this.runtime?.updateAgentValue(agentIndex, propertyIdentifier, value); + } } \ No newline at end of file diff --git a/src/parser/parser-util.ts b/src/parser/parser-util.ts index 18d1438..220a62f 100644 --- a/src/parser/parser-util.ts +++ b/src/parser/parser-util.ts @@ -19,7 +19,7 @@ export class ParserUtil { const objectDeclaration = ast as ObjectDeclaration; const identifier = objectDeclaration.identifier; - const count = objectDeclaration.count; + const count = ParserUtil.astToCode(objectDeclaration.count); code += `agent ${identifier} ${count} {\n`; code += objectDeclaration.body.map(declaration => `\t${ParserUtil.astToCode(declaration)}`).join("\n"); diff --git a/src/runtime/runtime.ts b/src/runtime/runtime.ts index 6042c83..3801bef 100644 --- a/src/runtime/runtime.ts +++ b/src/runtime/runtime.ts @@ -53,6 +53,11 @@ export class Runtime { this.output = { type: ValueType.Output, step: 0, agents: [] }; } + // update specific agent variable value + public updateAgentValue(agentIndex: number, propertyIdentifier: string, value: number): void { + this.previousAgents[agentIndex].variables.set(propertyIdentifier, { type: ValueType.Number, value } as NumberValue); + } + private evaluateProgram(program: Program): RuntimeValue { for (const statement of program.body) { switch (statement.type) {