forked from nim-lang/pas2nim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpas2nim.nim
64 lines (60 loc) · 1.85 KB
/
pas2nim.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
#
# Pas2nim - Pascal to Nim source converter
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
import
strutils, os, parseopt, llstream, ast, renderer, options, msgs,
paslex, pasparse, pathutils, lineinfos
const
Version = "0.9"
Usage = """
pas2nim - Pascal to Nim source converter
(c) 2012 Andreas Rumpf
Usage: pas2nim [options] inputfile [options]
Options:
-o, --out:FILE set output filename
--ref convert ^typ to ref typ (default: ptr typ)
--boot use special translation rules for the Nim compiler
-v, --version write pas2nim's version
-h, --help show this help
"""
proc main(infile, outfile: string, flags: set[TParserFlag]) =
var stream = llStreamOpen(AbsoluteFile infile, fmRead)
if stream == nil: rawMessage(gConfig, errGenerated, "cannot open file" & infile)
var p: TParser
openParser(p, infile, stream, flags)
var module = parseUnit(p)
closeParser(p)
renderModule(module, infile, outfile)
var
infile = ""
outfile = ""
flags: set[TParserFlag] = {}
for kind, key, val in getopt():
case kind
of cmdArgument: infile = key
of cmdLongOption, cmdShortOption:
case key
of "help", "h":
stdout.write(Usage)
quit(0)
of "version", "v":
stdout.write(Version & "\n")
quit(0)
of "o", "out": outfile = val
of "ref": incl(flags, pfRefs)
of "boot": flags = flags + {pfRefs, pfMoreReplacements, pfImportBlackList}
else: stdout.writeLine("[Error] unknown option: " & key)
of cmdEnd: assert(false)
if infile.len == 0:
# no filename has been given, so we show the help:
stdout.write(Usage)
else:
if outfile.len == 0:
outfile = changeFileExt(infile, "nim")
infile = addFileExt(infile, "pas")
main(infile, outfile, flags)