added if statements!

This commit is contained in:
Goren Barak 2023-12-30 16:18:12 -05:00
parent 1140573665
commit 27923e8c0e
4 changed files with 16 additions and 169 deletions

View file

@ -1,11 +0,0 @@
# Install rust if it isn't installed
if [!which rustc]; then
curl https://sh.rustup.rs -sSf | sh
fi
# Install cargo if it isn't installed
if [!which cargo]; then
curl https://sh.rustup.rs -sSf | sh
fi
cargo build -r

View file

@ -142,6 +142,22 @@ impl<'a> Vm {
self.stack.push(stack_top); self.stack.push(stack_top);
self.stack.push(stack_top); self.stack.push(stack_top);
}, },
"IF"|"if" => {
let expr = unwrap!();
let mut content = String::new();
let mut next = parser.inner_next();
while next != Some("THEN") && next != None {
content.push_str(next.unwrap());
content.push_str(" ");
next = parser.inner_next();
}
if expr == 0 {
// Do nothing.
} else {
self.vmrun(&mut Parser::new(content.as_str()));
}
},
":" => { ":" => {
let mut word_content = String::new(); let mut word_content = String::new();
let Some(Word(word_name)) = parser.next() else { println!("ERROR: Word name not given."); std::process::exit(1); }; let Some(Word(word_name)) = parser.next() else { println!("ERROR: Word name not given."); std::process::exit(1); };

View file

@ -1,151 +0,0 @@
use std::collections::HashMap;
use crate::parse::Parser;
use crate::parse::TokenType::*;
#[derive(Debug)]
pub struct Vm {
stack: Vec<u64>,
dictionary: Dictionary
}
#[derive(Debug, Default)]
struct Dictionary {
dp: u64,
data: HashMap<u64, u64>,
words: HashMap<String, String>
}
impl<'a> Vm {
pub fn new() -> Self {
Self {
stack: Vec::new(),
dictionary: Default::default()
}
}
pub fn vmrun(&mut self, parser: &mut Parser) {
macro_rules! unwrap {
() => {
match self.stack.pop() {
Some(v) => {
v
},
None => {
println!("ERROR: stack empty");
std::process::exit(1);
}
}
}
}
while let Some(item) = parser.next() {
match item {
Number(n) => self.stack.push(n),
Word(w) => {
match w {
"." => {
println!("{:?} ok", unwrap!());
},
"+" => {
let sum = unwrap!().wrapping_add(unwrap!());
self.stack.push(sum);
},
"*" => {
let prod = unwrap!().wrapping_mul(unwrap!());
self.stack.push(prod);
},
"/" => {
let stack_top = unwrap!();
let stack_second = unwrap!();
let quo = stack_second.wrapping_div(stack_top);
self.stack.push(quo);
},
"=" => {
if unwrap!() == unwrap!() {
self.stack.push(1);
} else {
self.stack.push(0);
}
},
"MOD"|"mod" => {
let stack_top = unwrap!();
let stack_second = unwrap!();
let rem = stack_second.wrapping_rem(stack_top);
self.stack.push(rem);
},
"/MOD"|"/mod" => {
let stack_top = unwrap!();
let stack_second = unwrap!();
let rem = stack_second.wrapping_rem(stack_top);
let quo = stack_second.wrapping_div(stack_top);
self.stack.push(rem);
self.stack.push(quo);
},
"SWAP"|"swap" => {
let stack_top = unwrap!();
let stack_second = unwrap!();
self.stack.push(stack_top);
self.stack.push(stack_second);
},
"HERE"|"here" => {
self.stack.push(self.dictionary.dp);
},
"@" => {
let addr = unwrap!();
self.stack.push(*self.dictionary.data.get(&addr).unwrap_or(&0));
},
"!" => {
let addr = unwrap!();
let content = unwrap!();
self.dictionary.data.insert(addr, content);
},
"," => {
let content = unwrap!();
self.dictionary.dp += 1;
self.dictionary.data.insert(self.dictionary.dp, content);
},
"ALLOT"|"allot" => {
self.dictionary.dp += unwrap!();
},
"CREATE"|"create" => {
self.dictionary.dp += 1;
let Some(Word(word_name)) = parser.next() else { println!("ERROR: Word name not given."); std::process::exit(1) };
self.dictionary.words.insert(word_name.to_string(), self.dictionary.dp.to_string());
},
"VARIABLE"|"variable" => {
self.dictionary.dp += 1;
let Some(Word(word_name)) = parser.next() else { println!("ERROR: Word name not given."); std::process::exit(1) };
self.dictionary.words.insert(word_name.to_string(), self.dictionary.dp.to_string());
},
"DUP"|"dup" => {
let stack_top = unwrap!();
self.stack.push(stack_top);
self.stack.push(stack_top);
},
":" => {
let mut word_content = String::new();
let Some(Word(word_name)) = parser.next() else { println!("ERROR: Word name not given."); std::process::exit(1); };
let mut next = parser.inner_next();
while next != Some(";") && next != None {
word_content.push_str(next.unwrap());
word_content.push_str(" ");
next = parser.inner_next();
}
self.dictionary.words.insert(word_name.to_string(), word_content);
},
word => {
let words = self.dictionary.words.clone();
let Some(word_content) = words.get(word) else {
println!("word {:?} does not exist", word);
std::process::exit(1);
};
self.vmrun(&mut Parser::new(word_content));
}
}
}
}
}
}
}

7
tags
View file

@ -44,10 +44,6 @@
!_TAG_KIND_DESCRIPTION!Rust s,struct /structural type/ !_TAG_KIND_DESCRIPTION!Rust s,struct /structural type/
!_TAG_KIND_DESCRIPTION!Rust t,typedef /Type Alias/ !_TAG_KIND_DESCRIPTION!Rust t,typedef /Type Alias/
!_TAG_KIND_DESCRIPTION!Rust v,variable /Global variable/ !_TAG_KIND_DESCRIPTION!Rust v,variable /Global variable/
!_TAG_KIND_DESCRIPTION!Sh a,alias /aliases/
!_TAG_KIND_DESCRIPTION!Sh f,function /functions/
!_TAG_KIND_DESCRIPTION!Sh h,heredoc /label for here document/
!_TAG_KIND_DESCRIPTION!Sh s,script /script files/
!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/ !_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/
!_TAG_OUTPUT_FILESEP slash /slash or backslash/ !_TAG_OUTPUT_FILESEP slash /slash or backslash/
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/ !_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
@ -55,15 +51,12 @@
!_TAG_PARSER_VERSION!D 0.0 /current.age/ !_TAG_PARSER_VERSION!D 0.0 /current.age/
!_TAG_PARSER_VERSION!JSON 0.0 /current.age/ !_TAG_PARSER_VERSION!JSON 0.0 /current.age/
!_TAG_PARSER_VERSION!Rust 0.0 /current.age/ !_TAG_PARSER_VERSION!Rust 0.0 /current.age/
!_TAG_PARSER_VERSION!Sh 0.0 /current.age/
!_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/ !_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/
!_TAG_PROC_CWD /home/goren/Code/fourth/ // !_TAG_PROC_CWD /home/goren/Code/fourth/ //
!_TAG_PROGRAM_AUTHOR Universal Ctags Team // !_TAG_PROGRAM_AUTHOR Universal Ctags Team //
!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/ !_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
!_TAG_PROGRAM_URL https://ctags.io/ /official site/ !_TAG_PROGRAM_URL https://ctags.io/ /official site/
!_TAG_PROGRAM_VERSION 6.0.0 /p6.0.20221218.0/ !_TAG_PROGRAM_VERSION 6.0.0 /p6.0.20221218.0/
!_TAG_ROLE_DESCRIPTION!Sh!heredoc endmarker /end marker/
!_TAG_ROLE_DESCRIPTION!Sh!script loaded /loaded/
0 target/debug/.fingerprint/fourth-0eb22537b83d0b36/test-bin-fourth.json /^{"rustc":3462548589188548201,"features":"[]","target":6449572106981162502,"profile":183265222628/;" o array:local 0 target/debug/.fingerprint/fourth-0eb22537b83d0b36/test-bin-fourth.json /^{"rustc":3462548589188548201,"features":"[]","target":6449572106981162502,"profile":183265222628/;" o array:local
0 target/debug/.fingerprint/fourth-c58712c3665fe72a/bin-fourth.json /^{"rustc":3462548589188548201,"features":"[]","target":6449572106981162502,"profile":110397424744/;" o array:local 0 target/debug/.fingerprint/fourth-c58712c3665fe72a/bin-fourth.json /^{"rustc":3462548589188548201,"features":"[]","target":6449572106981162502,"profile":110397424744/;" o array:local
0 target/debug/.fingerprint/fourth-d5bfd771d6ea8237/bin-fourth.json /^{"rustc":3462548589188548201,"features":"[]","target":6449572106981162502,"profile":131263742483/;" o array:local 0 target/debug/.fingerprint/fourth-d5bfd771d6ea8237/bin-fourth.json /^{"rustc":3462548589188548201,"features":"[]","target":6449572106981162502,"profile":131263742483/;" o array:local