Hello! So a few days ago we were having fun in #nim-offtopic (#offtopic in
Discord) and made this -
[https://play.nim-lang.org/#ix=2qcg](https://play.nim-lang.org/#ix=2qcg).
Then I thought "that's so cool!" and decided to try to make something even
**better**.
I present to you - **Nuglifier - the (not yet) ultimate Nim source code
uglifier /obfuscator!**
An example for what it can do (part of the full source file)
Before:
import math, strutils, parseutils, tables, strformat
type
MathFunction* = proc(args: seq[float]): float ## \
## Type of the procedure definition for custom math functions
MathCustomFun = object
# Number of arguments this function is allowed to be called with
argCount: int
# The function itself
fun: MathFunction
MathExpression = object
eval: Evaluator
input: string
len: int
ch: char
pos: int
Evaluator* = ref object
## Main instance of the math evaluator
hasFuncs: bool
hasVars: bool
funcs: TableRef[string, MathCustomFun]
vars: TableRef[string, float]
proc atEnd(expr: var MathExpression): bool =
expr.pos >= expr.len
Run
After (I can't paste zero-width space on Nim forum so it doesn't look as
indented and won't compile if you copy-paste)
#? replace(""," ") | replace("\t"," ")
iMP_orT math, strutils, parseutils, tables, strformat # import line
modified since imports are broken with case
ty_PE
Mat_HFu_Nc_TI_On* = pR_OC(aR_GS:
sEq[fL_OAT]): fL_OaT ##
\
Mat_hCusT_OMF_UN = oB_JE_CT
ar_gC_ouNt:inT
fUN:Mat_hF_UN_ctI_ON
MATh_Ex_pR_eSS_ION=ob_JE_CT
evAL:EV_aLU_AToR
iN_PUT:sT_RI_Ng
len: int
cH:cH_AR
pOS: int
EV_aLU_At_oR*=rEFoBJEct
ha_SF_UNCS:booL
ha_SV_ARS:bO_Ol
fUnCS: TAb_leR_eF[sT_rI_NG, MA_tH_Cu_St_OM_FUN]
vA_rs: TABL_eR_Ef[sTR_inG, flOAt]
procaTE_ND(eX_PR:vARMAt_HEXP_reSSi_oN):bO_ol=
eX_PR.pOS >= ex_Pr.lEn
pROCin_CPOS(eXpR:varMaTh_EXPR_Es_SI_ON,ad_dP_oS=1):cH_aR{.dI_SC_Ar_da_bLE.}=
exPR.pOs+=aDD_POs
eX_PR.cH=ifex_PR.aT_ENd():'\0'eL_SE:eXpr.inP_Ut[ex_pR.poS]
Run
Full examples of generated code are at
Case insensitivity -
[https://raw.githubusercontent.com/Yardanico/nuglifier/master/example_output/example_case.nim](https://raw.githubusercontent.com/Yardanico/nuglifier/master/example_output/example_case.nim)
Emojis -
[https://raw.githubusercontent.com/Yardanico/nuglifier/master/example_output/example_emoji.nim](https://raw.githubusercontent.com/Yardanico/nuglifier/master/example_output/example_emoji.nim)
# How does it work?
Right not it's pretty "simple" \- first of all, I used Nim's own lexer (took it
out of the compiler) to replace different symbols with new ones (e.g. change
their casing or replace them with emojis). If it replaces with emojis - right
now I do it the "dumb" way and just _haha SCFs go brrr_ \-
[https://nim-lang.org/docs/filters.html](https://nim-lang.org/docs/filters.html)
(Maybe I should look into using Nim compiler to parse AST to actually replace
symbols only without cheating with SCFs.)
After that I run my own small loop for indentation which changes it to one of 1
random characters (tab, zero-width space or normal space) for random depth.
Even if it's pretty simple right now, the results are already _very good_
Source code at
[https://github.com/Yardanico/nuglifier](https://github.com/Yardanico/nuglifier)
I'm open for feedback on this abomination, feel free to comment :)