Skip to content

Commit

Permalink
Fix if-elif-else chain indexes, update gas.myps
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsso committed Jul 30, 2021
1 parent b928f32 commit 0c1bcce
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 108 deletions.
12 changes: 5 additions & 7 deletions mips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl Mips {
match arg {
Arg::LineAbs(LineAbs(num)) if conf.remove_tags => {
if let Some(key) = num.as_alias() {
let i = tag_lines.get(key).unwrap();
let i = tag_lines.get(key).expect(key);
*num = Num::Lit(*i as f64);
}
}
Expand Down Expand Up @@ -675,13 +675,11 @@ impl Mips {
// let (s, e) = reg.lifetime();
for i in 0..n {
#[rustfmt::skip]
match (s == i, s <= i && i <= e, i == e) {
( true, true, true) => output.push_str(&format!("{:>2}", i % 10)),
( true, true, false) => output.push_str(&format!("{:>2}", i % 10)),
(false, true, false) => output.push_str("--"),
(false, true, true) => output.push_str(&format!("{:->2}", i % 10)),
match (i == s || i == e, s <= i && i <= e) {
(false, true) => output.push_str(" -"),
(true, true) => output.push_str(&format!("{:>2}", i % 10)),
_ => output.push_str(" |"),
};
}
}
output.push_str("\n");
}
Expand Down
28 changes: 28 additions & 0 deletions myps/src/ast/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use mips::MipsResult;

use crate::ast::{Block, Branch, Stmt};
use crate::{MypsError, MypsParser, MypsResult, Pair, Rule};
use itertools::chain;

#[derive(Debug)]
pub enum LineItem<'a> {
Expand Down Expand Up @@ -133,6 +134,33 @@ impl Item {
self.is_if() || self.is_elif() || self.is_else()
}

pub fn chain_id(&self) -> Option<usize> {
match self {
Self::Block(
Block {
branch: Branch::If { chain_id_opt, .. },
..
},
..,
) => chain_id_opt.clone(),
Self::Block(
Block {
branch: Branch::Elif { chain_id, .. },
..
},
..,
) |
Self::Block(
Block {
branch: Branch::Else { chain_id, .. },
..
},
..,
) => Some(*chain_id),
_ => None,
}
}

pub fn iter(&self) -> LineItemIter {
LineItemIter::new(self)
}
Expand Down
11 changes: 8 additions & 3 deletions myps/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ pub fn lex_lines<'a>(line_iter: impl Iterator<Item = String>) -> MypsResult<Item
if let Item::Block(block, ..) = item {
let items = &mut block.items;
for i in 0..items.len() {

if matches!(items[i], Item::Block(..)) {
if items[i].is_if_elif_else() {
let j = i + 1;
if j < items.len() {}
let prev_chain_id = if i > 0 {
items[i - 1].chain_id()
} else {
None
};
let next_is_elif_else = j < items.len() && items[j].is_elif_else();
match &mut items[i] {
Item::Block(
Expand All @@ -147,7 +152,7 @@ pub fn lex_lines<'a>(line_iter: impl Iterator<Item = String>) -> MypsResult<Item
},
..,
) => {
*chain_id = *next_chain_id - 1;
*chain_id = prev_chain_id.unwrap();
if next_is_elif_else {
*end_chain = false;
}
Expand All @@ -159,7 +164,7 @@ pub fn lex_lines<'a>(line_iter: impl Iterator<Item = String>) -> MypsResult<Item
},
..,
) => {
// *chain_id = *next_chain_id - 1;
*chain_id = prev_chain_id.unwrap();
}
_ => unreachable!(),
}
Expand Down
193 changes: 103 additions & 90 deletions myps/test-scripts/factory/gas.myps
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
tankC = d0
tankH = d1
tTSrc = d2
pTSrc = d3
nCPump = d4
nHPump = d5

def PipeAnalyzer = 435685051
def AdvFurnace = 545937711
def vF = 1000
def R = 8.314

def vCFactor = 6100 / 6000
def vHFactor = 6100 / 6000

furnace = d0
tankC = d1
tankH = d2
nCPump = d3
nHPump = d4
nIAnlzr = d5

def vF = 1000 # furnace volume
def R = 8.314 # universal gas consant
def dt = 0.5 # time step

# Source volume-mole factor (adjust per setup)
# e.g. 6100 = 1 furnace (6000) + 1 pipe (100) L
def vCFactor = (6100) / 6000
def vHFactor = (6100) / 6000

# PD coefficients and minimum error (perhaps adjust)
def kPF = 0.06
def kDF = 0.02
def kPC = 0.06
def kDC = 0.02
def kPH = 0.06
def kDH = 0.02
def ERRORF = 0.03
def ERRORC = 0.03
def ERRORH = 0.03

# Input target temperature/pressure format: PPPTTT
# ```
# input = db.Setting
# tT = input % 1000 * 10 K
# input = trunc(input / 1000)
# pT = input * 100 kPa
# ```
# e.g. 500050 -> tT = 50*10 = 500 K, pT = 500*100 = 50,000 kPa
loop:
yield()
if db.Setting != 0:
# (a) batch read temperature and pressure from furnace (tF/pF)
db.Setting = 2
yield()
nF = AdvFurnace.all.TotalMoles.avg
tF = AdvFurnace.all.Temperature.avg
pF = AdvFurnace.all.Pressure.avg

# (b) read tank temperatures
db.Setting = 3
yield()
input = db.Setting
if input < 0:
db.Setting = 0
elif input > 0:
# (a) unpack input temperature and pressure
input = trunc(input)
tT = (input % 1000) * 10
pT = trunc(input / 1000) * 100

# (b) batch read temperature and pressure from furnace (tF/pF)
nF = furnace.TotalMoles
tF = furnace.Temperature

# (c) read tank temperatures
tC = tankC.Temperature
tH = tankH.Temperature

# (c) read targets (tT/pT),
db.Setting = 4
yield()
tT = tTSrc.Setting
pT = pTSrc.Setting

# (d) calculate moles to remove and add from C and H (nR/nC/nH)
db.Setting = 5
yield()
# TODO: Scopes need to be restricted better?...
fix rF, rC, rH
tag calcMolesTargets:
nT = pT * vF / (R * tT)
nRC = nT * (tT - tH) / (tH - tF) + nF
nRH = nT * (tT - tC) / (tC - tF) + nF
#nRH = nT * (tC - tT) / (tF - tC) + nF
nR = max(0, min(nRC, nRH))
nI = nT - nF + nR
tI = (tT * nT - tF * (nF - nR)) / nI
Expand All @@ -59,65 +71,66 @@ loop:
rH = nH - nHI # calc H target (rH)

# (e) remove nR, mix nC and nH
db.Setting = 6
yield()
def kP = 0.06
def kD = 0.02
def dt = 0.5
def ERROR = 0.3

# TODO: To nR FIRST, then nC and nH with input turned on. that way don't blow the mix section

yield()
nCPump.On = 1
nCPump.Setting = 0
nHPump.On = 1
fix eF = nF - rF # fix F error (nF - rF)
fix eC = nC - rC # fix C error (nC - rC)
fix eH = nH - rH # fix H error (nH - rH)
fix eFPrev = 0 # fix F error (prev)
fix eCPrev = 0 # fix C error (prev)
fix eHPrev = 0 # fix H error (prev)
db.Setting = eF
yield()
db.Setting = eC
yield()
db.Setting = eH
yield()
db.Setting = 999
yield()
while eF > ERROR or eC > ERROR or eH > ERROR:
db.Setting = 1234
yield()
uF = (kP * eF) + (kD * (eF - eFPrev) / dt)
AdvFurnace.all.SettingOutput = uF # set furnace out

uC = (kP * eC) + (kD * (eC - eCPrev) / dt)
nCPump.Setting = uC # set C pump

uH = (kP * eH) + (kD * (eH - eHPrev) / dt)
nHPump.Setting = uH # set H pump

eFPrev = eF # save F error
eCPrev = eC # save C error
eHPrev = eH # save H error

yield()
nF = AdvFurnace.all.TotalMoles.avg
nC = vCFactor * tankC.TotalMoles
nH = vHFactor * tankH.TotalMoles
eF = nF - rF
eC = nC - rC
eH = nH - rH
nHPump.Setting = 0

# (f) remove nR, mix nC and nH
tag E:
fix eF = nF - rF # fix F error (nF - rF)
fix eC = nC - rC # fix C error (nC - rC)
fix eH = nH - rH # fix H error (nH - rH)
fix eFPrev = 0 # fix F error (prev)
fix eCPrev = 0 # fix C error (prev)
fix eHPrev = 0 # fix H error (prev)
# triple PD loop! controls reaching rF/C/H moles in F/C/H
# if rF is reached, start inputting the nI mixture in advance to (f)
tag PDLoop:
if eF > ERRORF:
uF = (kPF * eF) + (kDF * (eF - eFPrev) / dt)
furnace.SettingOutput = uF
eFPrev = eF
else:
furnace.SettingOutput = 0
furnace.SettingInput = 100

if eC > ERRORC:
uC = (kPC * eC) + (kDC * (eC - eCPrev) / dt)
nCPump.Setting = uC # set C pump
eCPrev = eC

if eH > ERRORH:
uH = (kPH * eH) + (kDH * (eH - eHPrev) / dt)
nHPump.Setting = uH # set H pump
eHPrev = eH

# check feedback and calculate new error
yield()
if eF > ERRORF:
nF = furnace.TotalMoles
eF = nF - rF

if eC > ERRORC:
nC = vCFactor * tankC.TotalMoles
eC = nC - rC

if eH > ERRORH:
nH = vHFactor * tankH.TotalMoles
eH = nH - rH

bgt(eF, ERRORF, PDLoop)
bgt(eC, ERRORC, PDLoop)
bgt(eH, ERRORH, PDLoop)
nCPump.On = 0
nHPump.On = 0

# (f) input the nC and nH gas (can the necessary run time be calculated?)
db.Setting = 7
yield()
AdvFurnace.all.SettingInput = 100
while PipeAnalyzer.all.TotalMoles.avg > 0:
# (g) input the nC and nH gas
furnace.SettingInput = 100
while furnace.TotalMoles > 0:
yield()
AdvFurnace.all.SettingInput = 0
furnace.SettingInput = 0

# (g) enjoy
# (?) enjoy
db.Setting = 0
16 changes: 8 additions & 8 deletions translator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ fn main() {
#[rustfmt::skip]
let mips = mips
.optimize(OptimizationConfig {
// remove_comments: true,
remove_comments: false,
remove_comments: true,
// remove_comments: false,

remove_empty: true,
// remove_empty: false,

// remove_empty_comments: true,
remove_empty_comments: false,
remove_empty_comments: true,
// remove_empty_comments: false,

remove_reg_aliases: true,
// remove_reg_aliases: false,

remove_dev_aliases: true,
// remove_dev_aliases: false,
// remove_dev_aliases: true,
remove_dev_aliases: false,

remove_defines: true,
// remove_defines: false,
Expand All @@ -74,8 +74,8 @@ fn main() {
}
println!("--------------------------------------------------------------------------------");
for (_i, line) in mips.lines.iter().enumerate() {
// println!("{:>w$}: {}", _i, line, w = w);
println!("{}", line);
println!("{:>w$}: {}", _i, line, w = w);
// println!("{}", line);
}
// for (i, (index, (s, e))) in mips.analyze_lifetimes().iter().enumerate() {
// println!("{}: {} ({},{})", i, index, s, e);
Expand Down

0 comments on commit 0c1bcce

Please sign in to comment.