Began adding support for variables

This commit is contained in:
2026-05-24 15:09:59 +01:00
parent 93ca1ea34a
commit 21bf659718
8 changed files with 287 additions and 24 deletions
+14
View File
@@ -18,6 +18,7 @@ use crate::
mod character_parse;
mod keyword_parse;
mod identifier_parse;
// Parse the tokens in a file
// Returns success or an error string
@@ -66,6 +67,19 @@ pub fn token_parse(
},
};
}
// Identifier
Some(tokenise::Token::Identifier(name)) =>
{
index = match identifier_parse::identifier_parse(index+1,tokens)
{
Ok(increment) => increment,
Err((err,increment)) =>
{
warn!("{err}");
increment
},
};
}
Some(_) =>
{
warn!("Unexpected token");
+29
View File
@@ -0,0 +1,29 @@
use crate::
{
// Internal code
character,
api,
tokenise,
UnwrapOrExit,
//Libs
Mutex,
Arc,
HashMap,
VecDeque,
info,
warn,
debug,
};
#[allow(unused_variables)]
pub fn identifier_parse
(
index: usize,
tokens: &[tokenise::Token],
) -> Result<usize,(String,usize)>
{
let mut sum_index: usize = index;
sum_index += 1;
Ok(sum_index)
}
+59 -3
View File
@@ -7,19 +7,40 @@ use crate::
pub enum Token
{
String(String),
#[allow(dead_code)]
Integer(i64), // idk someone might want to do a big number
#[allow(dead_code)]
Bool(bool),
#[allow(dead_code)]
Operator(Operator),
Keyword(String), // Keywords aren't checked for validity in this stage
#[allow(dead_code)] // This is unused rn, but am going to add it later
Identifier(String),
Bracket((Bracket,usize)), // Stores the index of the matching deliminator
Character(String),
}
#[derive(Debug,Clone,PartialEq, Eq)]
pub enum Bracket
#[derive(Debug,Clone,PartialEq,Eq)]
pub enum Bracket
{
Opening,
Closing,
}
#[derive(Debug, Clone)]
pub enum Operator
{
// Changing a value
Assignment,
Add,
Sub,
// Comparing a value
Comparison,
Greater,
Less,
GreaterOrEqual,
LessOrEqual,
}
pub fn get_string_token(tokens: &[Token], index: usize)
-> Result<String, String>
{
@@ -64,6 +85,29 @@ pub fn tokenise(file_contents: &str)
while index < space_seperated.len()
{
let mut item = space_seperated[index].to_string();
// Operator
let op: Option<Operator> = match item.as_str()
{
"=" => Some(Operator::Assignment),
"+" => Some(Operator::Add),
"-" => Some(Operator::Sub),
"==" => Some(Operator::Comparison),
">" => Some(Operator::Greater),
"<" => Some(Operator::Less),
">=" => Some(Operator::GreaterOrEqual),
"<=" => Some(Operator::LessOrEqual),
_ => None,
};
match op
{
Some(op) =>
{
tokenised_data.push(Token::Operator(op));
index += 1;
continue // skip the rest of this loop
},
None => (),
}
// Characters
if item.starts_with('@')
{
@@ -80,6 +124,18 @@ pub fn tokenise(file_contents: &str)
item = new_item;
tokenised_data.push(Token::String(item));
}
// variable/identifier
else if item.starts_with("$")
{
let mut chars = item.chars();
chars.next();
tokenised_data.push(Token::Identifier(chars.as_str().to_string()));
}
// Integer
else if item.parse::<i64>().is_ok()
{
tokenised_data.push(Token::Integer(item.parse::<i64>().unwrap()));
}
// Labels
else if item.ends_with(':')
{