Doesn't DMD as a library already exist without an external dub project?
Andrew Edwards
edwards.ac at gmail.com
Sun Sep 6 11:55:17 UTC 2020
My naïveté often gets the best of me, so if I seem to be jumping
off the deep end, please pull me back before I hurt myself.
I've been wondering why so much effort is necessary to make DMD
available as a library. Isn't DMD already a library? If you
remove the module containing main(), all you've got left are
library files. So why not merely expose them to the masses and
call it a day?
Thanks to @alphaglosined on IRC, I started thinking that to make
dmd accessible as a library, all that's required is to provide a
libdmd.a file in the appropriate directory and expose the dmd
directory for use with import statements.
Today I did just that to gain access to the lexer and can use it
without any problems.
Here's what I did:
1) modify dmd.conf to include -I%@P%/../../src/dmd
2) navigate to src/dmd/dmd and execute the command
dmd -lib -J../../../src -of=libdmd.a {root/*, console, \
entity, errors, filecache, globals, id, identifier, \
lexer, tokens, utf, utils, astbase, parse, transitivevisitor, \
permissivevisitor, strictvisitor}.d
2) move the file to the lib folder: mv libdmd.a
~/dlang/dmd-2.093.0/osx/lib/
The result? You be the judge:
(dmd-2.093.0)edwarac at rukongai ~> dmd diced //[1]
(dmd-2.093.0)edwarac at rukongai ~> ./diced diced.d
pragma
(
lib
,
"dmd"
)
;
import
dmd
.
lexer
;
import
dmd
.
tokens
;
import
std
.
file
;
import
std
.
stdio
;
import
std
.
conv
:
to
;
void
main
(
string
[
]
args
)
{
args
=
args
[
1
..
$
]
;
if
(
args
.
length
>
1
)
{
writeln
(
"Usage: diced [script]"
)
;
}
else
if
(
args
.
length
==
1
)
{
runFile
(
args
[
0
]
)
;
}
else
{
runPrompt
(
)
;
}
}
void
runFile
(
string
path
)
{
path
.
readText
.
run
;
}
void
runPrompt
(
)
{
for
(
;
;
)
{
write
(
"> "
)
;
auto
line
=
stdin
.
readln
(
)
;
if
(
line
is
null
)
break
;
run
(
line
)
;
}
}
void
run
(
string
source
)
{
scope
lex1
=
new
Lexer
(
null
,
source
.
ptr
,
0
,
source
.
length
,
0
,
0
)
;
TOK
tok
=
lex1
.
nextToken
(
)
;
while
(
tok
!=
TOK
.
endOfFile
)
{
writeln
(
lex1
.
token
.
toChars
(
)
.
to
!
string
)
;
tok
=
lex1
.
nextToken
(
)
;
}
}
Could it be that simple, or have I completely missed something?
The compiler frontend is missing from this demo, but it shouldn't
be too difficult to fix. So please let me know, other than that,
what have I missed/misunderstood?
--Andrew
[1] I could have used -L-ldmd here but chose to use a pragma
instead (both works)
More information about the Digitalmars-d
mailing list