Skip to content

Commit ef13386

Browse files
committed
Implement read, move variables out of ProcessorState
1 parent b5e3c25 commit ef13386

File tree

7 files changed

+548
-196
lines changed

7 files changed

+548
-196
lines changed

src/logic/ast.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ pub enum Statement {
1414
#[derive(Debug, Clone, PartialEq)]
1515
pub enum Instruction {
1616
// input/output
17+
Read {
18+
result: Value,
19+
target: Value,
20+
address: Value,
21+
},
1722
Draw {
1823
op: DrawOp,
1924
x: Value,

src/logic/grammar.lalrpop

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ match {
1414

1515
r"[^\r\n #\t;]+:" => LABEL,
1616

17+
"read",
1718
"draw",
1819
"print",
1920
"printchar",
@@ -178,6 +179,9 @@ Statement: Statement = {
178179
Instruction: Instruction = {
179180
// input/output
180181

182+
"read" <result:Value> <target:Value> <address:Value> =>
183+
Instruction::Read { <> },
184+
181185
"draw" <op:DrawOp0> =>
182186
optional_args!(Instruction::Draw { <>; x, y, p1, p2, p3, p4 }),
183187

@@ -494,6 +498,7 @@ Symbol = {
494498
SYMBOL,
495499
LABEL,
496500

501+
"read",
497502
"draw",
498503
"print",
499504
"printchar",

src/logic/vm/buildings.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::{cell::RefCell, rc::Rc};
1+
use std::{cell::RefCell, collections::HashMap, rc::Rc};
22

33
use strum_macros::IntoStaticStr;
44

55
use super::{
66
LogicVMBuilder, VMLoadError, VMLoadResult,
77
processor::{Processor, ProcessorBuilder, ProcessorState},
8-
variables::LValue,
8+
variables::{LValue, LVar},
99
};
1010
use crate::types::{
1111
Object, Point2, ProcessorConfig, SchematicTile,
@@ -177,6 +177,46 @@ impl Building {
177177
) -> VMLoadResult<Self> {
178178
Self::from_config(name, (*position).into(), config, builder)
179179
}
180+
181+
pub fn borrow_data<T, U, R>(
182+
&self,
183+
state: &ProcessorState,
184+
variables: &HashMap<String, LVar>,
185+
f_processor: T,
186+
f_other: U,
187+
) -> R
188+
where
189+
T: FnOnce(&ProcessorState, &HashMap<String, LVar>) -> R,
190+
U: FnOnce(&BuildingData) -> R,
191+
{
192+
match self.data.try_borrow() {
193+
Ok(data) => match &*data {
194+
BuildingData::Processor(p) => f_processor(&p.state, &p.variables),
195+
other => f_other(other),
196+
},
197+
Err(_) => f_processor(state, variables),
198+
}
199+
}
200+
201+
pub fn borrow_data_mut<T, U, R>(
202+
&self,
203+
state: &mut ProcessorState,
204+
variables: &HashMap<String, LVar>,
205+
f_processor: T,
206+
f_other: U,
207+
) -> R
208+
where
209+
T: FnOnce(&mut ProcessorState, &HashMap<String, LVar>) -> R,
210+
U: FnOnce(&mut BuildingData) -> R,
211+
{
212+
match self.data.try_borrow_mut() {
213+
Ok(mut data) => match &mut *data {
214+
BuildingData::Processor(p) => f_processor(&mut p.state, &p.variables),
215+
other => f_other(other),
216+
},
217+
Err(_) => f_processor(state, variables),
218+
}
219+
}
180220
}
181221

182222
#[derive(IntoStaticStr)]

0 commit comments

Comments
 (0)