# HG changeset patch # User Anders Johnsen # Date 1218378189 -7200 # Node ID d442ce39917c06f9abc1dc0401b9c0c31a967ae7 # Parent bb06ffcfb1705127a2f34a12f5ae333fa2259f5c Removed some of the stuff diff -r bb06ffcfb170 -r d442ce39917c dang/OptParse.d --- a/dang/OptParse.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,810 +0,0 @@ -/* -Copyright (c) 2007 Kirk McDonald - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ -/** - * Command-line option parsing, in the style of Python's optparse. - * - * Refer to the complete docs for more information. - */ -module dang.OptParse; - -import tango.io.Stdout; -import tango.text.Util : locate, locatePrior; -import tango.text.Ascii : toUpper; -import tango.stdc.stdlib : exit, EXIT_FAILURE, EXIT_SUCCESS; -import tango.text.convert.Integer : parse, toInt, toString = toString; -import tango.text.convert.Utf : toString, toString32; - -/* -Options may be in two forms: long and short. Short options start with a single -dash and are one letter long. Long options start with two dashes and may -consist of any number of characters (so long as they don't start with a dash, -though they may contain dashes). Options are case-sensitive. - -Short options may be combined. The following are equivalent: - -$ myapp -a -b -c -$ myapp -abc -$ myapp -ab -c - -If -f and --file are aliases of the same option, which accepts an argument, -the following are equivalent: - -$ myapp -f somefile.txt -$ myapp -fsomefile.txt -$ myapp --file somefile.txt -$ myapp --file=somefile.txt - -The following are also valid: - -$ myapp -abcf somefile.txt -$ myapp -abcfsomefile.txt -$ myapp -abc --file somefile.txt - -If an option occurs multiple times, the last one is the one recorded: - -$ myapp -f somefile.txt --file otherfile.txt - -Matches 'otherfile.txt'. -*/ - -bool startswith(char[] s, char[] start) { - if (s.length < start.length) return false; - return s[0 .. start.length] == start; -} -bool endswith(char[] s, char[] end) { - if (s.length < end.length) return false; - return s[$ - end.length .. $] == end; -} - -/// Thrown if client code tries to set up an improper option. -class OptionError : Exception { - this(char[] msg) { super(msg); } -} -// Thrown if client code tries to extract the wrong type from an option. -class OptionTypeError : Exception { - this(char[] msg) { super(msg); } -} - -/++ -This class represents the results after parsing the command-line. -+/ -class Options { - char[][][char[]] opts; - int[char[]] counted_opts; - /// By default, leftover arguments are placed in this array. - char[][] args; - - /// Retrieves the results of the Store and StoreConst actions. - char[] opIndex(char[] opt) { - char[][]* o = opt in opts; - if (o) { - return (*o)[0]; - } else { - return ""; - } - } - /// Retrieves the results of the Store action, when the type is Integer. - int value(char[] opt) { - char[][]* o = opt in opts; - if (o) { - return toInt((*o)[0]); - } else { - return 0; - } - } - /// Retrieves the results of the Append and AppendConst actions. - char[][] list(char[] opt) { - char[][]* o = opt in opts; - if (o) { - return *o; - } else { - return null; - } - } - /// Retrieves the results of the Append action, when the type is Integer. - int[] valueList(char[] opt) { - char[][]* o = opt in opts; - int[] l; - if (o) { - l.length = (*o).length; - foreach (i, s; *o) { - l[i] = toInt(s); - } - } - return l; - } - /// Retrieves the results of the Count action. - int count(char[] opt) { - int* c = opt in counted_opts; - if (c) { - return *c; - } else { - return 0; - } - } - /// Retrieves the results of the SetTrue and SetFalse actions. - bool flag(char[] opt) { - char[][]* o = opt in opts; - if (o) { - return (*o)[0] == "1"; - } else { - return false; - } - } -} - -// Options, args, this opt's index in args, name[, arg] -/// -alias void delegate(Options, inout char[][], inout int, char[], char[]) OptionCallbackFancyArg; -/// -alias void delegate(Options, inout char[][], inout int, char[], int) OptionCallbackFancyInt; -/// -alias void delegate(Options, inout char[][], inout int, char[]) OptionCallbackFancy; - -/// -alias void delegate(char[]) OptionCallbackArg; -/// -alias void delegate(int) OptionCallbackInt; -/// -alias void delegate() OptionCallback; - -/* -Actions: - * Store: name - * StoreConst: name, const_value - * Append: name - * AppendConst: name, const_value - * Count: name - * CallbackArg: dga - * CallbackVoid: dg -*/ -/// -enum Action { /+++/Store, /+++/StoreConst, /+++/Append, /+++/AppendConst, /+++/Count, /+++/SetTrue, /+++/SetFalse, /+++/Callback, /+++/CallbackFancy, /+++/Help /+++/} -/// -enum ArgType { /+++/None, /+++/String, /+++/Integer /+++/} - -ArgType defaultType(Action action) { - switch (action) { - case Action.Store, Action.Append, Action.Callback, Action.CallbackFancy: - return ArgType.String; - break; - default: - return ArgType.None; - break; - } -} - -/++ -This class represents a single command-line option. -+/ -class Option { - char[][] shortopts, longopts; - Action action; - ArgType type; - char[] name, argname; - char[] const_value; - - char[] default_string; - int default_value; - bool default_flag, has_default; - - OptionCallbackArg callback; - OptionCallbackInt int_callback; - OptionCallback void_callback; - - OptionCallbackFancyArg fancy_callback; - OptionCallbackFancyInt fancy_int_callback; - OptionCallbackFancy fancy_void_callback; - char[] helptext; - this( - char[][] shorts, char[][] longs, ArgType type, - Action act, char[] name, char[] const_value, - OptionCallbackArg dga, OptionCallback dg, - OptionCallbackInt dgi, - OptionCallbackFancyArg fdga, - OptionCallbackFancyInt fdgi, - OptionCallbackFancy fdg - ) { - this.shortopts = shorts; - this.longopts = longs; - this.action = act; - this.type = type; - this.name = name; - this.argname = toUpper(name.dup); - this.default_string = ""; - this.default_value = 0; - this.default_flag = false; - - // Perform sanity checks. - assert (name !is null); - switch (act) { - case Action.Store, Action.Append: - assert(type != ArgType.None); - break; - case Action.StoreConst, Action.AppendConst: - assert(type == ArgType.None); - assert(const_value !is null); - break; - case Action.Callback: - //assert(type != ArgType.None); - switch (type) { - case ArgType.String: - assert(dga !is null); - break; - case ArgType.Integer: - assert(dgi !is null); - break; - case ArgType.None: - assert(dg !is null); - break; - } - break; - case Action.CallbackFancy: - switch (type) { - case ArgType.String: - assert(fdga !is null); - break; - case ArgType.Integer: - assert(fdgi !is null); - break; - case ArgType.None: - assert(fdg !is null); - break; - } - default: - break; - } - this.const_value = const_value; - this.callback = dga; - this.int_callback = dgi; - this.void_callback = dg; - this.fancy_callback = fdga; - this.fancy_int_callback = fdgi; - this.fancy_void_callback = fdg; - } - char[] toString() { - int optCount = this.shortopts.length + this.longopts.length; - char[] result; - bool printed_arg = false; - foreach(i, opt; this.shortopts ~ this.longopts) { - result ~= opt; - if (i < optCount-1) { - result ~= ", "; - } else if (this.hasArg()) { - result ~= "=" ~ toUpper(this.argname.dup); - } - } - return result; - } - //enum Action { Store, StoreConst, Append, AppendConst, Count, SetTrue, SetFalse, Callback, CallbackFancy, Help } - void issue_default(Options results) { - // Only set the default if the option doesn't already have a value. - char[][]* val = this.name in results.opts; - switch (this.action) { - case Action.Store, Action.Append: - if (val !is null) return; - if (this.type == ArgType.String) { - results.opts[name] = [default_string]; - } else { - results.opts[name] = [.toString(default_value)]; - } - break; - case Action.StoreConst, Action.AppendConst: - if (val !is null) return; - results.opts[name] = [default_string]; - break; - case Action.SetTrue, Action.SetFalse: - if (val !is null) return; - if (default_flag) { - results.opts[name] = ["1"]; - } else { - results.opts[name] = ["0"]; - } - break; - default: - return; - } - } - // Does whatever this option is supposed to do. - void performAction(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { - int i; - if (this.type == ArgType.Integer) { - // Verify that it's an int. - i = parser.toOptInt(arg); - } - switch (this.action) { - case Action.Store: - results.opts[name] = [arg]; - break; - case Action.Append: - results.opts[name] ~= arg; - break; - case Action.StoreConst: - assert(arg is null, "Got unexpected argument for '"~name~"' option."); - results.opts[name] = [const_value]; - break; - case Action.AppendConst: - assert(arg is null, "Got unexpected argument for '"~name~"' option."); - results.opts[name] ~= const_value; - break; - case Action.Count: - assert(arg is null, "Got unexpected argument for '"~name~"' option."); - ++results.counted_opts[name]; - break; - case Action.SetTrue: - results.opts[name] = ["1"]; - break; - case Action.SetFalse: - results.opts[name] = ["0"]; - break; - case Action.Callback: - switch (type) { - case ArgType.String: - callback(arg); - break; - case ArgType.Integer: - int_callback(i); - break; - case ArgType.None: - void_callback(); - break; - } - break; - case Action.CallbackFancy: - switch (type) { - case ArgType.String: - fancy_callback(results, args, idx, name, arg); - break; - case ArgType.Integer: - fancy_int_callback(results, args, idx, name, i); - break; - case ArgType.None: - fancy_void_callback(results, args, idx, name); - break; - } - break; - case Action.Help: - parser.helpText(); - exit(EXIT_SUCCESS); - break; - } - } - /// Returns whether this option accepts an argument. - bool hasArg() { - return this.type != ArgType.None; - } - /// Sets the help text for this option. - Option help(char[] help) { - this.helptext = help; - return this; - } - /// Sets the name of this option's argument, if it has one. - Option argName(char[] argname) { - this.argname = argname; - return this; - } - Option def(char[] val) { - if ( - (this.type != ArgType.String || (this.action != Action.Store && this.action != Action.Append)) && - this.action != Action.StoreConst && this.action != Action.AppendConst - ) - throw new OptionError("Cannot specify string default for non-string option '"~this.name~"'"); - this.has_default = true; - this.default_string = val; - return this; - } - Option def(int val) { - if (this.type != ArgType.Integer || (this.action != Action.Store && this.action != Action.Append)) - throw new OptionError("Cannot specify integer default for non-integer option '"~this.name~"'"); - this.has_default = true; - this.default_value = val; - return this; - } - Option def(bool val) { - if (this.action != Action.SetTrue && this.action != Action.SetFalse) - throw new OptionError("Cannot specify boolean default for non-flag option '"~this.name~"'"); - this.has_default = true; - this.default_flag = val; - return this; - } - // Returns true if the passed option string matches this option. - bool matches(char[] _arg) { - dchar[] arg = toString32(_arg); - if ( - arg.length < 2 || - arg.length == 2 && (arg[0] != '-' || arg[1] == '-') || - arg.length > 2 && (arg[0 .. 2] != "--" || arg[2] == '-') - ) { - return false; - } - if (arg.length == 2) { - foreach (opt; shortopts) { - if (_arg == opt) { - return true; - } - } - } else { - foreach (opt; longopts) { - if (_arg == opt) { - return true; - } - } - } - return false; - } -} - -/++ -This class is used to define a set of options, and parse the command-line -arguments. -+/ -class OptionParser { - OptionCallbackArg leftover_cb; - /// An array of all of the options known by this parser. - Option[] options; - char[] name, desc; - /// The description of the programs arguments, as used in the Help action. - char[] argdesc; - private void delegate(char[]) error_callback; - - this(char[] desc="") { - this.name = ""; - this.desc = desc; - this.argdesc = "[options] args..."; - } - - /// Sets a callback, to override the default error behavior. - void setErrorCallback(void delegate(char[]) dg) { - error_callback = dg; - } - void unknownOptError(char[] opt) { - error("Unknown argument '"~opt~"'"); - } - void expectedArgError(char[] opt) { - error("'"~opt~"' option expects an argument."); - } - /// Displays an error message and terminates the program. - void error(char[] err) { - if (error_callback !is null) { - error_callback(err); - } else { - this.helpText(); - Stdout.formatln(err); - } - exit(EXIT_FAILURE); - } - int toOptInt(char[] s) { - int i; - uint ate; - i = .parse(s, 10u, &ate); - if (ate != s.length) - error("Could not convert '"~s~"' to an integer."); - return i; - } - - /// Displays useful "help" information about the program's options. - void helpText() { - int optWidth; - char[][] optStrs; - typedef char spacechar = ' '; - spacechar[] padding; - // Calculate the maximum width of the option lists. - foreach(i, opt; options) { - optStrs ~= opt.toString(); - if (optStrs[i].length > optWidth) { - optWidth = optStrs[i].length; - } - } - Stdout.formatln("Usage: {0} {1}", this.name, this.argdesc); - if (this.desc !is null && this.desc != "") Stdout.formatln(this.desc); - Stdout.formatln("\nOptions:"); - foreach(i, opt; options) { - padding.length = optWidth - optStrs[i].length; - Stdout.formatln(" {0}{1} {2}", optStrs[i], cast(char[])padding, opt.helptext); - } - } - - // Checks the passed arg against all the options in the parser. - // Returns null if no match is found. - Option matches(char[] arg) { - foreach(o; options) { - if (o.matches(arg)) { - return o; - } - } - return null; - } - char[] getBaseName(char[] path) { - version(Windows) { - char delimiter = '\\'; - } else { - char delimiter = '/'; - } - uint idx = locatePrior(path, delimiter); - if (idx == path.length) return path; - return path[idx+1 .. $]; - } - char[] getProgramName(char[] path) { - version(Windows) { - // (Unicode note: ".exe" only contains 4 code units, so this slice - // should Just Work.) (Although it remains to be seen how robust - // this code actually is.) - //Stdout.formatln(path); - //assert(path[$-4 .. $] == ".exe"); - //path = path[0 .. $-4]; - } - return getBaseName(path); - } - /// Parses the passed command-line arguments and returns the results. - Options parse(char[][] args) { - this.name = getProgramName(args[0]); - args = args[1 .. $]; - Options options = new Options; - /* - The issue is this: - - $ myapp -abc - - This might be three short opts, or one or two opts, the last of which - accepts an argument. In the three-opt case, we want to get: - - $ myapp -a -b -c - - In the one-opt case, we want: - - $ myapp -a bc - - In the two-opt case, we want: - - $ myapp -a -b c - - We also want to parse apart "--file=somefile" into "--file somefile" - */ - char[] opt, newopt, arg; - dchar[] opt32; - int idx; - Option match; - - for (int i=0; i= 2) { - opt32 = toString32(opt[1 .. $]); - foreach (j, c; opt32) { - newopt = .toString("-" ~ [c]); - match = matches(newopt); - if (match is null) { - unknownOptError(newopt); - } - if (match.hasArg) { - // This is the last char in the group, look to the - // next element of args for the arg. - if (j == opt32.length-1) { - if (i == args.length-1) expectedArgError(match.name); - arg = args[i+1]; - ++i; - // Otherwise, consume the rest of this group for - // the arg. - } else { - arg = .toString(opt32[j+1 .. $]); - match.performAction(this, options, args, i, arg); - break; - } - } else { - arg = null; - } - match.performAction(this, options, args, i, arg); - } - } else { - unknownOptError(opt); - } - } else { - if (this.leftover_cb is null) { - options.args ~= opt; - } else { - this.leftover_cb(opt); - } - } - } - foreach (o; this.options) { - o.issue_default(options); - } - return options; - } - - /++ - Overrides the default behavior of leftover arguments, calling this callback - with them instead of adding them an array. - +/ - void leftoverCallback(OptionCallbackArg dg) { - this.leftover_cb = dg; - } - /// - Option addOption(Option option) { - this.options ~= option; - return option; - } - // options action name type const_value dga dgv dgi fdga fdgi fdg - /// - Option addOption(char[][] options ...) { - return addOption(options, Action.Store, null, defaultType(Action.Store), null, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, char[] name) { - return addOption(options, Action.Store, name, defaultType(Action.Store), null, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, Action action) { - return addOption(options, action, null, defaultType(action), null, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, ArgType type) { - return addOption(options, Action.Store, null, type, null, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, Action action, ArgType type) { - return addOption(options, action, null, type, null, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, char[] name, Action action) { - return addOption(options, action, name, defaultType(action), null, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, char[] name, Action action, ArgType type) { - return addOption(options, action, name, type, null, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, Action action, char[] const_value) { - return addOption(options, action, null, defaultType(action), const_value, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, char[] name, char[] const_value) { - return addOption(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, char[] name, Action action, char[] const_value) { - return addOption(options, action, name, defaultType(action), const_value, null, null, null, null, null, null); - } - /// - Option addOption(char[][] options, OptionCallbackArg dg) { - return addOption(options, Action.Callback, null, ArgType.String, null, dg, null, null, null, null, null); - } - /// - Option addOption(char[][] options, OptionCallback dg) { - return addOption(options, Action.Callback, null, ArgType.None, null, null, dg, null, null, null, null); - } - /// - Option addOption(char[][] options, OptionCallbackInt dg) { - return addOption(options, Action.Callback, null, ArgType.Integer, null, null, null, dg, null, null, null); - } - /// - Option addOption(char[][] options, OptionCallbackFancyArg dg) { - return addOption(options, Action.CallbackFancy, null, ArgType.String, null, null, null, null, dg, null, null); - } - /// - Option addOption(char[][] options, OptionCallbackFancy dg) { - return addOption(options, Action.CallbackFancy, null, ArgType.None, null, null, null, null, null, null, dg); - } - /// - Option addOption(char[][] options, OptionCallbackFancyInt dg) { - return addOption(options, Action.CallbackFancy, null, ArgType.Integer, null, null, null, null, null, dg, null); - } - /// - Option addOption(char[][] options, char[] name, OptionCallbackFancyArg dg) { - return addOption(options, Action.CallbackFancy, name, ArgType.String, null, null, null, null, dg, null, null); - } - /// - Option addOption(char[][] options, char[] name, OptionCallbackFancy dg) { - return addOption(options, Action.CallbackFancy, name, ArgType.None, null, null, null, null, null, null, dg); - } - /// - Option addOption(char[][] options, char[] name, OptionCallbackFancyInt dg) { - return addOption(options, Action.CallbackFancy, name, ArgType.Integer, null, null, null, null, null, dg, null); - } - // Although users certainly /can/ call this, all those overloads are there - // for a reason. - Option addOption( - char[][] options, - Action action, char[] name, ArgType type, char[] const_value, - OptionCallbackArg callback, OptionCallback vcall, - OptionCallbackInt icall, - OptionCallbackFancyArg fdga, - OptionCallbackFancyInt fdgi, - OptionCallbackFancy fdg - ) { - char[][] shortopts; - char[][] longopts; - dchar[] opt; - Option option; - foreach (_opt; options) { - // (Unicode note: We convert to dchar[] so the length checks work - // out in the event of a short opt with a >127 character.) - opt = toString32(_opt); - if (opt.length < 2) { - throw new OptionError( - "invalid option string '" ~ _opt ~ "': must be at least two characters long" - ); - } else if (opt.length > 2) { - if (opt[0 .. 2] != "--" || opt[2] == '-') - throw new OptionError( - "invalid long option string '" ~ _opt ~ "': must start with --, followed by non-dash" - ); - longopts ~= _opt; - } else { - if (opt[0] != '-' || opt[1] == '-') - throw new OptionError( - "invalid short option string '" ~ _opt ~ "': must be of the form -x, where x is non-dash" - ); - shortopts ~= _opt; - } - } - if (name is null) { - // (Unicode note: We know '-' is a single code unit, so these - // slices are okay.) - if (longopts.length > 0) - name = longopts[0][2 .. $]; - else if (shortopts.length > 0) - name = shortopts[0][1 .. 2]; - else - throw new OptionError( - "No options provided to addOption!" - ); - } - option = new Option(shortopts, longopts, type, action, name, const_value, callback, vcall, icall, fdga, fdgi, fdg); - this.options ~= option; - return option; - } -} - diff -r bb06ffcfb170 -r d442ce39917c dang/compiler.d --- a/dang/compiler.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,360 +0,0 @@ -module dang.compiler; - -import tango.io.Stdout, - tango.core.Signal, - tango.core.Memory, - tango.sys.Process, - tango.time.StopWatch, - tango.text.Util, - tango.io.FileConduit, - tango.io.FilePath; - -import lexer.Lexer, - parser.Parser; - -import basic.SourceManager; - -import basic.Message; - -import ast.Module; - -import tools.AstPrinter, - tools.DotPrinter; - -import gen.CodeGen; - -import sema.Visitor, - sema.AstAction, - sema.BuildScopes, - sema.BuildSymbols, - sema.BuildTypes, - sema.CheckScopes, - sema.CheckTypes, - sema.LiteralInterpreter, - sema.VC, - sema.ObjectOriented; - -import tango.stdc.posix.unistd; -import tango.stdc.stdlib; - -import Opt = dang.OptParse; - -class NullAction : Action -{ -} - -void checkFiles(char[][] *files) -{ -// GC.disable(); - bool non_existant_files = false; - bool duplicate_files = false; - - char[][] validFiles; - - foreach (file; *files) - { - scope path = new FilePath(file); - - if (!path.exists) - { - Stderr.formatln("'{}' does not exist", file).newline; - non_existant_files = true; - - continue; - } - - bool fileInStack = false; - foreach (vFile; validFiles) - if (vFile == file) - { - fileInStack = true; - duplicate_files = true; - } - - if (fileInStack) - continue; - - validFiles ~= path.toString(); - } - - *files = validFiles; - - if (non_existant_files) - throw new Exception("All files given must exist"); - if (duplicate_files) - Stderr("warning: duplicate files ignored").newline; -} - -void main(char[][] args) -{ - char[][] filesToHandle; - - Signal!(char[][]*) preStart; - - Signal!(char[]) preLex; - Signal!(Lexer) postLex; - - Signal!(Lexer) preParse; - Signal!(Module[], SourceManager) postParse; - Signal!(Module[], SourceManager) postSema; - - preStart.attach(&checkFiles); - - auto argParse = new Opt.OptionParser(`Dang "D" compiler v0.0`); - - bool optimize = false; - bool inline = false; - - - SourceManager src_mgr = new SourceManager; - MessageHandler messages = new MessageHandler(src_mgr); - - argParse.addOption(["-h", "--help"], Opt.Action.Help) - .help("Show this help message"); - - argParse.addOption(["--ast-dump-dot"], - "what-to-do", Opt.Action.StoreConst, "dot") - .help("Output the AST in the dot format"); - argParse.addOption(["--ast-dump-code"], - "what-to-do", Opt.Action.StoreConst, "code") - .help("Output the AST as code"); - argParse.addOption(["--semantic-only"], - "what-to-do", Opt.Action.StoreConst, "exit") - .help("Exit after semantics and before codegen"); - argParse.addOption(["--gen-llvm"], - "what-to-do", Opt.Action.StoreConst, "gen-llvm") - .help("Compile to LLVM code (default)"); - argParse.addOption(["-c"], - "what-to-do", Opt.Action.StoreConst, "compile") - .help("Compile to .o or executeable"); - argParse.addOption(["--syntax-only"], - "what-to-do", Opt.Action.StoreConst, "parse") - .help("Only parse the file(s) and output parseing errors."); - - argParse.addOption( - ["-O","--optimize"], { - optimize = true; - } - ).help("Tell LLVM to do its standard optimizations"); - - argParse.addOption( - ["--inline"], { - inline = true; - } - ).help("Tell LLVM that its allowed to inline functions"); - - argParse - .addOption(["--time"], Opt.Action.SetTrue, "time") - .help("Time the various operations performed."); - - auto options = argParse.parse(args); - - filesToHandle ~= options.args; - - // Will throw exception if some files don't exist - preStart(&filesToHandle); - - struct Measurement { char[] label; double time; } - Measurement[] timings; - - auto what = options["what-to-do"]; - if (what == "" || what == "gen-llvm") - postSema.attach( - (Module[] modules, SourceManager sm) { - foreach(m ; modules) - { - if (!m.outputModule) - continue; - StopWatch w; w.start; - auto llvmGen = new CodeGen(); - auto file = new FileConduit(m.moduleName~".bc", FileConduit.WriteCreate); - llvmGen.gen(m, file.fileHandle, optimize, inline); - timings ~= Measurement("Generating LLVM bytecode", w.stop); - } - }); - else if (what == "compile") - postSema.attach( - (Module[] modules, SourceManager sm) { - foreach(m ; modules) - { - if (!m.outputModule) - continue; - StopWatch w; w.start; - auto llvmGen = new CodeGen(); - auto llc = new Process("llc","-o=-"); - auto gcc = new Process("gcc","-c","-o",m.moduleName~".o","-x","assembler","-"); - llc.execute(); - int i = dup(llc.stdin.fileHandle); - llc.stdin.detach; - llvmGen.gen(m, i, optimize, inline); - llc.wait(); - gcc.execute(); - gcc.stdin.copy(llc.stdout); - gcc.stdin.detach; - gcc.wait(); - timings ~= Measurement("Generating ASM", w.stop); - } - - }); - else if (what == "parse") - preParse.attach( - (Lexer lexer) { - auto parser = new Parser(messages); - auto action = new NullAction(); - parser.parse(src_mgr, lexer, action); - messages.checkErrors(ExitLevel.Parser); - exit(0); - }); - else if (what == "dot") - postSema.attach( - (Module[] m, SourceManager sm) { - StopWatch w; w.start; - // auto print = new DotPrinter(); -// print.print(m); - timings ~= Measurement("Generating dot output", w.stop); - }); - else if (what == "code") - postSema.attach( - (Module[] modules, SourceManager sm) { - StopWatch w; w.start; - auto print = new AstPrinter(sm); - foreach ( m ; modules ) - if (m.outputModule) - print.print(m); - timings ~= Measurement("Converting AST to text", w.stop); - }); - else if (what == "exit") - postSema.attach( - (Module[] modules, SourceManager sm) { - }); - StopWatch total; - total.start; - - Module[] modules; - - StopWatch watch; - watch.start; - foreach (file; filesToHandle) - { - preLex(file); - - auto start = src_mgr.addFile(file); - auto lexer = new Lexer(start, src_mgr, messages); - postLex(lexer); - - preParse(lexer); - - auto parser = new Parser(messages); - auto action = new AstAction(src_mgr); - modules ~= cast(Module)parser.parse(src_mgr, lexer, action); - timings ~= Measurement("Lex + Parse of '"~file~"'", watch.stop); - messages.checkErrors(ExitLevel.Parser); -/* - StopWatch watch2; - watch.start; - watch2.start; - Module[] mods = (new LoadModule).visit(m, src_mgr, messages); - (new ScopeBuilder).visit(m); - auto scope_builder = watch2.stop; - watch2.start; - (new ScopeCheck).visit(m); - auto scope_check = watch2.stop; - watch2.start; - (new TypeCheck).visit(m); - auto type_check = watch2.stop; - watch2.start; - - foreach (decl; m.decls) - decl.simplify(); - auto simplify = watch2.stop; - auto extra_stuff = watch.stop; - timings ~= Measurement("Extra stuff", watch.stop); - timings ~= Measurement(" - Building scopes", scope_builder); - timings ~= Measurement(" - Checking scopes", scope_check); - timings ~= Measurement(" - Checking types", type_check); - - postParse(m, src_mgr);*/ - } - - (new LiteralInterpreter(messages)).visit(modules); - messages.checkErrors; - postParse(modules, src_mgr); - - class ModuleLoader : Visitor!(void) - { - Module[] visit(Module[] modules, MessageHandler messages, SourceManager src_mgr) - { - this.modules = modules; - this.messages = messages; - this.src_mgr = src_mgr; - super.visit(modules); - return this.modules; - } - - override void visitImportDecl(ImportDecl decl) - { - char[] path = replace!(char)(decl.get,'.','/')~".d"; - - auto start = src_mgr.addFile(path); - auto lexer = new Lexer(start, src_mgr, messages); - - auto parser = new Parser(messages); - auto action = new AstAction(src_mgr); - - Module m = cast(Module)parser.parse(src_mgr, lexer, action); - modules ~= m; - m.outputModule = false; - // decl.env.mHandle.add(m); - messages.checkErrors(ExitLevel.Parser); - } - - Module[] modules; - SourceManager src_mgr; - MessageHandler messages; - } - - modules = (new ModuleLoader()).visit(modules, messages, src_mgr); - messages.checkErrors; - - (new BuildScopes).visit(modules); - (new BuildSymbols).visit(modules); - StopWatch watch2; - watch.start; - watch2.start; - - (new BuildTypes(messages)).visit(modules); - - (new CheckScopes(messages)).visit(modules); - messages.checkErrors; - auto scope_check = watch2.stop; - - watch2.start; - (new CheckTypes(messages)).visit(modules); - messages.checkErrors; - auto type_check = watch2.stop; - - watch2.start; - (new ObjectOriented(messages)).visit(modules); - messages.checkErrors; - auto object_check = watch2.stop; - - watch2.start; - auto vc = new VC; - vc.msg = messages; - foreach (m; modules) - m.verify(vc); - messages.checkErrors; - auto ast_verify = watch2.stop; - - foreach (m; modules) - foreach (decl; m.decls) - decl.simplify(); - - timings ~= Measurement("Total", total.stop); - postSema(modules, src_mgr); - - if (options.flag("time")) - foreach (m; timings) - Stderr.formatln("{,-45} {}ms", m.label, m.time*1e3); -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/array_1.d --- a/tests/code/array_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -struct Array -{ - int[] data; - int length; -} - -void insert(Array a, int v) -{ - a.length = a.length + 1; - a.data[a.length - 1] = v; -} - -int main() -{ - Array a; - a.length = 0; - - insert(a, 5); - - return a.data[0]; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/basic_1.d --- a/tests/code/basic_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ - -int x = 4; - -int main() -{ - long var1 = 1; - short var2 = 2; - nice(var1, var2); - return var2; -} - -int nice(long s, short t) -{ - byte x = 5 + t; - t = 5 + 1 * 5 * s + t; - return 2 * (t + -1) - x; -} - - diff -r bb06ffcfb170 -r d442ce39917c tests/code/basic_2.d --- a/tests/code/basic_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -//fail -int main() -{ - int x = y; - int y = 1; - return y; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/basic_types_1.d --- a/tests/code/basic_types_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -int main() -{ - // test some basic type conversions - byte a = 2; - short b = 3 * a; - int c = b + a; - long d = c * a / b; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/basic_types_2.d --- a/tests/code/basic_types_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -int main() -{ - byte a = 2; - short b = 3 * a; - int c = b + a; - long d = c * a / b; - - d = itol(2); - d = itol(a); - d = itol(b); - d = itol(c); - - c = stoi(a); - c = stoi(b); - - return 3; -} - -long itol(int x) { return x; } -int stoi(short x) { return x; } - diff -r bb06ffcfb170 -r d442ce39917c tests/code/bool_1.d --- a/tests/code/bool_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -int main() -{ - bool b = 1 < 2; - return b; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/bool_2.d --- a/tests/code/bool_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -int main() -{ - return 42 == 42; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/cast_1.d --- a/tests/code/cast_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ - -int main() -{ - byte y = 44; - - int b = cast(int)y * 66; - - if(b == 2904) - return 0; - else - return 1; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/cast_2.d --- a/tests/code/cast_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ - - - -int main() -{ - byte y = 44; - - int b = y * cast(byte)66; - - if(b == 88) - return 0; - else - return 1; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/float_1.d --- a/tests/code/float_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -int main() -{ - float a = 1.0 + 1; - double b = a + 1.0 + 2; - real c = b + a + 1e300; - c = c * a + a; - return c != f(); -} - -real f(); - diff -r bb06ffcfb170 -r d442ce39917c tests/code/float_2.d --- a/tests/code/float_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -int main() -{ - float a = 1.0 + 1; - double b = a + 1.0 + 2; - b = 1 + a; - return 0; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/func_1.d --- a/tests/code/func_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ - - -int main() -{ - testStruct t; - t.x = 5; - - return t.x; -} - -testStruct m(testStruct t) -{ - t.x = t.x - 5; - return t; -} - -struct testStruct -{ - int x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/function_pointer_1.d --- a/tests/code/function_pointer_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ - -struct A -{ - int foo(int a) - { - return a; - } - int x; -} - -void main() -{ - A a; - int* d = &a.x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/function_pointer_2.d --- a/tests/code/function_pointer_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -//fail -int main() -{ - int function(int) f = &foo; - return f(); -} - -int foo(int x) -{ - return x*x*x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/function_pointer_3.d --- a/tests/code/function_pointer_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ - -int main() -{ - int function(int) f = &foo; - return f(3); -} - -int foo(int x) -{ - return x*x*x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/if_1.d --- a/tests/code/if_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ - -int x = 4; - -int main() -{ - long var1 = 1; - short var2 = 2; - return nice(var1, var2); -} - -int nice(long s, short t) -{ - byte x = 5 + t; - if (x) - t = 5 + 1 * 5 * s + t; - return 2 * (t + -1) - x; -} - - diff -r bb06ffcfb170 -r d442ce39917c tests/code/if_2.d --- a/tests/code/if_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ - -int x = 4; - -int main() -{ - long var1 = 1; - short var2 = 2; - return nice(var1, var2); -} - -int nice(long s, short t) -{ - byte x = 5 + t; - if (x) - if (s) - t = 5 + 1 * 5 * s + t; - return 2 * (t + -1) - x; -} - - diff -r bb06ffcfb170 -r d442ce39917c tests/code/if_3.d --- a/tests/code/if_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -int main() -{ - int x = 0; - int y = 1; - if (x) - return 1; - else if (y == 2) - return 1; - return 0; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/if_4.d --- a/tests/code/if_4.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -int main() -{ - int x = 0; - int y = 1; - if (x) - return 1; - else if (y == 2) - return 1; - else - y = 0; - return y; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/math_1.d --- a/tests/code/math_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -int main() -{ - int x = 2; - int y = 3; - return 2 * (x + y); -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/math_2.d --- a/tests/code/math_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -int main() -{ - int x = 1 + 2 * 3 + 4 + 5; - int y = x - x; - return y + x - 15; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/math_3.d --- a/tests/code/math_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -//fail -int main() -{ - int x = x; - return x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/public_1.d --- a/tests/code/public_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -//fail - -struct A -{ - private int b; - public int c; -} - - -int main() -{ - A a; - a.b = 4; // should fail - a.c = 5; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/sarray_1.d --- a/tests/code/sarray_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -int main() -{ - int[10] a; - a[0] = 1; - a[1] = a[0]; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/sarray_2.d --- a/tests/code/sarray_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -int main() -{ - int[10] a; - // static array initialization is legal - int[10] b = a; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/sarray_3.d --- a/tests/code/sarray_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -//fail -int main() -{ - int[10] a; - // static array initialization is legal - int[10] b = a; - // static array assignment is illegal - b = a; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/sarray_4.d --- a/tests/code/sarray_4.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ - -int main() -{ - int[10] a; - int[10] b; - b[] = a; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/scope_1.d --- a/tests/code/scope_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -//fail -int main() -{ - int x = y; - int y = 1; - return x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/scope_2.d --- a/tests/code/scope_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -//fail -int main() -{ - int x = 10; - while (x > 0) - { - int y = 1; - x = x -y; - } - return y; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/scope_3.d --- a/tests/code/scope_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -int y = 0; - -int main() -{ - return x + y; -} - -int x = 0; - diff -r bb06ffcfb170 -r d442ce39917c tests/code/struct_1.d --- a/tests/code/struct_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ - -struct S -{ -} - -void main() -{ - S s; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/struct_2.d --- a/tests/code/struct_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ - -struct S -{ - int a; - int b; -} - -void main() -{ - S s; - s.a = 2; - s.b = s.a; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/struct_3.d --- a/tests/code/struct_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ - -struct S -{ - int a; -} - -void main() -{ - S s; - S s2 = s; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/struct_4.d --- a/tests/code/struct_4.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ - -struct A -{ - int a; -} - -struct B -{ - int b; - A a; -} - -void main() -{ - B b; - b.a.a = 1; - b.b = 2; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/struct_5.d --- a/tests/code/struct_5.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -// Test forward references in struct members -struct B -{ - int b; - A a; -} - -struct A -{ - int a; -} - -void main() -{ - B b; - b.b = 2; -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/struct_6.d --- a/tests/code/struct_6.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ - -struct A -{ - int foo() - { - return 5; - } -} - -int main() -{ - A a; - return a.foo(); -} diff -r bb06ffcfb170 -r d442ce39917c tests/code/struct_7.d --- a/tests/code/struct_7.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -struct A { int a; } -A f() -{ - A a; - return a; -} - -void main() { - A a = f(); -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/switch_1.d --- a/tests/code/switch_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ - -void main() -{ - int x; - switch (x) - { - } -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/switch_2.d --- a/tests/code/switch_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ - -void main() -{ - int x; - switch (x) - { - case 1: - } -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/switch_3.d --- a/tests/code/switch_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ - -void main() -{ - int x; - switch (x) - { - case 1, 2: - x = 2; - return; - case 3, 4: - x = 1; - return; - default: - } -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/switch_4.d --- a/tests/code/switch_4.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ - -int main() -{ - int x; - switch (x) - { - case 1, 2: - x = 2; - return x; - case 3, 4: - x = 1; - return x; - default: - return 0; - } -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/switch_5.d --- a/tests/code/switch_5.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -//fail -int main(int x) -{ - switch (x) - { - default: - return 0; - default: - return 1; - } -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/switch_6.d --- a/tests/code/switch_6.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -//fail -int main(int x) -{ - switch (x) - { - case 1, 2: - return 0; - case 2, 3: - return 1; - case 1, 3: - return 1; - } -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/while_1.d --- a/tests/code/while_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -int main() -{ - int x = 10; - while (x > 0) - x = x - 1; - return x; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/code/while_2.d --- a/tests/code/while_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -int main() -{ - int x = 10; - int res = 0; - while (x > 0) - { - res = res + x; - x = x - 1; - } - if (res == 55) - return 0; - return 1; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/lexer/Comments.d --- a/tests/lexer/Comments.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -/* - - - /++ - +++++ - - - /++++ - - */ - -int i = 0; - /// lalalala - -/**** - */ - -/+ - -/+ - -+/ - -/+ - -+/ - -+/ - -/+ /+/ -fdsafasdf -+/ +/ - diff -r bb06ffcfb170 -r d442ce39917c tests/lexer/Comments1.d --- a/tests/lexer/Comments1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -/* - - -*/ - -/*/ - -*/ - -/+ - -+/ - - -/+/ - -+/ diff -r bb06ffcfb170 -r d442ce39917c tests/lexer/Comments2.d --- a/tests/lexer/Comments2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -//fail - -/+ - - - diff -r bb06ffcfb170 -r d442ce39917c tests/parser/alias_1.d --- a/tests/parser/alias_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ - -int foo(int x) -{ - return x + 6; -} - -alias foo bar; -alias int* i; -alias int A; -alias int B; - -int main() -{ - int x = bar(5); - i y = &x; - - A a = 5; - B b = a; - - return 11 - *y; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/array_literal_1.d --- a/tests/parser/array_literal_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ - -int main() -{ - int[3] x = [1,2,3]; - return x[0]; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/assign_1.d --- a/tests/parser/assign_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ - - -int main() -{ - int x = 0; - x = 2; - x += 5; - return x; -/* x -= 2; - x *= 3; - x /= 4; - x %= 3; */ -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/basic_type_char_1.d --- a/tests/parser/basic_type_char_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ - - -int main() -{ - char c; - wchar w; - dchar d; - - return 0; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/class_1.d --- a/tests/parser/class_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ - -class A -{ - int af; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/class_2.d --- a/tests/parser/class_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ - -class A -{ - int a; -} - -class B : A -{ - int b; -} - -class C : B -{ - int c; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/extern_1.d --- a/tests/parser/extern_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ - -extern (D): - -void foo() -{ -} - -extern (C): - -void boo() -{ -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/extern_2.d --- a/tests/parser/extern_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ - -extern(C): -int main() -{ - return 0; -} - -extern(D) -int boo() -{ - return 0; -} - -int foo(int); diff -r bb06ffcfb170 -r d442ce39917c tests/parser/float_1.d --- a/tests/parser/float_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ - - -void main() -{ - float f1 = 4_.5_e+54; - float f2 = 4._5_e+34; - float f3 = 4.__5_e-2; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/for_1.d --- a/tests/parser/for_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ - -int main() -{ - int x = 0; - for(int i = 0; i < 5; i = i + 1) - { - x = x + i; - } - return x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/for_2.d --- a/tests/parser/for_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ - -void main() -{ - for( ; ; ) - { - } -} - - diff -r bb06ffcfb170 -r d442ce39917c tests/parser/function_pointer.d --- a/tests/parser/function_pointer.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ - -int main() -{ - g = &foo; - f = &g; - return g(2); -} - -int foo(int x) -{ - return x; -} - -int function(int x)* f; -int function(int x) g; diff -r bb06ffcfb170 -r d442ce39917c tests/parser/int_1.d --- a/tests/parser/int_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ - -int main() -{ - int i1 = 123_456; - int i2 = 1_2_3_4_5_6_; - - int i3 = 43_422_253; - long i4 = 34_322_523_123; - - long i5 = 43_422_253L; - long i6 = 34_322_523_123L; - - uint i7 = 43_422_253u; - ulong i8 = 18_446_744_073_709_551_615U; - - ulong i9 = 0UL; - ulong i10 = 18_446_744_073_709_551_615LU; - -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/interface_1.d --- a/tests/parser/interface_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -interface A -{ -} - -interface B : A -{ -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/new_1.d --- a/tests/parser/new_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2 +0,0 @@ -class A{this(int y){}int x;}struct B -{}void main(){B b;long x;A a=new A(x);} int x = 5; int foo(){int y = 5; y = y + x * 5; return y;} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/null_1.d --- a/tests/parser/null_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ - -int main() -{ - int* i = null; - - return 0; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/public_1.d --- a/tests/parser/public_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ - -public struct name -{ -static: - public void foo() - { - } -public -{ - private void bar() - { - } -auto: - final int i; -} - -static: - public void food() - { - } - private void bard() - { - } - final int id; -} - -void main() -{ -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/shift_1.d --- a/tests/parser/shift_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ - - -int main() -{ - int x = 4; - int y = 2; - - x = x << y; - x = x >> y; - x = x >>> y; - return x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/simple_missing_1.d --- a/tests/parser/simple_missing_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ - -int main(int x) -{ - x = 5; - - y = 6 * -x; - return x; -} - -int y; diff -r bb06ffcfb170 -r d442ce39917c tests/parser/simple_missing_2.d --- a/tests/parser/simple_missing_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -//fail - -int main(int x) -{ - x = 5; - - y = 6 * -x; - return x; -} - -int y diff -r bb06ffcfb170 -r d442ce39917c tests/parser/simple_missing_3.d --- a/tests/parser/simple_missing_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -//fail - -int main(int x) -{ - x = 5; - - y = 6 * -x; - return x; -} - -int diff -r bb06ffcfb170 -r d442ce39917c tests/parser/simple_missing_4.d --- a/tests/parser/simple_missing_4.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -//fail - -int main(int x) -{ - x = 5; - - y = 6 * -x; - return x; - diff -r bb06ffcfb170 -r d442ce39917c tests/parser/string_1.d --- a/tests/parser/string_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ - -int main() -{ - /* All examples taken from D's Language site */ - - char[4] s1 = "food"; - - char[5] s2 = r"hello"; - char[15] s3 = r"c:\root\foo.exe"; - char[4] s4 = r"ab\n"; - - char[5] s5 = `hello`; - char[15] s6 = `c:\root\foo.exe`; - char[4] s7 = `ab\n`; - - char[5] s10 = "hello"; - char[15] s11 = "c:\\root\\foo.exe"; - char[3] s12 = "ab\n"; - char[3] s13 = "ab -"; - - char[1] s14 = x"0A"; - char[6] s15 = x"00 FBCD 32FD 0A"; - - /* And some custom ones */ - - char[7] s16 = "\x61\u05D0\U000201A4"; - char[2] s17 = "\122\522"; - char[8] s18 = x"61 62 63 64 - 65 66 67 68"; - - char[3] s19 = "\®\&"; - - char[3] s20 = "\®\&"c; - wchar[2] s21 = "\®\&"w; - dchar[2] s22 = "\®\&"d; - - return 0; -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/struct_method_1.d --- a/tests/parser/struct_method_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -module struct_method_1; - -struct A -{ - int x; - int foo(int i) - { - return i; - } -} - -int main() -{ - A a; - a.x = 6; - return a.foo(a.x); -} diff -r bb06ffcfb170 -r d442ce39917c tests/parser/this_1.d --- a/tests/parser/this_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ - -class A -{ - this() - { - } -} - diff -r bb06ffcfb170 -r d442ce39917c tests/run.d --- a/tests/run.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,171 +0,0 @@ -// skip -module run.d; - -import tango.core.Array, - tango.io.FileConduit, - tango.io.FileScan, - tango.io.Stdout, - tango.sys.Process, - tango.text.Ascii, - tango.text.Regex, - tango.text.Util; - -// -- Settings -- -char[] compiler = "./Dang"; -char[] test_folder = "tests"; -char[] valid_filenames = r"^[^.].*"; -bool print_expected = false; -char[][] options; -// the tests can be sorted by one of the following functions -bool nameSort (FilePath a, FilePath b) { return a.name < b.name; } -bool pathSort (FilePath a, FilePath b) { return a.toString < b.toString; } -bool modifiedSort(FilePath a, FilePath b) { return a.modified < b.modified; } -bool createdSort (FilePath a, FilePath b) { return a.created < b.created; } -const sortBy = &pathSort; - -// -- end of settings - -enum TestResult -{ - Skipped, - Expected, - Unexpected -} - -void main(char[][] args) -{ - foreach (arg ; args[1..$]) - options ~= arg; - - scope scan = new FileScan; -// scope regex = new Regex(valid_filenames); // DMD FAILS!! ?? - // Return true for files/folders to include - bool filter(FilePath p, bool isDir) - { - if (isDir) - return p.name[0] != '.'; - else - return p.ext == "d" ; //&& regex.test(p.name); - } - scan.sweep(test_folder, &filter, true); - FilePath[] files = scan.files; - int total_tests = files.length; - - // Sort the result by the chosen function - default is the full path - sort(files, sortBy); - - int[TestResult.max + 1] results = 0; - foreach (i, ref test; files) - { - begin_test(i + 1, total_tests, test.name); - TestResult res = run_test(test); - results[res] += 1; - end_test(); - } - Stdout.format("\r{,80}\r", " "); - Stdout.newline; - int good = TestResult.Expected; - int bad = TestResult.Unexpected; - int tests_run = results[good] + results[bad]; - Stdout.formatln("{}/{} tests failed", results[bad], tests_run); -} - -void begin_test(int number, int total_tests, char[] name) -{ - char[60] progressbar = ' '; - int progress = number*progressbar.length/total_tests; - progressbar[0 .. progress] = '='; - if(progress) - progressbar[progress-1] = '>'; - Stdout.format("\r{}% - [{}]", 1e2 * number / total_tests, progressbar); - Stdout.flush(); - //Thread.sleep(0.05); -} - -void end_test() { } - -enum { - NoFail, - CompiletimeFail, - RuntimeFail -} - -private int min(int a, int b) { return a < b? a : b; } -TestResult run_test(ref FilePath p) -{ - auto file = new FileConduit(p.toString(), FileConduit.ReadExisting); - char[256] content; - int len = file.read(content); - file.close(); - char[] line = content[0 .. min(len, content.find('\n'))]; - - bool compile = true; - int fail = NoFail; - if (line.length >= 2 && line[0 .. 2] == "//") - { - foreach (command; line[2 .. $].delimiters(",;")) - { - switch (toLower(substitute(command, " ", ""))) - { - case "skip", "dontcompile": - compile = false; - break; - case "fail", "compilefail", - "compiletimefail", "failatcompiletime": - fail = CompiletimeFail; - break; - case "runtime", "runtimefail", "failatruntime": - fail = RuntimeFail; - Stderr("== Compiled tests will not be run! ==").newline; - return TestResult.Skipped; - default: - break; - } - break; - } - } - - if (compile) - { - auto o = compiler ~ options ~ p.toString; - auto process = new Process(o); - process.execute(); - auto result = process.wait(); - return resultOf(p, result.status, fail); - } - - return TestResult.Skipped; -} - -private TestResult resultOf(FilePath p, int result, int expected) -{ - char[] good(char[] s) - { - version (Posix) - return "\033[1;32m" ~ s ~ "\033[m"; - else - return s; - } - - char[] bad(char[] s) - { - s = s ~ " - Unexpected"; - version (Posix) - return "\033[1;31m" ~ s ~ "\033[m"; - else - return s; - } - - bool unexpected = expected == 0 ? result != 0 : result == 0; - auto f = unexpected? &bad : &good; - char[] s = (result == 0)? "SUCCESS" : "FAILURE"; - // always print if unexpeted, otherwise check the settings - if (unexpected || print_expected) - { - Stdout.format("\r{,80}\r", " "); - Stdout.format(" {,-45}", p); - Stdout(f(s)).newline; - } - return unexpected? TestResult.Unexpected : TestResult.Expected; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/sema/class_1.d --- a/tests/sema/class_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ - -class A -{ - this() - { - } - - int foo() - { - return 1; - } - - int boo() - { - return 0; - } -} - -class B : A -{ - this() - { - } - - int foo() - { - return 0; - } -} - -int main() -{ - B a = new B(); - return a.foo() + a.boo(); -} diff -r bb06ffcfb170 -r d442ce39917c tests/sema/deref_1.d --- a/tests/sema/deref_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -//fail -int main() -{ - int a = 2; - int b = *a; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/sema/deref_2.d --- a/tests/sema/deref_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -int main() -{ - int* a; - int* b; - a = b; - *a = 1; - - return *a == *b; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/sema/deref_3.d --- a/tests/sema/deref_3.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -int main() -{ - int* a; - int** b; - *a = 1; - *b = a; - - return *a == **b; -} - diff -r bb06ffcfb170 -r d442ce39917c tests/sema/function_overload_1.d --- a/tests/sema/function_overload_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ - - -void main() -{ - int x = 5; - foo(x); - long y = 12; - foo(y); -} - -int foo(long x); -int foo(int x); diff -r bb06ffcfb170 -r d442ce39917c tests/sema/public_1.d --- a/tests/sema/public_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -//fail - -public struct A -{ -//static: - public void foo() - { - bar(); - } - - private void bar() - { - } - final int i; -} - -private void bar() -{ -} - -void main() -{ - A a; - a.foo(); - a.bar(); - bar(); -} diff -r bb06ffcfb170 -r d442ce39917c tests/sema/scope_1.d --- a/tests/sema/scope_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -//fail - -void main() -{ - int x; - int x; -} diff -r bb06ffcfb170 -r d442ce39917c tests/sema/scope_2.d --- a/tests/sema/scope_2.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -//fail - -void main() -{ - x = 5; -} diff -r bb06ffcfb170 -r d442ce39917c tests/sema/shift_1.d --- a/tests/sema/shift_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ - -int main() -{ - int c; - c << 33; -} diff -r bb06ffcfb170 -r d442ce39917c tests/sema/type_check_1.d --- a/tests/sema/type_check_1.d Sun Aug 10 16:16:55 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -//fail -int main() -{ - int function(int s) m; - m = &main; -} -