Skip to content

Commit

Permalink
refactor(tests): Added some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasBoda committed Feb 20, 2024
1 parent 106cadc commit 3fce00e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
29 changes: 25 additions & 4 deletions code.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
agent data 1 {

property a = 1;
property b = 2;
agent person 300 {
const speed = 2;
property angle: random(0, 2 * pi()) = angle + choice(-0.1, 0.1);

property should_stay = prob(0.5);

property xNew: 0 = (x + speed * cos(angle)) % width();
property yNew: 0 = (y + speed * sin(angle)) % height();

property x: random(50, width() - 50) = if should_stay then x else xNew;
property y: random(50, height() - 50) = if should_stay then y else yNew;

const distance = 20;

property people = agents(person);
property close_people = filter(people => p => dist(x, y, p.x, p.y) <= distance);
property close_infected = filter(close_people => c => c.infected == true);

const timespan = 200;
property remaining: timespan = if infected then remaining - 1 else timespan;

property should_infect = prob(0.4);
property infected: prob(0.5) = (infected and remaining > 0) or (count(close_infected) > 0 and should_infect);

property coloured: false = infected;
}
46 changes: 43 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync, writeFileSync } from "fs";
import { Interpreter, InterpreterConfiguration, InterpreterOutput, Logger, Symbolizer, Symbol, Lexer, Token, Parser, Program } from "./src";
import { readFileSync } from "fs";
import { Interpreter, InterpreterConfiguration, InterpreterOutput, Logger } from "./src";

const filename = "code.txt";
const sourceCode = readFileSync(filename, "utf-8");
Expand Down Expand Up @@ -56,4 +56,44 @@ function run() {
}
}

run();
run();

/*
function doSomething(value: number) {
return value * value;
}
const data: number[] = [];
for (let i = 0; i < 10000; i++) {
data.push(i + 1);
}
const results: number[] = [];
if (isMainThread) {
for (let i = 0; i < 100; i++) {
const worker = new Worker(__filename, {
workerData: data.slice(i * 100, (i + 1) * 100)
});
worker.on("message", (result: number[]) => {
for (const a of result) {
results.push(a);
}
if (results.length === data.length) {
console.log('All workers finished. Results:', results);
}
});
}
} else {
const values = workerData as number[];
const outputs = [];
for (let i = 0; i < values.length; i++) {
const result = doSomething(values[i]);
outputs.push(result);
}
parentPort!.postMessage(outputs);
}
*/
1 change: 0 additions & 1 deletion tests/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, test } from "bun:test";
import { getProgram } from "./utils";
import { ObjectDeclaration, VariableDeclaration } from "../src";

describe("Parser", () => {

Expand Down
14 changes: 8 additions & 6 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { Symbolizer, Symbol, Parser, Program, Lexer, Token } from "../src";

export function getTokens(sourceCode: string): Token[] {
export function getSymbols(sourceCode: string): Symbol[] {
const symbolizer: Symbolizer = new Symbolizer(sourceCode);
const symbols: Symbol[] = symbolizer.symbolize();

return symbols;
}

export function getTokens(sourceCode: string): Token[] {
const symbols: Symbol[] = getSymbols(sourceCode);

const lexer: Lexer = new Lexer(symbols);
const tokens: Token[] = lexer.tokenize();

return tokens;
}

export function getProgram(sourceCode: string): Program {
const symbolizer: Symbolizer = new Symbolizer(sourceCode);
const symbols: Symbol[] = symbolizer.symbolize();

const lexer: Lexer = new Lexer(symbols);
const tokens: Token[] = lexer.tokenize();
const tokens: Token[] = getTokens(sourceCode);

const parser: Parser = new Parser(tokens);
const program: Program = parser.parse();
Expand Down

0 comments on commit 3fce00e

Please sign in to comment.