136 points · by StylifyYourBlog · 11y ago · 61 comments
\\/"-
\_/ wren v0.0.0
> var F = new Fn { | x | F.call(x + 1) }
> IO.print(F.call(10))
Segmentation fault
This shows two things. (1) it's not yet a "safe" language implementation; (2) it doesn't support proper tail calls. Both are fixable, of course, but the lack of proper tail calls is a Bad Language Smell.Its syntax is also a little strange. The following isn't accepted by the parser:
var F = new Fn { | x | if (x == 0) { return x } else { return F.call(x - 1) }}
IO.print(F.call(10))
but the same file with extra newlines is accepted just fine: var F = new Fn { | x |
if (x == 0) {
return x
} else {
return F.call(x - 1)
}
}
IO.print(F.call(10))
It also makes me a little sad that var F = new Fn { 0 }
returns 0, while var F = new Fn {
0
}
returns null.Not yet, but it's getting there! I have to fix this:
// TODO: Check for stack overflow.
On line 335 of wren_vm.c. The actual fix is pretty straightforward. You just store the maximum stack height with each function and check that that doesn't overflow on a call. I just haven't gotten around to it.Wren should be pretty good about most other boundary case behavior. If not (and like this issue too!) it's a high priority bug. Your scripting language shouldn't crash!
> (2) it doesn't support proper tail calls. Both are fixable, of course, but the lack of proper tail calls is a Bad Language Smell.
That's correct. I have implemented TCO before. Wren's predecessor Finch supported them. Fibers in Wren already support an analogous feature.
My current thoughts are that I likely will support this, but it will be explicitly opt-in. Instead of having the tail call automatically be detected and eliminate the parent stack frame, you'll have some specific syntax to say "this is a tailcall".
This is for two reasons:
1. Most of the time, having a useful stack trace is really handy for debugging. For better or worse, stack traces are a standard tool for most users to understand when their code does something wrong.
Eliminating all tail calls hurts this with little benefit in the majority of cases where you aren't doing any recursion (mutual or otherwise) and will never blow the stack.
Of course, you can do clever things to regenerate stack traces while still doing general TCO, but that adds a lot of complexity and simplicity of implementation is an important feature.
2. With a language that has a non-sexpr syntax, it can be very hard for a user to tell which call actually is the tail call. Consider something like:
{
return (foo.bar + !baz).call ? some.thing : other / wise
}
Can you quickly identify if this code won't blow the stack or not? If a user comes along and makes some tiny change to that statement, can they be confident their program won't start failing on larger inputs?Given that, I think it's good to visibly call out "Hey, there's a tail call here. It's important!" in the source code. I'm not sure what the syntax will look like yet, but that's my intent.
var F = new Fn { | x | if (x == 0) { return x } else { return F.call(x - 1) }}
Designing a syntax that's simple, expressive, and beautiful is tricky! Coming up with one that's easy to compile with a single pass compiler is even more tricky.In this case, because you don't have a newline after the "|", it is looking for an expression body. "if" is a statement, not an expression. There's a bunch of ways this could be improved:
1. Certainly, the error message should be better!
2. We could make "if" an expression. We could make everything an expression for that matter, though there's some pros and cons there.
3. If could allow statement bodies in single-line functions.
> but the same file with extra newlines is accepted just fine:
Yup. This is probably the most unusual corner of Wren's syntax. It's documented here:
http://munificent.github.io/wren/syntax.html#blocks
The constraints I'm working with are:
1. I wanted a terse way to define single-expression members and functions that return their result.
2. I'm not a fan of implicit returns in larger bodies. I don't like the idea of methods implicitly leaking the result of their last expression even when that isn't intentional, and I think it tends to lead to less efficient bytecode.
So I tried to come up with a specific syntax for a single-expression body. Other languages use stuff like "=>", but I wanted to try to not add more punctuation.
Since newlines are already significant in the language, I figured I'd try using the newline after "|" or "{" as the signal.
So far, I personally like how it feels, but feedback from others like you is definitely helpful. If it rubs a lot of people the wrong way, it's easily changed.
> Eliminating all tail calls hurts this with little benefit in the majority of cases where you aren't doing any recursion (mutual or otherwise) and will never blow the stack.
In "Finding the Needle"[0], SPJ et al came up with a nice stack trace representation for GHC that handles recursion. Not sure if you've seen the paper, but it might be worthwhile if you haven't (section 4, specifically).
[0]: http://research.microsoft.com/en-us/um/people/simonpj/papers...
I'm not sure if this is the place for such a far-reaching discussion, but I've never understood this point. What are the cons to making everything an expression?
https://mail.python.org/pipermail/python-ideas/2008-Septembe...
1. Any grammatical production can be used in an expression.
2. Function/method bodies implicitly return the result of the last expression they contain.
I really like #1, though even expression-oriented languages limit this. There's still something akin to "operator precedence" at the "statement-list" expression level. If, for example, you look at Ruby's grammar, you'll see it has productions called "statements".
This is to rule out totally bizarre things like:
1 + while (foo) { ... }
for (for (for (...) {}) {})
So, even if you treat everything as an expression, there are still some limitations as to where different grammatical productions can appear.In practice, I think the main thing you gain (in a language with an imperative-style syntax like Wren, Ruby, etc.) is the ability to have the right side of a return or assignment be a branching statement. Stuff like:
var fizzBuzz = if (n % 15) {
"FizzBuzz"
} else if (n % 5) {
"Buzz"
} else if (n % 3) {
"Fizz"
} else n
The other statements are less useful. I'm not even sure what the result of a looping expression should be, but I'm certain that I don't like CoffeeScript's answer (an array of all of the elements, which often gets discarded!). Break and return statements have no useful results. Variable declaration statements make it really hard to reason about scope when used in the middle of an expression.All this means that I think loosening up the grammar to make every statement an expression is mostly only useful to make branching an expression.
The other half is having functions implicitly return the result of the last expression. I really like that in some functions for brevity, and really dislike it in others. This may not be rational, but creeps me out that lots of Ruby methods just leak random values.
For me, I'd rather have an explicit form for bodies that implicity return their result and have the default be to not do that. So that's what I did for Wren. A single-line body implicitly returns its result, but a multi-line one does not:
class Person {
name { "Fred" } // Implicit return.
sayHi {
IO.write("Hi, I'm Fred!")
// Returns null.
}
}
I like this more than always having an implicit return, so this addresses point #2 above. I'm not opposed to allowing branch statements to be expressions, but I worry that the confusion it causes may outweight the benefit, so I conservatively left it out for now.Having a statement/expression distinction also makes the bytecode compilation a bit simpler. It's easier to tell which things emit values and which don't, but that's not hard to fix.
For what it's worth, I absolutely love reading this sort of spanning descriptive comments. It makes the code that much more interesting and gives it a healthy dash of personality.
I used to work with a guy who'd have header files start with pagefuls of comments with ASCII diagrams, book references, complete design rationale and what not, only to follow with just a handful of rather terse declarations, which still would be immediately clear thanks to the preceding narrative. That was so damn beautiful.
[0] https://github.com/munificent/wren/blob/46c1ba92492e9257aba6...
Wren is small. Wren is clean. Wren is fast. Wren is class-based. Wren is a scripting language.
If something is big, slow and message I don't care if it solves my problem when nothing else can. The only bullet point that comes close is the one talking about it being 'class-based', but I almost never hearing someone say "moving the code to classes will solve this problem". If that it is class based why would I use it over (as mentioned in the second sentence) Lua (or any one of the scripting "languages" that are just javascript in tiny form where I don't have to do any learning)? It seems like an easy pass for the majority of people that only read the main page.
What problems do users that need an embedded scripting language have? Just guessing, but probably #1 is the difficulty of integrating it into their existing project (and ease of extendibility with my app). Followed by language compatibility, control over garbage collection, and docs so I can spend more time on my app and less time mucking with the scripting language. I am guessing that something like these are the pain points that the main page should talk about.
http://munificent.github.io/wren/qa.html#why-did-you-create-...
> If something is big, slow and message I don't care if it solves my problem when nothing else can.
This is true of a general purpose language. I'm sure most Ruby or Python users couldn't tell you the first thing about how big or complex MRI or CPython is. As long as it works, it works.
Embedded languages are a different beast. When you drop something like Lua or Wren into your application, you're taking a direct dependency on its source code. You will end up stepping into in your debugger, reading its comments, etc. You will be shipping the language implementation itself physically with your application.
When that's the case, having a small, clean implementation becomes a very important feature.
> The only bullet point that comes close is the one talking about it being 'class-based', but I almost never hearing someone say "moving the code to classes will solve this problem".
Syntax is about usability, not utility. It's almost never a question of "Can I do this using classes." Everything is Turing complete, so you could use Brainfuck as your scripting language. The key question is, "Is it easier, more enjoyable, and less error-prone to use classes?"
If you're writing OOP-style code, which people increasingly do to script apps, I believe the answer is yes.
Here's an example of that kind of object-oriented programming in Lua:
Account = {}
Account.__index = Account
function Account.create(balance)
local acnt = {} -- our new object
setmetatable(acnt,Account) -- make Account handle lookup
acnt.balance = balance -- initialize our object
return acnt
end
function Account:withdraw(amount)
self.balance = self.balance - amount
end
-- create and use an Account
acc = Account.create(1000)
acc:withdraw(100)
Here's the same example in Wren: class Account {
new(balance) { _balance = balance }
withdraw(amount) { _balance = _balance - amount }
}
// create and use an Account
var acc = new Account(100)
acc.withdraw(100)
I believe this is a real productivity and maintainability improvement, and productivity is exactly what scripting languages are for.> Just guessing, but probably #1 is the difficulty of integrating it into their existing project (and ease of extendibility with my app).
Yup! That's also a key requirement (and one I still have a lot of work to do on.)
Edit: looks like your original comment made it through, and this repeat may best be deleted. Keep up the good work.
In other words, will you have a more powerful C++ API on top of the plain old C API? Or is that a project for someone else, once Wren is a bit more mature?
I have played around with Lua for years, and I like it in theory, mainly because it has a nice implementation. The C API is especially nice (vs say Python).
But personally I think the language itself is ugly, and inferior to Python in almost every way. Python's classes are a bit annoying to me, but Lua's idioms for classes are even worse (and I never felt prototypes really lived up to their promise). I could never get myself to write anything more than toys with Lua; it was just cumbersome.
Wren looks like a really solid design. I think scripting languages went way too far to the "left" with prototypes and the dynamic behavior of classes (including JS, etc.). This seems a lot more sensible and usable. It looks a bit like Dart (AFAICT), which is probably no coincidence?
EDIT: I noticed that the embedding API is not fleshed out yet. Random drive-by ideas/questions:
- Are you going to model it after Lua as you did the parser? I don't think that's a bad place to start -- the stack-based model seems strictly superior to dozens of ad hoc methods like Python. And I believe Python itself is a lot cleaner than Perl, Ruby, v8, or the JVM. The Lua authors did a great job with this comparison paper: http://www.jucs.org/jucs_13_6/c_apis_in_extension
- I always wanted channels between C/C++ and a scripting language. That is, an idiomatic and supported way of running the scripting language would be in another thread. I think of it as a single-threaded "control plane" in a scripting language, and then multithreaded C/C++ for the "data plane". And you use Go-like channels to communicate between the two. I wrote something like this for Python and multithreaded C++ (which is a nice of making the GIL irrelevant). This can of course be implemented as an optional layer on top of a Lua- or Python-like API.
- I've only played around with this a little, but there is a newer CFFI package for Python: https://cffi.readthedocs.org/en/release-0.8/
It is a compromise between 1) the "old style" C APIs where you have to write tons of glue, and then it ends up being auto-generated with some tool that nobody understands like SWIG; and 2) the new style like ctypes or LuaJIT where you have to duplicate type information in your source code. If you mess up or the ABI changes then you get crashes.
CFFI has a model where you paste in incomplete C declarations and it safely generates a binding, which I think is quite nice.
I guess it is a little C-specific. Not sure how it would interact with C++. I think both C and C++ are important use cases.
For me and my fellow disciples, it would therefore be really nice to have a constructor syntax that does this implicitly. So instead of writing:
class Customer {
new(customerNumber, name, address) {
_customerNumber = customerNumber
_name = name
_address = address
}
}
We could just write something like: class Customer {
new(customerNumber, name, address)
}
Or, if you like Scala: class Customer(customerNumber, name, address) {}
I'm a Java programmer by day, and some thoroughly depressing fraction of the lines of code in my codebases are of the form: private final AmazingObjectOfSomeKind amazing;
private final BrilliantObjectOfSomeOtherKind brilliant;
private final CrappyObjectOfAnyKind crappy;
public Foo(AmazingObjectOfSomeKind amazing,
BrilliantObjectOfSomeOtherKind brilliant,
CrappyObjectOfAnyKind crappy) {
this.amazing = amazing;
this.brilliant = brilliant;
this.crappy = crappy;
}
Users of other languages quite rightly point and laugh at me for this. Now, Wren doesn't have those types cluttering up the place, nor does it have the field declarations, but it does still have unnecessary verbosity, and that verbosity provides crevices for bugs to hide in (add a parameter to a constructor, forget to add a corresponding assignment). It might be nice to add a dash of syntactic sugar to eliminate this problem.Another facet of this school's approach is that objects are generally immutable. So instead of:
class Account {
withdraw(amount) { _balance = _balance - amount }
}
We would write: class Account {
withdraw(amount) { new Account(_balance - amount) }
}
(Although in fact, a bank account might be one of the cases where we wouldn't do this, but never mind)That gets old as soon as you have a few fields. Like:
class Account {
withdraw(amount) { new Account(_accountNumber, _sortCode, _balance - amount, _accountType) }
}
There's another bit of Scala sugar that is nice here - an automatic copy method which takes keyword arguments to override particular fields. Like: class Account {
withdraw(amount) { copy(balance = _balance - amount) } // other fields implicitly copied
}
Might something like that might be useful enough to justify its weight?The everything-is-a method approach is also really neat. For example, it's neater than C#'s half-arsed approach where you can have public fields but it's not recommended. However, I find there should still be an easy way of creating public properties. It's useful when all you want is a structure holding a bunch of values instead of a full-featured class. From what I can see, the shortest way of creating a public property in Wren seems to be:
class Cat {
new { _name = defaultName }
name { _name }
name=(value) { _name = value }
}
That can be a lot of boilerplate, especially when you need lots of public properties. But that's just a minor nitpick, of course. I'm looking forward to actually trying this language, but I'm not sure about its level of completeness right now. It seems like everything is pretty complete, apart from some parts of the documentation.Yup! I picked "fiber" mainly because it's shorter.
> The `Fiber.run` method allowing you to completely wreck the stack seems a bit dangerous at first glance.
In most cases, you'll use "call", but "run" is the fiber equivalent of tail call elimination.
> However, I find there should still be an easy way of creating public properties.
I'm trying to keep things minimal, but, yes, I'll likely add something for that. Wren grew out of a prototype-based language called Finch and the latter has "property definitions" that let you define a private field and accessor in one go:
http://finch.stuffwithstuff.com/objects.html
> I'm not sure about its level of completeness right now.
A lot of language features are implemented and everything that's implemented has pretty solid tests. There's still a bunch of more minor stuff to do, but what it really lacks is real-world usage.
I've been hacking on it in isolation (because implementing the language itself is the real fun part for me), but an embedded scripting language needs to be embedded in some real apps to get the critical feedback needed to make it useful.
To make some progress there, I'm starting to hack together a little game engine using Wren (think something like Löve2D), but I haven't put much time into that yet.
Unfortunately it doesn't build under GCC on linux right now, so use clang if you want to try it out.
Can you file a bug? I know another user ran into some problems with this too, but I thought we'd flushed them all out.
At a high level, Wren and Squirrel have a lot of overlap. They're both dynamically-typed embedded scripting languages with an eye towards games and a syntax that tries to look more familiar to someone from the C tradition.
Some differences are:
- Wren is much younger. That means it's much less mature, but it also has less baggage and cruft.
- Squirrel is a good bit more complex than Wren in general. I'm trying really hard to keep Wren small and simple and have a minimal set of orthogonal, combinable features. Squirrel has a larger baked-in feature set.
This means squirrel sometimes has multiple ways to accomplish the same goal: generators and threads, classes and tables, etc. With Wren I'm trying to get as much mileage from as few features as I can. (For example, Wren uses fibers for error-handling.)
- Squirrels tables are pretty similar to JS objects and Lua tables, so some of Squirrel has that prototype-y feel. That's kind of cool, but it also brings in some stuff I don't like. For example, I think Squirrel has the problem that Lua and JS have where "this" may not be bound to the object that you think it is because it doesn't have a real method/function separation. In Wren, "this" does exactly what you want, even in closures.
- Squirrel is written in C++. Wren is pure C99.
- Squirrel is primarily ref-counted with an opt-in mark-sweep GC (to clean up cycles, I assume). Wren is straight mark-sweep (though I anticipate going to incremental at some point for latency).
- Squirrel uses 8-bit or 16-bit character strings. Wren uses UTF-8 (or, at least, it will as that gets fleshed out more).
If you want something stable and usable today with all the features you could want, Squirrel is probably a good fit. If you're looking for something that feels a little more minimal and hews to a more modern aesthetic, that's what I'm aiming for with Wren.
Awesome. My primary interest in Lua and Squirrel was to embed a scripting language in a C environment on a microcontroller. This is perfectly doable (and has been done before) but Lua is particularly attractive because it is written in ANSI C.
I think it is interesting how similar embedded systems devs and game devs view their code with an eye towards performance and size. It is nice because embedded systems typically don't use newer languages and the developers tend to never move away from C as the language of choice (sometimes it seems like everyone is stuck in the 80s or 90s).
http://i.imgur.com/XMZE04i.png
If it takes off for game engines then it could be used for scripting canvas/webgl games too.
More work needed to make the REPL more user friendly in HTML (emscripten uses browser prompts to get input when you follow basic instructions to output to HTML but could tweak to have a more user-friendly REPL experience).
Additionally you can do things like the what has been done with the emscripten port of Lua and supply wren with a bridge interface to work with the DOM directly. From that it's a short path to having
<script language="text/wren"> with a bootstrap script to parse them out and feed to the wren engine :-)
Of course if it were as easy as typing this out i'd be doing it already!
But this is actually a cool little language with well thought out features. I can see it being very useful for embedding.
Does it make me unusual to find classical object models shouldn't be considered a 'usual' object model?
The fact that you can classify things into a classification tree does not mean that the classification is how things actually work. If it so happens that classification trees work for a problem, you can build them with trait inheritance, but structural inheritance hasn't been that useful IME, so the marrying of both types with classical inheritance just leaves a bad taste in my mouth.
Whether you choose to define your classes in the form of (possibly intersecting) sets of objects having some particular trait, or set of traits, or in some taxonomic tree is fairly irrelevant. The difference is largely a matter of taste. I think the point Bob (munificent) is making here is that 'scripting languages' (whatever you consider those to be) generally don't give you anything.
I have used Swift quite a lot in 2014, and loved the way to do ranges. But it's the complete opposite of Wren:
1...8 includes BOTH 1 and 8. Wren would not include 8.
Is this an error in the documentation, or the actual design of the language?
In the beta of Swift, 1..8 was not including 8. But I really like a change in the final version where it instead is done using 1..<8
It makes it much easier to discern between the two;
1...8 1..8
vs.
1...8 1..<8
It's the actual design. It follows Ruby and (I think?) Perl. Personally "..." feels more naturally inclusive to me since it has one more "." than ".." and thus one more item in the result.
But I thought it would be more helpful to follow in the footsteps of existing languages. The Swift solution is definitely explicit but looks kind of ugly to my eyes.
Syntax design can be hard.
for (0 <= i <= 10)
which has obvious (and transparent) variants for making one or both bounds exclusive. For bonus points for (10 >= i >= 1)
etc. for looping downwards. You could have unbounded loops too for (0 < i)
where you can tell which bit is meant to be declaring a loop variable because it's always the one after the inequality. For extra points, make mathematicians happy with things like this: for (0 <= i < j < 10)
which does 45 iterations or for (1 <= i,j,k <= n)
which does n^3 iterations, where again you can tell which bits are meant to be declaring loop variables: all but the first and last. These are less transparent because they don't make it clear what order things should be done in; they might also introduce the temptation to introduce fancy syntax to control this, to skip to "the next value of i", etc., which might be complexity that doesn't pay its way.This plays pretty well with filtering as in list comprehensions:
for (1 < i < j < 100) where (isGood(i) && isBad(j)) { ... }
One possibly-killer drawback to this kind of thing: it makes for a nice for-loop syntax but there's a lot to be said for saying that for-loops are always just iterating over some sort of collection, and turning these into collections might again require more syntactic complication than they justify. E.g., the simplest would be something like this: for (i in (i : 0 <= i <= 10))
which feels painfully repetitititititive with all those "i"s and doesn't generalize well to the fancy versions with multiple variables (but maybe those are a bad idea anyway). It's not hard to think of ways to reduce the repetition but they have their own drawbacks and in any case this comment is too long already.[EDITED to add: All this only makes sense if your syntax allows for "chained" inequality operators. But it really should anyway. (It looks like Wren's doesn't at present, maybe because it's hard to square with the principle that infix operators are always just single-arg method calls in disguise.)]
for (<variable name> in <expression>) { ... }
That's it. Note that there's no special support for numeric ranges. Instead, ".." and "..." are just regular operator calls. The number type implements them to return a range object. That lets you do: for (n in 1..10) { ... }
But also enables: var range = 1..10
for (n in range) { ... }
And even: var items = ["a", "b", "c", "d"]
var slice = items[1..2]
Making the range a first-class object lets you get a lot of mileage out of using it in different places, which is nice. In fact, a range derives from the sequence base class, so you can also do: var evens = (1..10).where {|n| n % 2 == 0}Wren is a replacement for Finch to me. I gave it a new name mainly so that I can keep Finch around in case other people want to take it and do something with it. I don't have any intention to work on it anymore.
If you like it, that's great! I think it has some neat ideas but I'm personally not that keen on prototypes these days. Finch could really use someone to own it. If you want to adopt it and do cool stuff with it, by all means do so.