MountainWest JavaScript 2014

Utiliza macros para extender la sintaxis y las funcionalidades de JavaScript

James Long  · 


Extracto de la transcripción automática del vídeo realizada por YouTube.

I'm James long I work for Mozilla I help developers write web apps and on the side I contribute to projects like sweet GS and as MJS and I pretty much like anything that has to do with low-level compiler suffer just cool stuff so if you go to my blogging

because the other stuff that I work on so I'm going to talk to you about sweet GS sweet GS is a macro system for JavaScript started at Mozilla about two years ago an intern named Tim Tim disney store started it and he did some fantastic work throughout

that summer he essentially was able to prove that this is possible in the past few years we've seen a whole lot of amazing contributions amazing features we've pretty much been able to just blow through a lot of academic papers and implement a lot

of really cool stuff so it's working really well and I've personally been impressed at the positivity of the reactions because usually Mackers have kind of a bad reputation and a lot of people have this weird taste in my mouth of them but people seem

excited about which makes me excited a few months ago I wrote this blog post stop writing JavaScript compilers make macros instead so if you saw it post I was one who wrote that and I was blown away again by the positive reception I was it got pretty popular

about 50,000 views there are some negative comments which is constructive and good but it proved to me that this is something that is something that people want and is something that really will enable you to do some awesome things JavaScript so let me let's

dig into a little bit I think sweet is a is a perfect name for for this because it either gives you the vision that you are drinking the sweet fluid of JavaScript and macros or you're on drugs so so when it takes there a few phases that your your program

goes through when you as you write code and to make it do something it needs to be parsed it needs to be compiled and it needs to be executed macros add another phase to this before all of that happens the expansion phase and this basically allows you to tell

the system certain patterns of code that you want to take and turn into something else and then the compiler or that the system will take that new code and then run through the last three phases so essentially it enables you you to do things like this you

can tell the system hey when you see a a VAR keyword and there's an object literal on the left-hand side I want to turn that into two bar statements which declare the variables x and y and set them to pasa x and pawpaws at why i'm sure that you recognize

we essentially just implemented abstractly the a basic variable D structuring which is in es6 of course I'm sure that you've you also see that the the first form of code is not valid JavaScript but because the expansion phase happens before any part

seen before any compiling happens we can take things that are completely invalid Java scripts and turn them into things that are valid JavaScript so if we take take a little bit of code this is just a function that replaces a space with dashes and then we

call it we're used to looking at it like this we kind of load this into our head and we simulate it in our head this is how we typically write code and recode this is how we should write and recode but we see that get name is called dash if I is called

what the result a serta replace is called within dash if I I'm in the result is passed to display name we're training training to recode this way and most most of you all should Rico this way if you're a compiler writer you typically could look

at code a little bit differently and it's true that you could say that macros are essentially hooks for a little mini compilers but it's actually much more than that which I'll I'll go into but let's let's look at this syntactically

what does this actually look like well there's the function token then there's a dash if I token then there's a left parenthesis then there's stir then there is a right parenthesis etc etc so you can kind of see this syntax as one long list

of tokens real real good macros are actually much more than just operating order list of tokens oh it wouldn't be that hard to somehow implement something in most languages that give you hooks into the compilot process where you received either a list

of tokens or a raw ASD and you mangled it and then you return it back and then it kept on doing it that's not very interesting it turns out in real-world practice that's incredibly hard to work with introduces all sorts of bugs and just really doesn't

work so a good macro system knows a little bit more about the code than just the syntactical structure so sweet GS knows about things like variable declarations so it actually knows the scope of every variable it knows about things called de limiters which

are any parentheses any curly brace or square brace so sweet jeaious actually works on a nested tree structure it doesn't work just on one long list of tokens essentially it works on tokens but there's a very very lightweight tree so what we get is

this lightweight ASC that sweet just can can work on now when I say ASC it's still really not even in ASC because it really doesn't represent of the true JavaScript semantics it's not grouped by semantics it's screwed by structure and this

means that we can still do all sorts of things that are are invalid JavaScript and turn them into valid JavaScript so this is what a macro definition looks like this is a macro do and you have for the sweet GS macros you have a list of rules and within those

rules you use pattern matching so if so this is the the first pattern in the rule so it checks to see if it matches this pattern and if it does match a pattern then it expands to the code inside the rule and so if we invoke do the first thing it does is it

checks if the left curly brace mattress which there is a left curly brace and then it checks if there is an expression within the curly brace and there is and then it checks the rightfully brace so it correctly matches and it returns the this rap this call

[ ... ]

Nota: se han omitido las otras 3.083 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.