Skip to content

Commit

Permalink
parse: pseudo-variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rpitasky committed Dec 10, 2024
1 parent 07b8d54 commit 21fec15
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
4 changes: 1 addition & 3 deletions ti-basic-optimizer/src/analyze/control_flow/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{
collections::{BTreeMap, BTreeSet},
};
use std::collections::{BTreeMap, BTreeSet};

use tifloats::{tifloat, Float};
use titokens::Token;
Expand Down
17 changes: 7 additions & 10 deletions ti-basic-optimizer/src/parse/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use crate::parse::components::{
matrix_name::MatrixName,
numeric_var_name::NumericVarName,
pic_image_name::{ImageName, PicName},
pseudovariable::PseudoVariable,
rand::Rand,
store_target::StoreTarget,
string::TIString,
Expand All @@ -33,6 +34,7 @@ mod matrix_name;
mod numeric_literal;
mod numeric_var_name;
mod pic_image_name;
mod pseudovariable;
mod rand;
mod store_target;
mod string;
Expand Down Expand Up @@ -83,9 +85,7 @@ pub enum Operand {
Ans,
I,
Rand(Rand),
GetKey,
GetDate,
StartTmr,
PseudoVariable(PseudoVariable),
NumericLiteral(tifloats::Float),
StringLiteral(TIString),
ListLiteral(TIList),
Expand Down Expand Up @@ -138,12 +138,11 @@ impl Parse for Operand {
}
Token::OneByte(0x2C) => Ok(Some(Self::I)),
Token::OneByte(0xAB) => Ok(Rand::parse(token, more)?.map(Self::Rand)),
Token::OneByte(0xAD) => Ok(Some(Self::GetKey)),
Token::TwoByte(0xEF, 0x09) => Ok(Some(Self::GetDate)),
Token::TwoByte(0xEF, 0x0B) => Ok(Some(Self::StartTmr)),
Token::OneByte(0xAD) | Token::TwoByte(0xEF, 0x09..=0x0E) => {
Ok(PseudoVariable::parse(token, more)?.map(Self::PseudoVariable))
}
Token::OneByte(0x2A) => Ok(TIString::parse(token, more)?.map(Self::StringLiteral)),
Token::OneByte(0x08) => Ok(TIList::parse(token, more)?.map(Self::ListLiteral)),
Token::OneByte(0x06) => Ok(TIMatrix::parse(token, more)?.map(Self::MatrixLiteral)),
Token::TwoByte(0xAA, _) => Ok(StringName::parse(token, more)?.map(Self::StringName)),
Token::TwoByte(0x5C, _) => {
if let Some(name) = MatrixName::parse(token, more)? {
Expand Down Expand Up @@ -204,9 +203,7 @@ impl Reconstruct for Operand {
Operand::Ans => vec![Token::OneByte(0x72)],
Operand::I => vec![Token::OneByte(0x2C)],
Operand::Rand(x) => x.reconstruct(config),
Operand::GetKey => vec![Token::OneByte(0xAD)],
Operand::GetDate => vec![Token::TwoByte(0xEF, 0x09)],
Operand::StartTmr => vec![Token::TwoByte(0xEF, 0x0B)],
Operand::PseudoVariable(x) => x.reconstruct(config),
Operand::NumericLiteral(x) => x.reconstruct(config),
Operand::StringLiteral(x) => x.reconstruct(config),
Operand::ListLiteral(x) => x.reconstruct(config),
Expand Down
30 changes: 30 additions & 0 deletions ti-basic-optimizer/src/parse/components/pseudovariable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::error_reporting::TokenReport;
use crate::parse::{Parse, Reconstruct};
use crate::Config;
use titokens::{Token, Tokens};

/// Pseudo-variables are like `GetKey` and `IsClockOn`- functions that return a value and never
/// accept arguments.
#[derive(Clone, Debug)]
pub struct PseudoVariable {
pub kind: Token,
}

impl Parse for PseudoVariable {
fn parse(token: Token, _more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if matches!(
token,
Token::OneByte(0xAD) | Token::TwoByte(0xEF, 0x09..=0x0E)
) {
Ok(Some(PseudoVariable { kind: token }))
} else {
Ok(None)
}
}
}

impl Reconstruct for PseudoVariable {
fn reconstruct(&self, _config: &Config) -> Vec<Token> {
vec![self.kind]
}
}

0 comments on commit 21fec15

Please sign in to comment.