aebabis / factorio-state-machine

MIT License
15 stars 8 forks source link

never compile #28

Closed crazyiop closed 2 years ago

crazyiop commented 4 years ago

Hi, This project is exactly what I needed, but I cannot manage to compile my state machine. I got some 'out of memory' error during my testing but I'm not sure it was not because of a mistake of mine. I wanted to try to run the code locally, so that I can try to dirty-hack it, but cannot figure how to do so. Some instruction about that would also be greatly appreciated !!

for reference here is what I tried to compile:

// City block spiral expander, to use with recursive blueprint
// G as input: from ghost reader mod
// output X, Y: offsets of blueprint, O: bp to print from book
10:
  W = 100 // bp width
  H = 50 // bp height
  L = 1 // current side lenght
  C = 1 // current cell on side (1..L)
  D = 0 // current direction
  X = 0
  Y = 0
  => 30
30: // handle one cell
  // TODO might need some step to clear area first
  O = 1
  D = D % 4
  => 40
40: // wait construction started
  G != 0 => 50 // ghost reader provided signal
50: // wait construction ended
  G == 0 => 60
60: // loop through the blueprint of the book
  O = O + 1
  O == 3 => 90
  => 40
90: // update directon, side lenght, x, y
  D == 1 => 110
  D == 2 => 120
  D == 3 => 130
  => 140
110:
  X = X - W
  => 150
120:
  Y = Y + H
  => 150
130:
  X = X + W
  => 150
140:
  Y = Y - H
  => 150
150:
  C = C + 1
  C > L => 160
  => 30
160:
  C = 1
  D = D + 1
  D % 2 == 0 => 170
  => 30
170:
  L = L + 1
  => 30
aebabis commented 4 years ago

Hi, I don't have time to look at this today, but I just wanted you to know that I saw this and will try to troubleshoot this soon

crazyiop commented 4 years ago

Cool :)

don't worry about that, take all the time you need/want !

aebabis commented 4 years ago

Hey @crazyiop, I tried adding your code incrementally, and it got slower and slower as I went.

My guess is the compiler has an infinite loop when making a state graph, but I wasn't able to test this theory because console.log statements weren't printing for some reason. It's been a long time since I've worked on this, so I can't remember why that would happen (my guess is Webpack-related). Any ideas?

crazyiop commented 4 years ago

Thanks for trying @aebabis .

I did not even successfully run it locally (I'm no web/js dev :p) so I did not really test anything. But I kind of found a workaround: as I also found that it is the size that was the implicit limit:

I compiled the code by parts (3 blocks). I just need to have the missing dummy redirection like :

(outside of block):
     => start of block

so all the blocks compiles. Then red wire each block together, remove the few 'init' combinator that may be in duplicate and remove the combinator that translate the dummy redirection. To ease debug/incremental tests, I spaced each block to have room for added instruction without recompiling everything.

So it's a bit of extra step, but still sooooo cool to have such complex circuit working from a few line of code :grinning: !!

bathtubed commented 4 years ago

I think I may have fixed this issue with my PR #29 . When I stepped through my large program I found that it out-of-memory errored on the parsing step alone, so I just optimized the grammar definition to include a tokenizer. It will now compile this example almost instantly.