Skip to content

Commit

Permalink
adding slots
Browse files Browse the repository at this point in the history
cleaning up names
adding a in memory store
  • Loading branch information
almibe committed Jun 4, 2024
1 parent 0bacdf1 commit d874857
Show file tree
Hide file tree
Showing 23 changed files with 359 additions and 455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ case class Test(val x: Int)
// override def allDatasets(): Iterator[DatasetName] = store.keySet.iterator

// /** Check if a given Dataset exists. */
// override def graphExists(graph: DatasetName): Boolean = store.contains(graph)
// override def networkExists(network: DatasetName): Boolean = store.contains(network)

// /** Returns all Datasets in a Ligature instance that start with the given
// * prefix.
Expand All @@ -38,65 +38,65 @@ case class Test(val x: Int)
// ): Iterator[DatasetName] =
// store.keys.filter(k => k.name >= start && k.name < end).iterator

// /** Creates a graph with the given name. TODO should probably return its own
// /** Creates a network with the given name. TODO should probably return its own
// * error type { InvalidDataset, DatasetExists, CouldNotCreateDataset }
// */
// override def createDataset(graph: DatasetName): Unit =
// if !this.store.contains(graph) then this.store += (graph -> DatasetStore())
// override def createDataset(network: DatasetName): Unit =
// if !this.store.contains(network) then this.store += (network -> DatasetStore())

// /** Deletes a graph with the given name. TODO should probably return its own
// /** Deletes a network with the given name. TODO should probably return its own
// * error type { InvalidDataset, CouldNotDeleteDataset }
// */
// override def deleteDataset(graph: DatasetName): Unit =
// val _ = this.store.remove(graph)
// override def deleteDataset(network: DatasetName): Unit =
// val _ = this.store.remove(network)

// override def allStatements(graph: DatasetName): Iterator[Statement] =
// this.store.get(graph) match
// override def allStatements(network: DatasetName): Iterator[Statement] =
// this.store.get(network) match
// case None => Iterator.empty
// case Some(value) => value.edges.iterator

// /** Initializes a QueryTx TODO should probably return its own error type
// * CouldNotInitializeQueryTx
// */
// override def query[T](graph: DatasetName)(fn: QueryTx => T): T =
// store.get(graph) match
// override def query[T](network: DatasetName)(fn: QueryTx => T): T =
// store.get(network) match
// case None => ???
// case Some(graphStore) =>
// val tx = InMemoryQueryTx(graphStore)
// case Some(networkStore) =>
// val tx = InMemoryQueryTx(networkStore)
// fn(tx)

// override def addStatements(graph: DatasetName, edges: Iterator[Statement]): Unit =
// this.store.get(graph) match {
// override def addStatements(network: DatasetName, edges: Iterator[Statement]): Unit =
// this.store.get(network) match {
// case None =>
// this.store.addOne((graph, DatasetStore(0, Set.from(edges.toSet))))
// this.store.addOne((network, DatasetStore(0, Set.from(edges.toSet))))
// case Some(store) => store.edges.addAll(edges)
// }
// // store.get(graph) match
// // store.get(network) match
// // case None => ???
// // case Some(graphStore) =>
// // case Some(networkStore) =>
// // edges
// // .fold(graphStore) { (graphStore, edge) =>
// // .fold(networkStore) { (networkStore, edge) =>
// // ???
// // //graphStore.copy(edges = graphStore.edges + edge)
// // //networkStore.copy(edges = networkStore.edges + edge)
// // }
// // .map { graphStore =>
// // store.updated(graph, graphStore)
// // .map { networkStore =>
// // store.updated(network, networkStore)
// // }

// override def removeStatements(graph: DatasetName, edges: Iterator[Statement]): Unit =
// this.store.get(graph) match {
// override def removeStatements(network: DatasetName, edges: Iterator[Statement]): Unit =
// this.store.get(network) match {
// case None => ???
// case Some(store) => store.edges.subtractAll(edges)
// }
// // store.get(graph) match
// // store.get(network) match
// // case None => ???
// // case Some(graphStore) =>
// // case Some(networkStore) =>
// // edges
// // .fold(graphStore) { (graphStore, edge) =>
// // graphStore.copy(edges = graphStore.edges - edge)
// // .fold(networkStore) { (networkStore, edge) =>
// // networkStore.copy(edges = networkStore.edges - edge)
// // }
// // .map { graphStore =>
// // store.updated(graph, graphStore)
// // .map { networkStore =>
// // store.updated(network, networkStore)
// // }

// override def close(): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ case class Test(val x: Int)
// assertEquals(instance.allDatasets().toList, List())
// }

// setup.test("creating a new graph") { instance =>
// setup.test("creating a new network") { instance =>
// instance.createDataset(testDataset)
// val res = instance.allDatasets().toList
// assertEquals(res, List(testDataset))
// }

// setup.test("check if datasets exist") { instance =>
// instance.createDataset(testDataset)
// val exists1 = instance.graphExists(testDataset)
// val exists2 = instance.graphExists(testDataset2)
// val exists1 = instance.networkExists(testDataset)
// val exists2 = instance.networkExists(testDataset2)
// val res = (exists1, exists2)
// assertEquals(res, (true, false))
// }
Expand Down Expand Up @@ -91,7 +91,7 @@ case class Test(val x: Int)
// assertEquals((res1, res2, res3), (2, 4, 5)) // TODO check instances not just counts
// }

// setup.test("create and delete new graph") { instance =>
// setup.test("create and delete new network") { instance =>
// instance.createDataset(testDataset)
// instance.deleteDataset(testDataset)
// instance.deleteDataset(testDataset2)
Expand Down Expand Up @@ -179,9 +179,9 @@ case class Test(val x: Int)
// // // // _ <- instance.createDataset(testDataset)
// // // // _ <- instance.write(testDataset) { tx =>
// // // // for {
// // // // entity <- tx.newLabel("entity-")
// // // // attribute <- tx.newLabel("attribute-")
// // // // value <- tx.newLabel("value-")
// // // // entity <- tx.newIdentifier("entity-")
// // // // attribute <- tx.newIdentifier("attribute-")
// // // // value <- tx.newIdentifier("value-")
// // // // _ <- tx.addStatement(Statement(entity, attribute, value))
// // // // } yield IO.unit
// // // // }
Expand All @@ -193,7 +193,7 @@ case class Test(val x: Int)
// // // // assert(it.entity.name.startsWith("entity-"))
// // // // assert(it.attribute.name.startsWith("attribute-"))
// // // // it.value match {
// // // // case Label(id) => assert(id.startsWith("value-"))
// // // // case Identifier(id) => assert(id.startsWith("value-"))
// // // // case _ => assert(false)
// // // // }
// // // // }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import dev.ligature.wander.printWanderValue
import dev.ligature.wander.printResult
import dev.ligature.wander.*
import com.typesafe.scalalogging.Logger
import dev.ligature.wander.modules.{std, wanderLibs}
import dev.ligature.wander.modules.{std}//, wanderLibs}

private class LigatureZeroMQ(val port: Int) extends Runnable with AutoCloseable {
val logger = Logger("LigatureZeroMQ")
private val zContext = ZContext()

override def run(): Unit =
val environment = std().combine(wanderLibs())
val environment = std()//.combine(wanderLibs())
val socket = zContext.createSocket(SocketType.REP)
socket.bind(s"tcp://localhost:$port")
var continue = true
Expand Down
14 changes: 6 additions & 8 deletions ligature/src/main/scala/dev/ligature/wander/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import dev.ligature.wander.*
import scala.collection.mutable.ListBuffer
import scala.util.boundary, boundary.break
import scala.collection.mutable
import dev.ligature.LigatureValue
import dev.ligature.Statement

enum Expression:
case FieldExpression(field: dev.ligature.wander.Field)
Expand All @@ -22,12 +20,12 @@ enum Expression:
case Array(value: Seq[Expression])
case Binding(name: Field, tag: Option[FieldPath], value: Expression)
case Module(values: Seq[(dev.ligature.wander.Field, Expression)])
case Graph(expressions: Seq[Expression])
case Network(expressions: Seq[Expression])
case Lambda(parameters: Seq[Field], body: Expression)
case WhenExpression(conditionals: Seq[(Expression, Expression)])
case Application(expressions: Seq[Expression])
case Grouping(expressions: Seq[Expression])
case QuestionMark
case Slot(name: String)

def eval(
expression: Expression,
Expand All @@ -52,9 +50,9 @@ def eval(
handleWhenExpression(conditionals, environment)
case Expression.Grouping(expressions) => handleGrouping(expressions, environment)
case Expression.Application(expressions) => handleApplication(expressions, environment)
case Expression.QuestionMark => Right((WanderValue.QuestionMark, environment))
case Expression.Slot(name) => Right((WanderValue.Slot(name), environment))
case Expression.Module(values) => handleModule(values, environment)
case Expression.Graph(expressions) => handleGraph(expressions, environment)
case Expression.Network(expressions) => handleNetwork(expressions, environment)
}

def readField(
Expand Down Expand Up @@ -143,7 +141,7 @@ def handleModule(
)
Right((WanderValue.Module(results.toMap), environment))

def handleGraph(
def handleNetwork(
expressions: Seq[Expression],
environment: Environment
): Either[WanderError, (WanderValue, Environment)] =
Expand All @@ -155,7 +153,7 @@ def handleGraph(
) =>
Right(
(
WanderValue.Graph(
WanderValue.Network(
Set(
Statement(
LigatureValue.Identifier(entity),
Expand Down
19 changes: 12 additions & 7 deletions ligature/src/main/scala/dev/ligature/wander/Lexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import java.util.HexFormat
import dev.ligature.wander.LigNibblers.identifierNibbler

enum Token:
case Label(value: String)
case Identifier(value: String)
case Slot(value: String)
case BooleanLiteral(value: Boolean)
case Spaces(value: String)
case Bytes(value: Seq[Byte])
Expand All @@ -38,7 +39,7 @@ enum Token:
case TaggedField(name: String, tag: String)
case OpenBrace, CloseBrace, Colon, OpenParen, CloseParen, NewLine,
Arrow, WideArrow, Dot, At, WhenKeyword, EqualSign, Comment,
OpenBracket, CloseBracket, QuestionMark, EndKeyword, Period,
OpenBracket, CloseBracket, EndKeyword, Period,
Backtick, Hash, Lambda, Pipe, Comma

def tokenize(input: String): Either[WanderError, Seq[Token]] = {
Expand Down Expand Up @@ -116,9 +117,6 @@ val atNib =
val pipeNib =
takeString("|").map(res => Token.Pipe)

val questionMarkNib =
takeString("?").map(res => Token.QuestionMark)

val equalSignTokenNib =
takeString("=").map(res => Token.EqualSign)

Expand Down Expand Up @@ -165,12 +163,19 @@ val integerTokenNib =
LigNibblers.numberNibbler.map(res => Token.IntegerValue(res.mkString.toLong))

val identifierTokenNib =
LigNibblers.identifierNibbler.map(res => Token.Label(res))
LigNibblers.identifierNibbler.map(res => Token.Identifier(res))

val spacesTokenNib =
concat(takeWhile[String](_ == " "))
.map(res => Token.Spaces(res.mkString))

val slotNib = takeAll(
takeString("?"),
optional(concat(takeWhile { (c: String) =>
"[a-zA-Z0-9-._~:/?#\\[\\]@!$&'()*+,;%=]".r.matches(c)
})),
).map(res => Token.Slot(res.tail.mkString))

val tokensNib: Nibbler[String, Seq[Token]] = repeat(
takeFirst(
spacesTokenNib,
Expand All @@ -197,7 +202,7 @@ val tokensNib: Nibbler[String, Seq[Token]] = repeat(
backtickTokenNib,
commentTokenNib,
equalSignTokenNib,
questionMarkNib,
slotNib,
hashTokenNib
)
)
Expand Down
2 changes: 1 addition & 1 deletion ligature/src/main/scala/dev/ligature/wander/Ligature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

package dev.ligature
package dev.ligature.wander

final case class DatasetName(name: String) extends Ordered[DatasetName]:
override def compare(that: DatasetName): Int = this.name.compare(that.name)
Expand Down
26 changes: 13 additions & 13 deletions ligature/src/main/scala/dev/ligature/wander/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ enum Term:
case IntegerValue(value: Long)
case StringValue(value: String, interpolated: Boolean = false)
case BooleanLiteral(value: Boolean)
case QuestionMark
case Slot(name: String)
case Array(value: Seq[Term])
case Identifier(value: String)
case Binding(field: Field, tag: Option[FieldPath], term: Term)
case Module(bindings: Seq[(Field, Term)])
case Graph(roots: Seq[GraphRoot])
case GraphRoot(terms: Seq[Term])
case Network(roots: Seq[NetworkRoot])
case NetworkRoot(terms: Seq[Term])
case WhenExpression(conditionals: Seq[(Term, Term)])
case Application(terms: Seq[Term])
case Grouping(terms: Seq[Term])
Expand Down Expand Up @@ -58,9 +58,9 @@ def parse(script: Seq[Token]): Either[WanderError, Seq[Term]] = {
}
}

val questionMarkTermNib: Nibbler[Token, Term] = gaze =>
val slotTermNib: Nibbler[Token, Term] = gaze =>
gaze.next() match
case Some(Token.QuestionMark) => Result.Match(Term.QuestionMark)
case Some(Token.Slot(name)) => Result.Match(Term.Slot(name))
case _ => Result.NoMatch

val booleanNib: Nibbler[Token, Term.BooleanLiteral] = gaze =>
Expand Down Expand Up @@ -90,7 +90,7 @@ val stringNib: Nibbler[Token, Term.StringValue] = gaze =>

val identifierNib: Nibbler[Token, Term.Identifier] = gaze =>
gaze.next() match
case Some(Token.Label(value)) => Result.Match(Term.Identifier(value))
case Some(Token.Identifier(value)) => Result.Match(Term.Identifier(value))
case _ => Result.NoMatch

val fieldNib: Nibbler[Token, Field] = gaze =>
Expand Down Expand Up @@ -212,16 +212,16 @@ val moduleNib: Nibbler[Token, Term.Module] = { gaze =>
case _ => Result.NoMatch
}

val graphNib: Nibbler[Token, Term.Graph] = { gaze =>
val networkNib: Nibbler[Token, Term.Network] = { gaze =>
val res = for
_ <- gaze.attempt(take(Token.OpenBrace))
entity <- gaze.attempt(identifierNib)
attribute <- gaze.attempt(identifierNib)
value <- gaze.attempt(identifierNib)
_ <- gaze.attempt(take(Token.CloseBrace))
yield Term.Graph(Seq(Term.GraphRoot(Seq(entity, attribute, value))))
yield Term.Network(Seq(Term.NetworkRoot(Seq(entity, attribute, value))))
res match
case Result.Match(Term.Graph(values)) => Result.Match(Term.Graph(values))
case Result.Match(Term.Network(values)) => Result.Match(Term.Network(values))
case _ => Result.NoMatch
}

Expand Down Expand Up @@ -285,10 +285,10 @@ val applicationInternalNib =
integerNib,
whenExpressionNib,
arrayNib,
graphNib,
networkNib,
moduleNib,
booleanNib,
questionMarkTermNib,
slotTermNib,
fieldPathTermNib
)

Expand All @@ -306,10 +306,10 @@ val expressionNib =
integerNib,
whenExpressionNib,
arrayNib,
graphNib,
networkNib,
moduleNib,
booleanNib,
questionMarkTermNib,
slotTermNib,
fieldPathTermNib,
pipeTermNib
)
Expand Down
Loading

0 comments on commit d874857

Please sign in to comment.