From 21fec15f2c1aef7b667af9bee14c657cbbe61395 Mon Sep 17 00:00:00 2001 From: Ryan Pitasky Date: Tue, 10 Dec 2024 15:26:43 -0500 Subject: [PATCH] parse: pseudo-variables --- .../src/analyze/control_flow/parser.rs | 4 +-- .../src/parse/components/mod.rs | 17 +++++------ .../src/parse/components/pseudovariable.rs | 30 +++++++++++++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 ti-basic-optimizer/src/parse/components/pseudovariable.rs diff --git a/ti-basic-optimizer/src/analyze/control_flow/parser.rs b/ti-basic-optimizer/src/analyze/control_flow/parser.rs index cc4b802..81cf5db 100644 --- a/ti-basic-optimizer/src/analyze/control_flow/parser.rs +++ b/ti-basic-optimizer/src/analyze/control_flow/parser.rs @@ -1,6 +1,4 @@ -use std::{ - collections::{BTreeMap, BTreeSet}, -}; +use std::collections::{BTreeMap, BTreeSet}; use tifloats::{tifloat, Float}; use titokens::Token; diff --git a/ti-basic-optimizer/src/parse/components/mod.rs b/ti-basic-optimizer/src/parse/components/mod.rs index 1d46b1e..f28f435 100644 --- a/ti-basic-optimizer/src/parse/components/mod.rs +++ b/ti-basic-optimizer/src/parse/components/mod.rs @@ -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, @@ -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; @@ -83,9 +85,7 @@ pub enum Operand { Ans, I, Rand(Rand), - GetKey, - GetDate, - StartTmr, + PseudoVariable(PseudoVariable), NumericLiteral(tifloats::Float), StringLiteral(TIString), ListLiteral(TIList), @@ -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)? { @@ -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), diff --git a/ti-basic-optimizer/src/parse/components/pseudovariable.rs b/ti-basic-optimizer/src/parse/components/pseudovariable.rs new file mode 100644 index 0000000..b6ccbd3 --- /dev/null +++ b/ti-basic-optimizer/src/parse/components/pseudovariable.rs @@ -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, 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 { + vec![self.kind] + } +}