Skip to content

Commit

Permalink
chore(docs): Added docstrings to all methods, classes and interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasBoda committed Apr 17, 2024
1 parent 5e41a95 commit e41e21a
Show file tree
Hide file tree
Showing 24 changed files with 915 additions and 103 deletions.
109 changes: 95 additions & 14 deletions src/interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export class Interpreter {
private program?: Program;

/**
* Generates an observable used for retrieving the interpreter's output
* Returns an observable emitting interpreter's output on each step
*
* @param sourceCode - source code of the simulation
* @param config - configuration of the interpreter
* @returns observable holding the interpreter's output
* @param sourceCode source code of the simulation
* @param config configuration of the interpreter
* @returns observable holding the interpreter's output subscription
*/
public get(sourceCode: string, config: InterpreterConfiguration): Observable<InterpreterOutput> {
try {
Expand All @@ -40,7 +40,7 @@ export class Interpreter {
code: 1,
message: (error as ErrorModel).toString()
}
})
});
}

return this.dataSubject.asObservable();
Expand All @@ -49,8 +49,8 @@ export class Interpreter {
/**
* Builds the interpreter and initializes the simulation
*
* @param sourceCode - source code to be initialized and built upon
* @param config - configuration of the interpreter
* @param sourceCode source code to be initialized and built
* @param config configuration of the interpreter
*/
public build(sourceCode: string, config: InterpreterConfiguration): void {
this.sourceCode = sourceCode;
Expand Down Expand Up @@ -81,62 +81,109 @@ export class Interpreter {
this.reset();
}

/**
* Rebuilds the interpreter
*/
public rebuild(): void {
this.runtime?.setProgram(this.program!);
this.currentStep--;
this.step();
}

public start() {
/**
* Starts the interpreter
*/
public start(): void {
this.currentStep = 0;
this.subscribe();
}

public reset() {
/**
* Resets the interpreter
*/
public reset(): void {
this.unsubscribe();
this.currentStep = 0;
this.runtime?.reset();
}

public pause() {
/**
* Pauses the interpreter
*/
public pause(): void {
this.unsubscribe();
}

public resume() {
/**
* Resumes the interpreter
*/
public resume(): void {
this.subscribe();
}

public step() {
/**
* Steps through the interpreter, performs one step
*/
public step(): void {
if (this.currentStep >= this.config.steps + 1) {
return;
}

this.dataSubject.next(this.getInterpreterOutput(this.currentStep++));
}

/**
* Returns the parsed program AST node
*
* @returns program AST node
*/
public getProgram(): Program {
return this.program!;
}

/**
* Updates the program AST node in the interpreter
*
* @param program new program AST node
*/
public setProgram(program: Program): void {
this.program = program;
}

/**
* Updates an agent's value in the simulation
*
* @param agentIndex index of the agent to update
* @param propertyIdentifier identifier of the agent's property to update
* @param value new value to update
*/
public updateAgentValue(agentIndex: number, propertyIdentifier: string, value: number): void {
this.runtime?.updateAgentValue(agentIndex, propertyIdentifier, value);
}

/**
* Subscribes to the interpreter's runtime changes
*/
private subscribe(): void {
this.subscription = interval(this.config.delay).pipe(
takeWhile(() => this.currentStep <= this.config.steps),
).subscribe(() => this.dataSubject.next(this.getInterpreterOutput(this.currentStep++)));
}

/**
* Unsubscribes from the interpreter's runtime changes
*/
private unsubscribe(): void {
this.subscription?.unsubscribe();
this.subscription = undefined;
}

/**
* Returns the output of the interpreter's runtime of the given step
*
* @param step step to evaluate
* @returns output of the interpreter's runtime
*/
private getInterpreterOutput(step: number): InterpreterOutput {
try {
const value: RuntimeValue = this.runtime!.run(step);
Expand All @@ -146,6 +193,12 @@ export class Interpreter {
}
}

/**
* Maps the runtime output to the interpreter output
*
* @param output output of the runtime
* @returns output of the interpreter
*/
private getRuntimeOutput(output: RuntimeOutput): InterpreterOutput {
return {
status: { code: 0 },
Expand All @@ -156,6 +209,12 @@ export class Interpreter {
} as InterpreterOutput;
}

/**
* Maps the runtime error to the interpreter output
*
* @param error error of the runtime
* @returns output of the interpreter
*/
private getRuntimeError(error: ErrorModel): InterpreterOutput {
return {
status: {
Expand All @@ -165,10 +224,22 @@ export class Interpreter {
};
}

/**
* Maps the runtime agent list to the output agent list
*
* @param agents list of runtime agents
* @returns list of output agents
*/
private getAgents(agents: RuntimeAgent[]): Agent[] {
return agents.map((agent: RuntimeAgent) => this.getAgent(agent));
}

/**
* Maps the runtime agent to the output agents
*
* @param agent runtime agent
* @returns output agent
*/
private getAgent(agent: RuntimeAgent): Agent {
const variables: { [key: string]: RuntimeValue } = {};
agent.variables.forEach((value, key) => {
Expand All @@ -182,8 +253,12 @@ export class Interpreter {
return { identifier: agent.identifier, variables } as Agent;
}

// interpreter's functions initialization

/**
* Creates the global width function
*
* @param width numeric width value
* @returns runtime function call
*/
private createWidthFunction(width: number): FunctionCall {
function widthFunction(args: RuntimeValue[]): RuntimeValue {
if (args.length !== 0) {
Expand All @@ -196,6 +271,12 @@ export class Interpreter {
return widthFunction;
}

/**
* Creates the global height function
*
* @param height numeric height value
* @returns runtime function call
*/
private createHeightFunction(height: number): FunctionCall {
function heightFunction(args: RuntimeValue[]): RuntimeValue {
if (args.length !== 0) {
Expand Down
5 changes: 5 additions & 0 deletions src/interpreter/model/agent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

/**
* Object representing an output agent
*/
export interface Agent {
/** identifier of the agent */
identifier: string;
/** map of agent variables */
variables: Object;
}
7 changes: 7 additions & 0 deletions src/interpreter/model/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@

/**
* Object representing the interpeter's configuration
*/
export interface InterpreterConfiguration {
/** number of the simulation steps */
steps: number;
/** delay between each step in milliseconds */
delay: number;
/** width of the visualisation window */
width: number;
/** height of the visualisation window */
height: number;
}
5 changes: 5 additions & 0 deletions src/interpreter/model/exit-status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

/**
* Object representing the exit status of the interpreter's output
*/
export interface ExitStatus {
/** exit code number */
code: number;
/** exit status message */
message?: string;
}
10 changes: 10 additions & 0 deletions src/interpreter/model/output.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Agent } from "./agent.ts";
import { ExitStatus } from "./exit-status.ts";

/**
* Object representing the output of the simulation
*/
export interface Output {
/** value of the current step */
step: number;
/** list of current agents */
agents: Agent[];
}

/**
* Object representing the output of the interpreter
*/
export interface InterpreterOutput {
/** exit status of the interpreter */
status: ExitStatus;
/** output values of the simulation */
output?: Output;
}
3 changes: 3 additions & 0 deletions src/lexer/keywords.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { TokenType } from "./model/index.ts";

/**
* Map of the reserved keywords and their types
*/
export const ReservedKeywords: Record<string, TokenType> = {
"agent": TokenType.Agent,

Expand Down
Loading

0 comments on commit e41e21a

Please sign in to comment.