GithubHelp home page GithubHelp logo

ast-pattern-matching's People

Contributors

austinmlv avatar krux02 avatar ryanc-signiq avatar udiknedormin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ast-pattern-matching's Issues

Pattern not actually lispRepr

In the documentation it says:

The of-branch is a tree that can at any time be generated via lispRepr on an arbitrary NimNode.

This isn't entirely true, when you call lispRepr you get the result with the nnk prefix.

Example API for supporting various types

As I promised in irc, here is a test file visualizing how I imagine such support for general object types and seq

I think the examples preserve fully the existing DSL, just adding three new patterns:
field: pattern, @[patterns] and *pattern

import unittest, macros

type
  H = enum HInt, HString

  A = ref object
    case kind: H:
    of HInt:
      i: int
    of HString:
      s: string
    players: seq[A]

proc hInt(i: int): A =
  A(kind: HInt, i: i, players: @[])

proc hString(s: string): A =
  A(kind: HString, s: s, players: @[])

macro matchLit*(typ: typed, litType: typed, f: untyped): untyped =
  discard


macro matchArgs*(typ: typed, f: untyped): untyped =
  discard

macro match*(obj: typed, args: varargs[untyped]): untyped =
  discard

macro matchtest(s: untyped, test: untyped): untyped =
  result = quote:
    test `s`:
      var expected {.inject.} = false
      `test`
      checkpoint "didn't match variant"
      check expected

template succeed: untyped =
  expected {.inject.} = true

suite "plan":
  matchtest "match variant field":
    var a = A(kind: HInt, i: 0)

    a.match:
    of HInt(i: `i`):
      check i == 0
      succeed
    else:
      discard

  matchtest "match variant literal":
    A.matchLit int, hInt
    A.matchLit string, hString

    var a = A(kind: HInt, i: 0)
    var b = A(kind: HString, s: "e")

    a.match:
    of 0:
      succeed
    else:
      discard

    b.match:
    of "f":
      discard
    of "e":
      succeed
    else:
      discard

  matchtest "match variant args with auto":
    A.matchArgs players

    var a = A(kind: HInt, i: 0, players: @[A(kind: HInt, i: 2, players: @[]), A(kind: HInt, i: 4, players: @[])])
    a.match:
    of HInt(HInt(2), HInt(4)):
      succeed
    else:
      discard

  matchtest "match normal objects":
    type
      Obj = object
        a*: int
        b*: int

    var a = Obj(a: 0, b: 0)
    a.match:
    of Obj(a: `a`, b: 0):
      check a == 0
      succeed
    else:
      discard

  matchtest "match seq":
    var a = @[0, 1, 2]
    a.match:
    of @[0]:
      discard
    of @[0, 1, `last`]:
      check last == 2
      succeed
    else:
      discard

    a.match:
    of @[`first`]:
      discard
    of @[0, *`last`]:
      check last == @[1, 2]
      succeed
    else:
      discard

Deprecated pragma placement

A deprecated pragma placement makes compiling ast-pattern-matching fail with a "invalid indentation" error. This is with the latest Nim release and Nim's devel branch. Older Nim releases only issue a warning.

I discovered this while installing the latest nimlsp Nimble package which uses ast_pattern_matching.

type Obj[T] = object {.inheritable.}

The above should be type Obj[T] {.inheritable.} = object

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.