Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nimpretty destoryes valid nim code #24508

Open
BlackLotus opened this issue Dec 4, 2024 · 1 comment
Open

nimpretty destoryes valid nim code #24508

BlackLotus opened this issue Dec 4, 2024 · 1 comment

Comments

@BlackLotus
Copy link

Description

So I'm trying to learn nim right now using the AOC. Got many problems with nim, but this was one of the most buggy ones

When running nimpretty on valid running code it can alter the code execution or make it not work at all.

Example

So this is not the most beautiful code, but nimpretty surely doesn't help

import nre
import strutils
import strformat

# look for xmas
#let inp="""MMMSXXMASM
#MSAMXMSMSA
#AMXSXMAAMM
#MSAMASMSMX
#XMASAMXAMM
#XXAMMXXAMA
#SMSMSASXSS
#SAXAMASAAA
#MAMMMXMMMM
#MXMXAXMASX"""

let inp = readFile("input")

let lines = inp.split("\n")

var xmascount = 0

let pattern = re"XMAS"
let pattern2 = re"SAMX"

echo "Lines"
# so dann suchen wir mal
# als erstes vorwaerts
for line in lines:
  echo line.findAll(pattern)
  echo line.findAll(pattern2)
  xmascount += len(line.findAll(pattern))
  xmascount += len(line.findAll(pattern2))

echo "Spalten"
#var spalten = seq[string]
#var spalten: seq[string] = @[]
# als naechstes spalten
echo(fmt"Zeilenlaenge ist {len(lines[0])}")
for i in 0..len(lines[0])-1:
  var neuezeile = ""
  for line in lines:
    if len(line) == len(lines[0]):
      neuezeile = fmt"{neuezeile}{line[i]}"
  echo neuezeile.findAll(pattern)
  echo neuezeile.findAll(pattern2)
  xmascount += len(neuezeile.findAll(pattern))
  xmascount += len(neuezeile.findAll(pattern2))

# und jetzt die... *schluck* diagonalen
# 4x

echo "Vertikal"
# jetzt iterieren wir ueber alle drueber wir fangen von unten links an
# (10,0), (9,0)(10,1), (8,0)(9,1)(10,2) ....
# ich gehe hier mal davon aus, dass es mehr zeilen gibt als spalten
#(x,y) -> x darf niemals groesser sein als len(lines) und y darf niemals groeeser sein als len(lines[0])
for i in countdown(len(lines)-4, -len(lines[0])+4):
  var start = i
  var y = 0
  if i < 0:
    y = -1*i
    start = 0
  var vertikale = ""
  var vertikale2 = ""
  for j in start..len(lines)-y-1:
#     echo(fmt"({j},{y+j-start})")
    if len(lines) >= j and len(lines[j]) > y+j-start:
      vertikale = fmt"{vertikale}{lines[j][y+j-start]}"
      vertikale2 = fmt"{vertikale2}{lines[j][len(lines[0])-(y+j-start)-1]}"
#      echo(fmt"{j},{y+j-start}")
#      echo(fmt"{j},{len(lines[0])-(y+j-start)-1}")
#    if len(lines) >= j and len(lines[j]) > 0 and len(lines[j]) > len(lines[0])-1-(y+j-start) and  len(lines[0])-1-(y+j-start)>=0:
#      vertikale2 = fmt"{vertikale}{lines[j][len(lines[0])-1-(y+j-start)]}"
#      echo vertikale.findAll(pattern)
#      echo vertikale.findAll(pattern2)
#      echo vertikale2.findAll(pattern)
#      echo vertikale2.findAll(pattern2)
  xmascount += len(vertikale.findAll(pattern))
  xmascount += len(vertikale.findAll(pattern2))
  xmascount += len(vertikale2.findAll(pattern))
  xmascount += len(vertikale2.findAll(pattern2))
echo(xmascount)

Executed yields the right result. After nimpretty I get this

import nre
import strutils
import strformat

# look for xmas
#let inp="""MMMSXXMASM
#MSAMXMSMSA
#AMXSXMAAMM
#MSAMASMSMX
#XMASAMXAMM
#XXAMMXXAMA
#SMSMSASXSS
#SAXAMASAAA
#MAMMMXMMMM
#MXMXAXMASX"""

let inp = readFile("input")

let lines = inp.split("\n")

var xmascount = 0

let pattern = re"XMAS"
let pattern2 = re"SAMX"

echo "Lines"
# so dann suchen wir mal
# als erstes vorwaerts
for line in lines:
  echo line.findAll(pattern)
  echo line.findAll(pattern2)
  xmascount += len(line.findAll(pattern))
  xmascount += len(line.findAll(pattern2))

echo "Spalten"
#var spalten = seq[string]
#var spalten: seq[string] = @[]
# als naechstes spalten
echo(fmt"Zeilenlaenge ist {len(lines[0])}")
for i in 0..len(lines[0])-1:
  var neuezeile = ""
  for line in lines:
    if len(line) == len(lines[0]):
      neuezeile = fmt"{neuezeile}{line[i]}"
  echo neuezeile.findAll(pattern)
  echo neuezeile.findAll(pattern2)
  xmascount += len(neuezeile.findAll(pattern))
  xmascount += len(neuezeile.findAll(pattern2))

# und jetzt die... *schluck* diagonalen
# 4x

echo "Vertikal"
# jetzt iterieren wir ueber alle drueber wir fangen von unten links an
# (10,0), (9,0)(10,1), (8,0)(9,1)(10,2) ....
# ich gehe hier mal davon aus, dass es mehr zeilen gibt als spalten
#(x,y) -> x darf niemals groesser sein als len(lines) und y darf niemals groeeser sein als len(lines[0])
for i in countdown(len(lines)-4, -len(lines[0])+4):
  var start = i
  var y = 0
  if i < 0:
    y = -1*i
    start = 0
  var vertikale = ""
  var vertikale2 = ""
  for j in start..len(lines)-y-1:
#     echo(fmt"({j},{y+j-start})")
  if len(lines) >= j and len(lines[j]) > y+j-start:
    vertikale = fmt"{vertikale}{lines[j][y+j-start]}"
    vertikale2 = fmt"{vertikale2}{lines[j][len(lines[0])-(y+j-start)-1]}"
#      echo(fmt"{j},{y+j-start}")
#      echo(fmt"{j},{len(lines[0])-(y+j-start)-1}")
#    if len(lines) >= j and len(lines[j]) > 0 and len(lines[j]) > len(lines[0])-1-(y+j-start) and  len(lines[0])-1-(y+j-start)>=0:
#      vertikale2 = fmt"{vertikale}{lines[j][len(lines[0])-1-(y+j-start)]}"
#      echo vertikale.findAll(pattern)
#      echo vertikale.findAll(pattern2)
#      echo vertikale2.findAll(pattern)
#      echo vertikale2.findAll(pattern2)
  xmascount += len(vertikale.findAll(pattern))
  xmascount += len(vertikale.findAll(pattern2))
  xmascount += len(vertikale2.findAll(pattern))
  xmascount += len(vertikale2.findAll(pattern2))
echo(xmascount)

I get

$ nim r part1.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: used config file '/etc/nim/config.nims' [Conf]
......................................................................
/home/thomas/aoc/2024/04/part1.nim(68, 3) Error: nestable statement requires indentation

Nim Version

$ nim --version
Nim Compiler Version 2.0.8 [Linux: amd64]
Compiled at 2024-09-08
Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release
$ nimpretty --version
0.2

Current Output

No response

Expected Output

No response

Known Workarounds

No response

Additional Information

No response

@metagn
Copy link
Collaborator

metagn commented Dec 5, 2024

Unless I am missing something, the only issue seems to be that nimpretty confuses the indent after for j in start..len(lines)-y-1:, likely due to the unindented comment which should be ignored as in Nim's tokenizer.

As shown by the label there are other bugs in nimpretty, nph is more recently maintained.

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

No branches or pull requests

2 participants