Skip to content

Commit

Permalink
Array element replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
spacebanana420 committed Jul 27, 2024
1 parent b8deb5c commit 07d1128
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
12 changes: 8 additions & 4 deletions doc/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ Tofu's language style is somewhat inspired by CPU architecture instruction sets.
* array
* arrget
* arradd

* arreplace
* call
* return
* exec
* stop
* sleep
* break

* print
* clear
* color
Expand Down Expand Up @@ -102,7 +101,7 @@ readstr variable1
print $variable1
```

## array, arrget and arradd
## array, arrget, arradd and arreplace

You can declare arrays as a container to multiple elements of integer or string value:

Expand All @@ -118,7 +117,12 @@ arradd info, random text
arrget info, variable1, 0
//Print variable1, which is an integer of value "45"
print variable1
print $variable1
//Changes the value of the first element of "info", re-declares variable1 and prints the new value
arreplace info, 500, 0
arrget info, variable1, 0
print $variable1
```

The value is determined by the user at runtime, but the name of the variable must be given.
Expand Down
7 changes: 6 additions & 1 deletion doc/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,10 @@ arradd info, random text
arrget info, variable1, 0
//Print variable1, which is an integer of value "45"
print variable1
print $variable1
//Changes the value of the first element of "info", re-declares variable1 and prints the new value
arreplace info, 500, 0
arrget info, variable1, 0
print $variable1
```
4 changes: 4 additions & 0 deletions examples/array.tofu
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ arradd stuff, 324
arrget stuff, variable1, 0

print $variable1

arreplace stuff, 500, 0
arrget stuff, variable1, 0
print $variable1
12 changes: 11 additions & 1 deletion src/runner/runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ private def loopScript(s: Seq[String], ifunc: Seq[Int], nfunc: Seq[String], i: I
case other =>
closeTofu(s"Type error! Value '$other' from array '$name' at index $index is not an Int or String!")
loopScript(s, ifunc, nfunc, i+1)
case "arreplace" =>
val (name, value, index) = parseArrayAccess(s(i))
val tvar = readVariable_class_safe(value)
if tvar.vartype == variable_type.integer then
replaceInArray(name, index, tvar.value_int)
else
if isInt(value) then replaceInArray(name, index, mkInt(value))
else
replaceInArray(name, index, tvar.value_str)
loopScript(s, ifunc, nfunc, i+1)
case "exec" =>
exec(s(i))
loopScript(s, ifunc, nfunc, i+1)
Expand Down Expand Up @@ -145,7 +155,7 @@ private def lineType(line: String, types: Vector[String] =
Vector(
"string", "readstr", "while", "sleep", "calcint", "int",
"print", "clear", "locate", "color", "if", "function", "exec", "call",
"break", "stop", "array", "arradd", "arrget"),
"break", "stop", "array", "arradd", "arrget", "arreplace"),
i: Int = 0): String =
if i >= types.length then "none"
else if startsWith_strict(line, types(i)) then types(i)
Expand Down
7 changes: 7 additions & 0 deletions src/variables/declare.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ def getFromArray(name: String, index: Int): Any =
array_val(arrayvar.pointer).get(index)
else
closeTofu(s"Error: Array $name not found or not an array type")

def replaceInArray(name: String, index: Int, value: Any) =
val arrayvar = TofuVar(name)
if arrayvar.vartype != variable_type.array then closeTofu(s"Error: Array $name not found or not an array type")
else
array_val(arrayvar.pointer).replace(value, index)

10 changes: 7 additions & 3 deletions src/variables/variables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ enum variable_type:
case string, integer, array, none

class TofuArray:
private var elements: Vector[Any] = Vector()
private var elements: Array[Any] = Array()

def add(value: Any) = elements = elements :+ value
def get(index: Int): Any =
if index >= elements.length then closeTofu(s"Array read error! Array of size ${elements.length} is too small for index $index!")
elements(index)
def replace(value: Any, i: Int) =
if i >= elements.length then closeTofu(s"Array read error! Array of size ${elements.length} is too small for index $i!")
elements(i) = value

def size(): Int = elements.size
def toVector(): Vector[Any] = elements
def toArray(): Array[Any] = elements

class TofuVar(name: String):
val input = name
Expand All @@ -40,7 +44,7 @@ class TofuVar(name: String):
vartype match
case variable_type.string => value_str
case variable_type.integer => value_int.toString()
case variable_type.array => value_array.toVector().toString()
case variable_type.array => value_array.toArray().toString()
case variable_type.none => ""

private def getType(): variable_type =
Expand Down

0 comments on commit 07d1128

Please sign in to comment.