Giter VIP home page Giter VIP logo

quasiquotes's Introduction

quasiquotes 0.1

Blocks of non-python code sprinkled in for extra seasoning.

What is a quasiquote

An quasiquote is a new syntactical element that allows us to embed non python code into our existing python code. The basic structure is as follows:

# coding: quasiquotes

[$name|some code goes here|]

This desuagars to:

name.quote_expr("some code goes here", frame, col_offset)

where frame is the executing stack frame and col_offset is the column offset of the quasiquoter.

This allows us to use slightly nicer syntax for our code. The # coding: quasiquotes is needed to enable this extension. The syntax is chosen to match haskell's quasiquote syntax from GHC 6.12. We need to use the older syntax (with the $) because python's grammar would be ambiguous without it at the quote open step. To simplify the tokenizer, we chose to use slighly more verbose syntax.

We may also use statement syntax for quasiquotes in a modified with block:

# coding: quasiquotes

with $name:
    some code goes here

This desuagars to:

name.quote_stmt("    some code goes here", frame, col_offset)

The c quasiquoter

The builtin c quasiquoter allows us to inline C code into our python. For example:

>>> from quasiquotes.c import c
>>> def f(a):
...     with $c:
...         printf("%ld\n", PyLong_AsLong(a));
...         a = Py_None;
...         Py_INCREF(a);
...     print(a)
...
>>> f(0)
0
None
>>> f(1)
1
None

Here we can see that the quasiquoter can read from and write to the local scope.

We can also use mutli-statement expressions:

>>> def cell_new(n):
...     return [$c|PyCell_New(n);]
...
>>> cell_new(1)
<cell at 0x7f8dde6cd5e8: int object at 0x7f8ddf956780>

Here we can see that the c quasiquoter is really convenient as a python interface into the C API.

quasiquotes's People

Contributors

llllllllll avatar bachmann1234 avatar

Watchers

 avatar

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.