From 22c0cc7531edc6eba384d6f53f8f64f6a5616296 Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 10 Apr 2024 20:48:41 +0000 Subject: Added `puts` --- README.md | 18 +++++++++++------- evaluator/builtins.go | 9 +++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4128a43..b90452d 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,9 @@ let result = if (x < y) { } else { subtract(x, y) }; // result = 15 + + +puts(result); // prints the value of result to the console ``` ## Types @@ -137,13 +140,14 @@ fn add(x, y) { Mana has a number of built-in functions that are available to the programmer. These functions are built into the language and can be used without having to define them. The following is a list of built-in functions that are available in Mana: -| Function | Description | Parameters | Example | Output | -| -------- | --------------------------------------------- | --------------------- | ---------------------- | -------------- | -| `len` | Returns the length of a string or an array | `string` | `len("Hello, World!")` | `13` | -| `push` | Appends an element to the end of an array | `array`, `expression` | `push([1, 2, 3], 4)` | `[1, 2, 3, 4]` | -| `first` | Returns the first element of an array | `array` | `first([1, 2, 3])` | `1` | -| `last` | Returns the last element of an array | `array` | `last([1, 2, 3])` | `3` | -| `rest` | Returns all but the first element of an array | `array` | `rest([1, 2, 3])` | `[2, 3]` | +| Function | Description | Parameters | Example | Output | +| -------- | --------------------------------------------- | --------------------- | ----------------------- | --------------- | +| `len` | Returns the length of a string or an array | `string` | `len("Hello, World!")` | `13` | +| `push` | Appends an element to the end of an array | `array`, `expression` | `push([1, 2, 3], 4)` | `[1, 2, 3, 4]` | +| `first` | Returns the first element of an array | `array` | `first([1, 2, 3])` | `1` | +| `last` | Returns the last element of an array | `array` | `last([1, 2, 3])` | `3` | +| `rest` | Returns all but the first element of an array | `array` | `rest([1, 2, 3])` | `[2, 3]` | +| `puts` | Prints a value to the console | `expression` | `puts("Hello, World!")` | `Hello, World!` | ## Strings diff --git a/evaluator/builtins.go b/evaluator/builtins.go index 1c7a312..a37a720 100644 --- a/evaluator/builtins.go +++ b/evaluator/builtins.go @@ -108,4 +108,13 @@ var builtins = map[string]*object.Builtin{ return &object.Array{Elements: newElements} }, }, + "puts": { + Fn: func(args ...object.Object) object.Object { + for _, arg := range args { + println(arg.Inspect()) + } + + return NULL + }, + }, } -- cgit v1.2.3