TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Sweet.js: Hygienic Macros for JavaScript

84 pointsby philfover 12 years ago

3 comments

saurikover 12 years ago
Despite spending a lot of time both in the design Wiki and in the talk discussing the importance of being able to determine whether a / indicates a regular expression literal or a division operator entirely within the lexer (as opposed to using the parser, which is how JavaScript is generally defined), the algorithm that this developer implemented does not actually work.<p>First off, an example where it works:<p><pre><code> a /5/ 7 </code></pre> If you run this through sjs you get:<p><pre><code> a / 5 / 7; </code></pre> This is because, in JavaScript, statements continue across line boundaries until they are either explicitly terminated by a semicolon or a syntax error, in which case the parse is retried at that point as if a semicolon had been provided. In this case, that means we have a single statement that is a division of these three expressions: a, 5, and 7.<p>However, let's take a more difficult case:<p><pre><code> a = function() {} /5/ 7 </code></pre> This is also a single statement: you are entirely allowed to attempt to divide a function literal by a number, you will simply get the value NaN as output. If you take this file and run it through node, adding a "console.log(a)" to the end, that is in fact what you will get: NaN. However, when first run through sjs, you instead get "[Function]".<p>The reason is that sjs translated the code to:<p><pre><code> a = function () { }; /5/; 7; </code></pre> This is incorrect, and demonstrates how difficult some of these underlying issues are when parsing languages that have intertwined lexer and parser state. :( Attempting some other test cases involving regular expressions (but not semicolon insertion) also failed: it seems a lot more work will need to be done on this before it will be able to process general input (and it is not 100% clear to me that the shortcut required is even possible: I haven't thought enough about it yet to say for certain, however).<p>(I work on the JavaScript parser for a compile-to-JS language used by people doing jailbroken-iOS development for live introspection of running processes, and thereby that was the first thing I was interested in: how well the parser worked. ;P I have intentions to add reader macros, and then replace all of the extra Objective-C syntax I added with them, but I haven't gotten around to it yet. FWIW: I actually found and fixed a bug in my parser while writing this comment. ;P)
评论 #4561467 未加载
saurikover 12 years ago
There seemed to be some confusion during the question and answer segment regarding the relative hygiene of macros in Clojure near the end of the motivation and design talk; while it was totally off-topic for the video (and it thereby made sense to take it offline), I personally wish I had been around afterwards to ask the guy who seemed so adamant that syntax-case was fundamentally better than the Clojure solution (which he claimed didn't do it correctly) why that was the case.<p>I'm totally willing to believe it, but based on my understanding (which sadly is somewhat limited for Scheme, but fairly in-depth for Clojure) it isn't intuitive to me: it would seem like the way you escape hygiene in Clojure (which by default achieves correct hygiene by attaching namespaces to symbols read for macros or inside of quasi-quote) is quite similar in semantics--but simpler in practice due to being exceedingly less verbose--than using syntax-&#62;datum and datum-&#62;syntax.
评论 #4561462 未加载
SchizoDuckieover 12 years ago
OK, either your description page doesn't get your point across at all, or you're missing a definition somewhere.<p>'Wish the function keyword in JavaScript wasn't so long? What if you could define functions with def instead?'<p>Erh, no? Why in the lord's name would I ? Is that your big selling point? 'I don't like function() because it's too long?'<p>'Want a better way to make "classy" objects?'<p>&#60;provides example that looks like php vomited on Javascript after mating with ruby&#62;<p>Why would you want to make javascript less like javascript, introduce a dependency to javascript that can read your language, and then compiles back into javascript in realtime, in a way that will obviously make debugging nearly impossible (like coffeescript)?<p>Am I the only one that doesnt understand the use case of this? Or should this have been presented as just another lexer/parser?
评论 #4562518 未加载
评论 #4561899 未加载