Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions calc/CalcEvaluator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2024-2025 DCal Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package distcompiler.calc

import cats.syntax.all.given

import distcompiler.*
import dsl.*
import distcompiler.Builtin.{Error, SourceMarker}
import distcompiler.calc.tokens.*

object CalcEvaluator extends PassSeq:
import distcompiler.dsl.*
import Reader.*
import CalcReader.*

def inputWellformed: Wellformed = distcompiler.calc.wellformed

private val simplifyPass = passDef:
wellformed := inputWellformed.makeDerived:
Node.Top ::=! Expression

pass(once = false, strategy = pass.bottomUp)
.rules:
on(
field(tok(Expression)) *> onlyChild(
tok(Add).withChildren:
field(tok(Expression) *> onlyChild(tok(Number)))
~ field(tok(Expression) *> onlyChild(tok(Number)))
~ eof
)
).rewrite: (left, right) =>
val leftNum = left.unparent().sourceRange.decodeString().toInt
val rightNum = right.unparent().sourceRange.decodeString().toInt

splice(
Expression(
Number(
(leftNum + rightNum).toString()
)
)
)
| on(
field(tok(Expression)) *> onlyChild(
tok(Sub).withChildren:
field(tok(Expression) *> onlyChild(tok(Number)))
~ field(tok(Expression) *> onlyChild(tok(Number)))
~ eof
)
).rewrite: (left, right) =>
val leftNum = left.unparent().sourceRange.decodeString().toInt
val rightNum = right.unparent().sourceRange.decodeString().toInt

splice(
Expression(
Number(
(leftNum - rightNum).toString()
)
)
)
| on(
field(tok(Expression)) *> onlyChild(
tok(Mul).withChildren:
field(tok(Expression) *> onlyChild(tok(Number)))
~ field(tok(Expression) *> onlyChild(tok(Number)))
~ eof
)
).rewrite: (left, right) =>
val leftNum = left.unparent().sourceRange.decodeString().toInt
val rightNum = right.unparent().sourceRange.decodeString().toInt

splice(
Expression(
Number(
(leftNum * rightNum).toString()
)
)
)
| on(
field(tok(Expression)) *> onlyChild(
tok(Div).withChildren:
field(tok(Expression) *> onlyChild(tok(Number)))
~ field(tok(Expression) *> onlyChild(tok(Number)))
~ eof
)
).rewrite: (left, right) =>
val leftNum = left.unparent().sourceRange.decodeString().toInt
val rightNum = right.unparent().sourceRange.decodeString().toInt

splice(
Expression(
Number(
(leftNum / rightNum).toString()
)
)
)

private val removeLayerPass = passDef:
wellformed := prevWellformed.makeDerived:
Node.Top ::=! Number

pass(once = true, strategy = pass.topDown)
.rules:
on(
tok(Expression).withChildren:
field(tok(Number))
~ eof
).rewrite: (number) =>
splice(
number.unparent()
)
127 changes: 127 additions & 0 deletions calc/CalcParser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2024-2025 DCal Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package distcompiler.calc

import cats.syntax.all.given

import distcompiler.*
import dsl.*
import distcompiler.Builtin.{Error, SourceMarker}
import distcompiler.calc.tokens.*

object CalcParser extends PassSeq:
import distcompiler.dsl.*
import Reader.*
import CalcReader.*

def inputWellformed: Wellformed =
CalcReader.wellformed.makeDerived:
Add ::= fields(
Expression,
Expression
)

Sub ::= fields(
Expression,
Expression
)

Mul ::= fields(
Expression,
Expression
)

Div ::= fields(
Expression,
Expression
)

Expression ::=! choice(
Number,
Add,
Sub,
Mul,
Div
)

private val mulDivPass = passDef:
wellformed := inputWellformed.makeDerived:
Node.Top ::=! repeated(choice(Expression, AddOp, SubOp))

pass(once = false, strategy = pass.topDown)
.rules:
on(
field(tok(Expression))
~ skip(tok(MulOp))
~ field(tok(Expression))
~ trailing
).rewrite: (left, right) =>
splice(
Expression(
Mul(
left.unparent(),
right.unparent()
)
)
)
| on(
field(tok(Expression))
~ skip(tok(DivOp))
~ field(tok(Expression))
~ trailing
).rewrite: (left, right) =>
splice(
Expression(
Div(
left.unparent(),
right.unparent()
)
)
)

private val addSubPass = passDef:
wellformed := prevWellformed.makeDerived:
Node.Top ::=! repeated(Expression)

pass(once = false, strategy = pass.topDown)
.rules:
on(
field(tok(Expression))
~ skip(tok(AddOp))
~ field(tok(Expression))
~ trailing
).rewrite: (left, right) =>
splice(
Expression(
Add(
left.unparent(),
right.unparent()
)
)
)
| on(
field(tok(Expression))
~ skip(tok(SubOp))
~ field(tok(Expression))
~ trailing
).rewrite: (left, right) =>
splice(
Expression(
Sub(
left.unparent(),
right.unparent()
)
)
)
94 changes: 94 additions & 0 deletions calc/CalcReader.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2024-2025 DCal Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package distcompiler.calc

import cats.syntax.all.given

import distcompiler.*
import dsl.*
import distcompiler.Builtin.{Error, SourceMarker}
import Reader.*
import distcompiler.calc.tokens.*

object CalcReader extends Reader:
override lazy val wellformed = Wellformed:
Node.Top ::= repeated(choice(Expression, AddOp, SubOp, MulOp, DivOp))

Number ::= Atom
AddOp ::= Atom
SubOp ::= Atom
MulOp ::= Atom
DivOp ::= Atom

Expression ::= fields(
Number
)

private val digit: Set[Char] = ('0' to '9').toSet
private val whitespace: Set[Char] = Set(' ', '\n', '\t')

private lazy val unexpectedEOF: Manip[SourceRange] =
consumeMatch: m =>
addChild(Error("unexpected EOF", SourceMarker(m)))
*> Manip.pure(m)

protected lazy val rules: Manip[SourceRange] =
commit:
bytes
.selecting[SourceRange]
.onOneOf(whitespace):
extendThisNodeWithMatch(rules)
.onOneOf(digit):
numberMode
.on('+'):
addChild(AddOp())
*> rules
.on('-'):
addChild(SubOp())
*> rules
.on('*'):
addChild(MulOp())
*> rules
.on('/'):
addChild(DivOp())
*> rules
.fallback:
bytes.selectOne:
consumeMatch: m =>
addChild(Error("invalid byte", SourceMarker(m)))
*> rules
| consumeMatch: m =>
on(theTop).check
*> Manip.pure(m)
| unexpectedEOF

private lazy val numberMode: Manip[SourceRange] =
commit:
bytes
.selecting[SourceRange]
.onOneOf(digit)(numberMode)
.fallback:
consumeMatch: m =>
m.decodeString().toIntOption match
case Some(value) =>
addChild(
Expression(
Number(m)
)
)
*> rules
case None =>
addChild(Error("invalid number format", SourceMarker(m)))
*> rules
Loading
Loading