mirror of
https://github.com/Alvin-Zilverstand/Tralalero_lang.git
synced 2026-03-06 03:06:38 +01:00
Refactor: Update all .tralla examples to new 'let' syntax and fix Matteeeo parsing. Acknowledge function return limitation.
This commit is contained in:
@@ -3,4 +3,4 @@ name = "Tralalero_lang"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
[dependencies]
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
9
functions.tralla
Normal file
9
functions.tralla
Normal file
@@ -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
|
||||
87
src/main.rs
87
src/main.rs
@@ -105,40 +105,65 @@ fn get_value(s: &str, variables: &HashMap<String, String>) -> Option<f64> {
|
||||
}
|
||||
|
||||
fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap<String, String>, functions: &HashMap<String, Function>) -> 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::<Vec<&str>>().join(" ");
|
||||
expression = expression.trim_end_matches(';').to_string();
|
||||
|
||||
// Try to parse as arithmetic expression (op1 op op2)
|
||||
let expr_parts = expression.split_whitespace().collect::<Vec<&str>>();
|
||||
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::<Vec<&str>>().join(" ");
|
||||
variables.insert(var_name.to_string(), value.trim_matches('"').to_string());
|
||||
}
|
||||
return pc + 1;
|
||||
} else if keyword == "Matteeeo" {
|
||||
let expression = words.collect::<Vec<&str>>().join(" ");
|
||||
let mut expression = words.collect::<Vec<&str>>().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<Strin
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if keyword == "Chimpanzini" {
|
||||
if let (Some(var_name), Some(op1_str), Some(op), Some(op2_str)) = (words.next(), words.next(), words.next(), words.next()) {
|
||||
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,
|
||||
_ => 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<Strin
|
||||
}
|
||||
}
|
||||
} else if keyword == "Unire" { // New: String Concatenation
|
||||
if let (Some(word2), Some(result_var), Some(str1_val), Some(str2_val)) = (words.next(), words.next(), words.next(), words.next()) {
|
||||
if let (Some(word2), Some(result_var), Some(str1_raw), Some(str2_raw)) = (words.next(), words.next(), words.next(), words.next()) {
|
||||
if word2 == "Corde" {
|
||||
let str1_val = str1_raw.trim_end_matches(';');
|
||||
let str2_val = str2_raw.trim_end_matches(';');
|
||||
let s1 = if str1_val.starts_with('"') && str1_val.ends_with('"') {
|
||||
str1_val.trim_matches('"').to_string()
|
||||
} else if let Some(val) = variables.get(str1_val) {
|
||||
|
||||
3
temp_test.tralla
Normal file
3
temp_test.tralla
Normal file
@@ -0,0 +1,3 @@
|
||||
Tralalero Tralala
|
||||
Matteeeo "Hello from Tralalero!"
|
||||
Bombardiro Crocodilo
|
||||
@@ -1,4 +1,4 @@
|
||||
Tralalero Tralala
|
||||
Biscottini my_variable "Hello from a variable!"
|
||||
Matteeeo my_variable
|
||||
let my_variable = "Hello from a variable!";
|
||||
Matteeeo my_variable;
|
||||
Bombardiro Crocodilo
|
||||
Reference in New Issue
Block a user