457043fc2ec0e6528a1fe322208c4c4abdfe3937 — Emery Hemingway 1 year, 1 month ago db645e1
term: implement `==` for Term
4 files changed, 77 insertions(+), 2 deletions(-)

M src/dhall/binary.nim
M src/dhall/parse.nim
M src/dhall/term.nim
M tests/test1.nim
M src/dhall/binary.nim => src/dhall/binary.nim +2 -0
@@ 1,5 1,7 @@
import ./term

export term.`==`

import cbor, cbor/bignum, bigints

import std/algorithm, std/math, std/options, std/streams, std/tables

M src/dhall/parse.nim => src/dhall/parse.nim +2 -0
@@ 1,5 1,7 @@
import term

export term.`==`

import bigints, npeg, npeg/lib/uri, npeg/lib/utf8

import algorithm, math, options, parseutils, tables, unicode

M src/dhall/term.nim => src/dhall/term.nim +72 -1
@@ 184,7 184,7 @@ type
      textExpr*: Term
    of tBinding:
      key*: string
      val*, ann* : Term
      val*, ann*: Term

func parseBuiltin*(s: string): BuiltinKind =
  case s

@@ 226,3 226,74 @@ func parseBuiltin*(s: string): BuiltinKind =
  of "Sort": bSort
    raise newException(ValueError, "invalid builtin " & s)

func `==`*(x, y: Term): bool =
  if x.isNil and y.isNil: result = true
    if x.kind == y.kind:
      template eq(field: untyped): bool = x.field == y.field
      result = case x.kind
      of tVar:
        eq(varName) and eq(varIndex)
      of tBuiltin:
      of tUniverse:
      of tApp:
        eq(appFun) and eq(appArgs)
      of tLambda:
        eq(lambdaLabel) and eq(lambdaType) and eq(lambdaBody)
      of tPi:
        eq(piLabel) and eq(piType) and eq(piBody)
      of tOp:
        eq(op) and eq(opL) and eq(opR)
      of tList:
        eq(listType) and eq(list)
      of tSome:
        eq(someType) and eq(someVal)
      of tMerge:
        eq(mergeHandler) and eq(mergeUnion) and eq(mergeAnn)
      of tRecordType:
      of tRecordLiteral:
      of tField:
        eq(fieldRecord) and eq(fieldName)
      of tProject:
        eq(projectRecord) and eq(projectNames)
      of tProjectType:
        eq(projectTypeRecord) and eq(projectTypeSelector)
      of tUnionType:
      of tBoolLiteral:
      of tIf:
        eq(ifCond) and eq(ifTrue) and eq(ifFalse)
      of tNaturalLiteral:
      of tIntegerLiteral:
      of tDoubleLiteral:
      of tTextLiteral:
        eq(textChunks) and eq(textSuffix)
      of tAssert:
      of tImport:
        eq(importCheck) and eq(importKind) and eq(importScheme) and eq(
            importHeaders) and eq(importElements) and eq(importQuery)
      of tLet:
        eq(letBinds) and eq(letBody)
      of tAnnotation:
        eq(annExpr) and eq(annAnn)
      of tToMap:
        eq(toMapBody) and eq(toMapAnn)
      of tEmptyList:
      of tTextChunk:
        eq(textPrefix) and eq(textExpr)
      of tBinding:
        eq(key) and eq(val) and eq(ann)
      else: false
        # the rest are only intermediate representations

M tests/test1.nim => tests/test1.nim +1 -1
@@ 51,7 51,7 @@ suite "binary-decode":
          termBin = decodeFile(testDir / testBase & "A.dhallb")
          termTxt = parse txt
        check(termTxt.encode == termBin.encode)
        check(termTxt == termBin)
  block failure:
    let testDir = "dhall-lang/tests/binary-decode/failure"
    for testBase in dhallTests(testDir, ".dhallb"):