Skip to content

Commit

Permalink
add range builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
AvicennaJr committed Oct 5, 2024
1 parent 1e2f5dd commit de38a57
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
61 changes: 61 additions & 0 deletions evaluator/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
55 changes: 55 additions & 0 deletions repl/docs/en/range.md
Original file line number Diff line number Diff line change
@@ -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.
55 changes: 55 additions & 0 deletions repl/docs/sw/range.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit de38a57

Please sign in to comment.