16 ms·
Gravity – An embeddable programming language
- th3iedkid 8y agoanyone using this can illustrate , how they use this ?
- cmjqol 8y agoIt's used for their tech called Creo [1] . It looks a bit like Fuse Tools [2] , a tech designed for mobile designer that bypass OS Specific UI API to directly draw on the phone using the GPU , sort of similar to Flutter in core architecture. [1]- https://creolabs.com/ https://creolabs.com/ [2]- https://www.fusetools.com/ https://www.fusetools.com/
- 3rdAccount 8y agoSo a language that transpiles to C similar to Nim I guess? Could I use this on Windows and create small binary executables? Any examples of this?
- loa-in-backup 8y agoIt's a virtual machine running some intermediate representation. From github: "Gravity has been developed from scratch for the Creo project in order to offer an easy way to write portable code for the iOS and Android platforms. It is written in portable C code that can be compiled on any platform using a C99 compiler. The VM code is about 2K lines long, the multipass compiler code is about 3K lines and the shared code is about 2K lines long. The compiler and virtual machine combined add less than 200KB to the executable on a 64 bit system."
- SlowRobotAhead 8y agoI find in annoying as an embedded engineer that “embedded” now mean phones which are more powerful than the PCs most of used relatively recently. Embedded to me means 8-32bit micros for real devices. Not PCs that happen to fit in your pocket. 200kB is out for even a fairly serious cortex M4.
- actsasbuffoon 8y agoI don't think they're referring to the type of device when they say the language is embeddable. If you wanted to create an app (maybe a text editor) with support for user-defined plugins, that's a good scenario for hosting an interpreter, or embedding it.
- wtetzner 8y agoEmbedded here means "embedded in another program".
- SlowRobotAhead 8y agoHere I am looking for both!
- loa-in-backup 8y ago"Getting started" link from github readme points to 404. Edit: so does "language documentation" link.
- caspervonb 8y agoNot to rain on your parade here but, in what world is a interpreter "fast and efficient"? Embeddable, minimal footprint and zero external dependencies etc are all excellent real selling points here.
- weberc2 8y agoWell, it’s not hard to beat Python and Ruby and PHP these days; I wouldn’t be surprised if this were quite a lot faster than those languages (or at least the reference implementations). EDIT: Do downvoters think I’m wrong? Mine isn’t really a contentious point of view—-Python and Ruby and PHP have a lot of features that make optimization difficult. Python pays my bills, so please don’t interpret my remarks as language smugness.
- projectramo 8y agoI didn't downvote you but you waded into a holy war. The main charge that python and ruby launch at each other is that the other is slow, then it doesn't matter because server costs are cheaper than human costs, then something about time to maintain and add features are the true costs and so on. So when you mention anything about those languages being slow, and someone just walked out of a four day internet dispute on reddit...
- weberc2 8y agoLol, thanks for the explanation. Usually the Python people just say, "Yeah, Python is slow; just write the hotpath in C", which is somewhat disingenuous but they aren't exactly denying that Python is slow (which is why I assumed my point of view was pretty garden-variety).
- uryga 8y agoIIRC, the K language's interpreter is/was really fast because it's tiny and fits in the processor's cache.
- one_thawt 8y ago
- ainar-g 8y ago>null >dynamic typing >naked if-else bodies I'm sorry, but I just don't see how this improves on anything. How will GravLang make me a better, more productive programmer? How will it make my programs more robust? Don't get me wrong, as a hobby project it's fine, but as a "real" language it doesn't have any selling points to me.
- api 8y agoIt doesn't improve much on e.g. JavaScript or Python. That's not the point. The point is that it's a small fast zero-dependency embeddable language that could be used for e.g. scripting in games or small devices. Wren is another nice one: https://github.com/munificent/wren https://github.com/munificent/wren
- ainar-g 8y agoDon't we have already Lua for that? It even has a state-of-the-art JIT compiler. I am all in for competition, but I can't see how both of these languages could improve over Lua.
- lasagnaphil 8y agoAlthough LuaJIT is crazy fast, it can't be used on iOS, so we first need to compare this with the unJITted version of Lua. Lua is pretty simple and elegant, but has its woes on being too flexible and wonky (especially with nil and global variables). It also doesn't support OOP syntax directly, so everyone started to create their own wonky class system, which broke easily because of Lua's wonkiness. Gravity seems like a good embedding language packaging a nice OOP syntax with a lot of syntactic sugar (such as properties and access specifiers), so I think some UI/game developers would choose this over Lua.
- ainar-g 8y agoI see your point, although I would probably use something like mruby[1] if I wanted something embeddable and OO. [1] http://mruby.org/ http://mruby.org/
- mcguire 8y ago"It is a class-based concurrent scripting language with a modern Swift like syntax." So, it looks like every other language designed in the last 40 years? This is good?
- lasagnaphil 8y agoIn the realm of embedded languages (which is dominated by Lua), Gravity seems like a great improvement. A lot of game developers attach a small scripting language to their game engines because C++ isn't a good language for rapid game logic prototyping, and takes ages to compile. The majority of them use Lua for this purpose. Lua is certainly simple and elegant, but has its shortcomings (no direct support for classes, 1-based indexing, indexing a wrong field returns nil, can accidentally declare global variables on assignment, lack of syntactic sugar...) I've looked at other languages such as Angelscript, Squirrel, Wren, etc..., to use for game development, but I think Gravity is the cleanest and has the most features so far. I'm still curious about the performance though (If it runs about the same as non-JIT Lua I'll call it a success, given how Lua is much simpler than Gravity) Even if you aren't a game developer, scripting languages also has its use with UI development. The majority of UI developers use Javascript with a combination of web technologies, so that's one example. Even QT developers use a scripting language called Qt Quick (which also resembles Javascript a bit), because C++ can be too tedious for rapid prototyping. The author of Gravity have also developed this language for mobile UI Development, so it seems good for its intended usage. The language isn't particularily shining on new concepts, but I think it will be very useful for some of us.
- sitkack 8y agoThe semantics and capabilities of Gravity mirror those of Lua almost exactly. Class systems, there are literally hundreds to choose from, basic Object/Method support is built into the language. As for globals detection, http://lua-users.org/wiki/DetectingUndefinedVariables http://lua-users.org/wiki/DetectingUndefinedVariables and non-existent attribute lookup https://www.lua.org/pil/13.4.4.html https://www.lua.org/pil/13.4.4.html Anyone programing in Lua should be well versed in http://www.luafaq.org/gotchas.html http://www.luafaq.org/gotchas.html Gravity's class system is inflexible compared to Lua where it has Multiple Inheritance https://github.com/limadm/lua-oo https://github.com/limadm/lua-oo Prototypical OO https://github.com/nusov/lua-object https://github.com/nusov/lua-object or anything else you desire. As for extending syntax and semantics, http://lua-users.org/wiki/MetatableEvents http://lua-users.org/wiki/MetatableEvents provide a clean mechanism to do so. Lua coroutines are more powerful than Gravity's fibers. I was hoping that Gravity had pattern matching support, but it looks like it does not. Given that Lua has more capabilities than Gravity could you substantiate > Lua is much simpler than Gravity In what ways?
- tom_mellior 8y ago"The VM code is about 2K lines long, the multipass compiler code is about 3K lines and the shared code is about 2K lines long." I was surprised by these numbers so I checked, and this is highly misleading (or very much out of date). The parser alone (https://github.com/marcobambini/gravity/blob/master/src/compiler/gravity_parser.c https://github.com/marcobambini/gravity/blob/master/src/comp...) is over 2K lines, and with the lexer and the AST definitions the frontend of the compiler is already comfortably above 3K lines. Also, the VM (https://github.com/marcobambini/gravity/blob/master/src/runtime/gravity_vm.c https://github.com/marcobambini/gravity/blob/master/src/runt...) is about 2K lines, but then there is another 3K lines in another "runtime" file (https://github.com/marcobambini/gravity/blob/master/src/runtime/gravity_core.c https://github.com/marcobambini/gravity/blob/master/src/runt...). Still, it's not a huge project, and it might be interesting as a point of study for people interested in compilers (to bytecode) or bytecode interpreters. The input syntax looks pretty as well. Interestingly, https://github.com/marcobambini/gravity/blob/master/src/runtime/gravity_vm.c https://github.com/marcobambini/gravity/blob/master/src/runt... also serves as a nice illustration of the fact that if tabs are allowed in source code, people will mix them with spaces, resulting in code that looks bad unless you have the exact same tab width preferences as the author.
- davelnewton 8y agoI'm not sure how the parser and lexer/etc. line count matters for the VM line count. shrug Definitely something I'll be looking at for my embedded work; it may fill a gap in my toolchain. And boo-hiss tabs.
- filmor 8y agoParser and lexer matter for the multipass compiler.
- davelnewton 8y agoWhich isn't the VM, right?
- weberc2 8y agoI’m a little bummed about the class-based bit; I generally want structs/records, not classes. Specifically, I want to avoid inheritance altogether (yes I know I don’t have to use it, but I still have to interact with it when libraries or coworkers use it). Still, this is a neat little scripting language, and best of luck.
- developer2 8y agoOn the off chance you're not already acquainted, you might really like Go. There are no classes, thus struct types are the bread and butter. "Methods" are lightweight syntactic sugar combining structs and functions; structs effectively double as classes, but the scope and complexity they offer are perhaps easier to juggle compared to the typical object-oriented language. The inheritance mechanism, implemented via interfaces, is interesting because it stresses conformance to behavior rather than explicit relationships between types. If your Circle struct type walks and talks like a Shape, then it is implicitly usable as a Shape. It's functionally identical to explicitly defined inheritance between types in other languages, but there's something about the _feel_ of developing with Go's interfaces. You begin to think more about the simple behaviors that each type should exhibit, rather than drawing hard lines in the sand about the hierarchy of relationships. Circles and Squares don't extend a base Shape type. Rather, a Circle can calculate its surface area. A Square can do the same. Conveniently, it just so happens that any type that can calculate its area is promoted to being usable anywhere a Shape is expected.
- SlowRobotAhead 8y agoIs there a Go VM for 32bit embedded?
- developer2 8y agoThe parent I replied to made me think so strongly of Go that I forgot the context regarding embedded systems. A quick search shows support for ARM; beyond that, I don't know if any options exist.
- PezzaDev 8y agoI don't see how a new language can claim to fast and efficient when it does nothing to encourage data-oriented programming.
- creatornator 8y agoA while back I was interested in learning more about programming compilers, so I checked this project out. I actually added array map, filter, reduce, and sort methods to the language, which was fun and a learning process.
- evincarofautumn 8y agoThat’s great! You’re the kind of person the hobbyist programming language development community needs. I’d encourage anyone who’s interested in learning more about compilers to see how they can contribute to a new language—it’s a great learning experience, one less thing for the primary author to do (a programming language can be an enormous undertaking), and who knows, you may be helping to build the next big thing.
- stevekemp 8y agoI did the same thing recently, extending the "monkey" language which is introduced in "Writing An Interpreter In Go". Now it supports comments, file I/O, string-primitives, regular expression support and similar things: https://github.com/skx/monkey/ https://github.com/skx/monkey/ Updating the lexer/parser/evaluator was nice and simple once you understand how the various parts hook together. The downside is that if you want to create a standard-library for your language you need to spend a lot of time on it.
- yoz-y 8y agoSeeing the overall style of the documentation logos and copy, I am kind of surprised that embedding this from Swift is not covered.
- lasagnaphil 8y agoMost scripting languages provide only a C interface, because it is what most of the other languages are universally compatible with. C++ codebases can use "extern C", other compiled languages like Rust or Swift can use C interop (for example: https://developer.apple.com/documentation/swift/swift_standard_library/c_interoperability https://developer.apple.com/documentation/swift/swift_standa...)
- inputcoffee 8y agoAs a hobbyist I love seeing new languages. I look at their features and a few simple examples and immediately imagine they'll solve all the issues I have faced with other languages. I generally appreciate the massive amount work that goes into something like this. But when I want to work on a project, I am wary of using something new. I just know in my heart I'll set the wrong flag on the compiler and the binary will end up in a directory I can't find and it'll take me two days to figure out how to do it. My question is this: if the main strength of this language is that uses Swift-like syntax, and lets you develop native apps in Android and iOS, why not just make an Android compiler for Swift?
- heavenlyblue 8y ago"issues I have faced with other languages" Which ones, specifically?
- inputcoffee 8y agoI mean that in the generic sense when you grow a project (I use python mostly) and run into issues invariably with the size and complexity.
- amelius 8y agoSounds not so light-weight.
- deleted 8y ago[deleted]
- bakery2k 8y agoIs it possible to embed Gravity in my C/C++ project and safely run untrusted Gravity code? The Lua implementation supports the creation of sandboxes to run untrusted scripts, but this is a feature that other embeddable languages (e.g. Wren, Python) don't seem to support. I feel like this would be a basic requirement - or is it common to embed a scripting language without limiting what scripts are able to do?
- SlowRobotAhead 8y agoAgreed, on my searching a VM to run foreign code is limited. You need a system that can run compiled code in order to be efficient, and interpreted is much more common but then you run into FFI issues. As to serialization of an interpreted it’s annoying that on embedded I’d have to send the letters “f” “o” “o” “(“ “x” “)” “;” which obviously kills some of the point here. mJS does the VM and FFI part OK, but I didn’t like some of the way it handled JS types coming back to C, it wasn’t clear for example that a C defined value passed in would return as a signed JS/NaN until converted.
- stormbrew 8y agoI think the issue is really just that effective sandboxing is really really hard to pull off right without also severely limiting the capabilities of the language/environment in undesirable ways, even for simple applications. Given that most of these things are volunteer projects and making strong claims about security is dangerous when you can't back it up, it's a tricky space most projects just don't want to touch.
- kevin_thibedeau 8y agoTcl is the granddaddy of embeddable scripting languages. It has had safe sub-interpreters for untrusted code for quite a while. They were added back when it had a shot of being a contender against JavaScript and safe script evaluation was deemed necessary.
- rkeene2 8y agoTcl supports this, and many systems use it: https://www.tcl.tk/man/tcl8.6/TclCmd/safe.htm https://www.tcl.tk/man/tcl8.6/TclCmd/safe.htm
- vitriol83 8y agoCan anyone suggest a good embedded language with static or at least optional typing ? I feel this is a gap in the market for embedded languages.
- ainar-g 8y agoI wonder if the lack of strongly statically typed embeddable languages is due to an accident or a fundamental limitation of implementations of such languages. Is the type checker code too big? Is it too complex?
- vitriol83 8y agoWriting a type checker is conceptually much more specialised than just a scripting language. Certainly I have no idea how to write one!
- adwn 8y agoMoreover, designing the actual type system is at least as hard as implementing the type checker. Coming up with a static type system that is flexible/ergonomic, expressive/powerful, sound, easy to use, and also a good match for mutable state and imperative code is really, really difficult, as demonstrated by C, C++, and Java. A flexible/ergonomic dynamic type system is trivial in comparison. Soundness is handled at runtime when the exact value of each expression is known, and ease of use is almost a given.
- rurban 8y agoNot really. You just need to walk up both types until you find a match. It's similar to a simple eval matcher, which is about 10 lines in lisp. https://github.com/perl11/Bla https://github.com/perl11/Bla has typically the smallest and easiest to understand type inferencer and checker.
- PeCaN 8y agoIt's actually extremely easy, particularly if you're familiar with Prolog (the core algorithm is a couple lines in Prolog, so knowing how that works is useful for implementing it in other languages). I suspect the lack of statically typed scripting languages is more of a historical accident than anything.
- stinos 8y agoFor anyone who considers this interesting and/or looking for alternatives: apart from the well-known Lua there's also MicroPython [1] whos specs actually read a lot like the specs of Gravity but then some more (well, it's Python 3.5 minus a couple of features, also compiles with C99 compilers, is fairly small for being a Python, embeddable, more mature at this point, been tested on more platforms/architectures, ...). [1] http://micropython.org/ http://micropython.org/ https://github.com/micropython/micropython/ https://github.com/micropython/micropython/
- fundamental 8y agoMRuby[1] is another option in this space. From what I can tell the work for MRuby began around the same time as micropython, though micropython has seen more adoption. [1] http://github.com/mruby/mruby http://github.com/mruby/mruby
- ccccccccccccc 8y agomruby has a much higher adoption in Japan, but I don't think it ever really caught on outside of it
- PeCaN 8y agoI was surprised nobody mentioned mruby in this thread until now. It's tiny, super easy to embed, and Ruby is a fun language. Also, mruby has the benefit over most of these other languages of having had a recent thorough security audit[1]. 1. http://mruby.sh/201703270126.html http://mruby.sh/201703270126.html
- petre 8y agoIt's still quite huge compared to Lua, not to mention slower. And gems need to be enabled at compile time.
- SlowRobotAhead 8y agoJust a nitpick... but I’ve seen people who aren’t working on what i would consider typical embedded recommend eLua and MicroPython readily without ever mentioning the downsides that mPy needs 128k+ ROM and includes a ridiculous standard lib most of which you would never use on embedded, and eLua supports some Cortex m3 and is a dead project. These two you mentioned are more of people using Rasp Pi syyle embedded (Cortex m-series vs Cortex A/R series... small devices vs phone processors). The issue is everyone has this “great idea” for an embedded language to replace C which means device headers in the Lang, and that’s way too much to support. If the goal is to replace C imo it’s a waste of time. But there is a market IMO for an embedded lang that you can execute tests and scripts from C in a VM that can foreign function interface back to C functions. There is on that uses JS called mJS but I found the one of the guys that supports it to be extremely rude and unhelpful. Documention is lacking. You basically need to be an expert in JS to get started. And then they doubled their ROM use age from 23k to ~50k. But it does seem to be the best out there for the goals I stated. I’m still on the hunt.
- bitmapbrother 8y ago>Gravity syntax is designed to be familiar to people coming from C-like languages like Javascript, Swift, C++, C# and many more. We started working on this new language a year before Apple announced Swift and we were happily surprised to discovered how similar both syntax appear. Amusing how the language looks so much like Kotlin - a language released in 2011 and 3 years before Swift.
- vorg 8y ago> syntax is designed to be familiar to people coming from C-like languages like Javascript, Swift, C++, C# and many more Besides Kotlin, other such modern C-like languages are Java, Dart, Go, and Groovy. But because Gravity is dynamically typed, a better peer language comparison would be Javascript, Groovy, Dart, and Swift. Each of those languages was designed for a specific platform, i.e: * Javascript for the web in 1995 * Apache Groovy for the JVM in 2003 (still used for Gradle build scripts) * Dart for the JVM and for Javascript (and more latterly for Fucshia, Android, and iOS) in 2011 * Swift for iOS in 2014 It's good to see modern dynamically typed languages reject Python's indentation and Ruby's `end` keyword, and settle around curly braces for scoping.
- hajile 8y agoDart has always had optional static types. With 2.0, those be come mandatory. If you are familiar with Swift, then dart would be Swift with a GC rather than ref counting and without tuples (or rather, the reverse would be true as dart is older than Swift).
- carapace 8y agoBlank page with JS disabled.
- cmjqol 8y agoAt first look I love how the language is designed. Now I'm curious about the "embedded" part . Apple doesn't allow any VM or JIT to be embedded for iOS application. Either I'm missing something or else this tech is not compliant with Apple Security Guidelines.
- vortico 8y agoApple, always promoting and inspiring new technologies!
- Matthias247 8y agoAs far as I understand the bytecode is interpreted (not JITed to native instructions), which is afaik OK on iOS.
- bjoli 8y agoDoes anyone have numbers on how light it is? How much memory Does it need? How big is the library? Does it compare favourably to Lua or Guile? Guile shouldn't be hard to beat for size, but that's generally not what they try to achieve.
- pq0ak2nnd 8y agoAs an embedded programmer, it is still very hard to displace C and will be for some time. The two main reasons: the compilers are well-behaved, and the debug tools are very, very mature. One thing newcomers overlook is the importance of understanding what the compiler does to the code. C++ is notorious for being painfully unpredictable, and in the embedded world, that is a death sentence. mbed is learning this the hard way. On the other side, debug tools are critical. Unlike the decade long surge of web-related programming with its sophisticated GUI-based debugging, embedded is a completely different beast, and requires much more attention to detail since reprogramming costs are so high. Because C has such a robust hardware debugging ecosystem, it is a clear winner. It'll be a long time before anything knocks C down as #1 in the embedded space. Even as the definition of embedded stretches to include *nix variants (like Yocto), there will always be the squeeze to get rid of that and run bare metal with C.
- craftoman 8y agoSuch an underrated project. IMHO if this one was launched by Facebook or Google now things would be different like 15k+ plus stars on GitHub and tech news sites posting articles with titles like "a new game changer language" and stuff.