Skip to content

Commit

Permalink
array docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tacheraSasi committed Dec 15, 2024
1 parent dd82077 commit 5a02297
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 130 deletions.
6 changes: 3 additions & 3 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestNextToken(t *testing.T) {
{token.TRUE, "true"},
{token.SEMICOLON, ";"},
{token.RBRACE, "}"},
{token.ELSE, "sivyo"},
{token.ELSE, "else"},
{token.LBRACE, "{"},
{token.RETURN, "return"},
{token.FALSE, "false"},
Expand Down Expand Up @@ -154,11 +154,11 @@ func TestNextToken(t *testing.T) {
{token.RBRACE, "}"},
{token.RBRACE, "}"},
{token.NULL, "tupu"},
{token.FOR, "kwa"},
{token.FOR, "for"},
{token.IDENT, "i"},
{token.COMMA, ","},
{token.IDENT, "v"},
{token.IN, "ktk"},
{token.IN, "in"},
{token.IDENT, "j"},
{token.EOF, ""},
}
Expand Down
2 changes: 1 addition & 1 deletion parser/for.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (p *Parser) parseForExpression() ast.Expression {
return p.parseForInExpression(expression)
}

// In future will allow: kwa i = 0; i<10; i++ {print(i)}
// In future will allow: for i = 0; i<10; i++ {print(i)}
// expression.Identifier = p.curToken.Literal
// expression.StarterName = &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal}
// if expression.StarterName == nil {
Expand Down
44 changes: 22 additions & 22 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestReturnStatements(t *testing.T) {
expectedValue interface{}
}{
{"rudisha 5;", 5},
{"rudisha kweli;", true},
{"rudisha true;", true},
{"rudisha bangi;", "bangi"},
}

Expand Down Expand Up @@ -188,8 +188,8 @@ func TestParsingPrefixExpressions(t *testing.T) {
}{
{"!5;", "!", 5},
{"-15;", "-", 15},
{"!kweli", "!", true},
{"!sikweli", "!", false},
{"!true", "!", true},
{"!false", "!", false},
}

for _, tt := range prefixTests {
Expand Down Expand Up @@ -261,9 +261,9 @@ func TestParsingInfixExpressions(t *testing.T) {
{"5 <= 5;", 5, "<=", 5},
{"5 || 5;", 5, "||", 5},
{"5 && 5;", 5, "&&", 5},
{"kweli == kweli", true, "==", true},
{"kweli != sikweli", true, "!=", false},
{"sikweli == sikweli", false, "==", false},
{"true == true", true, "==", true},
{"true != false", true, "!=", false},
{"false == false", false, "==", false},
}

for _, tt := range infixTests {
Expand Down Expand Up @@ -341,20 +341,20 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
"((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
},
{
"kweli",
"kweli",
"true",
"true",
},
{
"sikweli",
"sikweli",
"false",
"false",
},
{
"3 > 5 == sikweli",
"((3 > 5) == sikweli)",
"3 > 5 == false",
"((3 > 5) == false)",
},
{
"3 < 5 == kweli",
"((3 < 5) == kweli)",
"3 < 5 == true",
"((3 < 5) == true)",
},
{
"1 + (2 + 3) + 4",
Expand All @@ -373,8 +373,8 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
"(-(5 + 5))",
},
{
"!(kweli == kweli)",
"(!(kweli == kweli))",
"!(true == true)",
"(!(true == true))",
},
{
"a + add(b * c) + d",
Expand Down Expand Up @@ -501,8 +501,8 @@ func TestBooleanExpression(t *testing.T) {
input string
expectedBoolean bool
}{
{"kweli;", true},
{"sikweli;", false},
{"true;", true},
{"false;", false},
}

for _, tt := range tests {
Expand Down Expand Up @@ -534,7 +534,7 @@ func TestBooleanExpression(t *testing.T) {
}

func TestIfExpression(t *testing.T) {
input := `kama (x < y) { x }`
input := `if (x < y) { x }`

l := lexer.New(input)
p := New(l)
Expand Down Expand Up @@ -578,7 +578,7 @@ func TestIfExpression(t *testing.T) {
}

func TestIfElseExpression(t *testing.T) {
input := `kama (x < y) { x } sivyo { y }`
input := `if (x < y) { x } else { y }`

l := lexer.New(input)
p := New(l)
Expand Down Expand Up @@ -874,7 +874,7 @@ func TestParsingDictLiteralsIntegerKeys(t *testing.T) {
}

func TestParsingDictLiteralsBoolKeys(t *testing.T) {
input := `{kweli: 1, sikweli: 2}`
input := `{true: 1, false: 2}`

l := lexer.New(input)
p := New(l)
Expand Down Expand Up @@ -1033,7 +1033,7 @@ func TestShorthandAssignment(t *testing.T) {
}

func TestForExpression(t *testing.T) {
input := `kwa i, v ktk j {print(i)}`
input := `for i, v in j {print(i)}`

l := lexer.New(input)
p := New(l)
Expand Down
4 changes: 2 additions & 2 deletions repl/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (pg playground) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case tea.KeyCtrlR:
if strings.Contains(pg.editor.Value(), "input") {
pg.output.SetContent(styles.HelpStyle.Italic(false).Render("Samahani, huwezi kutumia `input()` kwa sasa."))
pg.output.SetContent(styles.HelpStyle.Italic(false).Render("Samahani, huwezi kutumia `input()` for sasa."))
} else {
// this is just for the output will find a better solution
code := strings.ReplaceAll(pg.editor.Value(), "print", "_print")
Expand Down Expand Up @@ -178,7 +178,7 @@ func (pg playground) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.MouseLeft:
if zone.Get(pg.id + "run").InBounds(msg) {
if strings.Contains(pg.editor.Value(), "input") {
pg.output.SetContent(styles.HelpStyle.Italic(false).Render("Samahani, huwezi kutumia `input()` kwa sasa."))
pg.output.SetContent(styles.HelpStyle.Italic(false).Render("Samahani, huwezi kutumia `input()` for sasa."))
} else {
// this is just for the output will find a better solution
code := strings.ReplaceAll(pg.editor.Value(), "print", "_print")
Expand Down
4 changes: 2 additions & 2 deletions repl/docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ This documentation is intended for people with some experience in programming. I
- [Single-Line Comments](comments.md#single-line-comments)
- [Multi-Line Comments](comments.md#multi-line-comments)
- [Conditional Statements in vint](ifStatements.md#conditional-statements-in-vint)
- [If Statement (Kama)](ifStatements.md#if-statement-(kama))
- [Else If and Else Blocks (Au Kama and Sivyo)](ifStatements.md#else-if-and-else-blocks-(au-kama-and-sivyo))
- [If Statement (if)](ifStatements.md#if-statement-(if))
- [Else If and Else Blocks (Au if and else)](ifStatements.md#else-if-and-else-blocks-(au-if-and-else))
- [Dictionaries in vint](dictionaries.md#dictionaries-in-vint)
- [Creating Dictionaries](dictionaries.md#creating-dictionaries)
- [Accessing Elements](dictionaries.md#accessing-elements)
Expand Down
66 changes: 33 additions & 33 deletions repl/docs/en/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ Arrays in vint are versatile data structures that can hold multiple items, inclu
To create an array, use square brackets [] and separate items with commas:

```s
orodha = [1, "pili", kweli]
list = [1, "second", true]
```
## Accessing and Modifying Array Elements

Arrays in vint are zero-indexed. To access an element, use the element's index in square brackets:

```s
namba = [10, 20, 30]
jina = namba[1] // jina is 20
num = [10, 20, 30]
n = num[1] // n is 20
```

You can reassign an element in an array using its index:

```s
namba[1] = 25
num[1] = 25
```

## Concatenating Arrays
Expand All @@ -37,71 +37,71 @@ c = a + b

## Checking for Array Membership

Use the `ktk` keyword to check if an item exists in an array:
Use the `in` keyword to check if an item exists in an array:

```s
namba = [10, 20, 30]
print(20 ktk namba) // will print kweli
num = [10, 20, 30]
print(20 in num) // will print true
```

## Looping Over Arrays

You can use the kwa and ktk keywords to loop over array elements. To loop over just the values, use the following syntax:
You can use the for and in keywords to loop over array elements. To loop over just the values, use the following syntax:

```
namba = [1, 2, 3, 4, 5]
num = [1, 2, 3, 4, 5]
kwa thamani ktk namba {
print(thamani)
for value in num {
print(value)
}
```

To loop over both index and value pairs, use this syntax:

```s
majina = ["Juma", "Asha", "Haruna"]
man = ["Juma", "Asha", "Haruna"]
kwa idx, jina ktk majina {
print(idx, "-", jina)
for idx, n in man {
print(idx, "-", n)
}
```

## Array Methods

Arrays in vint have several built-in methods:

### idadi()
### length()

idadi() returns the length of an array:
length() returns the length of an array:

```s
a = [1, 2, 3]
urefu = a.idadi()
urefu = a.length()
print(urefu) // will print 3
```

### sukuma()
### push()

sukuma() adds one or more items to the end of an array:
push() adds one or more items to the end of an array:

```s
a = [1, 2, 3]
a.sukuma("s", "g")
a.push("s", "g")
print(a) // will print [1, 2, 3, "s", "g"]
```

### yamwisho()
### last()

yamwisho() returns the last item in an array, or tupu if the array is empty:
last() returns the last item in an array, or tupu if the array is empty:

```s
a = [1, 2, 3]
mwisho = a.yamwisho()
print(mwisho) // will print 3
last_el = a.last()
print(last_el) // will print 3
b = []
mwisho = b.yamwisho()
print(mwisho) // will print tupu
last_el = b.last()
print(last_el) // will print tupu
```

### map()
Expand All @@ -110,21 +110,21 @@ map() goes through every element in the array and applies the passed function to
```s
a = [1, 2, 3]
b = a.map(func(x){rudisha x*2})
b = a.map(func(x){return x*2})
print(b) // [2, 4, 6]
```

### chuja()
### filter()

chuja() will go through every single element of an array and checks if that element returns true or false when passed into a function. It will return a new array with elements that returned true:
filter() will go through every single element of an array and checks if that element returns true or false when passed into a function. It will return a new array with elements that returned true:
```s
a = [1, 2, 3, 4]
b = a.chuja(func(x){
kama (x % 2 == 0)
{rudisha kweli}
rudisha sikweli
b = a.filter(func(x){
if (x % 2 == 0)
{return true}
return false
})
print(b) // [2, 4]
Expand Down
14 changes: 7 additions & 7 deletions repl/docs/en/dictionaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Keys can be strings, integers, floats, or booleans, while values can be any data
k = {
"jina": "Juma",
"umri": 25,
kweli: "kweli",
true: "true",
"salimu": func(x) { print("habari", x) },
"sina value": tupu
}
Expand All @@ -29,7 +29,7 @@ Access individual elements in a dictionary using their keys:

```s
print(k[kweli]) // kweli
print(k[true]) // true
print(k["salimu"]("Juma")) // habari Juma
```

Expand Down Expand Up @@ -64,12 +64,12 @@ print(vyakula) // {"a": "apple", "b": "banana", "c": "carrot", "d": "daikon"}

## Checking If a Key Exists in a Dictionary

Use the ktk keyword to check if a key exists in a dictionary:
Use the in keyword to check if a key exists in a dictionary:

```s
"umri" ktk k // kweli
"urefu" ktk k // sikweli
"umri" in k // true
"urefu" in k // false
```

## Looping Over a Dictionary
Expand All @@ -79,7 +79,7 @@ Loop over a dictionary to access its keys and values:
```s
hobby = {"a": "asili", "b": "baiskeli", "c": "chakula"}
kwa i, v ktk hobby {
for i, v in hobby {
print(i, "=>", v)
}
```
Expand All @@ -93,7 +93,7 @@ c => chakula
Loop over just the values:

```s
kwa v ktk hobby {
for v in hobby {
print(v)
}
```
Expand Down
Loading

0 comments on commit 5a02297

Please sign in to comment.