Skip to content

Commit c2e843e

Browse files
committed
Clean up public API
1 parent 8200948 commit c2e843e

File tree

8 files changed

+93
-75
lines changed

8 files changed

+93
-75
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ rustflags = ["-C", "target-cpu=native"]
44
[alias]
55
mlog = "run --release --features mlog --bin mlog --"
66
mlogv32 = "run --release --features mlogv32 --bin mlogv32 --"
7+
test-nostd = "test --no-default-features --features no_std,serde_alloc"

src/bin/mlog.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ use clap_stdin::FileOrStdin;
55
use mindustry_rs::{
66
types::{Object, ProcessorConfig, ProcessorLinkConfig},
77
vm::{
8-
Building, BuildingData, HYPER_PROCESSOR, LOGIC_PROCESSOR, LogicVMBuilder, MEMORY_BANK,
9-
MEMORY_CELL, MESSAGE, MICRO_PROCESSOR, WORLD_PROCESSOR,
8+
Building, BuildingData, LogicVMBuilder,
9+
buildings::{
10+
HYPER_PROCESSOR, LOGIC_PROCESSOR, MEMORY_BANK, MEMORY_CELL, MESSAGE, MICRO_PROCESSOR,
11+
WORLD_PROCESSOR,
12+
},
1013
},
1114
};
1215
use strum_macros::EnumString;

src/bin/mlogv32.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use itertools::Itertools;
2828
use mindustry_rs::{
2929
types::{Object, PackedPoint2, ProcessorConfig, Schematic},
3030
vm::{
31-
Building, BuildingData, LObject, LValue, LVar, LogicVM, LogicVMBuilder, MEMORY_BANK,
32-
MESSAGE, MICRO_PROCESSOR, SWITCH, WORLD_PROCESSOR,
31+
Building, BuildingData, LObject, LValue, LVar, LogicVM, LogicVMBuilder,
32+
buildings::{MEMORY_BANK, MESSAGE, MICRO_PROCESSOR, SWITCH, WORLD_PROCESSOR},
3333
},
3434
};
3535
use serde::Deserialize;
@@ -666,13 +666,13 @@ fn main() -> Result<(), Box<dyn Error>> {
666666
VMCommand::SetBreakpoint(Some(value)) => {
667667
config
668668
.state
669-
.set_variable(u16str!("BREAKPOINT_ADDRESS"), value.into())?;
669+
.set_variable(u16str!("BREAKPOINT_ADDRESS"), value.into());
670670
tui_println!(debug, "Breakpoint set: {value:#010x}");
671671
}
672672
VMCommand::SetBreakpoint(None) => {
673673
config
674674
.state
675-
.set_variable(u16str!("BREAKPOINT_ADDRESS"), LValue::NULL)?;
675+
.set_variable(u16str!("BREAKPOINT_ADDRESS"), LValue::NULL);
676676
tui_println!(debug, "Breakpoint cleared.");
677677
}
678678
VMCommand::PrintVar(name, radix) => {

src/vm/buildings.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use strum_macros::IntoStaticStr;
66
use widestring::U16String;
77

88
use super::{
9-
LObject, LVar, LogicVM, LogicVMBuilder, Processor, ProcessorBuilder, ProcessorState,
10-
VMLoadError, VMLoadResult, instructions::InstructionResult, variables::LValue,
9+
InstructionResult, LObject, LValue, LVar, LogicVM, LogicVMBuilder, Processor, ProcessorBuilder,
10+
ProcessorState, VMLoadError, VMLoadResult,
1111
};
1212
use crate::types::{
1313
LAccess, Object, PackedPoint2,
@@ -54,18 +54,11 @@ impl Building {
5454
Ok(Self::new(Self::get_block(name)?, position, data))
5555
}
5656

57-
fn get_block(name: &str) -> VMLoadResult<&'static Block> {
58-
content::blocks::FROM_NAME
59-
.get(name)
60-
.copied()
61-
.ok_or_else(|| VMLoadError::UnknownBlockType(name.to_string()))
62-
}
63-
6457
pub fn from_config(
6558
name: &str,
6659
position: PackedPoint2,
6760
config: &Object,
68-
#[cfg_attr(not(feature = "std"), allow(unused_variables))] builder: &LogicVMBuilder,
61+
_builder: &LogicVMBuilder,
6962
) -> VMLoadResult<Self> {
7063
let data = match name {
7164
MICRO_PROCESSOR | LOGIC_PROCESSOR | HYPER_PROCESSOR | WORLD_PROCESSOR => {
@@ -74,7 +67,7 @@ impl Building {
7467
name,
7568
position,
7669
&ProcessorConfig::parse(config)?,
77-
builder,
70+
_builder,
7871
);
7972
#[cfg(not(feature = "std"))]
8073
panic!("processor config parsing is not supported on no_std");
@@ -199,6 +192,13 @@ impl Building {
199192
) -> VMLoadResult<Self> {
200193
Self::from_config(name, *position, config, builder)
201194
}
195+
196+
fn get_block(name: &str) -> VMLoadResult<&'static Block> {
197+
content::blocks::FROM_NAME
198+
.get(name)
199+
.copied()
200+
.ok_or_else(|| VMLoadError::UnknownBlockType(name.to_string()))
201+
}
202202
}
203203

204204
impl PartialEq for Building {

src/vm/instructions.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ use num_traits::float::FloatCore;
1111
use widestring::{U16Str, u16str};
1212

1313
use super::{
14-
Constants, LObject, LogicVM, VMLoadError, VMLoadResult, borrow_data,
15-
buildings::BuildingData,
16-
processor::{MAX_TEXT_BUFFER, ProcessorState},
17-
variables::{Content, F64_DEG_RAD, F64_RAD_DEG, LString, LValue, LVar, RAD_DEG},
14+
BuildingData, Content, LObject, LString, LValue, LVar, LogicVM, ProcessorState, VMLoadError,
15+
VMLoadResult,
16+
buildings::borrow_data,
17+
processor::MAX_TEXT_BUFFER,
18+
variables::{Constants, F64_DEG_RAD, F64_RAD_DEG, RAD_DEG},
1819
};
1920
use crate::{
2021
parser::ast::{self, ConditionOp, LogicOp, TileLayer},
@@ -24,6 +25,7 @@ use crate::{
2425
content,
2526
},
2627
utils::{RapidHashMap, u16format},
28+
vm::variables::VariableIndex,
2729
};
2830

2931
const MAX_IPT: i32 = 1000;
@@ -127,11 +129,15 @@ impl InstructionBuilder {
127129
.or_else(|| globals.get(&name))
128130
.cloned()
129131
// then see if there's already a variable with this name
130-
.or_else(|| variables.get_index_of(&name).map(LVar::Variable))
132+
.or_else(|| {
133+
variables
134+
.get_index_of(&name)
135+
.map(|i| LVar::Variable(VariableIndex(i)))
136+
})
131137
// if none of those exist, create a new variable defaulting to null
132138
.unwrap_or_else(|| {
133139
let (i, _) = variables.insert_full(name, LValue::NULL);
134-
LVar::Variable(i)
140+
LVar::Variable(VariableIndex(i))
135141
})
136142
}
137143
ast::Value::String(value) => LVar::Constant(value.into()),

src/vm/mod.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ use std::time::Instant;
55

66
use thiserror::Error;
77

8-
pub use self::{buildings::*, processor::*, variables::*};
8+
use self::variables::Constants;
9+
pub use self::{
10+
buildings::{Building, BuildingData, CustomBuildingData},
11+
instructions::InstructionResult,
12+
processor::{InstructionHook, Processor, ProcessorBuilder, ProcessorState},
13+
variables::{Content, LObject, LString, LValue, LVar},
14+
};
915
#[cfg(feature = "std")]
1016
use crate::types::{Schematic, SchematicTile};
1117
use crate::{types::PackedPoint2, utils::RapidHashMap};
1218

13-
mod buildings;
19+
pub mod buildings;
1420
pub mod instructions;
1521
mod processor;
16-
mod variables;
22+
pub mod variables;
1723

1824
const MILLIS_PER_SEC: u64 = 1_000;
1925
const NANOS_PER_MILLI: u32 = 1_000_000;
@@ -50,6 +56,12 @@ impl LogicVM {
5056
builder.build()
5157
}
5258

59+
pub fn from_buildings(buildings: impl IntoIterator<Item = Building>) -> VMLoadResult<Self> {
60+
let mut builder = LogicVMBuilder::new();
61+
builder.add_buildings(buildings);
62+
builder.build()
63+
}
64+
5365
pub fn building(&self, position: PackedPoint2) -> Option<&Building> {
5466
self.buildings_map
5567
.get(&position)
@@ -154,10 +166,7 @@ impl LogicVMBuilder {
154166
};
155167
}
156168

157-
pub fn add_buildings<T>(&mut self, buildings: T)
158-
where
159-
T: IntoIterator<Item = Building>,
160-
{
169+
pub fn add_buildings(&mut self, buildings: impl IntoIterator<Item = Building>) {
161170
for building in buildings.into_iter() {
162171
self.add_building(building);
163172
}
@@ -269,11 +278,10 @@ mod tests {
269278
use pretty_assertions::assert_eq;
270279
use widestring::u16str;
271280

272-
use super::LogicVMBuilder;
281+
use super::*;
273282
use crate::{
274283
parser::ast,
275284
types::{PackedPoint2, content},
276-
vm::{Building, BuildingData, LObject, LVar, processor::ProcessorBuilder},
277285
};
278286

279287
#[test]
@@ -370,9 +378,12 @@ mod tests {
370378
use widestring::{U16Str, U16String, u16str};
371379

372380
use super::{
373-
buildings::{LOGIC_PROCESSOR, WORLD_PROCESSOR},
374-
instructions::{Instruction, InstructionResult},
375-
processor::Processor,
381+
buildings::{
382+
HYPER_PROCESSOR, LOGIC_PROCESSOR, MEMORY_BANK, MEMORY_CELL, MESSAGE, MICRO_PROCESSOR,
383+
SWITCH, WORLD_CELL, WORLD_PROCESSOR,
384+
},
385+
instructions::Instruction,
386+
variables::Constants,
376387
*,
377388
};
378389
use crate::{
@@ -381,13 +392,6 @@ mod tests {
381392
Team, colors::COLORS, content,
382393
},
383394
utils::u16format,
384-
vm::{
385-
buildings::{
386-
HYPER_PROCESSOR, MEMORY_BANK, MEMORY_CELL, MESSAGE, MICRO_PROCESSOR, SWITCH,
387-
WORLD_CELL,
388-
},
389-
variables::{Content, LValue, LVar},
390-
},
391395
};
392396

393397
fn single_processor_vm(name: &str, code: &str) -> LogicVM {

src/vm/processor.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ use itertools::Itertools;
1313
#[allow(unused_imports)]
1414
use num_traits::float::FloatCore;
1515
use replace_with::replace_with_or_default_and_return;
16-
use thiserror::Error;
1716
use widestring::{U16Str, U16String};
1817

1918
use super::{
20-
BuildingData, Constants, LValue, LogicVM, LogicVMBuilder, VMLoadError, VMLoadResult,
21-
buildings::Building,
22-
instructions::{Instruction, InstructionBuilder, InstructionResult, InstructionTrait, Noop},
23-
variables::{LVar, Variables},
19+
Building, BuildingData, InstructionResult, LValue, LVar, LogicVM, LogicVMBuilder, VMLoadError,
20+
VMLoadResult,
21+
instructions::{Instruction, InstructionBuilder, InstructionTrait, Noop},
22+
variables::{Constants, Variables},
2423
};
2524
#[cfg(feature = "std")]
2625
use crate::parser::LogicParser;
@@ -211,12 +210,6 @@ impl Processor {
211210
}
212211
}
213212

214-
#[derive(Debug, Error)]
215-
pub enum SetVariableError {
216-
#[error("Variable not found.")]
217-
NotFound,
218-
}
219-
220213
#[derive(Debug, Clone)]
221214
pub struct ProcessorState {
222215
enabled: bool,
@@ -241,7 +234,7 @@ pub struct ProcessorState {
241234
pub printbuffer: U16String,
242235

243236
pub(super) locals: Constants,
244-
pub variables: Variables,
237+
pub(super) variables: Variables,
245238
}
246239

247240
impl ProcessorState {
@@ -325,26 +318,21 @@ impl ProcessorState {
325318

326319
// these aren't used internally, so don't bother inlining
327320

328-
/// Checks if a variable or local constant exists.
321+
/// Checks if a variable exists in this processor.
329322
pub fn has_variable(&self, name: &U16Str) -> bool {
330323
self.variables.contains_key(name) || self.locals.contains_key(name)
331324
}
332325

333-
/// Looks up a variable or local constant by name.
326+
/// Looks up a variable by name in this processor.
334327
pub fn variable<'a>(&'a self, name: &U16Str) -> Option<Cow<'a, LValue>> {
335-
self.variables
336-
.get(name)
337-
.map(Cow::Borrowed)
338-
.or_else(|| self.locals.get(name).map(|v| v.get(self)))
328+
self.variables.get(name).map(Cow::Borrowed)
339329
}
340330

341-
pub fn set_variable(&mut self, name: &U16Str, value: LValue) -> Result<(), SetVariableError> {
342-
if self.variables.contains_key(name) {
343-
self.variables[name] = value;
344-
Ok(())
345-
} else {
346-
Err(SetVariableError::NotFound)
347-
}
331+
/// Sets the value of an existing variable in this processor.
332+
///
333+
/// ***Panics*** if the variable does not exist.
334+
pub fn set_variable(&mut self, name: &U16Str, value: LValue) {
335+
self.variables[name] = value;
348336
}
349337
}
350338

0 commit comments

Comments
 (0)