From bab430316c0c5b88a59c1145a92c55bb55c7da11 Mon Sep 17 00:00:00 2001 From: mszulecki Date: Wed, 21 Feb 2007 17:18:01 +0000 Subject: First attempt at an autotools D helloworld. git-svn-id: http://svn.sukimashita.com/repos/dskel/trunk@2 4281df72-ff29-0410-8fee-2d9ac0c5f5a7 --- src/Makefile.am | 11 +++++++ src/helloworld.d | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/Makefile.am create mode 100755 src/helloworld.d (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..520df7e --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,11 @@ +SUFFIXES= .d + +.d.o: + $(GDC) $(DFLAGS) -c -o $@ $< + +bin_PROGRAMS = helloworld + +helloworld_SOURCES = helloworld.d +helloworld_LDADD = $(DEPS_LIBS) + +AM_CPPFLAGS = $(DEPS_CFLAGS) diff --git a/src/helloworld.d b/src/helloworld.d new file mode 100755 index 0000000..c3e6843 --- /dev/null +++ b/src/helloworld.d @@ -0,0 +1,93 @@ +/* sh style script syntax is supported */ + +/* Hello World in D + To compile: + dmd hello.d + or to optimize: + dmd -O -inline -release hello.d +*/ + +import std.stdio; + +void main(char[][] args) +{ + writefln("Hello World, Reloaded"); + + // auto type inference and built-in foreach + foreach (argc, argv; args) + { + // Object Oriented Programming + CmdLin cl = new CmdLin(argc, argv); + // Improved typesafe printf + writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv); + // Automatic or explicit memory management + delete cl; + } + + // Nested structs and classes + struct specs + { + // all members automatically initialized + int count, allocated; + } + + // Nested functions can refer to outer + // variables like args + specs argspecs() + { + specs* s = new specs; + // no need for '->' + s.count = args.length; // get length of array with .length + s.allocated = typeof(args).sizeof; // built-in native type properties + foreach (argv; args) + s.allocated += argv.length * typeof(argv[0]).sizeof; + return *s; + } + + // built-in string and common string operations + writefln("argc = %d, " ~ "allocated = %d", + argspecs().count, argspecs().allocated); +} + +class CmdLin +{ + private int _argc; + private char[] _argv; + +public: + this(int argc, char[] argv) // constructor + { + _argc = argc; + _argv = argv; + } + + int argnum() + { + return _argc + 1; + } + + char[] argv() + { + return _argv; + } + + char[] suffix() + { + char[] suffix = "th"; + switch (_argc) + { + case 0: + suffix = "st"; + break; + case 1: + suffix = "nd"; + break; + case 2: + suffix = "rd"; + break; + default: + break; + } + return suffix; + } +} -- cgit v1.1-32-gdbae