From de38a577bdc1d70db71ccea042e8a80e94d7d8d8 Mon Sep 17 00:00:00 2001 From: Fuad Date: Sat, 5 Oct 2024 10:50:29 +0300 Subject: [PATCH] add range builtin --- evaluator/builtins.go | 61 +++++++++++++++++++++++++++++++++++++++++++ repl/docs/en/range.md | 55 ++++++++++++++++++++++++++++++++++++++ repl/docs/sw/range.md | 55 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 repl/docs/en/range.md create mode 100644 repl/docs/sw/range.md diff --git a/evaluator/builtins.go b/evaluator/builtins.go index 7b51767..945cbec 100644 --- a/evaluator/builtins.go +++ b/evaluator/builtins.go @@ -95,6 +95,58 @@ var builtins = map[string]*object.Builtin{ return &object.File{Filename: filename, Content: string(file)} }, }, + "mfululizo": { + Fn: func(args ...object.Object) object.Object { + if len(args) < 1 || len(args) > 3 { + return newError("Samahani, mfululizo inahitaji hoja 1 hadi 3, wewe umeweka %d", len(args)) + } + + var start, end, step int64 + var err error + + switch len(args) { + case 1: + end, err = getIntValue(args[0]) + if err != nil { + return newError("Hoja lazima iwe nambari nzima") + } + start, step = 0, 1 + case 2: + start, err = getIntValue(args[0]) + if err != nil { + return newError("Hoja ya kwanza lazima iwe nambari nzima") + } + end, err = getIntValue(args[1]) + if err != nil { + return newError("Hoja ya pili lazima iwe nambari nzima") + } + step = 1 + case 3: + start, err = getIntValue(args[0]) + if err != nil { + return newError("Hoja ya kwanza lazima iwe nambari nzima") + } + end, err = getIntValue(args[1]) + if err != nil { + return newError("Hoja ya pili lazima iwe nambari nzima") + } + step, err = getIntValue(args[2]) + if err != nil { + return newError("Hoja ya tatu lazima iwe nambari nzima") + } + if step == 0 { + return newError("Hatua haiwezi kuwa sifuri") + } + } + + elements := []object.Object{} + for i := start; (step > 0 && i < end) || (step < 0 && i > end); i += step { + elements = append(elements, &object.Integer{Value: i}) + } + + return &object.Array{Elements: elements} + }, + }, // "jumla": { // Fn: func(args ...object.Object) object.Object { @@ -135,3 +187,12 @@ var builtins = map[string]*object.Builtin{ // }, // }, } + +func getIntValue(obj object.Object) (int64, error) { + switch obj := obj.(type) { + case *object.Integer: + return obj.Value, nil + default: + return 0, fmt.Errorf("expected integer, got %T", obj) + } +} diff --git a/repl/docs/en/range.md b/repl/docs/en/range.md new file mode 100644 index 0000000..32d6eac --- /dev/null +++ b/repl/docs/en/range.md @@ -0,0 +1,55 @@ +## Range Function (mfululizo) + +The `mfululizo` function generates a sequence of numbers. It can be used in loops or to create arrays of sequential numbers. + +### Syntax + +```go +mfululizo(end) +mfululizo(start, end) +mfululizo(start, end, step) +``` + +### Parameters + +- `end`: The upper limit of the sequence (exclusive). +- `start` (optional): The starting value of the sequence. Default is 0. +- `step` (optional): The increment between each number in the sequence. Default is 1. + +### Return Value + +Returns an array of integers. + +### Examples + +```go +// Generate numbers from 0 to 4 +kwa i katika mfululizo(5) { + andika(i) +} +// Output: 0 1 2 3 4 + +// Generate numbers from 1 to 9 +kwa i katika mfululizo(1, 10) { + andika(i) +} +// Output: 1 2 3 4 5 6 7 8 9 + +// Generate even numbers from 0 to 8 +kwa i katika mfululizo(0, 10, 2) { + andika(i) +} +// Output: 0 2 4 6 8 + +// Generate numbers in reverse order +kwa i katika mfululizo(10, 0, -1) { + andika(i) +} +// Output: 10 9 8 7 6 5 4 3 2 1 +``` + +### Notes + +- The `end` value is exclusive, meaning the sequence will stop before reaching this value. +- If a negative `step` is provided, `start` should be greater than `end`. +- The `step` value cannot be zero. diff --git a/repl/docs/sw/range.md b/repl/docs/sw/range.md new file mode 100644 index 0000000..d8e73c3 --- /dev/null +++ b/repl/docs/sw/range.md @@ -0,0 +1,55 @@ +## Kitendakazi cha Mfululizo + +Kitendakazi cha `mfululizo` hutoa mfululizo wa nambari, sawa na kitendakazi cha `range()` cha Python. Kinaweza kutumika katika vitanzi au kuunda safu za nambari zinazofuatana. + +### Muundo + +```go +mfululizo(mwisho) +mfululizo(mwanzo, mwisho) +mfululizo(mwanzo, mwisho, hatua) +``` + +### Vipengele + +- `mwisho`: Kikomo cha juu cha mfululizo (haijumuishwi). +- `mwanzo` (si lazima): Thamani ya kuanzia ya mfululizo. Chaguo-msingi ni 0. +- `hatua` (si lazima): Ongezeko kati ya kila nambari katika mfululizo. Chaguo-msingi ni 1. + +### Thamani Inayorudishwa + +Hurudisha safu ya nambari kamili. + +### Mifano + +```go +// Toa nambari kutoka 0 hadi 4 +kwa i katika mfululizo(5) { + andika(i) +} +// Tokeo: 0 1 2 3 4 + +// Toa nambari kutoka 1 hadi 9 +kwa i katika mfululizo(1, 10) { + andika(i) +} +// Tokeo: 1 2 3 4 5 6 7 8 9 + +// Toa nambari shufwa kutoka 0 hadi 8 +kwa i katika mfululizo(0, 10, 2) { + andika(i) +} +// Tokeo: 0 2 4 6 8 + +// Toa nambari kwa mpangilio wa kurudi nyuma +kwa i katika mfululizo(10, 0, -1) { + andika(i) +} +// Tokeo: 10 9 8 7 6 5 4 3 2 1 +``` + +### Vidokezo + +- Thamani ya `mwisho` haijumuishwi, ikimaanisha mfululizo utasimama kabla ya kufikia thamani hii. +- Ikiwa `hatua` hasi imetolewa, `mwanzo` inapaswa kuwa kubwa kuliko `mwisho`. +- Thamani ya `hatua` haiwezi kuwa sifuri.