Skip to content

Commit

Permalink
nativeStrings vint examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tacheraSasi committed Nov 28, 2024
1 parent f47fdb5 commit 2ddfd18
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
20 changes: 20 additions & 0 deletions object/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package object
import (
// "fmt"
"hash/fnv"
"strconv"
// "strconv"
"strings"
)
Expand Down Expand Up @@ -76,6 +77,25 @@ func (s *String) len(args []Object) Object {
return &Integer{Value: int64(len(s.Value))}
}

// toInt converts the string to an integer
func (s *String) toInt(args []Object) Object {
// Ensure no arguments are provided
if len(args) != 0 {
return newError("toInt() expects 0 arguments, got %d", len(args))
}

// Try to convert the string value to an integer
numVal, err := strconv.Atoi(s.Value)
if err != nil {
// Return an error if conversion fails
return newError("Failed to convert '%s' to an integer", s.Value)
}

// Return the integer value as an Integer object
return &Integer{Value: int64(numVal)}
}


// upper converts the string to uppercase.
func (s *String) upper(args []Object) Object {
if len(args) != 0 {
Expand Down
2 changes: 2 additions & 0 deletions vintLang/cli.vint
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//This module is still experimental

import cli

args = cli.parseArgs("arg1=val1 arg2=val2")
Expand Down
3 changes: 2 additions & 1 deletion vintLang/logicals.vint
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// Define a function to test 'and', 'or', and 'not'
let test_logical_operators = func () {
// Testing 'and' operator
let result_and = and(true, false) // Should return false
let result_and = and(1+2==3, false) // Should return false
print("Result of true AND false: ", result_and)
print(1+2==4)

// Testing 'or' operator
let result_or = or(false, true) // Should return true
Expand Down
8 changes: 8 additions & 0 deletions vintLang/nativeStrings.vint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name = "Tachera Sasi"

print(name.split(""))
print(name.reverse())
print(name.len())
print(name.upper())
print(name.lower())
print(name.upper())
13 changes: 13 additions & 0 deletions vintLang/packages.vint
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
Not working Still in development
*/


package vint{
init = func(){
name = "vint"
a = "hiofehoi"
}
print(@.name)
}
print(@.a)
1 change: 0 additions & 1 deletion vintLang/strings.vint
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Sample usage of the string module

import "string"

// Example 1: Trim whitespace
Expand Down

0 comments on commit 2ddfd18

Please sign in to comment.