nirc.nim 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2023 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Nir Compiler.
  10. import ".." / ic / [bitabs, rodfiles]
  11. import nirinsts, nirtypes, nirlineinfos, nirfiles, cir
  12. proc view(filename: string) =
  13. let m = load(filename)
  14. var res = ""
  15. allTreesToString m.code, m.lit.strings, m.lit.numbers, m.symnames, res
  16. res.add "\n# TYPES\n"
  17. nirtypes.toString res, m.types
  18. echo res
  19. import std / [syncio, parseopt]
  20. proc writeHelp =
  21. echo """Usage: nirc view|c <file.nir>"""
  22. quit 0
  23. proc main =
  24. var inp = ""
  25. var cmd = ""
  26. for kind, key, val in getopt():
  27. case kind
  28. of cmdArgument:
  29. if cmd.len == 0: cmd = key
  30. elif inp.len == 0: inp = key
  31. else: quit "Error: too many arguments"
  32. of cmdLongOption, cmdShortOption:
  33. case key
  34. of "help", "h": writeHelp()
  35. of "version", "v": stdout.write "1.0\n"
  36. of cmdEnd: discard
  37. if inp.len == 0:
  38. quit "Error: no input file specified"
  39. case cmd
  40. of "", "view":
  41. view inp
  42. of "c":
  43. let outp = inp & ".c"
  44. cir.generateCode inp, outp
  45. main()