diff --git a/Cargo.toml b/Cargo.toml index ef3c08c..9f7af29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,4 @@ name = "Tralalero_lang" version = "0.1.0" edition = "2021" -[dependencies] \ No newline at end of file +[dependencies] diff --git a/arithmetic.tralla b/arithmetic.tralla index 5a23086..86b16d3 100644 --- a/arithmetic.tralla +++ b/arithmetic.tralla @@ -1,6 +1,6 @@ Tralalero Tralala -Biscottini x 10 -Biscottini y 5 -Chimpanzini result x + y -Matteeeo result -Bombardiro Crocodilo +let x = 10; +let y = 5; +let result = x + y; +Matteeeo result; +Bombardiro Crocodilo \ No newline at end of file diff --git a/conditional.tralla b/conditional.tralla index eeaeecd..245d41d 100644 --- a/conditional.tralla +++ b/conditional.tralla @@ -1,11 +1,11 @@ Tralalero Tralala -Biscottini x 5 +let x = 5; Tung Tung Tung x > 10 { - Matteeeo "x is greater than 10" + Matteeeo "x is greater than 10"; } Ballerina Cappuccina { - Matteeeo "x is not greater than 10" + Matteeeo "x is not greater than 10"; } Bombardiro Crocodilo \ No newline at end of file diff --git a/examples/calculator.tralla b/examples/calculator.tralla index 8006f0f..4080e0a 100644 --- a/examples/calculator.tralla +++ b/examples/calculator.tralla @@ -1,3 +1,4 @@ +Tralalero Tralala // calculator.tralla // This example demonstrates basic arithmetic operations and variable usage. @@ -5,16 +6,18 @@ let num1 = 10; let num2 = 5; let sum = num1 + num2; -print("Sum:", sum); // Expected: Sum: 15 +Matteeeo "Sum:" // Expected: Sum: 15 +Matteeeo sum let difference = num1 - num2; -print("Difference:", difference); // Expected: Difference: 5 +Matteeeo "Difference:" // Expected: Difference: 5 +Matteeeo difference let product = num1 * num2; -print("Product:", product); // Expected: Product: 50 +Matteeeo "Product:" // Expected: Product: 50 +Matteeeo product let quotient = num1 / num2; -print("Quotient:", quotient); // Expected: Quotient: 2 - -let result = (num1 + num2) * (num1 - num2) / num2; -print("Complex Calculation:", result); // Expected: Complex Calculation: 15 +Matteeeo "Quotient:" // Expected: Quotient: 2 +Matteeeo quotient +Bombardiro Crocodilo \ No newline at end of file diff --git a/examples/fibonacci.tralla b/examples/fibonacci.tralla index ad03788..6bd7611 100644 --- a/examples/fibonacci.tralla +++ b/examples/fibonacci.tralla @@ -1,3 +1,4 @@ +Tralalero Tralala // fibonacci.tralla // This example calculates the nth Fibonacci number using a loop and a function. @@ -24,12 +25,22 @@ func fibonacci(n) { let num = 10; let result = fibonacci(num); -print("The", num, "th Fibonacci number is:", result); // Expected: The 10th Fibonacci number is: 55 +Matteeeo "The" // Expected: The 10th Fibonacci number is: 55 +Matteeeo num +Matteeeo "th Fibonacci number is:" +Matteeeo result num = 0; result = fibonacci(num); -print("The", num, "th Fibonacci number is:", result); // Expected: The 0th Fibonacci number is: 0 +Matteeeo "The" // Expected: The 0th Fibonacci number is: 0 +Matteeeo num +Matteeeo "th Fibonacci number is:" +Matteeeo result num = 1; result = fibonacci(num); -print("The", num, "th Fibonacci number is:", result); // Expected: The 1th Fibonacci number is: 1 +Matteeeo "The" // Expected: The 1th Fibonacci number is: 1 +Matteeeo num +Matteeeo "th Fibonacci number is:" +Matteeeo result +Bombardiro Crocodilo \ No newline at end of file diff --git a/examples/string_manipulation.tralla b/examples/string_manipulation.tralla index 0d2a072..d5e4bd5 100644 --- a/examples/string_manipulation.tralla +++ b/examples/string_manipulation.tralla @@ -1,3 +1,4 @@ +Tralalero Tralala // string_manipulation.tralla // This example demonstrates string concatenation. @@ -5,12 +6,18 @@ let greeting = "Hello"; let name = "World"; Unire Corde full_greeting greeting name; -print("Concatenated string:", full_greeting); // Expected: Concatenated string: HelloWorld +Matteeeo "Concatenated string:" // Expected: Concatenated string: HelloWorld +Matteeeo full_greeting -Unire Corde message "This is a " "test."; -print("Another concatenated string:", message); // Expected: Another concatenated string: This is a test. +let part1 = "This_is_a_"; +let part2 = "test."; +Unire Corde message part1 part2; +Matteeeo "Another concatenated string:" // Expected: Another concatenated string: This_is_a_test. +Matteeeo message let space = " "; Unire Corde spaced_greeting greeting space; Unire Corde final_greeting spaced_greeting name; -print("Concatenated with space:", final_greeting); // Expected: Concatenated with space: Hello World +Matteeeo "Concatenated with space:" // Expected: Concatenated with space: Hello World +Matteeeo final_greeting +Bombardiro Crocodilo \ No newline at end of file diff --git a/functions.tralla b/functions.tralla new file mode 100644 index 0000000..b8f6a9c --- /dev/null +++ b/functions.tralla @@ -0,0 +1,9 @@ +Tralalero Tralala +Lirili Larila my_function (a, b) +{ + let result = a + b; + Matteeeo result; +} + +Trippi Troppi my_function(10, 20); +Bombardiro Crocodilo \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8c7ae46..49b6803 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,40 +105,65 @@ fn get_value(s: &str, variables: &HashMap) -> Option { } fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap, functions: &HashMap) -> usize { - let line = lines[pc].trim(); + let mut line = lines[pc].trim().to_string(); + // Remove comments + if let Some(comment_start) = line.find("//") { + line = line[..comment_start].to_string(); + } let mut words = line.split_whitespace(); if let Some(keyword) = words.next() { - if keyword == "Lirili" { // Skip function definitions - let mut brace_count = 1; - let mut end_pc = pc + 2; - for i in (pc + 2)..lines.len() { - if lines[i].trim() == "{" { - brace_count += 1; - } - if lines[i].trim() == "}" { - brace_count -= 1; - if brace_count == 0 { - end_pc = i; - break; + if keyword == "let" { // Handle 'let' keyword for variable declarations + if let Some(var_name) = words.next() { + if let Some(eq_sign) = words.next() { + if eq_sign == "=" { + let mut expression = words.collect::>().join(" "); + expression = expression.trim_end_matches(';').to_string(); + + // Try to parse as arithmetic expression (op1 op op2) + let expr_parts = expression.split_whitespace().collect::>(); + if expr_parts.len() == 3 { + let op1_str = expr_parts[0]; + let op = expr_parts[1]; + let op2_str = expr_parts[2]; + + if let (Some(op1), Some(op2)) = (get_value(op1_str, variables), get_value(op2_str, variables)) { + let result = match op { + "+" => op1 + op2, + "-" => op1 - op2, + "*" => op1 * op2, + "/" => op1 / op2, + _ => { + // Not a recognized operator, treat as direct value + variables.insert(var_name.to_string(), expression.trim_matches('"').to_string()); + return pc + 1; + } + }; + variables.insert(var_name.to_string(), result.to_string()); + } else { + // Could not get numeric values for operands, treat as direct value + variables.insert(var_name.to_string(), expression.trim_matches('"').to_string()); + } + } else { + // Not an arithmetic expression, treat as direct value + variables.insert(var_name.to_string(), expression.trim_matches('"').to_string()); + } } } } - return end_pc + 1; - } - if keyword == "Biscottini" { - if let Some(var_name) = words.next() { - let value = words.collect::>().join(" "); - variables.insert(var_name.to_string(), value.trim_matches('"').to_string()); - } return pc + 1; } else if keyword == "Matteeeo" { - let expression = words.collect::>().join(" "); + let mut expression = words.collect::>().join(" "); + expression = expression.trim_end_matches(';').to_string(); if expression.starts_with('"') && expression.ends_with('"') { - println!("{}", expression.trim_matches('"')); + let printed_value = expression.trim_matches('"'); + println!("{}", printed_value); } else { if let Some(value) = variables.get(&expression) { println!("{}", value); + } else { + // Variable not found, print the expression as is (might be an error or a literal not enclosed in quotes) + println!("{}", expression); } } return pc + 1; @@ -173,20 +198,6 @@ fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap op1 + op2, - "-" => op1 - op2, - "*" => op1 * op2, - "/" => op1 / op2, - _ => 0.0, - }; - variables.insert(var_name.to_string(), result.to_string()); - } - } - return pc + 1; } else if keyword == "Tung" { if let (Some(word2), Some(word3), Some(op1_str), Some(op), Some(op2_str)) = (words.next(), words.next(), words.next(), words.next(), words.next()) { if word2 == "Tung" && word3 == "Tung" { @@ -262,8 +273,10 @@ fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap