basic structure/instruction creation macro beginnings

This commit is contained in:
Rowan Torbitzky-Lane 2025-04-02 22:27:48 -05:00
parent bd4c57599a
commit ed70888a30
7 changed files with 76 additions and 0 deletions

View File

@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
rand = "0.9.0"
paste = "1.0.15"

1
src/instructions/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod numeric;

View File

@ -0,0 +1,39 @@
//! # Numeric Instructions
//!
//! This file contains numeric instructions for int and float.
use crate::push::state::*;
use paste::paste;
use std::ops::Add;
fn _add<T: Add<Output = T>>(val0: T, val1: T) -> T {
val0 + val1
}
#[macro_export]
macro_rules! make_instruction {
($stack_name:ident, $fn_name:ident) => {
paste::item! {
fn [< $stack_name $fn_name >] (state: &mut PushState) -> Option< {
let val0 = state.$stack_name[0];
let val1 = state.$stack_name[1];
$fn_name(val0, val1);
println!("{val0} {val1}");
}
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_test() {
let mut test_state = EMPTY_STATE;
test_state.int = vec![1, 2];
// assert_eq!(vec![1, 2, 3], _add(&mut test_state).int);
make_instruction!(int, _add);
int_add(&mut test_state);
}
}

View File

@ -1,3 +1,6 @@
mod instructions;
mod push;
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");
} }

3
src/push/interpreter.rs Normal file
View File

@ -0,0 +1,3 @@
use crate::push::state;
pub fn interpret_program() {}

2
src/push/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod interpreter;
pub mod state;

26
src/push/state.rs Normal file
View File

@ -0,0 +1,26 @@
#[derive(Debug, Clone)]
pub struct PushState {
pub int: Vec<i64>,
pub float: Vec<i64>,
pub string: Vec<Vec<u8>>,
pub bool: Vec<bool>,
pub char: Vec<u8>,
pub vector_int: Vec<Vec<i64>>,
pub vector_float: Vec<Vec<f64>>,
pub vector_string: Vec<Vec<Vec<u8>>>,
pub vector_bool: Vec<Vec<bool>>,
pub vector_char: Vec<u8>,
}
pub const EMPTY_STATE: PushState = PushState {
int: vec![],
float: vec![],
string: vec![],
bool: vec![],
char: vec![],
vector_int: vec![],
vector_float: vec![],
vector_string: vec![],
vector_bool: vec![],
vector_char: vec![],
};