From 89833ab7505242095d8165b998fac664184d011f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Bo=C4=8Fa?= Date: Thu, 18 Apr 2024 14:48:01 +0200 Subject: [PATCH] feat(example): Added example program --- LICENSE.md | 21 --------------------- example/index.ts | 41 +++++++++++++++++++++++++++++++++++++++++ example/source-code.txt | 9 +++++++++ exec/agent-lang.ts | 9 ++++----- package.json | 2 +- 5 files changed, 55 insertions(+), 27 deletions(-) delete mode 100644 LICENSE.md create mode 100644 example/index.ts create mode 100644 example/source-code.txt diff --git a/LICENSE.md b/LICENSE.md deleted file mode 100644 index cdd8f51..0000000 --- a/LICENSE.md +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2023 Tomáš Boďa - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/example/index.ts b/example/index.ts new file mode 100644 index 0000000..089d26b --- /dev/null +++ b/example/index.ts @@ -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(); \ No newline at end of file diff --git a/example/source-code.txt b/example/source-code.txt new file mode 100644 index 0000000..718ded6 --- /dev/null +++ b/example/source-code.txt @@ -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; +} \ No newline at end of file diff --git a/exec/agent-lang.ts b/exec/agent-lang.ts index c97f9dc..e5f5b28 100644 --- a/exec/agent-lang.ts +++ b/exec/agent-lang.ts @@ -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 { @@ -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) { @@ -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(); @@ -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}`); diff --git a/package.json b/package.json index ad08378..efd3aa3 100644 --- a/package.json +++ b/package.json @@ -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",