Skip to content

Commit d358d1f

Browse files
committed
Add numRows and numCols functions. Fix matrix item asign
1 parent 5e7379f commit d358d1f

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

interpreter/shared/src/main/scala/dev/sacode/flowrun/ast/PredefinedFunction.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ enum PredefinedFunction(val name: String) {
2020
case Ln extends PredefinedFunction("ln")
2121
case Log10 extends PredefinedFunction("log")
2222
case Log2 extends PredefinedFunction("log2")
23-
// strings
23+
// strings, arrays
2424
case Length extends PredefinedFunction("length")
2525
case CharAt extends PredefinedFunction("charAt")
26+
// matrices
27+
case NumRows extends PredefinedFunction("numRows")
28+
case NumCols extends PredefinedFunction("numCols")
2629
// conversions
2730
case RealToInteger extends PredefinedFunction("real2int")
2831
case StringToInteger extends PredefinedFunction("string2int")

interpreter/shared/src/main/scala/dev/sacode/flowrun/eval/Interpreter.scala

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ final class Interpreter(
519519
)
520520
exprValue match
521521
case IntegerVal(newValue) =>
522-
values(index1)(index1) = newValue
522+
values(index1)(index2) = newValue
523523
exprValue
524524
case other =>
525525
throw EvalException(
@@ -536,10 +536,10 @@ final class Interpreter(
536536
)
537537
exprValue match
538538
case RealVal(newValue) =>
539-
values(index1)(index1) = newValue
539+
values(index1)(index2) = newValue
540540
exprValue
541541
case IntegerVal(newValue) =>
542-
values(index1)(index1) = newValue
542+
values(index1)(index2) = newValue
543543
exprValue
544544
case other =>
545545
throw EvalException(
@@ -556,7 +556,7 @@ final class Interpreter(
556556
)
557557
exprValue match
558558
case StringVal(newValue) =>
559-
values(index1)(index1) = newValue
559+
values(index1)(index2) = newValue
560560
exprValue
561561
case other =>
562562
throw EvalException(
@@ -573,7 +573,7 @@ final class Interpreter(
573573
)
574574
exprValue match
575575
case BooleanVal(newValue) =>
576-
values(index1)(index1) = newValue
576+
values(index1)(index2) = newValue
577577
exprValue
578578
case other =>
579579
throw EvalException(
@@ -690,7 +690,7 @@ final class Interpreter(
690690
IntegerVal(value)
691691
} catch {
692692
case _: NumberFormatException =>
693-
throw EvalException(s"You entered invalid value for '${sym.asVar}' : '${inputValue}'.", id)
693+
throw EvalException(s"You entered invalid value for '${matrixName}[${index1}][${index2}]' : '${inputValue}'.", id)
694694
}
695695
case RunVal.RealMatrixVal(values) =>
696696
if !values.indices.contains(index1) then
@@ -706,7 +706,7 @@ final class Interpreter(
706706
RealVal(value)
707707
} catch {
708708
case _: NumberFormatException =>
709-
throw EvalException(s"You entered invalid value for '${sym.asVar}' : '${inputValue}'.", id)
709+
throw EvalException(s"You entered invalid value for '${matrixName}[${index1}][${index2}]' : '${inputValue}'.", id)
710710
}
711711
case RunVal.StringMatrixVal(values) =>
712712
if !values.indices.contains(index1) then
@@ -732,7 +732,7 @@ final class Interpreter(
732732
BooleanVal(value)
733733
} catch {
734734
case _: IllegalArgumentException =>
735-
throw EvalException(s"You entered invalid value for '${sym.asVar}' : '${inputValue}'.", id)
735+
throw EvalException(s"You entered invalid value for '${matrixName}[${index1}][${index2}]' : '${inputValue}'.", id)
736736
}
737737
case _ =>
738738
throw EvalException(
@@ -760,7 +760,7 @@ final class Interpreter(
760760
IntegerVal(value)
761761
} catch {
762762
case _: NumberFormatException =>
763-
throw EvalException(s"You entered invalid value for '${sym.asVar}' : '${inputValue}'.", id)
763+
throw EvalException(s"You entered invalid value for '${arrayName}[${index}]' : '${inputValue}'.", id)
764764
}
765765
case RunVal.RealArrayVal(values) =>
766766
if !values.indices.contains(index) then
@@ -771,7 +771,7 @@ final class Interpreter(
771771
RealVal(value)
772772
} catch {
773773
case _: NumberFormatException =>
774-
throw EvalException(s"You entered invalid value for '${sym.asVar}' : '${inputValue}'.", id)
774+
throw EvalException(s"You entered invalid value for '${arrayName}[${index}]' : '${inputValue}'.", id)
775775
}
776776
case RunVal.StringArrayVal(values) =>
777777
if !values.indices.contains(index) then
@@ -787,7 +787,7 @@ final class Interpreter(
787787
BooleanVal(value)
788788
} catch {
789789
case _: IllegalArgumentException =>
790-
throw EvalException(s"You entered invalid value for '${sym.asVar}' : '${inputValue}'.", id)
790+
throw EvalException(s"You entered invalid value for '${arrayName}[${index}]' : '${inputValue}'.", id)
791791
}
792792
case _ =>
793793
throw EvalException(
@@ -1331,6 +1331,23 @@ final class Interpreter(
13311331
args.head match
13321332
case n: StringVal => Future(IntegerVal(n.value.toInt))
13331333
case _ => throw EvalException(s"Expected a String argument in function ${func.name}", id)
1334+
// matrices
1335+
case func @ NumRows =>
1336+
validateArgsNumber(id, func.name, 1, args.size)
1337+
args.head match
1338+
case m: IntegerMatrixVal => Future(IntegerVal(m.values.length))
1339+
case m: RealMatrixVal => Future(IntegerVal(m.values.length))
1340+
case m: StringMatrixVal => Future(IntegerVal(m.values.length))
1341+
case m: BooleanMatrixVal => Future(IntegerVal(m.values.length))
1342+
case _ => throw EvalException(s"Expected Matrix argument in function ${func.name}", id)
1343+
case func @ NumCols =>
1344+
validateArgsNumber(id, func.name, 1, args.size)
1345+
args.head match
1346+
case m: IntegerMatrixVal => Future(IntegerVal(if m.values.isEmpty then 0 else m.values(0).length))
1347+
case m: RealMatrixVal => Future(IntegerVal(if m.values.isEmpty then 0 else m.values(0).length))
1348+
case m: StringMatrixVal => Future(IntegerVal(if m.values.isEmpty then 0 else m.values(0).length))
1349+
case m: BooleanMatrixVal => Future(IntegerVal(if m.values.isEmpty then 0 else m.values(0).length))
1350+
case _ => throw EvalException(s"Expected Matrix argument in function ${func.name}", id)
13341351
// misc
13351352
case ReadInput =>
13361353
state = State.WAITING_FOR_INPUT

0 commit comments

Comments
 (0)