3 ms·
I've been advised to a write a preprocessor so that I can make a language that compiles to C. I'm mostly fine with C, except I want to make it more readable and
by jokoon 8y ago
I've been advised to a write a preprocessor so that I can make a language that compiles to C. I'm mostly fine with C, except I want to make it more readable and with a 'sweeter' syntax.
Features I want are:
* pythonic indent
* no trailing semicolon
* vector, hashmap and map containers
* immutable strings
- chriswarbo 8y ago> Features I want are: * pythonic indent * no trailing semicolon I would recommend against making up a completely new syntax, since there would be no support in any existing tools (parsers, linters, IDEs, formatters, ...). Instead, I'd suggest picking a general-purpose syntax with the properties you want (indentation for grouping, newlines for sequencing). One excellent example is called "sweet-expressions", which was originally invented for Scheme but can actually be used to represent any structured data (especially code): https://srfi.schemers.org/srfi-110 https://srfi.schemers.org/srfi-110 You can use tools like "sweeten" and "unsweeten" from https://sourceforge.net/projects/readable/files https://sourceforge.net/projects/readable/files to convert between sweet-expressions and s-expressions. S-expressions are just raw syntax trees, so "unsweeten" is basically a ready-made parser which you can pipe into any other program you like, e.g. linters, macro expanders, pretty-printers, etc. To make your language compile to C, you would need some way to "render" those syntax trees into C code. There are existing tools to do this, for example the C-Mera system can read in C syntax trees and write out C code https://github.com/kiselgra/c-mera https://github.com/kiselgra/c-mera Your preprocessor could then be as simple as `unsweeten | cm c` To add completely new features like immutable strings you would just need to define some representation for them in the syntax trees (or, if you want to make them the default for "double quotes" you should define an alternative representation for non-immutable strings), then write some find/replace macros to convert occurrences of this representation into an equivalent (but more verbose and elaborate) set of C syntax trees. Stick this find/replace step in between `unsweeten` and `cm` and you've got brand new syntax for very little work.
- citycide 8y agoHave you tried Nim? It has (almost?) everything you're looking for and compiles to C, C++ or JS. https://nim-lang.org https://nim-lang.org
- klibertp 8y ago> It has (almost?) No, it has all the features mentioned in GP. It's worth noting, though, that Nim is a higher-level language than Zig appears to be. Nim is garbage-collected, has an object system with multi-methods, has proper lambdas and higher-order functions, uses a kind of uniform access principle (`foo.func()` is the same as `func(foo)`, basically), has destructors and defer blocks, exceptions, iterators, generics, operator overloading, AST based (but procedural) macros and templates, a kind of type-classes (called concepts), built-in concurrency (thread pool) support, and more. I'm not sure how well it would work on microcontroller, for example, although its garbage-collector is tunable in terms of memory and times constraints. But for anything higher-level than that, Nim is a really nice language, which reads very similar to Python but is natively compiled and much faster (among other features). A quick example to back up the similarity claim: proc getMem() : tuple[total: int, free: int] = let (output, _) = execCmdEx("free") fields = output .split("\n")[1] .split() .filterIt(it != "") return (fields[1].parseInt(), fields[^1].parseInt()) Really worth taking a look at, if you want conciseness and performance without compromising readability.
- nimmer 8y agoIt works on microcontrollers just fine using the new GC or by disabling it.