Skip to content

Commit d30c384

Browse files
committed
Iteration on namings
1 parent 8e78581 commit d30c384

File tree

13 files changed

+116
-119
lines changed

13 files changed

+116
-119
lines changed

README.md

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -235,88 +235,85 @@ functionality, I/O operations, and performance-critical code written in C.
235235
Native functions in C must follow this signature:
236236

237237
```c
238-
tea_value_t your_function_name(tea_context_t* context, const tea_function_args_t* args)
238+
tea_val_t your_function_name(tea_ctx_t* context, const tea_fn_args_t* args)
239239
```
240240
241241
The function receives a context and a list of arguments, and must return a `tea_value_t`. Arguments are accessed by calling `tea_function_args_pop()` in a loop. Here's an example:
242242
243243
```c
244244
// Native function to print values (similar to built-in print)
245-
static tea_value_t tea_print_values(tea_context_t* context, const tea_function_args_t* args) {
245+
static tea_val_t tea_print_values(tea_ctx_t* context, const tea_fn_args_t* args) {
246246
for (;;) {
247-
tea_variable_t* arg = tea_function_args_pop(args);
247+
tea_var_t* arg = tea_fn_args_pop(args);
248248
if (!arg) {
249249
break; // No more arguments
250250
}
251251
252-
const tea_value_t value = arg->value;
252+
const tea_val_t value = arg->val;
253253
switch (value.type) {
254-
case TEA_VALUE_I32:
254+
case TEA_V_I32:
255255
printf("%d ", value.i32);
256256
break;
257-
case TEA_VALUE_F32:
257+
case TEA_V_F32:
258258
printf("%f ", value.f32);
259259
break;
260-
case TEA_VALUE_STRING:
261-
printf("%s ", value.string);
262-
break;
263-
case TEA_VALUE_INSTANCE:
264-
case TEA_VALUE_INVALID:
260+
case TEA_V_INST:
261+
case TEA_V_UNDEF:
265262
break;
266263
}
267264
268-
tea_free_variable(context, arg);
265+
tea_free_var(context, arg);
269266
}
270267
printf("\n");
271268
272-
return tea_value_invalid();
269+
return tea_val_undef();
273270
}
274271
275272
// Native function to add two numbers
276-
static tea_value_t tea_add_numbers(tea_context_t* context, const tea_function_args_t* args) {
277-
tea_variable_t* arg1 = tea_function_args_pop(args);
278-
tea_variable_t* arg2 = tea_function_args_pop(args);
273+
static tea_val_t tea_add_numbers(tea_ctx_t* context, const tea_fn_args_t* args) {
274+
tea_var_t* arg1 = tea_fn_args_pop(args);
275+
tea_var_t* arg2 = tea_fn_args_pop(args);
279276
280277
if (!arg1 || !arg2) {
281278
// Handle error case - not enough arguments
282-
if (arg1) tea_free_variable(context, arg1);
283-
if (arg2) tea_free_variable(context, arg2);
284-
return tea_value_invalid();
279+
if (arg1) tea_free_var(context, arg1);
280+
if (arg2) tea_free_var(context, arg2);
281+
return tea_val_undef();
285282
}
286283
287-
if (arg1->value.type == TEA_VALUE_I32 && arg2->value.type == TEA_VALUE_I32) {
288-
tea_value_t result = {0};
289-
result.type = TEA_VALUE_I32;
290-
result.i32 = arg1->value.i32 + arg2->value.i32;
284+
if (arg1->val.type == TEA_V_I32 && arg2->val.type == TEA_V_I32) {
285+
tea_val_t result = {0};
286+
result.type = TEA_V_I32;
287+
result.i32 = arg1->val.i32 + arg2->val.i32;
291288
292-
tea_free_variable(context, arg1);
293-
tea_free_variable(context, arg2);
289+
tea_free_var(context, arg1);
290+
tea_free_var(context, arg2);
294291
return result;
295292
}
296293
297-
tea_free_variable(context, arg1);
298-
tea_free_variable(context, arg2);
299-
return tea_value_invalid();
294+
tea_free_var(context, arg1);
295+
tea_free_var(context, arg2);
296+
return tea_val_undef();
300297
}
301298
```
302299

303300
### Binding Functions
304301

305-
Register your native functions with the Tea context using `tea_bind_native_function`:
302+
Register your native functions with the Tea context using `tea_bind_native_fn`:
306303

307304
```c
308305
int main() {
309-
tea_context_t context;
310-
tea_interpret_init(&context, "example.tea");
306+
tea_ctx_t context;
307+
tea_interp_init(&context, "example.tea");
311308

312309
// Bind native functions
313-
tea_bind_native_function(&context, "print", tea_print);
314-
tea_bind_native_function(&context, "add_numbers", tea_add_numbers);
315-
tea_bind_native_function(&context, "print_values", tea_print_values);
310+
tea_bind_native_fn(&context, "print", tea_print);
311+
tea_bind_native_fn(&context, "add_numbers", tea_add_numbers);
312+
tea_bind_native_fn(&context, "print_values", tea_print_values);
316313

317314
// Execute Tea code...
318315

319-
tea_interpret_cleanup(&context);
316+
tea_interp_cleanup(&context);
320317
return 0;
321318
}
322319
```
@@ -339,23 +336,23 @@ print_values('Result:', sum, 'Done');
339336

340337
### Tea Value Types
341338

342-
Native functions work with the `tea_value_t` type system:
339+
Native functions work with the `tea_val_t` type system:
343340

344-
- `TEA_VALUE_I32` - 32-bit signed integers
345-
- `TEA_VALUE_F32` - 32-bit floating-point numbers
346-
- `TEA_VALUE_STRING` - Null-terminated strings
347-
- `TEA_VALUE_INSTANCE` - Complex objects (structs)
348-
- `TEA_VALUE_INVALID` - Uninitialized or error state
341+
- `i32` (TEA_V_I32) - 32-bit signed integers
342+
- `f32` (TEA_V_F32) - 32-bit floating-point numbers
343+
- `string` (TEA_V_INST) - Null-terminated strings wrapped as instances
344+
- `instance` (TEA_V_INST) - Complex objects (structs)
345+
- `undef` (TEA_V_UNDEF) - Uninitialized or error state
349346

350347
### Best Practices
351348

352-
1. **Always validate arguments**: Use `tea_function_args_pop()` to safely access arguments and check for NULL
353-
2. **Handle errors gracefully**: Return `tea_value_invalid()` for error conditions
349+
1. **Always validate arguments**: Use `tea_fn_args_pop()` to safely access arguments and check for NULL
350+
2. **Handle errors gracefully**: Return `tea_val_undef()` for error conditions
354351
3. **Memory management**:
355-
- Always call `tea_free_variable(context, arg)` for each argument you pop
356-
- Strings returned from native functions should be allocated with `rtl_malloc`
352+
- Always call `tea_free_var(context, arg)` for each argument you pop
353+
- Strings returned from native functions should be allocated with `tea_malloc`
357354
4. **Performance**: Use native functions for computationally intensive operations
358-
5. **Argument handling**: Process arguments in the order they were passed by calling `tea_function_args_pop()` sequentially
355+
5. **Argument handling**: Process arguments in the order they were passed by calling `tea_fn_args_pop()` sequentially
359356

360357
## Building
361358

examples/002_basic_types.tea

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ let is_false = 2 > 10; // Results in false (0)
2222
let equality = 5 == 5; // Results in true (1)
2323

2424
// Type annotations can be explicit when needed
25-
let explicit_int: i32 = 100;
26-
let explicit_float: f32 = 25.0;
25+
let explicit_i32: i32 = 100;
26+
let explicit_f32: f32 = 25.0;
2727
let explicit_string: string = 'Type annotated string';

examples/012_functions.tea

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ fn greet() {
66
}
77

88
// Function with parameters
9-
fn add_numbers(a: int, b: int) {
9+
fn add_numbers(a: i32, b: i32) {
1010
let sum = a + b;
1111
// sum is calculated but not returned
1212
}
1313

1414
// Function with return type and return statement
15-
fn multiply(x: int, y: int) -> int {
15+
fn multiply(x: i32, y: i32) -> i32 {
1616
let result = x * y;
1717
return result;
1818
}
1919

2020
// Function with single parameter
21-
fn square(n: int) -> int {
21+
fn square(n: i32) -> i32 {
2222
return n * n;
2323
}
2424

2525
// Function that returns a constant
26-
fn get_magic_number() -> int {
26+
fn get_magic_number() -> i32 {
2727
return 42;
2828
}
2929

3030
// Function with floating-point parameters and return
31-
fn calculate_area(width: float, height: float) -> float {
31+
fn calculate_area(width: f32, height: f32) -> f32 {
3232
return width * height;
3333
}
3434

@@ -43,7 +43,7 @@ let complex_result = multiply(3, 4) + square(2); // 12 + 4 = 16
4343
add_numbers(10, 20); // Note: add_numbers doesn't return a value
4444

4545
// Function with conditional logic
46-
fn absolute_value(x: int) -> int {
46+
fn absolute_value(x: i32) -> i32 {
4747
if x < 0 {
4848
return -x;
4949
} else {
@@ -52,7 +52,7 @@ fn absolute_value(x: int) -> int {
5252
}
5353

5454
// Function with loop
55-
fn factorial(n: int) -> int {
55+
fn factorial(n: i32) -> i32 {
5656
let mut result = 1;
5757
let mut i = 1;
5858

@@ -65,22 +65,22 @@ fn factorial(n: int) -> int {
6565
}
6666

6767
// Function that calls other functions
68-
fn calculate_perimeter(width: float, height: float) -> float {
68+
fn calculate_perimeter(width: f32, height: f32) -> f32 {
6969
let double_width = multiply(2, width); // Note: multiply expects int, this may cause issues
7070
let double_height = multiply(2, height); // Note: multiply expects int, this may cause issues
7171
return double_width + double_height;
7272
}
7373

7474
// Functions with different parameter counts
75-
fn no_params() -> int {
75+
fn no_params() -> i32 {
7676
return 1;
7777
}
7878

79-
fn one_param(x: int) -> int {
79+
fn one_param(x: i32) -> i32 {
8080
return x + 1;
8181
}
8282

83-
fn three_params(a: int, b: int, c: int) -> int {
83+
fn three_params(a: i32, b: i32, c: i32) -> i32 {
8484
return a + b + c;
8585
}
8686

examples/013_function_returns.tea

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Advanced function features: return values, complex logic, and function composition
22

33
// Function with multiple return points
4-
fn classify_number(n: int) -> int {
4+
fn classify_number(n: i32) -> i32 {
55
if n < 0 {
66
return -1; // Negative
77
}
@@ -12,7 +12,7 @@ fn classify_number(n: int) -> int {
1212
}
1313

1414
// Function with complex calculation
15-
fn fibonacci(n: int) -> int {
15+
fn fibonacci(n: i32) -> i32 {
1616
if n <= 1 {
1717
return n;
1818
}
@@ -34,7 +34,7 @@ fn fibonacci(n: int) -> int {
3434
fn square(x: i32) -> i32 { return x * x; }
3535

3636
// Function that uses other functions
37-
fn sum_of_squares(a: int, b: int) -> int {
37+
fn sum_of_squares(a: i32, b: i32) -> i32 {
3838
let sq_a = square(a); // Assumes square function from previous example
3939
let sq_b = square(b); // Assumes square function from previous example
4040
return sq_a + sq_b;
@@ -47,15 +47,15 @@ fn create_greeting(name: string) -> string {
4747
}
4848

4949
// Function with optional type parameter
50-
fn safe_divide(a: int, b: int) -> int? {
50+
fn safe_divide(a: i32, b: i32) -> i32? {
5151
if b == 0 {
5252
return null; // Cannot divide by zero
5353
}
5454
return a / b;
5555
}
5656

5757
// Function that processes arrays/collections conceptually
58-
fn find_maximum(a: int, b: int, c: int) -> int {
58+
fn find_maximum(a: i32, b: i32, c: i32) -> i32 {
5959
let mut max_ab = 0;
6060

6161
if a > b {
@@ -72,7 +72,7 @@ fn find_maximum(a: int, b: int, c: int) -> int {
7272
}
7373

7474
// Function with accumulator pattern
75-
fn sum_range(start: int, end: int) -> int {
75+
fn sum_range(start: i32, end: i32) -> i32 {
7676
let mut sum = 0;
7777
let mut current = start;
7878

@@ -85,7 +85,7 @@ fn sum_range(start: int, end: int) -> int {
8585
}
8686

8787
// Nested function calls
88-
fn complex_calculation(x: int, y: int) -> int {
88+
fn complex_calculation(x: i32, y: i32) -> i32 {
8989
let step1 = multiply(x, 2); // x * 2
9090
let step2 = square(y); // y^2
9191
let step3 = sum_of_squares(step1, step2); // (x*2)^2 + (y^2)^2
@@ -107,7 +107,7 @@ let combined = fibonacci(5) + sum_range(1, 5); // 5 + 15 = 20
107107
let comparison = find_maximum(10, 20, 15) > square(4); // 20 > 16 = 1
108108

109109
// Functions with early returns
110-
fn check_conditions(a: int, b: int, c: int) -> int {
110+
fn check_conditions(a: i32, b: i32, c: i32) -> i32 {
111111
if a <= 0 {
112112
return 0; // Early return for invalid input
113113
}

examples/014_mutable_functions.tea

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn mut increment_counter() {
1212
}
1313

1414
// Mutable function with parameters that still modifies global state
15-
fn mut add_to_total(amount: int) {
15+
fn mut add_to_total(amount: i32) {
1616
global_total = global_total + amount;
1717
}
1818

@@ -25,14 +25,14 @@ fn mut reset_and_increment() {
2525
}
2626

2727
// Regular function (non-mutable) for comparison
28-
fn get_current_state() -> int {
28+
fn get_current_state() -> i32 {
2929
// This function can read global variables but cannot modify them
3030
let current_sum = global_counter + global_total;
3131
return current_sum;
3232
}
3333

3434
// Mutable function with conditional modifications
35-
fn mut conditional_update(threshold: int) {
35+
fn mut conditional_update(threshold: i32) {
3636
if global_counter < threshold {
3737
global_counter = global_counter + 5;
3838
} else {
@@ -42,7 +42,7 @@ fn mut conditional_update(threshold: int) {
4242
}
4343

4444
// Mutable function with loops
45-
fn mut batch_increment(times: int) {
45+
fn mut batch_increment(times: i32) {
4646
let mut i = 0;
4747
while i < times {
4848
global_counter = global_counter + 1;
@@ -66,13 +66,13 @@ reset_and_increment(); // global_counter = 1, global_total = 10
6666
let mut status_flag = 0;
6767
let mut error_count = 0;
6868

69-
fn mut update_status(new_status: int, errors: int) {
69+
fn mut update_status(new_status: i32, errors: i32) {
7070
status_flag = new_status;
7171
error_count = error_count + errors;
7272
}
7373

7474
// Mutable function with return value (can both modify and return)
75-
fn mut increment_and_get() -> int {
75+
fn mut increment_and_get() -> i32 {
7676
global_counter = global_counter + 1;
7777
return global_counter;
7878
}

0 commit comments

Comments
 (0)