Skip to content

Conversation

@Rudxain
Copy link

@Rudxain Rudxain commented Oct 10, 2022

Closes #2

  • memory simulator: includes "+/- x 256 removal" (wrap-around normalizer), "mutual-cancel", and "+/- 129+ swapper"
  • remove all ops after final . or , or ]
  • cell-reset normalization
  • cell-reset-after-mut recognizer: +++[-] -> [-] (doesn't apply to ,,,[-])
  • zero-before-loop elimination: includes consecutive loops (deferred)
  • infinite-loop tail-elimination (deferred)
  • pure-output loop: [...] -> [.] (deferred, and doesn't apply to [,,,])
  • compressor optimizer

@baris-inandi baris-inandi mentioned this pull request Oct 12, 2022
@baris-inandi baris-inandi self-requested a review October 12, 2022 15:15
@baris-inandi baris-inandi marked this pull request as ready for review October 16, 2022 17:07
@baris-inandi baris-inandi marked this pull request as draft October 16, 2022 17:13
@Rudxain
Copy link
Author

Rudxain commented Nov 24, 2022

I got kinda "mentally-blocked" at this point, so I'll write pseudo-code, then translate to Go. I think doing that might help me solve the algorithm faster

@Rudxain
Copy link
Author

Rudxain commented Apr 2, 2024

Thank you (everyone reading this) for your patience. I'm sorry for "abandoning" this project. Since I had to reinstall my OS, I didn't find time to install the Golang toolchain. I'll be back in ~2 months, if everything goes fine IRL (please read my profile for context)

@baris-inandi
Copy link
Owner

No problem!

Thank you (everyone reading this) for your patience. I'm sorry for "abandoning" this project. Since I had to reinstall my OS, I didn't find time to install the Golang toolchain. I'll be back in ~2 months, if everything goes fine IRL (please read my profile for context)

@Rudxain Rudxain changed the title Update minify.go revamp minifier Apr 15, 2024
@Rudxain Rudxain changed the title revamp minifier enhance minifier Apr 15, 2024
Rudxain added 2 commits April 15, 2024 18:39
- minor edits to comments
- write some pseudo-code to remember how to implement mem-simulator algorithm
@Rudxain
Copy link
Author

Rudxain commented Apr 18, 2024

Now that bfgo has support for loading "memory-dumps", some of the minifier's assumptions would be broken (such as zero-loop). I propose we add optimization levels (like a compiler). Here's a draft:

// Minification level
type Level int

// enum emulation
// https://stackoverflow.com/a/14426447
const (
	// only removes non-BF chars,
	// therefore it has maximum portability
	// across implementations.
	BASIC Level = iota

	// assumes code isn't a main program.
	// useful for "libraries" and "modules", such as subroutines.
	LIB Level = iota

	// assume all code will be run as-is
	MAIN Level = iota

	// same as MAIN, but with multi-pass enabled (slow)
	MAX Level = iota
)

I want to branch that into its own PR, as this PR is already blocking on multiple features

@baris-inandi
Copy link
Owner

As I mentioned in the updates at #5, I added -mem to debug bf.style. Initially, I thought it could be a feature on its own. However, since it is interpreter-only and that it does not really add much to the toolkit, I will remove it.

If we add bf.style inside bfgo, I will use memory-dumping internally without exposing the feature to the user so that the minifier is not affected. The -mem flag should be removed from main soon. Once I commit the change, the new minifier's initial assumptions will continue to be valid.

@Rudxain
Copy link
Author

Rudxain commented Apr 21, 2024

oh ok. But still, I believe it'll be useful to have (at least) 2 minification levels (main & lib), since not all BF code is meant to be executed as-is, but rather copy-pasted (inlined) into other programs.

Loading/dumping mem can be useful for users who want to debug their code, although REPL seems to be enough for simple cases, and there's websites that provide full-blown debuggers (even stepping!), so I agree with not implementing mem

@baris-inandi
Copy link
Owner

Memory-dumping is removed at main!

@Rudxain
Copy link
Author

Rudxain commented May 29, 2024

I got a new optimization idea! I named it "infinite-loop tail-elimination" (I added it to the task-list):

It consists of recognizing "obvious" ♾️-loops of the form [🧠] +0xff [.], where 🧠 is literally any arbitrary code, +0xff is (+/-)<256 times (assuming the mem-sim already normalized them), and . is repeated 0-or-more times.

If the +0xff[.] is at the very beginning of the main (not lib) program, then it's also assumed to be inf.

This allows the minifier to do transformations like these [ [>] + [...] + [>,] ] -> [[>]+[...]] (unreachable code elimination). We can't always remove the outer loop, because it may (indirectly) conditionally-execute the inf-loop, so we gotta be conservative here.

I'm aware [...] = [.], but that's a different optimization , and it also applies to [,,,]

@Rudxain
Copy link
Author

Rudxain commented Nov 23, 2024

Since the PR has been delayed for a long time, I'll defer ZBL and ILTE to a follow-up PR. I'll implement the mem-sim as it's high-priority

@Rudxain
Copy link
Author

Rudxain commented Sep 7, 2025

Now that I know "advanced" git, I'll cleanup the history by squashing and re-ordering commits. This will require a force-push because of the hash changes. I'll do it later, when I have free time, to think carefully about which commits to squash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better minifier

2 participants