From 20f7cb9a4be15f4e6acf8697da5eeea89864437e Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Fri, 12 Dec 2025 17:36:57 -0700 Subject: [PATCH] wip --- rust_compiler/libs/optimizer/src/lib.rs | 1 + rust_compiler/src/ffi/mod.rs | 14 +++-- test.slang | 72 +++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 test.slang diff --git a/rust_compiler/libs/optimizer/src/lib.rs b/rust_compiler/libs/optimizer/src/lib.rs index 88a016d..aaac6a4 100644 --- a/rust_compiler/libs/optimizer/src/lib.rs +++ b/rust_compiler/libs/optimizer/src/lib.rs @@ -812,3 +812,4 @@ fn remove_unreachable_code<'a>( } (output, changed) } + diff --git a/rust_compiler/src/ffi/mod.rs b/rust_compiler/src/ffi/mod.rs index e85f0ec..f302a53 100644 --- a/rust_compiler/src/ffi/mod.rs +++ b/rust_compiler/src/ffi/mod.rs @@ -1,8 +1,9 @@ use compiler::{CompilationResult, Compiler}; use helpers::{Documentation, Span}; +use optimizer::optimize; use parser::{sys_call::SysCall, Parser}; use safer_ffi::prelude::*; -use std::io::BufWriter; +use std::io::{BufWriter, Write}; use tokenizer::{ token::{Token, TokenType}, Tokenizer, @@ -127,11 +128,11 @@ pub fn free_docs_vec(v: safer_ffi::Vec) { pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> FfiCompilationResult { let res = std::panic::catch_unwind(|| { let input = String::from_utf16_lossy(input.as_slice()); - let mut writer = BufWriter::new(Vec::new()); + let mut tmp = BufWriter::new(Vec::new()); let tokenizer = Tokenizer::from(input.as_str()); let parser = Parser::new(tokenizer); - let compiler = Compiler::new(parser, &mut writer, None); + let compiler = Compiler::new(parser, &mut tmp, None); let res = compiler.compile(); @@ -139,6 +140,13 @@ pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> FfiCompilat return (safer_ffi::String::EMPTY, res.source_map); } + let mut writer = BufWriter::new(Vec::new()); + + for instruction in optimize(res.instructions) { + _ = writer.write_all(instruction.instruction.to_string().as_bytes()); + _ = writer.write_all(b"\n"); + } + let Ok(compiled_vec) = writer.into_inner() else { return (safer_ffi::String::EMPTY, res.source_map); }; diff --git a/test.slang b/test.slang new file mode 100644 index 0000000..b02f6d1 --- /dev/null +++ b/test.slang @@ -0,0 +1,72 @@ +/// Laree script V1 + +device self = "db"; +device larre = "d0"; +device exportChute = "d1"; + +const TOTAL_SLOTS = 19; +const EXPORT_CHUTE = 1; +const START_STATION = 2; + +let currentIndex = 0; + +/// Waits for the larre to be idle before continuing +fn waitForIdle() { + yield(); + while (!larre.Idle) { + yield(); + } +} + +/// Instructs the Larre to go to the chute and deposit +/// what is currently in its arm +fn deposit() { + larre.Setting = EXPORT_CHUTE; + waitForIdle(); + larre.Activate = true; + waitForIdle(); + exportChute.Open = false; +} + +/// This function is responsible for checking the plant under +/// the larre at this index, and harvesting if applicable +fn checkAndHarvest(currentIndex) { + if (currentIndex <= EXPORT_CHUTE || ls(larre, 255, "Seeding") < 1) { + return; + } + + // harvest from this device + while (ls(larre, 255, "Mature")) { + yield(); + larre.Activate = true; + } + let hasRemainingPlant = ls(larre, 255, "Occupied"); + + // move to the export chute + larre.Setting = EXPORT_CHUTE; + waitForIdle(); + deposit(); + if (hasRemainingPlant) { + deposit(); + } + + larre.Setting = currentIndex; + waitForIdle(); + + if (ls(larre, 0, "Occupied")) { + larre.Activate = true; + } + waitForIdle(); +} + +loop { + yield(); + if (!larre.Idle) { + continue; + } + let newIndex = currentIndex + 1 > TOTAL_SLOTS ? START_STATION : currentIndex + 1; + + checkAndHarvest(currentIndex); + larre.Setting = newIndex; + currentIndex = newIndex; +}