Skip to content

Commit

Permalink
Merge pull request #37 from bliutech/18-utils-add-a-benchmark-to-coun…
Browse files Browse the repository at this point in the history
…t-the-number-of-boolean-operations

18 utils add a benchmark to count the number of boolean operations
  • Loading branch information
bliutech authored Jul 19, 2024
2 parents a48e61b + a967a57 commit 61bde0f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from unittest import TestCase

from parser.ast import Expr
from parser.parse import Parser
from utils.metrics import OpCounter


class TestCount(TestCase):
def test_counter(self) -> None:
p: Parser = Parser()
tree: Expr = p.parse(["!", "(", "A", "&", "!", "B", "|", "C", ")", "<EOF>"])
counter: OpCounter = OpCounter()
self.assertEqual(counter.getCount(), 0)

tree.accept(counter)

self.assertEqual(counter.getCount(), 4)
28 changes: 28 additions & 0 deletions utils/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import override

from parser.ast import VarExpr, NotExpr, ParenExpr, AndExpr, OrExpr
from parser.visitor import Visitor


class OpCounter(Visitor):
"""Counts the number of boolean operators visited"""

_count: int = 0

@override
def visitNotExpr(self, nex: NotExpr) -> None:
OpCounter._count += 1
nex.first.accept(self)

@override
def visitAndExpr(self, aex: AndExpr) -> None:
OpCounter._count += 1
aex.first.accept(self)

@override
def visitOrExpr(self, oex: OrExpr) -> None:
OpCounter._count += 1
oex.first.accept(self)

def getCount(self) -> int:
return self._count

0 comments on commit 61bde0f

Please sign in to comment.