krux02 / ast-pattern-matching Goto Github PK
View Code? Open in Web Editor NEWA library to do pattern matching on the AST in the Nim programming language.
A library to do pattern matching on the AST in the Nim programming language.
In the documentation it says:
The of-branch is a tree that can at any time be generated via
lispRepr
on an arbitraryNimNode
.
This isn't entirely true, when you call lispRepr you get the result with the nnk
prefix.
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
arg.matchAst:
of nnkSym:
arg.matchAst:
of ident"float32":
return "float"
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.
The above should be type Obj[T] {.inheritable.} = object
basically this doesn't work:
result = @[]
fragmentPart.matchAstRecursive:
of `sym` @ nnkSym:
result.add sym
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.