Skip to content

Commit

Permalink
Added Float Data Type, and Added a New BrainRot Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
PranavVerma-droid committed Oct 5, 2024
1 parent 8ab0b93 commit 52e039a
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 22 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.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "Tidal"
version = "1.4.0"
edition = "2021"
version = "1.5.0"


[dependencies]

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Tidal
<i>PS, I have just changed the Name of the Programming Language from `Blue Lagoon` to `Tidal`, so, if you see Blue Lagoon written anywhere, just know, it was the old name of the Language!</i>

A Language Made using Rust and LLVM. <br>
A Language Made using Rust. <br>
Made by Pranav Verma.

Uses the Extension `.td`
Uses the Extension `.td` and `.br`.

## 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
12 changes: 12 additions & 0 deletions code/BrainRot/brainrot-1.br
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
rizzler a = 10 no cap
sigma b = 20.4 no cap

skibidi(a) no cap
skibidi(fanum tax(a)) no cap

skibidi("") no cap

skibidi(b) no cap
skibidi(fanum tax(b)) no cap

sussy This should be ignored. baka
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions code/Normal/file-9.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var a = 10.23;
var b = 123;


print(a / b);

a = 0.0;

if (type(a) == float) {
print("a is a float");
} else {
print("a is not a float.");
}

for(var i = 0; i < 10; i = i + 1;) {
print(i);
if (i == 5) {
break;
} else {
a = a + i;
}
}
52 changes: 51 additions & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn interpret_node(node: &ASTNode, symbol_table: &mut HashMap<String, (Value, boo
match node {
ASTNode::Number(val) => Value::Number(*val),
ASTNode::String(val) => Value::String(val.clone()),
ASTNode::Float(val) => Value::Float(*val),
ASTNode::Boolean(val) => Value::Boolean(*val),
ASTNode::Null => Value::Null,
ASTNode::BinaryOp(left, op, right) => {
Expand All @@ -34,7 +35,7 @@ fn interpret_node(node: &ASTNode, symbol_table: &mut HashMap<String, (Value, boo
Token::Plus => Value::Number(l + r),
Token::Minus => Value::Number(l - r),
Token::Multiply => Value::Number(l * r),
Token::Divide => Value::Number(l / r),
Token::Divide => Value::Float(l as f64 / r as f64),
Token::Equal => Value::Boolean(l == r),
Token::NotEqual => Value::Boolean(l != r),
Token::Greater => Value::Boolean(l > r),
Expand All @@ -44,6 +45,53 @@ 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
match op {
Token::Plus => Value::Float(l + r),
Token::Minus => Value::Float(l - r),
Token::Multiply => Value::Float(l * r),
Token::Divide => Value::Float(l / r),
Token::Equal => Value::Boolean(l == r),
Token::NotEqual => Value::Boolean(l != r),
Token::Greater => Value::Boolean(l > r),
Token::Less => Value::Boolean(l < r),
Token::GreaterEqual => Value::Boolean(l >= r),
Token::LessEqual => Value::Boolean(l <= r),
_ => panic!("Unsupported operator for floats"),
}
}
(Value::Number(l), Value::Float(r)) => { // Mixed number and float operations
let l = l as f64;
match op {
Token::Plus => Value::Float(l + r),
Token::Minus => Value::Float(l - r),
Token::Multiply => Value::Float(l * r),
Token::Divide => Value::Float(l / r),
Token::Equal => Value::Boolean(l == r),
Token::NotEqual => Value::Boolean(l != r),
Token::Greater => Value::Boolean(l > r),
Token::Less => Value::Boolean(l < r),
Token::GreaterEqual => Value::Boolean(l >= r),
Token::LessEqual => Value::Boolean(l <= r),
_ => panic!("Unsupported operator for mixed number and float"),
}
}
(Value::Float(l), Value::Number(r)) => { // Mixed float and number operations
let r = r as f64;
match op {
Token::Plus => Value::Float(l + r),
Token::Minus => Value::Float(l - r),
Token::Multiply => Value::Float(l * r),
Token::Divide => Value::Float(l / r),
Token::Equal => Value::Boolean(l == r),
Token::NotEqual => Value::Boolean(l != r),
Token::Greater => Value::Boolean(l > r),
Token::Less => Value::Boolean(l < r),
Token::GreaterEqual => Value::Boolean(l >= r),
Token::LessEqual => Value::Boolean(l <= r),
_ => panic!("Unsupported operator for mixed float and number"),
}
}
(Value::String(s), Value::String(t)) => {
match op {
Token::Plus => Value::String(s + &t),
Expand Down Expand Up @@ -78,6 +126,7 @@ fn interpret_node(node: &ASTNode, symbol_table: &mut HashMap<String, (Value, boo
Value::Number(n) => println!("{}", n),
Value::String(s) => println!("{}", s),
Value::Boolean(b) => println!("{}", b),
Value::Float(f) => println!("{}", f),
Value::Null => println!("null"),
Value::Type(t) => println!("{}", t),
Value::Break => println!("break"),
Expand Down Expand Up @@ -139,6 +188,7 @@ fn interpret_node(node: &ASTNode, symbol_table: &mut HashMap<String, (Value, boo
Value::Number(_) => "int",
Value::String(_) => "str",
Value::Boolean(_) => "bool",
Value::Float(_) => "float",
Value::Null => "null",
Value::Type(_) => "type",
Value::Break => "break",
Expand Down
14 changes: 12 additions & 2 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum Token {
Else,
Identifier(String),
Number(i32),
Float(f64),
String(String),
Boolean(bool),
TypeLiteral(String),
Expand Down Expand Up @@ -112,15 +113,24 @@ impl<'a> Lexer<'a> {

fn read_number(&mut self, first_digit: char) -> Token {
let mut number = first_digit.to_string();
let mut is_float = false;
while let Some(&ch) = self.input.peek() {
if ch.is_digit(10) {
number.push(ch);
self.input.next();
} else if ch == '.' && !is_float {
is_float = true;
number.push(ch);
self.input.next();
} else {
break;
}
}
Token::Number(number.parse().unwrap())
if is_float {
Token::Float(number.parse().unwrap())
} else {
Token::Number(number.parse().unwrap())
}
}

fn read_identifier_or_keyword(&mut self, first_char: char) -> Token {
Expand All @@ -147,7 +157,7 @@ impl<'a> Lexer<'a> {
"for" => Token::For,
"break" => Token::Break,
"continue" => Token::Continue,
"bool" | "int" | "str" => Token::TypeLiteral(identifier),
"bool" | "int" | "str" | "float" => Token::TypeLiteral(identifier),
_ => Token::Identifier(identifier),
}
}
Expand Down
98 changes: 84 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;
use std::fs;
use std::path::Path;
use std::collections::HashMap;

mod interpreter;
mod lexer;
Expand All @@ -18,10 +19,11 @@ fn main() {
std::process::exit(1);
}


let filename = &args[1];
if !filename.ends_with(".td") {
eprintln!("Error: Input file must have a .td extension");
let is_brain_rot = filename.ends_with(".br");

if !filename.ends_with(".td") && !is_brain_rot {
eprintln!("Error: Input file must have a .td or .br extension");
std::process::exit(1);
}

Expand All @@ -35,24 +37,92 @@ fn main() {
let contents = fs::read_to_string(filename)
.expect("Something went wrong reading the file");

// Brain Rot Parser
let processed_contents = if is_brain_rot {
preprocess_brain_rot(&contents)
} else {
contents
};

// Parser
let mut parser = parser::Parser::new(&contents);
let mut parser = parser::Parser::new(&processed_contents);

// Parser to AST
let ast = parser.parse();

// Interpreter
interpreter::interpret(ast, is_verbose);
}

fn help() {
println!("");
println!("Tidal Programming Language");
println!("Made by Pranav Verma - For the Lagoon Project.");
println!("");
println!("Usage: td <file.td | file.br> [--verbose | -v]");
println!("Options:");
println!(" --verbose, -v Enable verbose output");
println!(" help, --help, -h Display this help message");
println!("");
}

fn preprocess_brain_rot(input: &str) -> String {
let replacements: HashMap<&str, &str> = [
("rizzler", "var"),
("sigma", "novar"),
/* ("be", "="), */
("no cap", ";"),
("skibidi", "print"),
("fanum tax", "type"),
("sussy", "/*"),
("baka", "*/"),
].iter().cloned().collect();

let mut result = String::new();
let mut buffer = String::new();

fn help() {
println!("");
println!("Tidal Programming Language");
println!("Made by Pranav Verma - For the Lagoon Project.");
println!("");
println!("Usage: td <file.td> [--verbose | -v]");
println!("Options:");
println!(" --verbose, -v Enable verbose output");
println!(" help, --help, -h Display this help message");
println!("");
let chars: Vec<char> = input.chars().collect();
let mut i = 0;

while i < chars.len() {
let mut matched = false;

for (&key, &value) in &replacements {
if chars[i..].starts_with(&key.chars().collect::<Vec<_>>()) {
result.push_str(value);
i += key.len();
matched = true;
break;
}
}

if !matched {
if chars[i].is_alphabetic() || chars[i].is_whitespace() {
buffer.push(chars[i]);
} else {
if !buffer.is_empty() {
let trimmed = buffer.trim();
if let Some(&replacement) = replacements.get(trimmed) {
result.push_str(replacement);
} else {
result.push_str(&buffer);
}
buffer.clear();
}
result.push(chars[i]);
}
i += 1;
}
}

if !buffer.is_empty() {
let trimmed = buffer.trim();
if let Some(&replacement) = replacements.get(trimmed) {
result.push_str(replacement);
} else {
result.push_str(&buffer);
}
}

result
}
7 changes: 7 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub enum Value {
Number(i32),
String(String),
Boolean(bool),
Float(f64),
Null,
Type(String),
Break,
Expand All @@ -17,6 +18,7 @@ pub enum ASTNode {
Number(i32),
String(String),
Boolean(bool),
Float(f64),
Null,
BinaryOp(Box<ASTNode>, Token, Box<ASTNode>),
Print(Box<ASTNode>),
Expand Down Expand Up @@ -240,6 +242,11 @@ impl<'a> Parser<'a> {
self.eat(Token::Number(num));
ASTNode::Number(num)
}
Token::Float(val) => {
let num = *val;
self.eat(Token::Float(num));
ASTNode::Float(num)
}
Token::String(val) => {
let s = val.clone();
self.eat(Token::String(s.clone()));
Expand Down

0 comments on commit 52e039a

Please sign in to comment.