Skip to content

Commit

Permalink
feat(example): Added example program
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasBoda committed Apr 18, 2024
1 parent bafb5a6 commit 89833ab
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
21 changes: 0 additions & 21 deletions LICENSE.md

This file was deleted.

41 changes: 41 additions & 0 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { readFileSync, writeFileSync } from "node:fs";
import { Interpreter, InterpreterConfiguration, InterpreterOutput, Output } from "../index.ts";
import { exit } from "node:process";

interface ProgramOutput {
steps: Output[];
}

const outputFile = "./example/output.json";

const filename = "./example/source-code.txt";
const sourceCode = readFileSync(filename, "utf-8");
const config: InterpreterConfiguration = { steps: 10, delay: 200, width: 500, height: 500 };

const interpreter: Interpreter = new Interpreter();
const programOutput: ProgramOutput = { steps: [] };

interpreter.get(sourceCode, config).subscribe((output: InterpreterOutput) => {
if (output.status.code !== 0 || !output.output) {
console.log(output.status.message);
exit(1);
}

console.log("Evaluating step " + output.output!.step + "/" + config.steps);

programOutput.steps.push(output.output!);
writeFileSync(outputFile, JSON.stringify(programOutput, null, 2));

if (output.output!.step === config.steps) {
console.log("-----------------");
console.log("Finished running");
console.log("Output has been written to " + outputFile);
console.log("-----------------");
exit(0);
}
});

console.log("-----------------");
console.log("Running AgentLang");
console.log("-----------------");
interpreter.start();
9 changes: 9 additions & 0 deletions example/source-code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
agent snowflake 10 {
const speed = random(8, 15);

property x: random(0, width()) = x;
property y: random(0, height()) = (y + speed) % height();

const w = 10;
const h = 10;
}
9 changes: 4 additions & 5 deletions exec/agent-lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Interpreter, InterpreterOutput, Output } from "../index.ts";
import { Logger } from "./logger.ts";
import { Parser } from "./parser.ts";
import { Process } from "./process.ts";
import { performance } from "node:perf_hooks";
import { Performance } from "./performance.ts";

interface ProgramOutput {
Expand All @@ -14,7 +13,7 @@ interface ProgramOutput {
const { sourceCode, outputFile, config, debug, compact } = await Parser.initialize();

const interpreter: Interpreter = new Interpreter();
const programOtput: ProgramOutput = { steps: [] };
const programOutput: ProgramOutput = { steps: [] };

interpreter.get(sourceCode, config).subscribe((output: InterpreterOutput) => {
if (output.status.code !== 0 || !output.output) {
Expand All @@ -33,7 +32,7 @@ interpreter.get(sourceCode, config).subscribe((output: InterpreterOutput) => {
Logger.info(`Evaluating step ${step}/${steps} (${elapsed}ms) (${Performance.slowdown(delay, elapsed)}x slowdown)`);
}

programOtput.steps.push(output.output!);
programOutput.steps.push(output.output!);

if (output.output!.step === config.steps) {
Performance.now();
Expand All @@ -45,9 +44,9 @@ interpreter.get(sourceCode, config).subscribe((output: InterpreterOutput) => {
Logger.info(`Finished running (${elapsed}s) (${Performance.slowdown(expectedTime, actualTime)}x slowdown)`);

if (compact) {
writeFileSync(outputFile, JSON.stringify(programOtput));
writeFileSync(outputFile, JSON.stringify(programOutput));
} else {
writeFileSync(outputFile, JSON.stringify(programOtput, null, 2));
writeFileSync(outputFile, JSON.stringify(programOutput, null, 2));
}
Logger.done(`Output has been written to ${outputFile}`);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"main": "index.ts",
"scripts": {
"start": "npx bun run index.ts",
"start": "npx bun run ./example/index.ts",
"test": "npx bun test",
"build-current-platform": "deno compile -A --unstable-sloppy-imports --output ./prod/current-platform/agent-lang ./exec/agent-lang.ts",
"build-x86_64-unknown-linux-gnu": "deno compile -A --unstable-sloppy-imports --output ./prod/x86_64-unknown-linux-gnu/agent-lang --target x86_64-unknown-linux-gnu ./exec/agent-lang.ts",
Expand Down

0 comments on commit 89833ab

Please sign in to comment.