Skip to content

Commit

Permalink
Update: Implemented a Basic loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
PranavVerma-droid committed Oct 6, 2024
1 parent 62746bb commit d960e65
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "Tidal"
version = "1.5.0"
version = "1.6.0"


[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A Language Made using Rust. <br>
Made by Pranav Verma.

Uses the Extension `.td` and `.br`. <br>
Please Check the [Wiki](https://github.com/PranavVerma-droid/Tidal/wiki) for more Information.
Please Check the [Wiki](https://github.com/PranavVerma-droid/Tidal/wiki) for documentation and syntax.

## Download
The Latest Compiled Build Can Be Found in the [Releases](https://github.com/PranavVerma-droid/Blue-Lagoon/releases) (For Windows and Linux)
Expand Down
9 changes: 9 additions & 0 deletions code/BrainRot/brainrot-4.br
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rizzler i = 0 no cap

yeet (true) {
skibidi(i) no cap
i = i + 1 no cap
drip (i == 1000) {
aura +69420 no cap
}
}
27 changes: 27 additions & 0 deletions code/Normal/file-12.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var previousVariable = 1.0;
var currentVariable = 1.0;
var nextVariable;



/* var finalResult = "0 1 1 ";

for(var i = 0; i >= 0; i = i + 1;) {
nextVariable = float(previousVariable + currentVariable);
finalResult = (finalResult + str(nextVariable) + " ");
previousVariable = currentVariable;
currentVariable = nextVariable;
}

print(finalResult); */

print(0);
print(1);
print(1);

for(var i = 0; i <= 1500; i = i + 1;) {
nextVariable = float(previousVariable + currentVariable);
print(nextVariable);
previousVariable = currentVariable;
currentVariable = nextVariable;
}
9 changes: 9 additions & 0 deletions code/Normal/file-13.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var i = 0;

while (true) {
print(i);
i = i + 1;
if (i == 1000) {
break;
}
}
24 changes: 21 additions & 3 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn interpret_node(node: &ASTNode, symbol_table: &mut HashMap<String, (Value, boo
_ => panic!("Unsupported operator for numbers"),
}
}
(Value::Float(l), Value::Float(r)) => { // New case for float operations
(Value::Float(l), Value::Float(r)) => {
match op {
Token::Plus => Value::Float(l + r),
Token::Minus => Value::Float(l - r),
Expand All @@ -60,7 +60,7 @@ fn interpret_node(node: &ASTNode, symbol_table: &mut HashMap<String, (Value, boo
_ => panic!("Unsupported operator for floats"),
}
}
(Value::Number(l), Value::Float(r)) => { // Mixed number and float operations
(Value::Number(l), Value::Float(r)) => {
let l = l as f64;
match op {
Token::Plus => Value::Float(l + r),
Expand All @@ -76,7 +76,7 @@ fn interpret_node(node: &ASTNode, symbol_table: &mut HashMap<String, (Value, boo
_ => panic!("Unsupported operator for mixed number and float"),
}
}
(Value::Float(l), Value::Number(r)) => { // Mixed float and number operations
(Value::Float(l), Value::Number(r)) => {
let r = r as f64;
match op {
Token::Plus => Value::Float(l + r),
Expand Down Expand Up @@ -136,6 +136,24 @@ fn interpret_node(node: &ASTNode, symbol_table: &mut HashMap<String, (Value, boo
}
Value::Null
},
ASTNode::While(condition, body) => {
loop {
let cond_value = interpret_node(condition, symbol_table, is_verbose, true);
if let Value::Boolean(false) = cond_value {
break;
}

for stmt in body {
let result = interpret_node(stmt, symbol_table, is_verbose, true);
match result {
Value::Break => return Value::Null,
Value::Continue => break,
_ => {}
}
}
}
Value::Null
},
ASTNode::Var(name, expr, is_mutable) => {
let value = if let Some(expr) = expr {
interpret_node(expr, symbol_table, is_verbose, in_loop)
Expand Down
2 changes: 2 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum Token {
RBracket,
Null,
For,
While,
Break,
Continue,
Comma,
Expand Down Expand Up @@ -156,6 +157,7 @@ impl<'a> Lexer<'a> {
"true" => Token::Boolean(true),
"false" => Token::Boolean(false),
"for" => Token::For,
"while" => Token::While,
"break" => Token::Break,
"continue" => Token::Continue,
"int" | "str" | "float" | "bool" => {
Expand Down
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ fn main() {
// collect args
let args: Vec<String> = env::args().collect();

// verbose
// verbose mode flag check
let is_verbose = args.contains(&String::from("--verbose")) || args.contains(&String::from("-v"));

// error display lul
if args.len() < 2 || args.contains(&String::from("help")) || args.contains(&String::from("--help")) || args.contains(&String::from("-h")) {
help();
std::process::exit(1);
Expand All @@ -39,7 +40,7 @@ fn main() {

// Brain Rot Parser
let processed_contents = if is_brain_rot {
preprocess_brain_rot(&contents)
preprocess_skibidi(&contents)
} else {
contents
};
Expand All @@ -66,7 +67,8 @@ fn help() {
println!("");
}

fn preprocess_brain_rot(input: &str) -> String {
//okay, here is where the brainrot starts ☠️☠️
fn preprocess_skibidi(input: &str) -> String {
let replacements: HashMap<&str, &str> = [
("rizzler", "var"),
("sigma", "novar"),
Expand All @@ -75,6 +77,7 @@ fn preprocess_brain_rot(input: &str) -> String {
("skibidi", "print"),
("fanum tax", "type"),
("bussin", "for"),
("yeet", "while"),
("sussy", "/*"),
("baka", "*/"),
("aura +69420", "break"),
Expand Down
14 changes: 14 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum ASTNode {
TypeCast(String, Box<ASTNode>),
If(Box<ASTNode>, Vec<ASTNode>, Vec<(ASTNode, Vec<ASTNode>)>, Option<Vec<ASTNode>>),
For(Box<ASTNode>, Box<ASTNode>, Box<ASTNode>, Vec<ASTNode>),
While(Box<ASTNode>, Vec<ASTNode>),
Break,
Continue,
}
Expand Down Expand Up @@ -77,6 +78,7 @@ impl<'a> Parser<'a> {
Token::For => self.parse_for_loop(),
Token::Break => self.parse_break(),
Token::Continue => self.parse_continue(),
Token::While => self.parse_while_loop(),
Token::Type => self.parse_type(),
_ => panic!("Unexpected token in statement: {:?}", self.current_token),
}
Expand All @@ -91,6 +93,18 @@ impl<'a> Parser<'a> {
ASTNode::Type(Box::new(expr))
}

fn parse_while_loop(&mut self) -> ASTNode {
self.eat(Token::While);
self.eat(Token::LParen);
let condition = self.parse_expr();
self.eat(Token::RParen);
self.eat(Token::LBrace);
let body = self.parse_block();
self.eat(Token::RBrace);

ASTNode::While(Box::new(condition), body)
}

fn parse_if_statement(&mut self) -> ASTNode {
self.eat(Token::If);
self.eat(Token::LParen);
Expand Down

0 comments on commit d960e65

Please sign in to comment.