Skip to content

Commit 656e628

Browse files
authored
Merge pull request #27 from sacode387/add-arrays
Add arrays
2 parents 6a0280f + 3388f6d commit 656e628

30 files changed

+365
-161
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313

1414
- name: Setup Scala
1515
uses: japgolly/setup-everything-scala@v3.1
16+
with:
17+
java-version: 11
1618

1719
- name: Build and test
1820
shell: bash

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- uses: actions/setup-java@v4
1313
with:
1414
distribution: temurin
15-
java-version: 8
15+
java-version: 11
1616
cache: sbt
1717
- run: sbt ci-release
1818
env:

build.sbt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
inThisBuild(
22
List(
3-
scalaVersion := "3.4.2",
3+
scalaVersion := "3.6.2",
44
evictionErrorLevel := Level.Warn,
55
publish / skip := true,
66
scalafmtSbt := true,
@@ -30,9 +30,8 @@ lazy val editor = (project in file("editor"))
3030
.settings(
3131
name := "flowrun-editor",
3232
libraryDependencies ++= Seq(
33-
"com.lihaoyi" %%% "scalatags" % "0.12.0",
34-
"com.lihaoyi" %%% "utest" % "0.8.1" % Test,
35-
"com.lihaoyi" %%% "pprint" % "0.8.1" % Test
33+
"com.lihaoyi" %%% "scalatags" % "0.13.1",
34+
"com.lihaoyi" %%% "utest" % "0.8.5" % Test
3635
),
3736
scalacOptions ++= Seq(
3837
"-Xmax-inlines",
@@ -55,11 +54,10 @@ lazy val interpreter = crossProject(JVMPlatform, JSPlatform)
5554
.settings(
5655
name := "flowrun-interpreter",
5756
libraryDependencies ++= Seq(
58-
"io.github.cquiroz" %%% "scala-java-time" % "2.5.0",
59-
"com.outr" %%% "reactify" % "4.0.8",
60-
"ba.sake" %%% "tupson" % "0.11.0",
61-
"com.lihaoyi" %%% "utest" % "0.8.1" % Test,
62-
"com.lihaoyi" %%% "pprint" % "0.8.1" % Test
57+
"io.github.cquiroz" %%% "scala-java-time" % "2.6.0",
58+
"com.outr" %%% "reactify" % "4.1.3",
59+
"ba.sake" %%% "tupson" % "0.13.0",
60+
"com.lihaoyi" %%% "utest" % "0.8.5" % Test
6361
),
6462
scalacOptions ++= Seq(
6563
"-Xmax-inlines",

editor/src/main/scala/dev/sacode/flowrun/FlowRunElements.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class FlowRunElements(
8080
val configDialog: dom.html.Element =
8181
mountElem.querySelector(".flowrun-config").asInstanceOf[dom.html.Element]
8282
val zoomResetButton =
83-
mountElem.querySelector(".flowrun-btn-zoom-reset").asInstanceOf[dom.html.Element]
83+
mountElem.querySelector(".flowrun-btn-zoom-reset").asInstanceOf[dom.html.Element]
8484

8585
val clearOutputBtn = mountElem.querySelector(".flowrun-btn-clear-output").asInstanceOf[dom.html.Element]
8686

editor/src/main/scala/dev/sacode/flowrun/edit/CtxMenu.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class CtxMenu(programModel: ProgramModel, flowRunElements: FlowRunElements, flow
139139

140140
addDeclareButton.addEventListener(
141141
"click",
142-
(_: dom.MouseEvent) => addStatement(Declare(AST.newId, "x", Expression.Type.Integer, None))
142+
(_: dom.MouseEvent) => addStatement(Declare(AST.newId, "x", Expression.Type.Integer, None, 1))
143143
)
144144

145145
addAssignButton.addEventListener(

editor/src/main/scala/dev/sacode/flowrun/edit/FlowchartPresenter.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ class FlowchartPresenter(
3131
)
3232
)
3333

34-
graphviz.on("end", { (thiz: js.Dynamic) =>
35-
thiz.zoomSelection().on("dblclick.zoom", null)
36-
}: js.ThisFunction0[js.Dynamic, Unit])
34+
graphviz.on(
35+
"end",
36+
{ (thiz: js.Dynamic) =>
37+
thiz.zoomSelection().on("dblclick.zoom", null)
38+
}: js.ThisFunction0[js.Dynamic, Unit]
39+
)
3740

3841
loadCurrentFunction()
3942

editor/src/main/scala/dev/sacode/flowrun/edit/StatementEditor.scala

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,33 @@ final class StatementEditor(
9898
val typeInputElem = newTypeInput(Expression.Type.VarTypes.toSeq, statement.tpe) { newType =>
9999
val updatedStmt = programModel.findStatement(stmtId).asInstanceOf[Declare].copy(tpe = newType)
100100
programModel.updateStmt(updatedStmt)
101+
edit(stmtId) // refresh because array inputs are not same as scalar
101102
}
102103
val exprInputElem = newExprInput(statement.id, 10, statement.initValue.getOrElse(""), "123") { newExprText =>
103104
val newValueOpt = Option.when(newExprText.nonEmpty)(newExprText)
104105
val updatedStmt = programModel.findStatement(stmtId).asInstanceOf[Declare].copy(initValue = newValueOpt)
105106
programModel.updateStmt(updatedStmt)
106107
}
107-
108+
val arrayLengthInput = newExprInput(statement.id, 10, statement.lengthValue.toString, "5") { newLengthText =>
109+
val newLength = newLengthText.trim.toIntOption.getOrElse(0)
110+
val updatedStmt = programModel.findStatement(stmtId).asInstanceOf[Declare].copy(lengthValue = newLength)
111+
programModel.updateStmt(updatedStmt)
112+
}
108113
flowRunElements.stmtOutput.innerText = ""
109114
flowRunElements.stmtOutput.appendChild(
110115
stmtElem(
111116
nameInputElem,
112117
typeInputElem,
113-
span(" = "),
114-
exprInputElem
118+
if statement.tpe.isArray then
119+
frag(
120+
span(" of length "),
121+
arrayLengthInput
122+
)
123+
else
124+
frag(
125+
span(" = "),
126+
exprInputElem
127+
)
115128
).render
116129
)
117130
nameInputElem.focus()
@@ -165,7 +178,8 @@ final class StatementEditor(
165178
exprInputElem.focus()
166179

167180
case statement: Assign =>
168-
val nameInputElem = newNameInput(10, statement.name, "x") { newName =>
181+
// dont validate because of array[x+1]
182+
val nameInputElem = newNameInput(10, statement.name, "x", validate = false) { newName =>
169183
val updatedStmt = programModel.findStatement(stmtId).asInstanceOf[Assign].copy(name = newName)
170184
programModel.updateStmt(updatedStmt)
171185
}
@@ -330,7 +344,7 @@ final class StatementEditor(
330344
): dom.html.Select = {
331345
val newInput = flowRunElements.newInputSelect
332346
types.foreach { tpe =>
333-
val typeItem = option(value := tpe.toString)(tpe.toString).render
347+
val typeItem = option(value := tpe.toString)(tpe.pretty).render
334348
newInput.add(typeItem)
335349
}
336350
newInput.value = selectedTpe.toString

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ factor -> unary (("*" | "/" | "%") unary)* ;
1515
unary -> ("!" | "-") unary
1616
| atom ;
1717
atom -> NUMBER | STRING | "true" | "false" | "null"
18-
| ID
18+
| ID | ID[expression]
1919
| "(" expression ")" ;
2020
*/
2121

@@ -28,6 +28,29 @@ object Expression:
2828
case Real
2929
case String
3030
case Boolean
31+
case IntegerArray
32+
case RealArray
33+
case StringArray
34+
case BooleanArray
35+
36+
def pretty: String = this match
37+
case Void => "Void"
38+
case Integer => "Integer"
39+
case Real => "Real"
40+
case String => "String"
41+
case Boolean => "Boolean"
42+
case IntegerArray => "Integer[]"
43+
case RealArray => "Real[]"
44+
case StringArray => "String[]"
45+
case BooleanArray => "Boolean[]"
46+
47+
def isArray: Boolean = this match
48+
case IntegerArray => true
49+
case RealArray => true
50+
case StringArray => true
51+
case BooleanArray => true
52+
case _ => false
53+
3154
object Type:
3255
val VarTypes = Type.values.filterNot(_ == Type.Void)
3356

@@ -58,6 +81,7 @@ enum Atom:
5881
case RealLit(value: Double)
5982
case StringLit(value: String)
6083
case Identifier(name: String)
84+
case ArrayIndexAccess(name: String, indexExpr: Expression)
6185
case TrueLit
6286
case FalseLit
6387
case Parens(expression: Expression)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ object Statement:
4141
id: String,
4242
name: String,
4343
tpe: Expression.Type,
44-
initValue: Option[String]
44+
initValue: Option[String],
45+
lengthValue: Int = 0
4546
) extends Statement
4647
derives JsonRW:
4748
override def duplicated: Declare = copy(id = AST.newId)
4849
override def label =
49-
val maybeExprText = initValue.map(e => s" = $e").getOrElse("")
50+
// dont show initializer for array
51+
val maybeExprText = initValue.map(e => s" = $e").filterNot(_ => tpe.isArray).getOrElse("")
5052
s"$name$maybeExprText"
5153
override def verboseLabel =
52-
val maybeExprText = initValue.map(e => s" = $e").getOrElse("")
54+
// dont show initializer for array
55+
val maybeExprText = initValue.map(e => s" = $e").filterNot(_ => tpe.isArray).getOrElse("")
5356
s"$name: $tpe$maybeExprText"
5457

5558
case class Assign(id: String, name: String, value: String) extends Statement derives JsonRW:

interpreter/shared/src/main/scala/dev/sacode/flowrun/codegen/CGenerator.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ class CGenerator(val programAst: Program) extends CodeGenerator {
6565
import Statement._
6666
stmt match {
6767
case _: Begin => // noop
68-
case Declare(id, name, tpe, maybeInitValue) =>
69-
val key = SymbolKey(name, Symbol.Kind.Variable, id)
70-
symTab.add(id, key, tpe, None)
71-
val initValue = maybeInitValue.getOrElse(defaultValue(tpe))
68+
case d: Declare =>
69+
val key = SymbolKey(d.name, Symbol.Kind.Variable, d.id)
70+
symTab.add(d.id, key, d.tpe, None)
71+
val initValue = d.initValue.getOrElse(defaultValue(d.tpe))
7272
val initValueExpr = parseGenExpr(initValue)
73-
tpe match
73+
d.tpe match
7474
case Expression.Type.String =>
75-
addLine(s"char ${name}[] = $initValueExpr;", id)
76-
case _ => addLine(s"${genType(tpe)} $name = $initValueExpr;", id)
75+
addLine(s"char ${d.name}[] = $initValueExpr;", d.id)
76+
case _ => addLine(s"${genType(d.tpe)} ${d.name} = $initValueExpr;", d.id)
7777

7878
case Assign(id, name, value) =>
7979
val genValue = parseGenExpr(value)

0 commit comments

Comments
 (0)