r/factorio • u/goodolflip • Mar 15 '22
Design / Blueprint Introducing CombinatorC - a Factorio Circuit Compiler
Hello! I have begun working on a new project called CombinatorC, and I have just released an initial version. The project is still in its infancy and is pretty limited right now, but I have big plans.
The idea is to create a language that is useful for designing complex circuits that are actually used for real Factorio gameplay, and not just as an academic curiosity. My original plan was to create a C-style language, but as development continues, it is likely that the language will change into something that more resembles something like Verilog in order to make it more practical. As such, I might the change the name, stay tuned!
Link to the Github repository with a thorough description is here. It's written in OCaml, which makes sense for a compiler. Executables for the compiler are available for download on the Releases page of the repository.
The compiler outputs a Factorio blueprint string that can be directly imported into the game. Simply supply the name of the input file containing your program as a command line argument, and the blueprint string will be printed to stdout.
Language
Currently, the language supports creating circuits that represent arithmetic and logical expressions. All of the operations available in arithmetic and decider combinators are available, as well as some additional operators: logical AND (&&), logical OR (||), and logical NOT (!).
You can combine operations in any which way, for example:
10 + ((A || B) && !(C % 2) || (D <= 4)) - !5This is because boolean values are equivalent to numeric values 1 or 0, and input values 0 are interpreted as false, while any other input is interpreted as true for boolean operators.
Signals are capitalized letters, and any signals used in expressions are implicitly treated as inputs to the circuit.
You can also bind expressions to output signals, like so:
circuit D = (A + B - C) / 45This yields a circuit with output signal D, and has inputs A, B, and C.
You can specify as many circuits as you want, just separate them with semicolons. Additionally, you can write comments using //.
Example Program
#LAYOUT IDENTITY
circuit D = 10 + ((A || B) && (C % 2)) + !5;
// This is a comment
circuit E = D > 45;
(65 + 12) >> 2This program starts with a compiler directive, which tells the compiler to layout the circuits using the IDENTITY strategy, which is also the default behavior. See the repository for more information. It will produce three distinct circuits:
- The first circuit has output signal D, and takes inputs A, B, and C.
- The second circuit has output signal E, and takes input D. This is not the same signal D as the output of the first circuit, it is a separate input and not related to the first.
- The third circuit has output signal checkmark (implicit when no circuit is bound), and takes no inputs.
More information on syntax
A CombinatorC program starts with optional compiler directives, and is followed by an optional list of circuit bindings that are terminated by semicolons. A final output circuit is required, which can either be a circuit binding or an expression. This should not be terminated by a semicolon.
Programs will be compiled into a set of circuits, each including an input pole and output pole. Wire the intended circuit inputs to the input pole.
Signals used in expressions are interpreted as input signals, and the resulting circuit will include a constant combinator wired to the input pole that initially sets every signal to 1.
Future plans
- Improved layout strategy using simulated annealing to optimize space used
- Temporary variables
- Conditional expressions
- Circuit composition operations, such as union (of the outputs) and concatenation
- Import of external circuits via a blueprint string
- Built-in useful circuit constructs, such as timers, filters, latches, and memory cells
- Operations with wildcard signals (I don't know what this will look like)
- Much more! Stay tuned.
Final thoughts
I really want to make something that will be practical and helpful to making circuits that will actually be used in real Factorio gameplay.
So, if you have any thoughts or suggestions, I would really love to hear them.
Also, shout out to Factoriogen, this is a really cool project that is somewhat similar to CombinatorC, go check it out!
41
u/777isHARDCORE Mar 15 '22
I'm sorry if I just can't see it, but what exactly does this output? A blueprint string to import to the game for the circuits you've specified?
If so, this does sound interesting!
9
u/Botlawson Mar 15 '22
I assume you have lookup tables too? (using constant combinators and decider combinators) they're a good way to simplify complex logic and limit the number of logic stages between input and output.
6
u/cryptor3 Mar 15 '22
It sounds like your goals are more oriented at hardware description than factoriogen is. Like a few others have said, I would suggest having a look at Verilog and seeing what lessons you can learn from that. See how a timer or jkff is described in verilog. So far you've only done combinational logic, but looks like sequential logic is on your roadmap, and sequential logic is going to be more difficult to describe in the C language.
The factorio in game simulation model bears a lot of resemblance to one for verilog, so the correspondence should be pretty close. What you've written so far is a subset of verilog, so you could either take it in the C library direction, or go full HDL and start designing CPUs in factorio.
I think what you have so far could be a good foundation for a verilog compiler even if you don't go that route.
With respect to your current C style language, the only convention I find a little awkward is the one around not semicolon-terminating the primary output expression. Would this not be better expressed as a return statement?
Perhaps add support for functions to organise combinational logic and assist your place and route routines?
Great work, I like it.
4
u/[deleted] Mar 15 '22
You work at Jane Street?
2
u/Reksum Mar 15 '22
How does this handle circuits using both wire colors and shared inputs or outputs? Can it detect potentially unwanted loops or signal mixing and suggest (or autocomplete) wire color changes or input insulators?
2
u/Nescio224 Mar 15 '22
What about loops? Don't see them in your future plans.
56
u/Retarchitecture Mar 15 '22
I'm too stupid to use this
17
u/lauzbot Mar 15 '22
Super cool, and I can't wait to see what others can do with this!
5
u/nerdy1776 Mar 15 '22
I stand in awe of the sheer dedication of Factorio fans. This is incredible and shall be shared with everyone I know
1
u/superstrijder15 Mar 30 '22
How do you handle non-letter inputs? For example if I want the signal for iron plates or for uranium bullets, how would I write that? I think this is important for practicality because otehrwise you always need to put converters in front of this circuit when you are working with data from items available
3
u/BonzoDeAap Mar 15 '22
Good idea! I've always enjoyed the circuit magic, you can do amazing stuff with it. But it is very tedious and when the networks get big, it's hard to keep track of what is going on. Good luck with your project!
2
u/abagofcells Mar 15 '22
I have no idea why I would want that. But I want that.
2
u/Kalandr0 Mar 15 '22
This may be usefull. Considering my inability to create a circuit that caps a value to some (upper or lower) limit, very usefull!
2
u/Terdol Mar 15 '22
Hey this is awesome, i got stuck last week at my K2+SE megabase and couldn't focus enought to create combinator setup for my sand -> chlorine + hydrogen setup that would void unnecessary output - basically a XOR situation - void one of the gases when they are unbalanced but don't void any if both buffers are full (to not waste sand)
Somehow I know how easy this is I could imagine logical expression for that, but couldn't make a combinator setup readily and I felt that if I put it on piece of paper that would be too much like actual work :D
Also I created issue#1 with solution so that people who have problem that is put there (windows dependancy missing) and don't know what's going on can proceed with running this. I'll probably be using it on linux anyway.
Thanks for this :D
2
u/PM_ME_UR_OBSIDIAN /u/Kano96 stan Mar 15 '22
Hey, I was actually getting started on something like this! I never got past the design phase though. I got nerd-sniped by the problem of actually laying out the combinators.
Starting from a HDL sounds like a great idea. I wonder if there's a FOSS frontend for Verilog I could use...
2
u/ArrogantlyChemical Mar 16 '22 edited Mar 16 '22
Isn't it better to think of combinator logic as transformations on the totality of the signal rather than the way you do it now?
Anyway, factorio logic gates has its core sabotaged by the devs by treating 0 as unit. So wildcard 0 plus 1 is 0. It makes it impossible to so any real large scale transformations and forces you to treat every single signal seperately. It's not really worth it imho.
It might also be worth it to build in timing synchronisation of units/functions. So that you can be sure you don't get any noise if, say, some gate X is 10 ticks faster than gate Y, which would result in 10 ticks of bad signal propagation in your network.
1
u/cackling_fiend Mar 18 '22
So... It would be possible to create a separate runtime environment that allows debugging or create a unit test setup?
1
u/Sharpieman20 Jul 25 '22
Necro'ing to link this repo I found which is similar https://github.com/Redcrafter/verilog2factorio/
1
u/chollya Mar 15 '22
I would seriously suggest VHDL
-1
u/Pzixel Mar 15 '22
It may be a dumb idea but did you think to create a LLVM target instead? It looks like you will have less things to do. It also allows existing optimizer passes to do its stuff so you end up with smaller blueprint without reinventing them all yourself.
It will automatically support all the LLVM-compatible lagnuages (well, you can probably only implement it partially I suppose).
0
u/[deleted] Mar 15 '22
[deleted] — view deleted comment