├── ASCII.cs ├── FILES ├── IArgumentLexer.cs ├── IArgumentParser.cs ├── IAstNode.cs ├── IAstNodeQueue.cs ├── IAstNodeTypeMgr.cs ├── IAstWriter.cs ├── ICSharpReswords.cs ├── ICapabilities.cs ├── ICompilerOptions.cs ├── IDiagnostics.cs ├── IDialects.cs ├── IDotWriter.cs ├── IFileIO.cs ├── IInfile.cs ├── ILexer.cs ├── INonTerminals.cs ├── IParser.cs ├── IPathname.cs ├── ITerminals.cs ├── ITokenSet.cs ├── LICENSE ├── README.md ├── Result.cs ├── compiler-option-grammar.txt ├── compiler-options.txt ├── imp ├── ArgumentLexer.cs ├── ArgumentParser.cs ├── Capabilities.cs ├── CompilerOptions.cs ├── Dialects.cs ├── FileIO.cs ├── Infile.cs ├── Lexer.cs ├── Parser.cs ├── PlatformInfo.cs ├── ProductInfo.cs ├── Program.cs └── Terminals.cs ├── m2sharp-grammar.gll ├── modula2.xml ├── tests ├── first-and-follow │ └── dummy └── m2sources │ ├── CRLF.mod │ ├── Comments.PIM.mod │ ├── Comments.mod │ ├── DerivOrSubrType.def │ ├── EmptyFieldListSeq.PIM.mod │ ├── EmptyFieldListSeq.mod │ ├── EmptyStmtSeq.mod │ ├── IdentList.def │ ├── Lex.mod │ ├── ProcType.PIM.def │ ├── ProcType.def │ ├── Semicolon.PIM.mod │ ├── Semicolon.mod │ └── VarSizeRec.mod └── xamarin ├── AssemblyInfo.cs ├── m2sharp.csproj └── m2sharp.sln /ASCII.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * ASCII.cs 26 | * 27 | * ASCII code constants. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* -------------------------------------------------------------------------- 49 | * ASCII codes used within the compiler. 50 | * ----------------------------------------------------------------------- */ 51 | 52 | public static class ASCII { 53 | 54 | /* ASCII NUL is used as string terminator in M2 strings */ 55 | 56 | public const char NUL = '\u0000'; 57 | 58 | /* ASCII EOT is used to signal the end of a source file */ 59 | 60 | public const char EOT = '\u0004'; 61 | 62 | /* TAB, LF and CRLF are legal within M2 source files */ 63 | 64 | public const char TAB = '\u0009'; 65 | public const char LF = '\u000A'; 66 | public const char CR = '\u000D'; 67 | 68 | /* SPACE */ 69 | 70 | public const char SPACE = '\u0020'; 71 | 72 | /* BACKSLASH */ 73 | 74 | public const char BACKSLASH = '\\'; 75 | 76 | /* IsCtrl(ch) -- returns true if ch is control character */ 77 | 78 | public static bool IsCtrl (char ch) { 79 | return (ch >= NUL) && (ch < SPACE); 80 | } /* end IsCtrl */ 81 | 82 | /* IsUpper(ch) -- returns true if ch is uppercase letter */ 83 | 84 | public static bool IsUpper (char ch) { 85 | return (ch >= 'A') && (ch <= 'Z'); 86 | } /* end IsUpper */ 87 | 88 | /* IsLower(ch) -- returns true if ch is lowercase letter */ 89 | 90 | public static bool IsLower (char ch) { 91 | return (ch >= 'a') && (ch <= 'z'); 92 | } /* end IsLower */ 93 | 94 | /* IsDigit(ch) -- returns true if ch is digit */ 95 | 96 | public static bool IsDigit (char ch) { 97 | return (ch >= '0') && (ch <= '9'); 98 | } /* end IsDigit */ 99 | 100 | /* IsAtoF(ch) -- returns true if ch is in 'A' .. 'F' */ 101 | 102 | public static bool IsAtoF (char ch) { 103 | return (ch >= 'A') && (ch <= 'F'); 104 | } /* end IsAtoF */ 105 | 106 | /* IsAlnum(ch) -- returns true if ch is alpha-numeric */ 107 | 108 | public static bool IsAlnum (char ch) { 109 | return 110 | ((ch >= 'A') && (ch <= 'Z')) || 111 | ((ch >= 'a') && (ch <= 'z')) || 112 | ((ch >= '0') && (ch <= '9')); 113 | } /* end IsAlnum */ 114 | 115 | } /* ASCII */ 116 | 117 | } /* namespace */ 118 | 119 | /* END OF FILE */ -------------------------------------------------------------------------------- /FILES: -------------------------------------------------------------------------------- 1 | M2Sharp -- Modula-2 to C# Translator and Compiler 2 | 3 | * Files in the repository * 4 | 5 | (1) Documents 6 | 7 | README.md 8 | repo landing page, a brief description of the project 9 | 10 | LICENSE 11 | the full license text of the license used by the project 12 | 13 | FILES 14 | summary of files and their purpose, (this file) 15 | 16 | m2j-grammar.gll 17 | formal LL(1) grammar for Modula-2 used by the project 18 | 19 | modula2.xml 20 | syntax mode definition for MonoDevelop/Xamarin Studio 21 | 22 | 23 | (2) Source Code 24 | 25 | ASCII.cs 26 | provides constants for ASCII codes used in the compiler 27 | used by the file reader and lexer class 28 | 29 | IInfile.cs 30 | interface for the file reader class 31 | 32 | Infile.cs 33 | source file reader class, 34 | manages file buffer, current position, lookahead, line and column counters 35 | used by the lexer class to read symbols for tokenisation and obtain lexemes 36 | 37 | ITerminals.cs 38 | interface for the terminals class 39 | defines enumerated tokens representing terminal symbols, 40 | 41 | Terminals.cs 42 | terminals class, 43 | classifies tokens, 44 | used by the lexer class for symbol tokenisation 45 | 46 | ILexer.cs 47 | interface for the lexer class 48 | 49 | Lexer.cs 50 | lexer class, 51 | reads source file via file reader class and tokenises the input, 52 | detects lexical faux-pas and errors, 53 | reports warnings and errors using the Diagnostics class 54 | used by the parser class for syntax analysis 55 | 56 | INonTerminals.cs 57 | interface for the nonterminals class 58 | defines enumerated productions representing non-terminal symbols, 59 | 60 | NonTerminals.cs 61 | nonterminals class 62 | provides FIRST() and FOLLOW() sets for each non-terminal symbol 63 | used by the parser class for syntax analysis 64 | 65 | IParser.cs 66 | interface for the parser class 67 | 68 | Parser.cs 69 | parser class 70 | reads token stream via lexer class and performs syntax analysis 71 | detects syntactical faux-pas and errors, 72 | reports warnings and errors using the Diagnostics class 73 | builds an abstract syntax tree using the AST class 74 | 75 | IAstNode.cs 76 | interface for the abstract syntax tree class 77 | 78 | AstNode.cs 79 | abstract syntax tree class 80 | provides an API to build tree nodes of different node types 81 | used by the parser class to build an abstract syntax tree 82 | 83 | IAstNodeTypeMgr.cs 84 | interface for abstract syntax tree node type checks 85 | defines enumerated values representing tree node types 86 | 87 | AstNodeTypeMgr.cs 88 | abstract syntax tree node type checker class 89 | provides an API for tree node integrity checks 90 | used by the abstract syntax tree class when creating tree nodes 91 | 92 | IAstWriter.cs 93 | interface for AST writer class 94 | 95 | AstWriter.cs 96 | AST writer class 97 | writes an AST in S-expression format to a file 98 | 99 | IDotWriter.cs 100 | interface for AST to DOT exporter class 101 | 102 | DotWriter.cs 103 | AST to DOT exporter class 104 | writes an AST in GraphViz DOT format to a file 105 | 106 | ICSharpReswords.cs 107 | interface for C# reserved word recogniser class 108 | 109 | CSharpReswords.cs 110 | C# reserved word recogniser class 111 | used by code generator to avoid name conflicts in output 112 | 113 | IDiagnostics.cs 114 | interface for the diagnostics class 115 | defines enumerated diagnostic codes 116 | 117 | Diagnostics.cs 118 | diagnostics class 119 | provides methods to emit formatted warnings and error messages 120 | used by lexer and parser classes during lexical and syntactical analysis 121 | 122 | Result.cs 123 | generic result class 124 | used by constructors to return paired results, 125 | the first item is the value to return, the second item a status code 126 | 127 | (to be continued) 128 | 129 | END OF FILE 130 | -------------------------------------------------------------------------------- /IArgumentLexer.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IArgumentLexer.cs 26 | * 27 | * Public interface for command line argument lexer. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* --------------------------------------------------------------------------- 49 | * type ArgumentToken 50 | * --------------------------------------------------------------------------- 51 | * Enumerated token values representing command line arguments. 52 | * ------------------------------------------------------------------------ */ 53 | 54 | public enum ArgumentToken { 55 | UNKNOWN, 56 | 57 | /* information options */ 58 | 59 | HELP, /* --help, -h */ 60 | VERSION, /* --version, -V */ 61 | LICENSE, /* --license */ 62 | 63 | /* dialect options */ 64 | 65 | PIM3, /* --pim3 */ 66 | PIM4, /* --pim4 */ 67 | EXT, /* --ext */ 68 | 69 | /* dialect qualifier options */ 70 | 71 | SAFER, /* --safer */ 72 | COMPLIANT, /* --compliant */ 73 | 74 | /* singe product options */ 75 | 76 | SYNTAX_ONLY, /* --syntax-only */ 77 | AST_ONLY, /* --ast-only */ 78 | GRAPH_ONLY, /* --graph-only */ 79 | XLAT_ONLY, /* --xlat-only */ 80 | OBJ_ONLY, /* --obj-only */ 81 | 82 | /* multiple product options */ 83 | 84 | AST, /* --ast */ 85 | NO_AST, /* --no-ast */ 86 | GRAPH, /* --graph */ 87 | NO_GRAPH, /* --no-graph */ 88 | XLAT, /* --xlat */ 89 | NO_XLAT, /* --no-xlat */ 90 | OBJ, /* --obj */ 91 | NO_OBJ, /* --no-obj */ 92 | 93 | /* identifier options */ 94 | 95 | USE_IDENTIFIERS_VERBATIM, /* --use-identifiers-verbatim */ 96 | TRANSLITERATE_IDENTIFIERS, /* --transliterate-identifiers */ 97 | 98 | /* comment options */ 99 | 100 | PRESERVE_COMMENTS, /* --preserve-comments */ 101 | STRIP_COMMENTS, /* --strip-comments */ 102 | 103 | /* capability options */ 104 | 105 | SYNONYMS, /* --synonyms */ 106 | NO_SYNONYMS, /* --no-synonyms */ 107 | OCTAL_LITERALS, /* --octal-literals */ 108 | NO_OCTAL_LITERALS, /* --no-octal-literals */ 109 | EXPLICIT_CAST, /* --explicit-cast */ 110 | NO_EXPLICIT_CAST, /* --no-explicit-cast */ 111 | COROUTINES, /* --coroutines */ 112 | NO_COROUTINES, /* --no-coroutines */ 113 | VARIANT_RECORDS, /* --variant-records */ 114 | NO_VARIANT_RECORDS, /* --no-variant-records */ 115 | LOCAL_MODULES,ES, /* --local-modules */ 116 | NO_LOCAL_MODULES, /* --no-local-modules */ 117 | LOWLINE_IDENTIFIERS, /* --lowline-identifiers */ 118 | NO_LOWLINE_IDENTIFIERS, /* --no-lowline-identifiers */ 119 | TO_DO_STATEMENT, /* --to-do-statement */ 120 | NO_TO_DO_STATEMENT, /* --no-to-do-statement */ 121 | 122 | /* source file argument */ 123 | 124 | SOURCE_FILE, 125 | 126 | /* diagnostic options */ 127 | 128 | VERBOSE, /* --verbose, -v */ 129 | LEXER_DEBUG, /* --lexer-debug */ 130 | PARSER_DEBUG, /* --parser-debug */ 131 | SHOW_SETTINGS, /* --show-settings */ 132 | ERRANT_SEMICOLONS, /* --errant-semicolons */ 133 | 134 | /* end of input sentinel */ 135 | 136 | END_OF_INPUT 137 | 138 | } /* ArgumentToken */ 139 | 140 | 141 | /* --------------------------------------------------------------------------- 142 | * interface IArgumentLexer 143 | * --------------------------------------------------------------------------- 144 | * This interface describes a singleton class. 145 | * Since C# does not fully support the concept of information hiding, this 146 | * interface is entirely comprised of comments for documentation purposes. 147 | * ------------------------------------------------------------------------ */ 148 | 149 | public interface IArgumentLexer { 150 | 151 | /* --------------------------------------------------------------------------- 152 | * method InitWithArgs(args) 153 | * --------------------------------------------------------------------------- 154 | * Initialises the argument lexer class with the given arguments. 155 | * ------------------------------------------------------------------------ */ 156 | 157 | // public static void InitWithArgs (string[] args); 158 | 159 | 160 | /* --------------------------------------------------------------------------- 161 | * method NextToken() 162 | * --------------------------------------------------------------------------- 163 | * Reads and consumes the next commmand line argument and returns its token. 164 | * ------------------------------------------------------------------------ */ 165 | 166 | // public static ArgumentToken NextToken(); 167 | 168 | 169 | /* --------------------------------------------------------------------------- 170 | * method LastArg() 171 | * --------------------------------------------------------------------------- 172 | * Returns the argument string of the last consumed argument or null if the 173 | * end of input token has been returned by a prior call to NextToken(). 174 | * ------------------------------------------------------------------------ */ 175 | 176 | // public static string LastArg (); 177 | 178 | 179 | /* --------------------------------------------------------------------------- 180 | * method IsInfoRequest(sym) 181 | * --------------------------------------------------------------------------- 182 | * Returns true if sym is an information request 183 | * ------------------------------------------------------------------------ */ 184 | 185 | // public static bool IsInfoRequest (ArgumentToken sym); 186 | 187 | 188 | /* --------------------------------------------------------------------------- 189 | * method IsCompilationRequest(sym) 190 | * --------------------------------------------------------------------------- 191 | * Returns true if sym is a compilation request 192 | * ------------------------------------------------------------------------ */ 193 | 194 | // public static bool IsCompilationRequest (ArgumentToken sym); 195 | 196 | 197 | /* --------------------------------------------------------------------------- 198 | * method IsDialectOption(sym) 199 | * --------------------------------------------------------------------------- 200 | * Returns true if sym is a dialect option 201 | * ------------------------------------------------------------------------ */ 202 | 203 | // public static bool IsDialectOption (ArgumentToken sym); 204 | 205 | 206 | /* --------------------------------------------------------------------------- 207 | * method IsDialectQualifierOption(sym) 208 | * --------------------------------------------------------------------------- 209 | * Returns true if sym is a dialect qualifier option 210 | * ------------------------------------------------------------------------ */ 211 | 212 | // public static bool IsDialectQualifierOption (ArgumentToken sym); 213 | 214 | 215 | /* --------------------------------------------------------------------------- 216 | * method IsProductOption(sym) 217 | * --------------------------------------------------------------------------- 218 | * Returns true if sym is a product option 219 | * ------------------------------------------------------------------------ */ 220 | 221 | // public static bool IsProductOption (ArgumentToken sym); 222 | 223 | 224 | /* --------------------------------------------------------------------------- 225 | * method IsSingleProductOption(sym) 226 | * --------------------------------------------------------------------------- 227 | * Returns true if sym is a single product option 228 | * ------------------------------------------------------------------------ */ 229 | 230 | // public static bool IsSingleProductOption (ArgumentToken sym); 231 | 232 | 233 | /* --------------------------------------------------------------------------- 234 | * method IsMultipleProductsOption(sym) 235 | * --------------------------------------------------------------------------- 236 | * Returns true if sym is a multiple product option 237 | * ------------------------------------------------------------------------ */ 238 | 239 | // public static bool IsMultipleProductsOption (ArgumentToken sym); 240 | 241 | 242 | /* --------------------------------------------------------------------------- 243 | * method IsIdentifierOption(sym) 244 | * --------------------------------------------------------------------------- 245 | * Returns true if sym is an identifier option 246 | * ------------------------------------------------------------------------ */ 247 | 248 | // public static bool IsIdentifierOption (ArgumentToken sym); 249 | 250 | 251 | /* --------------------------------------------------------------------------- 252 | * method IsCommentOption(sym) 253 | * --------------------------------------------------------------------------- 254 | * Returns true if sym is a comment option 255 | * ------------------------------------------------------------------------ */ 256 | 257 | // public static bool IsCommentOption (ArgumentToken sym); 258 | 259 | 260 | /* --------------------------------------------------------------------------- 261 | * method IsCapabilityOption(sym) 262 | * --------------------------------------------------------------------------- 263 | * Returns true if sym is a capability option 264 | * ------------------------------------------------------------------------ */ 265 | 266 | // public static bool IsCapabilityOption (ArgumentToken sym); 267 | 268 | 269 | /* --------------------------------------------------------------------------- 270 | * method IsDiagnosticsOption(sym) 271 | * --------------------------------------------------------------------------- 272 | * Returns true if sym is a diagnostic option 273 | * ------------------------------------------------------------------------ */ 274 | 275 | // public static bool IsDiagnosticsOption (ArgumentToken sym); 276 | 277 | 278 | } /* IArgumentLexer */ 279 | 280 | } /* namespace */ 281 | 282 | /* END OF FILE */ -------------------------------------------------------------------------------- /IArgumentParser.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IOptionParser.cs 26 | * 27 | * Public interface for command line argument parser class. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* --------------------------------------------------------------------------- 49 | * type ArgumentStatus 50 | * --------------------------------------------------------------------------- 51 | * Enumerated status values representing command line arguments. 52 | * ------------------------------------------------------------------------ */ 53 | 54 | public enum ArgumentStatus { 55 | Success, 56 | HelpRequested, 57 | VersionRequested, 58 | LicenseRequested, 59 | ErrorsEncountered 60 | } /* ArgumentStatus */ 61 | 62 | 63 | /* --------------------------------------------------------------------------- 64 | * interface IArgumentParser 65 | * --------------------------------------------------------------------------- 66 | * This interface describes a singleton class. 67 | * Since C# does not fully support the concept of information hiding, this 68 | * interface is entirely comprised of comments for documentation purposes. 69 | * ------------------------------------------------------------------------ */ 70 | 71 | public interface IArgumentParser { 72 | 73 | /* --------------------------------------------------------------------------- 74 | * method ParseOptions(args) 75 | * --------------------------------------------------------------------------- 76 | * Parses command line arguments and sets compiler options accordingly. 77 | * ------------------------------------------------------------------------ */ 78 | 79 | // public static ArgumentStatus ParseOptions (string[] args); 80 | 81 | 82 | /* --------------------------------------------------------------------------- 83 | * method SourceFile() 84 | * --------------------------------------------------------------------------- 85 | * Returns sourcefile 86 | * ------------------------------------------------------------------------ */ 87 | 88 | // public static string SourceFile (); 89 | 90 | 91 | /* --------------------------------------------------------------------------- 92 | * method ErrorCount() 93 | * --------------------------------------------------------------------------- 94 | * Returns error count 95 | * ------------------------------------------------------------------------ */ 96 | 97 | // public static uint ErrorCount (); 98 | 99 | 100 | } /* OptionParser */ 101 | 102 | } /* namespace */ 103 | 104 | /* END OF FILE */ -------------------------------------------------------------------------------- /IAstNode.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IAstNode.cs 26 | * 27 | * Public abstract syntax tree (AST) interface. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* --------------------------------------------------------------------------- 49 | * AST status codes 50 | * ------------------------------------------------------------------------ */ 51 | 52 | public enum AstNodeStatus { 53 | Success, 54 | TODO 55 | } /* AstNodeStatus */ 56 | 57 | 58 | public interface IAstNode { 59 | 60 | /* --------------------------------------------------------------------------- 61 | * method EmptyNode() 62 | * --------------------------------------------------------------------------- 63 | * Returns the empty node singleton. 64 | * ------------------------------------------------------------------------ */ 65 | 66 | Result EmptyNode (); 67 | 68 | 69 | /* --------------------------------------------------------------------------- 70 | * constructor NewNode(nodeType, subnode0, subnode1, subnode2, ...) 71 | * --------------------------------------------------------------------------- 72 | * Creates new branch node of the given node type, stores the subnodes of 73 | * the argument list in the node and returns the node, or null on failure. 74 | * 75 | * pre-conditions: 76 | * o node_type must be a valid node type 77 | * o a non-empty list of valid ast nodes must be passed 78 | * and type and number of subnodes must match the given node type. 79 | * 80 | * post-conditions: 81 | * o newly created ast node is returned 82 | * 83 | * error-conditions: 84 | * o if node_type is invalid, no node is created and null is returned 85 | * o if type and number of subnodes does not match the given node type, 86 | * no node is created and null is returned 87 | * ------------------------------------------------------------------------ */ 88 | 89 | Result 90 | NewNode (IAstNodeType nodeType, params IAstNode[] subnodes); 91 | 92 | 93 | /* --------------------------------------------------------------------------- 94 | * method NewListNode(nodeType, nodeList) 95 | * --------------------------------------------------------------------------- 96 | * Allocates a new branch node of the given node type, stores the subnodes of 97 | * the given node list in the node and returns the node, or null on failure. 98 | * ------------------------------------------------------------------------ */ 99 | 100 | Result 101 | NewListNode (IAstNodeType nodeType, NodeList list); 102 | 103 | 104 | /* --------------------------------------------------------------------------- 105 | * method NewTerminalNode(nodeType, value) 106 | * --------------------------------------------------------------------------- 107 | * Allocates a new terminal node of the given node type, stores the given 108 | * terminal value in the node and returns the node, or null on failure. 109 | * ------------------------------------------------------------------------ */ 110 | 111 | Result 112 | NewTerminalNode (IAstNodeType nodeType, string value); 113 | 114 | 115 | /* --------------------------------------------------------------------------- 116 | * method NewTerminalListNode(nodeType, terminalValueList) 117 | * --------------------------------------------------------------------------- 118 | * Allocates a new terminal node of the given node type, stores the values of 119 | * the given value list in the node and returns the node, or null on failure. 120 | * ------------------------------------------------------------------------ */ 121 | 122 | Result 123 | NewTerminalListNode (IAstNodeType nodeType, TermList list); 124 | 125 | 126 | /* --------------------------------------------------------------------------- 127 | * method NodeTypeOf(node) 128 | * --------------------------------------------------------------------------- 129 | * Returns the node type of node, or null if node is null. 130 | * ------------------------------------------------------------------------ */ 131 | 132 | IAstNodeType NodeTypeOf (IAstNode node); 133 | 134 | 135 | /* --------------------------------------------------------------------------- 136 | * method SubnodeCountOf(node) 137 | * --------------------------------------------------------------------------- 138 | * Returns the number of subnodes or values of node. 139 | * ------------------------------------------------------------------------ */ 140 | 141 | int SubnodeCountOf (IAstNode node); 142 | 143 | 144 | /* --------------------------------------------------------------------------- 145 | * method SubnodeForIndex(node, index) 146 | * --------------------------------------------------------------------------- 147 | * Returns the subnode of node with the given index or null if no subnode of 148 | * the given index is stored in node. 149 | * ------------------------------------------------------------------------ */ 150 | 151 | Result SubnodeForIndex (IAstNode node, int index); 152 | 153 | 154 | /* --------------------------------------------------------------------------- 155 | * method ValueForIndex(node, index) 156 | * --------------------------------------------------------------------------- 157 | * Returns the value stored at the given index in a terminal node, 158 | * or NULL if the node does not store any value at the given index. 159 | * ------------------------------------------------------------------------ */ 160 | 161 | string ValueForIndex (IAstNode node, int index); 162 | 163 | 164 | /* --------------------------------------------------------------------------- 165 | * convenience method Value(node) 166 | * --------------------------------------------------------------------------- 167 | * Invokes ValueForIndex() with an index of zero. 168 | * ------------------------------------------------------------------------ */ 169 | 170 | string value (IAstNode node); 171 | 172 | 173 | /* --------------------------------------------------------------------------- 174 | * method replaceSubnode(inNode, atIndex, withSubnode) 175 | * --------------------------------------------------------------------------- 176 | * Replaces a subnode and returns the replaced node, or null on failure. 177 | * ------------------------------------------------------------------------ */ 178 | 179 | Result 180 | ReplaceSubnode (IAstNode inNode, int atIndex, IAstNode withSubnode); 181 | 182 | 183 | /* --------------------------------------------------------------------------- 184 | * method ReplaceValue(inNode, atIndex, withValue) 185 | * --------------------------------------------------------------------------- 186 | * Replaces a value and returns the replaced value, or null on failure. 187 | * ------------------------------------------------------------------------ */ 188 | 189 | string ReplaceValue (IAstNode inNode, int atIndex, string withValue); 190 | 191 | 192 | } /* IAstNode */ 193 | 194 | } /* namespace */ 195 | 196 | /* END OF FILE */ 197 | -------------------------------------------------------------------------------- /IAstNodeQueue.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IAstNodeQueue.cs 26 | * 27 | * Public interface for AST node queue type. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace M2SF.M2Sharp { 47 | 48 | public interface IAstNodeQueue { 49 | 50 | /* -------------------------------------------------------------------------- 51 | * constructor newFromList(node, ...) 52 | * -------------------------------------------------------------------------- 53 | * Returns a newly allocated AST node queue object that includes the nodes 54 | * passed as arguments of a variadic argument list. 55 | * ----------------------------------------------------------------------- */ 56 | 57 | // public static AstNodeQueue newFromList (params AstNode[] nodeList); 58 | 59 | 60 | /* -------------------------------------------------------------------------- 61 | * method Count() 62 | * -------------------------------------------------------------------------- 63 | * Returns the number of nodes in the receiver. 64 | * ----------------------------------------------------------------------- */ 65 | 66 | public uint Count (); 67 | 68 | 69 | /* -------------------------------------------------------------------------- 70 | * method Contains(node) 71 | * -------------------------------------------------------------------------- 72 | * Returns true if node is stored in the receiver, else false. 73 | * ----------------------------------------------------------------------- */ 74 | 75 | public bool Contains (AstNode node); 76 | 77 | 78 | /* -------------------------------------------------------------------------- 79 | * method Enqueue(node) 80 | * -------------------------------------------------------------------------- 81 | * Enqueues node in the receiver. Returns true on success, else false. 82 | * ----------------------------------------------------------------------- */ 83 | 84 | public bool Enqueue (AstNode node); 85 | 86 | 87 | /* -------------------------------------------------------------------------- 88 | * method EnqueueUnique(node) 89 | * -------------------------------------------------------------------------- 90 | * Enqueues node in the receiver if and only if the node is not already 91 | * present in the receiver. Returns true on success, else false. 92 | * ----------------------------------------------------------------------- */ 93 | 94 | public bool EnqueueUnique (AstNode node); 95 | 96 | 97 | /* -------------------------------------------------------------------------- 98 | * method Dequeue() 99 | * -------------------------------------------------------------------------- 100 | * Removes the tail of the receiver and returns it, or null on failure. 101 | * ----------------------------------------------------------------------- */ 102 | 103 | public AstNode Dequeue (); 104 | 105 | 106 | /* -------------------------------------------------------------------------- 107 | * method Reset() 108 | * -------------------------------------------------------------------------- 109 | * Removes all entries from the receiver. 110 | * ----------------------------------------------------------------------- */ 111 | 112 | public void Reset (); 113 | 114 | 115 | } /* IAstNodeQueue */ 116 | 117 | } /* M2SF.M2Sharp */ 118 | 119 | /* END OF FILE */ -------------------------------------------------------------------------------- /IAstWriter.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IAstWriter.cs 26 | * 27 | * Public interface for AST output to file. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | public interface IAstWriter { 49 | 50 | /* --------------------------------------------------------------------------- 51 | * method WriteAst(path, ast, charsWritten) 52 | * --------------------------------------------------------------------------- 53 | * Writes the given abstract syntax tree in S-expression format to the given 54 | * output file at the given path and returns a status code. Passes the 55 | * number of characters written back in out-parameter chars_written. 56 | * ------------------------------------------------------------------------ */ 57 | 58 | IOStatus WriteAst (string path, IAstNode ast, out uint charsWritten); 59 | 60 | 61 | } /* IAstWriter */ 62 | 63 | } /* namespace */ 64 | 65 | /* END OF FILE */ 66 | -------------------------------------------------------------------------------- /ICSharpReswords.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * ICSharpReswords.cs 26 | * 27 | * Public interface for C# reserved word recogniser. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | public interface ICSharpReswords { 49 | 50 | /* --------------------------------------------------------------------------- 51 | * function IsResword(lexeme) 52 | * --------------------------------------------------------------------------- 53 | * Tests if lexeme matches reserved words and contextual reserved words of 54 | * the C# language and returns true in case of a match, otherwise false. 55 | * 56 | * Reserved words: 57 | * abstract, as, base, bool, break, byte, case, catch, char, checked, class, 58 | * const, continue, decimal, default, delegate, do, double, else, enum, 59 | * event, explicit, extern, false, finally, fixed, float, for, foreach, 60 | * goto, if, implicit, in, int, interface, internal, is, lock, long, 61 | * namespace, new, null, object, operator, out, override, params, private, 62 | * protected, public, readonly, ref, return, sbyte, sealed, short, sizeof, 63 | * stackalloc, static, string, struct, switch, this, throw, true, try, 64 | * typeof, uint, ulong, unchecked, unsafe, ushort, using, virtual, void, 65 | * volatile, while; 66 | * 67 | * Contextual reserved words: 68 | * add, alias, ascending, async, await, descending, dynamic, from, get, 69 | * global, group, into, join, let, orderby, partial, remove, select, set, 70 | * value, var, where, yield; 71 | * ------------------------------------------------------------------------ */ 72 | 73 | bool IsResword (string lexeme); 74 | 75 | 76 | } /* ICSharpReswords */ 77 | 78 | } /* namespace */ 79 | 80 | /* END OF FILE */ 81 | -------------------------------------------------------------------------------- /IDiagnostics.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IDiagnostics.cs 26 | * 27 | * Public diagnostics interface. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | using System.Collections.Generic; 49 | 50 | /* --------------------------------------------------------------------------- 51 | * type DiagCode 52 | * --------------------------------------------------------------------------- 53 | * Enumerated diagnostic codes representing compilation warnings and errors. 54 | * ------------------------------------------------------------------------ */ 55 | 56 | public enum DiagCode { 57 | /* Null Error */ 58 | 59 | Unknown, 60 | 61 | /* Option Errors */ 62 | 63 | ErrorInvalidOption, 64 | ErrorInvalidArgument, 65 | ErrorMissingFilename, 66 | ErrorInvalidFilename, 67 | ErrorInputFileNotFound, 68 | 69 | /* Lexical Warnings and Errors */ 70 | 71 | WarnDisabledCodeSection, 72 | ErrorInvalidInputChar, 73 | ErrorEndOfFileInBlockComment, 74 | ErrorNewLineInStringLiteral, 75 | ErrorEndOfFileInStringLiteral, 76 | ErrorInvalidEscapeSequence, 77 | ErrorEndOfFileInPragma, 78 | ErrorMissingStringDelimiter, 79 | ErrorMissingSuffix, 80 | ErrorMissingExponent, 81 | 82 | /* Syntax Warnings and Errors */ 83 | 84 | ErrorUnexpectedToken, 85 | WarnSemicolonAfterFieldListSeq, 86 | WarnEmptyFieldListSeq, 87 | WarnSemicolonAfterFormalParamList, 88 | WarnSemicolonAfterStatementSeq, 89 | WarnEmptyStatementSeq, 90 | ERROR_Y, /* Y */ 91 | 92 | /* Semantic Errors */ 93 | 94 | ERROR_Z /* Z */ 95 | 96 | } /* Code */ 97 | 98 | 99 | public interface IDiagnostics { 100 | 101 | /* --------------------------------------------------------------------------- 102 | * method isOptionError(code) 103 | * --------------------------------------------------------------------------- 104 | * Returns true if code represents an option error code, otherwise false. 105 | * ------------------------------------------------------------------------ */ 106 | 107 | bool isOptionError (DiagCode code); 108 | 109 | 110 | /* --------------------------------------------------------------------------- 111 | * method isLexicalError(code) 112 | * --------------------------------------------------------------------------- 113 | * Returns true if code represents a lexical error code, otherwise false. 114 | * ------------------------------------------------------------------------ */ 115 | 116 | bool isLexicalError (DiagCode code); 117 | 118 | 119 | /* --------------------------------------------------------------------------- 120 | * method isSyntaxError(error) 121 | * --------------------------------------------------------------------------- 122 | * Returns true if code represents a syntax error code, otherwise false. 123 | * ------------------------------------------------------------------------ */ 124 | 125 | bool isSyntaxError (DiagCode code); 126 | 127 | 128 | /* --------------------------------------------------------------------------- 129 | * method isSemanticError(code) 130 | * --------------------------------------------------------------------------- 131 | * Returns true if code represents a semantic error code, otherwise false. 132 | * ------------------------------------------------------------------------ */ 133 | 134 | bool isSemanticError (DiagCode code); 135 | 136 | 137 | /* --------------------------------------------------------------------------- 138 | * method diagMsgText(code) 139 | * --------------------------------------------------------------------------- 140 | * Returns a pointer to an immutable human readable message string for the 141 | * given diagnostic code or null if the code is not a valid diagnostic code. 142 | * ------------------------------------------------------------------------ */ 143 | 144 | string diagMsgText (DiagCode code); 145 | 146 | 147 | /* --------------------------------------------------------------------------- 148 | * method emitError(code) 149 | * --------------------------------------------------------------------------- 150 | * Emits an error message for code to the console. 151 | * ------------------------------------------------------------------------ */ 152 | 153 | void emitError (DiagCode code); 154 | 155 | 156 | /* --------------------------------------------------------------------------- 157 | * method emitErrorWithOffendingStr(code, offendingStr) 158 | * --------------------------------------------------------------------------- 159 | * Emits an error message for code and offendingStr to the console. 160 | * ------------------------------------------------------------------------ */ 161 | 162 | void emitErrorWithOffendingStr (DiagCode code, string offendingStr); 163 | 164 | 165 | /* --------------------------------------------------------------------------- 166 | * method emitErrorWithPos(code, line, column) 167 | * --------------------------------------------------------------------------- 168 | * Emits an error message for code, line and column to the console. 169 | * ------------------------------------------------------------------------ */ 170 | 171 | void emitErrorWithPos (DiagCode code, uint line, uint column); 172 | 173 | 174 | /* --------------------------------------------------------------------------- 175 | * method emitErrorWithChr(error, line, column, offendingChr) 176 | * --------------------------------------------------------------------------- 177 | * Emits an error message for code, line, column and offendingChr to the 178 | * console. 179 | * ------------------------------------------------------------------------ */ 180 | 181 | void emitErrorWithChr 182 | (DiagCode code, uint line, uint column, char offendingChr); 183 | 184 | 185 | /* --------------------------------------------------------------------------- 186 | * method m2c_emit_error_w_lex(error, line, column, offendingLexeme) 187 | * --------------------------------------------------------------------------- 188 | * Emits an error message for code, line, column and offendingLexeme to the 189 | * console. 190 | * ------------------------------------------------------------------------ */ 191 | 192 | void emitErrorWithLex 193 | (DiagCode code, uint line, uint column, string offendingLexeme); 194 | 195 | 196 | /* --------------------------------------------------------------------------- 197 | * method emitSyntaxErrorWithToken(line, col, unexpToken, offLex, expToken) 198 | * --------------------------------------------------------------------------- 199 | * Emits a syntax error message of the following format to the console: 200 | * line: n, column: m, unexpected offending-symbol offending-lexeme found 201 | * expected token 202 | * ------------------------------------------------------------------------ */ 203 | 204 | void emitSyntaxErrorWithToken 205 | (uint line, uint column, 206 | Token unexpectedToken, 207 | string offendingLexeme, 208 | Token expectedToken); 209 | 210 | 211 | /* --------------------------------------------------------------------------- 212 | * method emitSyntaxErrorWithSet(line, col, unexpToken, offLex, expTokenSet) 213 | * --------------------------------------------------------------------------- 214 | * Emits a syntax error message of the following format to the console: 215 | * line: n, column: m, unexpected offending-symbol offending-lexeme found 216 | * expected set-symbol-1, set-symbol-2, set-symbol-3, ... or set-symbol-N 217 | * ------------------------------------------------------------------------ */ 218 | 219 | void emitSyntaxErrorWithSet 220 | (uint line, uint column, 221 | Token unexpectedToken, 222 | string offendingLexeme, 223 | SortedSet expectedTokenSet); 224 | 225 | 226 | /* --------------------------------------------------------------------------- 227 | * method emitWarningWithPos(code, line, column) 228 | * --------------------------------------------------------------------------- 229 | * Emits a warning message for code, line and column to the console. 230 | * ------------------------------------------------------------------------ */ 231 | 232 | void emitWarningWithPos (DiagCode code, uint line, uint column); 233 | 234 | 235 | /* --------------------------------------------------------------------------- 236 | * method emitWarningWithRange(error, firstLine, lastLine) 237 | * --------------------------------------------------------------------------- 238 | * Emits a warning message for range from firstLine to lastLine. 239 | * ------------------------------------------------------------------------ */ 240 | 241 | void emitWarningWithRange (DiagCode code, uint firstLine, uint lastLine); 242 | 243 | 244 | } /* ProtoDiagnostics */ 245 | 246 | } /* namespace */ 247 | 248 | /* END OF FILE */ 249 | -------------------------------------------------------------------------------- /IDialects.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IDialects.cs 26 | * 27 | * public interface of compiler dialect settings class 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | using System; 47 | 48 | namespace org.m2sf.m2sharp { 49 | 50 | /* --------------------------------------------------------------------------- 51 | * type Dialect 52 | * --------------------------------------------------------------------------- 53 | * Enumerated values representing supported dialects. 54 | * ------------------------------------------------------------------------ */ 55 | 56 | public enum Dialect { 57 | 58 | /* Programming in Modula-2, 3rd Edition, 1983 */ 59 | 60 | PIM3, 61 | 62 | /* Programming in Modula-2, 4th Edition, 1985 */ 63 | 64 | PIM4, 65 | 66 | /* Extended with selected features from Modula-2 Revision 2010 */ 67 | 68 | Extended 69 | } /* end Dialect */ 70 | 71 | 72 | /* --------------------------------------------------------------------------- 73 | * Capabilities by Dialect 74 | * --------------------------------------------------------------------------- 75 | * 76 | * Capabilities | PIM3 | PIM4 | Extended 77 | * ------------------------+----------------+----------------+---------------- 78 | * Synonyms | off, mutable | off, mutable | off, immutable 79 | * LineComments | off, immutable | off, immutable | on, immutable 80 | * PrefixLiterals | off, immutable | off, immutable | on, immutable 81 | * SuffixLiterals | on, immutable | on, immutable | off, immutable 82 | * OctalLiterals | off, mutable | off, mutable | off, immutable 83 | * LowlineIdentifiers | off, immutable | off, immutable | off, mutable 84 | * EscapeTabAndNewline | off, immutable | off, immutable | on, immutable 85 | * BackslashSetDiffOp | off, immutable | off, immutable | on, immutable 86 | * PostfixIncAndDec | off, immutable | off, immutable | on, immutable 87 | * IntraCommentPragmas | on, immutable | on, immutable | off, immutable 88 | * IsoPragmaDelimiters | off, immutable | off, immutable | on, immutable 89 | * ------------------------+----------------+----------------+---------------- 90 | * SubtypedCardinals | off, immutable | on, immutable | off, immutable 91 | * SafeStringTermination | off, immutable | on, immutable | on, immutable 92 | * ImportVarsImmutable | off, immutable | off, immutable | on, immutable 93 | * ConstParameters | off, immutable | off, immutable | on, immutable 94 | * VariadicParameters | off, immutable | off, immutable | on, immutable 95 | * AdditionalTypes | off, immutable | off, immutable | on, immutable 96 | * AdditionalFunctions | off, immutable | off, immutable | on, immutable 97 | * UnifiedConversion | off, immutable | off, immutable | on, immutable 98 | * ExplicitCast | on, mutable | on, mutable | on, immutable 99 | * ------------------------+----------------+----------------+---------------- 100 | * Coroutines | off, mutable | off, mutable | off, immutable 101 | * VariantRecords | off, mutable | off, mutable | off, immutable 102 | * ExtensibleRecords | off, immutable | off, immutable | on, immutable 103 | * IndeterminateRecords | off, immutable | off, immutable | on, immutable 104 | * UnqualifiedImport | on, immutable | on, immutable | off, immutable 105 | * LocalModules | off, mutable | off, mutable | off, immutable 106 | * WithStatement | on, immutable | on, immutable | off, immutable 107 | * ToDoStatement | off, immutable | off, immutable | on, mutable 108 | * ------------------------+----------------+----------------+---------------- 109 | * 110 | * Default capabilities are denoted by value "on". 111 | * Mutable capabilities are denoted by value "mutable". 112 | * ------------------------------------------------------------------------ */ 113 | 114 | 115 | /* --------------------------------------------------------------------------- 116 | * interface IDialects 117 | * --------------------------------------------------------------------------- 118 | * This interface describes a singleton class. 119 | * Since C# does not fully support the concept of information hiding, this 120 | * interface is entirely comprised of comments for documentation purposes. 121 | * ------------------------------------------------------------------------ */ 122 | 123 | public interface IDialects { 124 | 125 | /* --------------------------------------------------------------------------- 126 | * method DefaultCapabilitiesFor(dialect) 127 | * --------------------------------------------------------------------------- 128 | * Returns an unsigned integer representing a bitset that contains all default 129 | * capabilities for the given dialect. Bits are indexed, with the least sig- 130 | * nificant bit having index 0. The indices correspond to the enumerated 131 | * capability values of enumeration type Capability. 132 | * ------------------------------------------------------------------------ */ 133 | 134 | // public static uint DefaultCapabilitiesFor ( Dialect dialect ); 135 | 136 | 137 | /* --------------------------------------------------------------------------- 138 | * method IsDefaultCapability(dialect, capability) 139 | * --------------------------------------------------------------------------- 140 | * Returns true if the given capability is a default capability of the given 141 | * dialect, else false. 142 | * ------------------------------------------------------------------------ */ 143 | 144 | // public static bool IsDefaultCapability ( Dialect dialect, Capability cap ); 145 | 146 | 147 | /* --------------------------------------------------------------------------- 148 | * method IsMutableCapability(dialect, capability) 149 | * --------------------------------------------------------------------------- 150 | * Returns true if the given capability is a mutable capability of the given 151 | * dialect, else false. 152 | * ------------------------------------------------------------------------ */ 153 | 154 | // public static bool IsMutableCapability ( Dialect dialect, Capability cap ); 155 | 156 | 157 | } /* IDialects */ 158 | 159 | } /* namespace */ 160 | 161 | /* END OF FILE */ -------------------------------------------------------------------------------- /IDotWriter.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IDotWriter.cs 26 | * 27 | * Public interface for AST export to GraphViz DOT. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | public interface IDotWriter { 49 | 50 | /* --------------------------------------------------------------------------- 51 | * method WriteDot(path, ast, charsWritten) 52 | * --------------------------------------------------------------------------- 53 | * Writes the given abstract syntax tree in GraphViz DOT format to the given 54 | * output file at the given path and returns a status code. Passes the 55 | * number of characters written back in out-parameter chars_written. 56 | * ------------------------------------------------------------------------ */ 57 | 58 | IOStatus WriteDot (string path, IAstNode ast, out uint charsWritten); 59 | 60 | 61 | } /* IDotWriter */ 62 | 63 | } /* namespace */ 64 | 65 | /* END OF FILE */ 66 | -------------------------------------------------------------------------------- /IInfile.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IInfile.cs 26 | * 27 | * Public interface for M2Sharp source file reader. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* --------------------------------------------------------------------------- 49 | * Infile status codes 50 | * ------------------------------------------------------------------------ */ 51 | 52 | public enum InfileStatus { 53 | Success, 54 | InvalidReference, 55 | FileNotFound, 56 | FileAccessDenied, 57 | AllocationFailed, 58 | SourceFileIsEmpty, 59 | AttemptToReadPastEOF, 60 | IOSubsystemError 61 | } /* InfileStatus */ 62 | 63 | 64 | public interface IInfile { 65 | 66 | /* --------------------------------------------------------------------------- 67 | * Properties: file size, line and column counter limits 68 | * ------------------------------------------------------------------------ */ 69 | 70 | uint MaxSize (); /* returns the maximum file buffer size (260000) */ 71 | 72 | uint MaxLines (); /* returns the line counter limit (64000) */ 73 | 74 | uint MaxColumns (); /* returns the column counter limit (250) */ 75 | 76 | 77 | /* --------------------------------------------------------------------------- 78 | * constructor Open(filename) 79 | * --------------------------------------------------------------------------- 80 | * Opens the given file, creates a new infile instance, associates the file 81 | * with the newly created instance and returns a result pair with the infile 82 | * reference and a status code. 83 | * 84 | * pre-conditions: 85 | * o filename must reference an existing, accessible file. 86 | * 87 | * post-conditions: 88 | * o new infile created and returned 89 | * o line and column counters of the newly created infile are set to 1 90 | * o Success is returned in status 91 | * 92 | * error-conditions: 93 | * o if the file represented by filename cannot be found 94 | * infile is null, status is FileNotFound 95 | * o if the file represented by filename cannot be accessed 96 | * infile is null, status is FileAccessDenied 97 | * ------------------------------------------------------------------------ */ 98 | 99 | Result Open (string filename); 100 | 101 | 102 | /* --------------------------------------------------------------------------- 103 | * method ReadChar() 104 | * --------------------------------------------------------------------------- 105 | * Reads the lookahead character from infile, advancing the current reading 106 | * position, updating line and column counter and returns its character code. 107 | * Returns ASCII.EOT if the lookahead character lies beyond the end of infile. 108 | * 109 | * pre-conditions: 110 | * o infile must be open 111 | * 112 | * post-conditions: 113 | * o character code of lookahead character or ASCII.EOT is returned 114 | * o current reading position and line and column counters are updated 115 | * o infile status is set to Success 116 | * 117 | * error-conditions: 118 | * o none 119 | * ------------------------------------------------------------------------ */ 120 | 121 | char ReadChar (); 122 | 123 | 124 | /* --------------------------------------------------------------------------- 125 | * method MarkLexeme() 126 | * --------------------------------------------------------------------------- 127 | * Marks the current lookahead character as the start of a lexeme. 128 | * 129 | * pre-conditions: 130 | * o infile must be open 131 | * 132 | * post-conditions: 133 | * o position of lookahead character is stored internally 134 | * 135 | * error-conditions: 136 | * o none 137 | * ------------------------------------------------------------------------ */ 138 | 139 | void MarkLexeme (); 140 | 141 | 142 | /* --------------------------------------------------------------------------- 143 | * method ReadMarkedLexeme() 144 | * --------------------------------------------------------------------------- 145 | * Returns a string object with the character sequence starting with the 146 | * character that has been marked using method markLexeme() and ending 147 | * with the last consumed character. Returns null if no marker has 148 | * been set or if the marked character has not been consumed yet. 149 | * 150 | * pre-conditions: 151 | * o infile must be open 152 | * o lexeme must have been marked using method markLexeme() 153 | * o character at the marked position must have been consumed 154 | * 155 | * post-conditions: 156 | * o marked position is cleared 157 | * o string with lexeme is returned 158 | * 159 | * error-conditions: 160 | * o if no marker has been set or marked character has not been consumed, 161 | * no operation is carried out and null is returned 162 | * ------------------------------------------------------------------------ */ 163 | 164 | string ReadMarkedLexeme (); 165 | 166 | 167 | /* --------------------------------------------------------------------------- 168 | * method SourceForLine(line) 169 | * --------------------------------------------------------------------------- 170 | * Returns a string object with the source of the given line number. 171 | * 172 | * pre-conditions: 173 | * o infile must be open 174 | * o parameter line must be greater than zero 175 | * 176 | * post-conditions: 177 | * o string with source of line is returned 178 | * 179 | * error-conditions: 180 | * o line is negative or zero upon entry, 181 | * no operation is carried out and null is returned 182 | * ------------------------------------------------------------------------ */ 183 | 184 | string SourceForLine (uint line); 185 | 186 | 187 | /* --------------------------------------------------------------------------- 188 | * method ConsumeChar() 189 | * --------------------------------------------------------------------------- 190 | * Consumes the current lookahead character, advancing the current reading 191 | * position, updating line and column counter and returns the character code 192 | * of the new lookahead character that follows the consumed character. 193 | * Returns ASCII.EOT if the lookahead character lies beyond the end of infile. 194 | * 195 | * pre-conditions: 196 | * o infile must be open 197 | * 198 | * post-conditions: 199 | * o character code of lookahead character or ASCII.EOT is returned 200 | * o current reading position and line and column counters are updated 201 | * o file status is set to Success 202 | * 203 | * error-conditions: 204 | * o none 205 | * ------------------------------------------------------------------------ */ 206 | 207 | char ConsumeChar (); 208 | 209 | 210 | /* --------------------------------------------------------------------------- 211 | * method NextChar() 212 | * --------------------------------------------------------------------------- 213 | * Reads the lookahead character from infile without advancing the current 214 | * reading position and returns its character code. Returns ASCII.EOT if 215 | * the lookahead character lies beyond the end of infile. 216 | * 217 | * pre-conditions: 218 | * o infile must be open 219 | * 220 | * post-conditions: 221 | * o character code of lookahead character or ASCII.EOT is returned 222 | * o current reading position and line and column counters are NOT updated 223 | * o file status is set to Success 224 | * 225 | * error-conditions: 226 | * o none 227 | * ------------------------------------------------------------------------ */ 228 | 229 | char NextChar (); 230 | 231 | 232 | /* --------------------------------------------------------------------------- 233 | * method LA2Char() 234 | * --------------------------------------------------------------------------- 235 | * Reads the second lookahead character from infile without advancing the 236 | * current reading position and returns its character code. Returns ASCII.EOT 237 | * if the second lookahead character lies beyond the end of infile. 238 | * 239 | * pre-conditions: 240 | * o infile must be open 241 | * 242 | * post-conditions: 243 | * o character code of second lookahead character or EOT is returned 244 | * o current reading position and line and column counters are NOT updated 245 | * o file status is set to Success 246 | * 247 | * error-conditions: 248 | * o none 249 | * ------------------------------------------------------------------------ */ 250 | 251 | char LA2Char (); 252 | 253 | 254 | /* --------------------------------------------------------------------------- 255 | * method filename() 256 | * --------------------------------------------------------------------------- 257 | * Returns the filename associated with infile. 258 | * ------------------------------------------------------------------------ */ 259 | 260 | string filename (); 261 | 262 | 263 | /* --------------------------------------------------------------------------- 264 | * method Status() 265 | * --------------------------------------------------------------------------- 266 | * Returns the status of the last operation on infile. 267 | * ------------------------------------------------------------------------ */ 268 | 269 | InfileStatus Status (); 270 | 271 | 272 | /* --------------------------------------------------------------------------- 273 | * method EOF() 274 | * --------------------------------------------------------------------------- 275 | * Returns true if the current reading position of infile lies beyond the end 276 | * of the associated file, returns false otherwise. This method should be 277 | * called whenever ASCII.EOT is read to ascertain that EOF has been reached. 278 | * ------------------------------------------------------------------------ */ 279 | 280 | bool EOF (); 281 | 282 | 283 | /* --------------------------------------------------------------------------- 284 | * method CurrentLine() 285 | * --------------------------------------------------------------------------- 286 | * Returns the current line counter of infile. 287 | * ------------------------------------------------------------------------ */ 288 | 289 | uint CurrentLine (); 290 | 291 | 292 | /* --------------------------------------------------------------------------- 293 | * method CurrentColumn() 294 | * --------------------------------------------------------------------------- 295 | * Returns the current column counter of infile. 296 | * ------------------------------------------------------------------------ */ 297 | 298 | uint CurrentColumn (); 299 | 300 | 301 | /* --------------------------------------------------------------------------- 302 | * method Close() 303 | * --------------------------------------------------------------------------- 304 | * Closes the file associated with infile and returns a status code. 305 | * 306 | * pre-conditions: 307 | * o infile must be open 308 | * 309 | * post-conditions: 310 | * o associated file is closed 311 | * o Success is returned 312 | * 313 | * error-conditions: 314 | * o none 315 | * ------------------------------------------------------------------------ */ 316 | 317 | InfileStatus Close (); 318 | 319 | 320 | } /* IInfile */ 321 | 322 | } /* M2SF.M2Sharp */ 323 | 324 | /* END OF FILE */ 325 | -------------------------------------------------------------------------------- /ILexer.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * ILexer.cs 26 | * 27 | * Public lexer interface. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* --------------------------------------------------------------------------- 49 | * Lexer status codes 50 | * ------------------------------------------------------------------------ */ 51 | 52 | public enum LexerStatus { 53 | Success, 54 | FileNotFound, 55 | FileAccessDenied, 56 | NotInitialized, 57 | TODO 58 | } /* LexerStatus */ 59 | 60 | 61 | public interface ILexer { 62 | 63 | /* --------------------------------------------------------------------------- 64 | * Factory Methods 65 | * --------------------------------------------------------------------------- 66 | * Since C# does not fully support the concept of information hiding, 67 | * factory methods are specified as comments for documentation purposes. 68 | * The class constructor must be hidden to prevent clients from using it. 69 | * ------------------------------------------------------------------------ */ 70 | 71 | 72 | /* --------------------------------------------------------------------------- 73 | * Lexical limits 74 | * ------------------------------------------------------------------------ */ 75 | 76 | uint MaxIdentLength (); /* default 32 */ 77 | 78 | uint CommentNestingLimit (); /* default 100 */ 79 | 80 | 81 | /* --------------------------------------------------------------------------- 82 | * factory method newLexer(filename) 83 | * --------------------------------------------------------------------------- 84 | * Creates a new lexer instance, opens an input file, associates the file with 85 | * the newly created lexer and returns a Result pair with the lexer reference 86 | * and a status value. 87 | * 88 | * pre-conditions: 89 | * o filename must refer to an existing and accessible input file 90 | * 91 | * post-conditions: 92 | * o lexer is created 93 | * o status is set to Success 94 | * 95 | * error-conditions: 96 | * o if the file represented by filename cannot be found 97 | * lexer is set to null, status is set to FileNotFound 98 | * o if the file represented by filename cannot be accessed 99 | * lexer is set to null, status is set to FileAccessDenied 100 | * ----------------------------------------------------------------------- */ 101 | 102 | // public static Result NewLexer (string filename); 103 | 104 | 105 | /* -------------------------------------------------------------------------- 106 | * method ReadSym() 107 | * -------------------------------------------------------------------------- 108 | * Reads the lookahead symbol from the source file associated with lexer and 109 | * consumes it, thus advancing the current reading position, then returns 110 | * the symbol's token. 111 | * 112 | * pre-conditions: 113 | * o lexer instance must have been created with constructor NewLexer() 114 | * so that it is associated with an input source file 115 | * 116 | * post-conditions: 117 | * o character code of lookahead character or EOF is returned 118 | * o current reading position and line and column counters are updated 119 | * o file status is set to Success 120 | * 121 | * error-conditions: 122 | * o if no source file is associated with lexer, no operation is carried out 123 | * and status is set to NotInitialized 124 | * ----------------------------------------------------------------------- */ 125 | 126 | Token ReadSym(); 127 | 128 | 129 | /* -------------------------------------------------------------------------- 130 | * method NextSym() 131 | * -------------------------------------------------------------------------- 132 | * Reads the lookahead symbol from the source file associated with lexer but 133 | * does not consume it, thus not advancing the current reading position, 134 | * then returns the symbol's token. 135 | * 136 | * pre-conditions: 137 | * o lexer instance must have been created with constructor NewLexer() 138 | * so that it is associated with an input source file 139 | * 140 | * post-conditions: 141 | * o token of lookahead symbol is returned 142 | * o current reading position and line and column counters are NOT updated 143 | * o file status is set to Success 144 | * 145 | * error-conditions: 146 | * o if no source file is associated with lexer, no operation is carried out 147 | * and status is set to NotInitialized 148 | * ----------------------------------------------------------------------- */ 149 | 150 | Token NextSym (); 151 | 152 | 153 | /* -------------------------------------------------------------------------- 154 | * method ConsumeSym() 155 | * -------------------------------------------------------------------------- 156 | * Consumes the lookahead symbol and returns the token of the new lookahead 157 | * symbol. 158 | * ----------------------------------------------------------------------- */ 159 | 160 | Token ConsumeSym (); 161 | 162 | 163 | /* -------------------------------------------------------------------------- 164 | * method Filename() 165 | * -------------------------------------------------------------------------- 166 | * Returns the filename associated with the lexer instance. 167 | * ----------------------------------------------------------------------- */ 168 | 169 | string Filename (); 170 | 171 | 172 | /* -------------------------------------------------------------------------- 173 | * method Status() 174 | * -------------------------------------------------------------------------- 175 | * Returns the status of the last operation on lexer. 176 | * ----------------------------------------------------------------------- */ 177 | 178 | LexerStatus Status (); 179 | 180 | 181 | /* -------------------------------------------------------------------------- 182 | * method LookaheadLexeme() 183 | * -------------------------------------------------------------------------- 184 | * Returns the lexeme of the lookahead symbol. 185 | * ----------------------------------------------------------------------- */ 186 | 187 | string LookaheadLexeme (); 188 | 189 | 190 | /* -------------------------------------------------------------------------- 191 | * method CurrentLexeme() 192 | * -------------------------------------------------------------------------- 193 | * Returns the lexeme of the most recently consumed symbol. 194 | * ----------------------------------------------------------------------- */ 195 | 196 | string CurrentLexeme (); 197 | 198 | 199 | /* -------------------------------------------------------------------------- 200 | * method LookaheadLine() 201 | * -------------------------------------------------------------------------- 202 | * Returns the line counter of the lookahead symbol. 203 | * ----------------------------------------------------------------------- */ 204 | 205 | uint LookaheadLine (); 206 | 207 | 208 | /* -------------------------------------------------------------------------- 209 | * method CurrentLine() 210 | * -------------------------------------------------------------------------- 211 | * Returns the line counter of the most recently consumed symbol. 212 | * ----------------------------------------------------------------------- */ 213 | 214 | uint CurrentLine (); 215 | 216 | 217 | /* -------------------------------------------------------------------------- 218 | * method lookaheadColumn() 219 | * -------------------------------------------------------------------------- 220 | * Returns the column counter of the lookahead symbol. 221 | * ----------------------------------------------------------------------- */ 222 | 223 | uint LookaheadColumn (); 224 | 225 | 226 | /* -------------------------------------------------------------------------- 227 | * method CurrentColumn() 228 | * -------------------------------------------------------------------------- 229 | * Returns the column counter of the most recently consumed symbol. 230 | * ----------------------------------------------------------------------- */ 231 | 232 | uint CurrentColumn (); 233 | 234 | 235 | /* -------------------------------------------------------------------------- 236 | * method PrintLineAndMarkColumn(line, column) 237 | * -------------------------------------------------------------------------- 238 | * Prints the given source line of the current symbol to the console and 239 | * marks the given coloumn with a caret '^'. 240 | * ----------------------------------------------------------------------- */ 241 | 242 | void PrintLineAndMarkColumn (uint line, uint column); 243 | 244 | 245 | } /* ILexer */ 246 | 247 | } /* namespace */ 248 | 249 | /* END OF FILE */ 250 | -------------------------------------------------------------------------------- /INonTerminals.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * INonTerminals.cs 26 | * 27 | * Public interface for Non-Terminal's FIRST and FOLLOW set lookup. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace M2SF.M2Sharp { 47 | 48 | /* -------------------------------------------------------------------------- 49 | * type Production 50 | * -------------------------------------------------------------------------- 51 | * Enumerated production values representing Modula-2 non-terminal symbols. 52 | * ----------------------------------------------------------------------- */ 53 | 54 | public enum Production { 55 | /* Productions with unique FIRST and FOLLOW sets */ 56 | 57 | DefinitionModule, /* definitionModule */ 58 | Import, /* import */ 59 | QualifiedImport, /* qualifiedImport */ 60 | UnqualifiedImport, /* unqualifiedImport */ 61 | IdentList, /* identList */ 62 | Definition, /* definition */ 63 | ConstDefinition, /* constDefinition */ 64 | TypeDefinition, /* typeDefinition */ 65 | Type, /* type */ 66 | DerivedOrSubrangeType, /* derivedOrSubrangeType */ 67 | Qualident, /* qualident */ 68 | Range, /* range */ 69 | EnumType, /* enumType */ 70 | SetType, /* setType */ 71 | CountableType, /* countableType */ 72 | ArrayType, /* arrayType */ 73 | ExtensibleRecordType, /* extensibleRecordType */ 74 | FieldListSequence, /* fieldListSequence */ 75 | VariantRecordType, /* variantRecordType */ 76 | VariantFieldListSeq, /* variantFieldListSeq */ 77 | VariantFieldList, /* variantFieldList */ 78 | VariantFields, /* variantFields */ 79 | Variant, /* variant */ 80 | CaseLabelList, /* caseLabelList */ 81 | CaseLabels, /* caseLabels */ 82 | PointerType, /* pointerType */ 83 | ProcedureType, /* procedureType */ 84 | SimpleFormalType, /* simpleFormalType */ 85 | ProcedureHeader, /* procedureHeader */ 86 | ProcedureSignature, /* procedureSignature */ 87 | SimpleFormalParams, /* simpleFormalParams */ 88 | ImplementationModule, /* implementationModule */ 89 | ProgramModule, /* programModule */ 90 | ModulePriority, /* modulePriority */ 91 | Block, /* block */ 92 | Declaration, /* declaration */ 93 | TypeDeclaration, /* typeDeclaration */ 94 | VarSizeRecordType, /* varSizeRecordType */ 95 | VariableDeclaration, /* variableDeclaration */ 96 | ProcedureDeclaration, /* procedureDeclaration */ 97 | ModuleDeclaration, /* moduleDeclaration */ 98 | Export, /* export */ 99 | StatementSequence, /* statementSequence */ 100 | Statement, /* statement */ 101 | AssignmentOrProcCall, /* assignmentOrProcCall */ 102 | ActualParameters, /* actualParameters */ 103 | ExpressionList, /* expressionList */ 104 | ReturnStatement, /* returnStatement */ 105 | WithStatement, /* withStatement */ 106 | IfStatement, /* ifStatement */ 107 | CaseStatement, /* caseStatement */ 108 | Case, /* case */ 109 | LoopStatement, /* loopStatement */ 110 | WhileStatement, /* whileStatement */ 111 | RepeatStatement, /* repeatStatement */ 112 | ForStatement, /* forStatement */ 113 | Designator, /* designator */ 114 | Selector, /* selector */ 115 | Expression, /* expression */ 116 | SimpleExpression, /* simpleExpression */ 117 | Term, /* term */ 118 | SimpleTerm, /* simpleTerm */ 119 | Factor, /* factor */ 120 | DesignatorOrFuncCall, /* designatorOrFuncCall */ 121 | SetValue, /* setValue */ 122 | Element, /* element */ 123 | 124 | /* Productions with alternative FIRST or FOLLOW sets */ 125 | 126 | /* Dependent on option --const-parameters */ 127 | FormalType, /* formalType */ 128 | AttributedFormalType, /* attributedFormalType */ 129 | FormalParamList, /* formalParamList */ 130 | FormalParams, /* formalParams */ 131 | AttribFormalParams, /* attribFormalParams */ 132 | 133 | /* Dependent on option --no-variant-records */ 134 | TypeDeclarationTail; /* typeDeclarationTail */ 135 | 136 | } /* Production */ 137 | 138 | 139 | interface INonTerminals { 140 | 141 | /* -------------------------------------------------------------------------- 142 | * method Count() -- Returns the number of productions 143 | * ----------------------------------------------------------------------- */ 144 | 145 | public uint Count (); 146 | 147 | 148 | /* -------------------------------------------------------------------------- 149 | * method IsOptionDependent(p) 150 | * -------------------------------------------------------------------------- 151 | * Returns true if p is dependent on any compiler option, else false. 152 | * ----------------------------------------------------------------------- */ 153 | 154 | public bool IsOptionDependent (Production p); 155 | 156 | 157 | /* -------------------------------------------------------------------------- 158 | * method IsConstParamDependent(p) 159 | * -------------------------------------------------------------------------- 160 | * Returns true if p is dependent on CONST parameter option, else false. 161 | * ----------------------------------------------------------------------- */ 162 | 163 | public bool IsConstParamDependent (Production p); 164 | 165 | 166 | /* -------------------------------------------------------------------------- 167 | * method IsVariantRecordDependent(p) 168 | * -------------------------------------------------------------------------- 169 | * Returns true if p is dependent on variant record type option, else false. 170 | * ----------------------------------------------------------------------- */ 171 | 172 | public bool IsVariantRecordDependent (Production p); 173 | 174 | 175 | /* -------------------------------------------------------------------------- 176 | * method FIRST(p) 177 | * -------------------------------------------------------------------------- 178 | * Returns a tokenset with the FIRST set of production p. 179 | * ----------------------------------------------------------------------- */ 180 | 181 | public TokenSet FIRST (Production p); 182 | 183 | 184 | /* -------------------------------------------------------------------------- 185 | * method FOLLOW(p) 186 | * -------------------------------------------------------------------------- 187 | * Returns a tokenset with the FOLLOW set of production p. 188 | * ----------------------------------------------------------------------- */ 189 | 190 | public TokenSet FOLLOW (Production p); 191 | 192 | 193 | /* -------------------------------------------------------------------------- 194 | * method NameForProduction(p) 195 | * -------------------------------------------------------------------------- 196 | * Returns a string with a human readable name for production p. 197 | * ----------------------------------------------------------------------- */ 198 | 199 | public string NameForProduction (Production p); 200 | 201 | 202 | } /* INonTerminals */ 203 | 204 | } /* M2SF.M2Sharp */ 205 | 206 | /* END OF FILE */ -------------------------------------------------------------------------------- /IParser.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IParser.cs 26 | * 27 | * Public parser interface. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* --------------------------------------------------------------------------- 49 | * Parser status codes 50 | * ------------------------------------------------------------------------ */ 51 | 52 | public enum ParserStatus { 53 | Success, 54 | FileNotFound, 55 | FileAccessDenied, 56 | TODO 57 | } /* ParserStatus */ 58 | 59 | 60 | public interface IParser { 61 | 62 | /* --------------------------------------------------------------------------- 63 | * method CheckSyntaxForDef(filename) 64 | * --------------------------------------------------------------------------- 65 | * Syntax checks a Modula-2 .DEF file represented by filename. 66 | * ------------------------------------------------------------------------ */ 67 | 68 | ParserStatus CheckSyntaxForDef (string filename); 69 | 70 | 71 | /* --------------------------------------------------------------------------- 72 | * method CheckSyntaxForMod(filename) 73 | * --------------------------------------------------------------------------- 74 | * Syntax checks a Modula-2 .MOD file represented by filename. 75 | * ------------------------------------------------------------------------ */ 76 | 77 | ParserStatus CheckSyntaxForMod (string filename); 78 | 79 | 80 | /* --------------------------------------------------------------------------- 81 | * method ParseDef(filename) 82 | * --------------------------------------------------------------------------- 83 | * Parses a Modula-2 .DEF file represented by filename and returns an AST. 84 | * ------------------------------------------------------------------------ */ 85 | 86 | IAstNode ParseDef (string filename); 87 | 88 | 89 | /* --------------------------------------------------------------------------- 90 | * method ParseMod(filename) 91 | * --------------------------------------------------------------------------- 92 | * Parses a Modula-2 .MOD file represented by filename and returns an AST. 93 | * ------------------------------------------------------------------------ */ 94 | 95 | IAstNode ParseMod (string filename); 96 | 97 | 98 | } /* IParser */ 99 | 100 | } /* namespace */ 101 | 102 | /* END OF FILE */ 103 | -------------------------------------------------------------------------------- /IPathname.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * IPathname.cs 26 | * 27 | * Public interface to portable pathname library. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | 49 | /* --------------------------------------------------------------------------- 50 | * Filetypes 51 | * ------------------------------------------------------------------------ */ 52 | 53 | public enum SuffixTypes { 54 | None, /* no suffix */ 55 | Def, /* .def or .DEF */ 56 | Mod, /* .mod or .MOD */ 57 | Sym, /* .sym or .SYM */ 58 | Ast, /* .ast or .AST */ 59 | Dot, /* .dot or .DOT */ 60 | Obj, /* .obj or .OBJ */ 61 | Other /* any others */ 62 | } /* SuffixTypes */ 63 | 64 | 65 | /* --------------------------------------------------------------------------- 66 | * Pathname status codes 67 | * ------------------------------------------------------------------------ */ 68 | 69 | public enum PathnameStatus { 70 | Success, 71 | InvalidPath, 72 | InvalidFilename, 73 | InvalidReference, 74 | AllocationFailed 75 | } /* PathnameStatus */ 76 | 77 | 78 | public interface ILexer { 79 | 80 | /* --------------------------------------------------------------------------- 81 | * Factory Methods 82 | * --------------------------------------------------------------------------- 83 | * Since C# does not fully support the concept of information hiding, 84 | * factory methods are specified as comments for documentation purposes. 85 | * The class constructor must be hidden to prevent clients from using it. 86 | * ------------------------------------------------------------------------ */ 87 | 88 | /* --------------------------------------------------------------------------- 89 | * factory method NewFromOSPath(osPath) 90 | * --------------------------------------------------------------------------- 91 | * Creates a new pathname instance, initialised with the path given in osPath 92 | * and returns a Result pair with the pathname reference and a status value. 93 | * 94 | * pre-conditions: 95 | * o the path given in osPath must be a valid pathname 96 | * 97 | * post-conditions: 98 | * o pathname is created 99 | * o status is set to Success 100 | * 101 | * error-conditions: 102 | * o if the path given in osPath is not a valid pathname 103 | * pathname is set to null, status is set to InvalidPath 104 | * ----------------------------------------------------------------------- */ 105 | 106 | // public static Result 107 | // NewFromOSPath (String osPath); 108 | 109 | 110 | /* --------------------------------------------------------------------------- 111 | * factory method NewFromComponents(dirpath, filename, suffix) 112 | * --------------------------------------------------------------------------- 113 | * Creates a new pathname instance, initialised with the given components 114 | * and returns a Result pair with the pathname reference and a status value. 115 | * 116 | * pre-conditions: 117 | * o the path represented by the components must be a valid pathname 118 | * 119 | * post-conditions: 120 | * o pathname is created 121 | * o status is set to Success 122 | * 123 | * error-conditions: 124 | * o if the path represented by the components is not a valid pathname 125 | * pathname is set to null, status is set to InvalidPath 126 | * ----------------------------------------------------------------------- */ 127 | 128 | // public static Result 129 | // NewFromComponents (String dirpath, String filename, String suffix); 130 | 131 | 132 | /* -------------------------------------------------------------------------- 133 | * method FullPath() 134 | * -------------------------------------------------------------------------- 135 | * Returns an interned string with the full pathname of the receiver. 136 | * 137 | * pre-conditions: 138 | * o the receiver must have been created with constructor NewFromOSPath() 139 | * or NewFromComponents() so that it is associated with a valid OS path. 140 | * 141 | * post-conditions: 142 | * o interned string with the full pathname of the receiver is returned 143 | * 144 | * error-conditions: 145 | * o if no OS path is associated with the receiver, null is returned 146 | * ----------------------------------------------------------------------- */ 147 | 148 | public String FullPath(); 149 | 150 | 151 | /* -------------------------------------------------------------------------- 152 | * method DirPath() 153 | * -------------------------------------------------------------------------- 154 | * Returns an interned string with the directory path of the receiver. 155 | * 156 | * pre-conditions: 157 | * o the receiver must have been created with constructor NewFromOSPath() 158 | * or NewFromComponents() so that it is associated with a valid OS path. 159 | * 160 | * post-conditions: 161 | * o interned string with the directory path of the receiver is returned 162 | * 163 | * error-conditions: 164 | * o if no OS path is associated with the receiver, null is returned 165 | * ----------------------------------------------------------------------- */ 166 | 167 | public String DirPath(); 168 | 169 | 170 | /* -------------------------------------------------------------------------- 171 | * method Filename() 172 | * -------------------------------------------------------------------------- 173 | * Returns an interned string with the filename of the receiver. 174 | * 175 | * pre-conditions: 176 | * o the receiver must have been created with constructor NewFromOSPath() 177 | * or NewFromComponents() so that it is associated with a valid OS path. 178 | * 179 | * post-conditions: 180 | * o interned string with the filename of the receiver is returned 181 | * 182 | * error-conditions: 183 | * o if no OS path is associated with the receiver, null is returned 184 | * ----------------------------------------------------------------------- */ 185 | 186 | public String Filename(); 187 | 188 | 189 | /* -------------------------------------------------------------------------- 190 | * method Basename() 191 | * -------------------------------------------------------------------------- 192 | * Returns an interned string with the basename of the receiver's filename. 193 | * 194 | * pre-conditions: 195 | * o the receiver must have been created with constructor NewFromOSPath() 196 | * or NewFromComponents() so that it is associated with a valid OS path. 197 | * 198 | * post-conditions: 199 | * o interned string with the basename of the receiver's filename is 200 | * returned 201 | * 202 | * error-conditions: 203 | * o if no OS path is associated with the receiver, null is returned 204 | * ----------------------------------------------------------------------- */ 205 | 206 | public String Basename(); 207 | 208 | 209 | /* -------------------------------------------------------------------------- 210 | * method Suffix() 211 | * -------------------------------------------------------------------------- 212 | * Returns an interned string with the suffix of the receiver's filename. 213 | * 214 | * pre-conditions: 215 | * o the receiver must have been created with constructor NewFromOSPath() 216 | * or NewFromComponents() so that it is associated with a valid OS path. 217 | * 218 | * post-conditions: 219 | * o interned string with the suffix of the receiver's filename is returned 220 | * 221 | * error-conditions: 222 | * o if no OS path is associated with the receiver, null is returned 223 | * ----------------------------------------------------------------------- */ 224 | 225 | public String Suffix(); 226 | 227 | 228 | /* -------------------------------------------------------------------------- 229 | * method SuffixType() 230 | * -------------------------------------------------------------------------- 231 | * Returns the enumerated suffix type value of the receiver's suffix. 232 | * 233 | * pre-conditions: 234 | * o the receiver must have been created with constructor NewFromOSPath() 235 | * or NewFromComponents() so that it is associated with a valid OS path. 236 | * 237 | * post-conditions: 238 | * o the enumerated value of the suffix type of the receiver's suffix 239 | * is returned 240 | * 241 | * error-conditions: 242 | * o if no OS path is associated with the receiver, value None is returned 243 | * ----------------------------------------------------------------------- */ 244 | 245 | public SuffixTypes SuffixType(); 246 | 247 | 248 | /* -------------------------------------------------------------------------- 249 | * static method IsValidOSPath(osPath) 250 | * -------------------------------------------------------------------------- 251 | * Returns true if osPath contains a valid pathname, otherwise false. 252 | * 253 | * pre-conditions: 254 | * o osPath must not be null. 255 | * 256 | * post-conditions: 257 | * o if the path given in osPath is valid, true is returned 258 | * o if the path given in osPath is invalid, false is returned 259 | * 260 | * error-conditions: 261 | * o if osPath is null, false is returned 262 | * ----------------------------------------------------------------------- */ 263 | 264 | // public static bool IsValidOSPath(String osPath); 265 | 266 | 267 | /* -------------------------------------------------------------------------- 268 | * static method IsValidFilename(filename) 269 | * -------------------------------------------------------------------------- 270 | * Returns true if filename contains a valid filename, otherwise false. 271 | * 272 | * pre-conditions: 273 | * o filename must not be null. 274 | * 275 | * post-conditions: 276 | * o if the given filename is valid, true is returned 277 | * o if the given filename is invalid, false is returned 278 | * 279 | * error-conditions: 280 | * o if filename is null, false is returned 281 | * ----------------------------------------------------------------------- */ 282 | 283 | // public static bool IsValidFilename(String filename); 284 | 285 | 286 | } /* IPathname */ 287 | 288 | } /* namespace */ 289 | 290 | /* END OF FILE */ -------------------------------------------------------------------------------- /ITerminals.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * ITerminals.cs 26 | * 27 | * Public interface for terminal symbols' token and lexeme lookup. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* --------------------------------------------------------------------------- 49 | * type Token 50 | * --------------------------------------------------------------------------- 51 | * Enumerated token values representing Modula-2 terminal symbols. 52 | * ------------------------------------------------------------------------ */ 53 | 54 | public enum Token { 55 | /* Null Token */ 56 | 57 | Unknown, /* invalid token */ 58 | 59 | /* Reserved Words */ 60 | 61 | AND, ARGLIST, ARRAY, BEGIN, BY, CASE, CONST, DEFINITION, DIV, DO, ELSE, 62 | ELSIF, END, EXIT, EXPORT, FOR, FROM, IF, IMPLEMENTATION, IMPORT, IN, LOOP, 63 | MOD, MODULE, NOT, OF, OPAQUE, OR, POINTER, PROCEDURE, QUALIFIED, RECORD, 64 | REPEAT, RETURN, SET, THEN, TO, TYPE, UNTIL, VAR, WHILE, WITH, 65 | 66 | /* Identifiers */ 67 | 68 | Identifier, 69 | 70 | /* Literals */ 71 | 72 | StringLiteral, IntLiteral, RealLiteral, CharLiteral, 73 | 74 | /* Malformed Literals */ 75 | 76 | MalformedString, MalformedInteger, MalformedReal, /* invalid tokens */ 77 | 78 | /* Pragmas */ 79 | 80 | Pragma, 81 | 82 | /* Special Symbols */ 83 | 84 | Plus, /* '+' */ 85 | Minus, /* '-' */ 86 | Equal, /* '+' */ 87 | NotEqual, /* '#' */ 88 | Less, /* '<' */ 89 | LessEqual, /* '<=' */ 90 | Greater, /* '>' */ 91 | GreaterEqual, /* '>=' */ 92 | Asterisk, /* '*' */ 93 | Solidus, /* '/' */ 94 | Backslash, /* '\\' */ 95 | Assign, /* ':=' */ 96 | Comma, /* ',' */ 97 | Period, /* '.' */ 98 | Colon, /* ':' */ 99 | Semicolon, /* ';' */ 100 | Range, /* '..' */ 101 | Deref, /* '^' */ 102 | Bar, /* '|' */ 103 | LeftParen, /* '(' */ 104 | RightParen, /* ')' */ 105 | LeftBracket, /* '[' */ 106 | RightBracket, /* ']' */ 107 | LeftBrace, /* '{' */ 108 | RightBrace, /* '}' */ 109 | EndOfFile 110 | 111 | /* Synonyms */ 112 | 113 | /* '&' is a synonym for AND, mapped to token AND */ 114 | /* '~' is a synonym for NOT, mapped to token NOT */ 115 | /* '<>' is a synonym for '#', mapped to token NOTEQUAL */ 116 | 117 | } /* Token */ 118 | 119 | 120 | /* --------------------------------------------------------------------------- 121 | * interface ITerminals 122 | * --------------------------------------------------------------------------- 123 | * This interface describes a singleton class. 124 | * Since C# does not fully support the concept of information hiding, this 125 | * interface is entirely comprised of comments for documentation purposes. 126 | * ------------------------------------------------------------------------ */ 127 | 128 | interface ITerminals { 129 | 130 | /* --------------------------------------------------------------------------- 131 | * method IsValid(token) 132 | * --------------------------------------------------------------------------- 133 | * Returns true if token represents a valid token, otherwise false. 134 | * ------------------------------------------------------------------------ */ 135 | 136 | // public static bool IsValid (Token token); 137 | 138 | 139 | /* --------------------------------------------------------------------------- 140 | * method IsResword(token) 141 | * --------------------------------------------------------------------------- 142 | * Returns true if token represents a reserved word, otherwise false. 143 | * ------------------------------------------------------------------------ */ 144 | 145 | // public static bool IsResword (Token token); 146 | 147 | 148 | /* --------------------------------------------------------------------------- 149 | * method IsLiteral(token) 150 | * --------------------------------------------------------------------------- 151 | * Returns true if token represents a literal, otherwise false. 152 | * ------------------------------------------------------------------------ */ 153 | 154 | // public static bool IsLiteral (Token token); 155 | 156 | 157 | /* --------------------------------------------------------------------------- 158 | * method IsMalformedLiteral(token) 159 | * --------------------------------------------------------------------------- 160 | * Returns true if token represents a malformed literal, otherwise false. 161 | * ------------------------------------------------------------------------ */ 162 | 163 | // public static bool IsMalformedLiteral (Token token); 164 | 165 | 166 | /* --------------------------------------------------------------------------- 167 | * method IsSpecialSymbol(token) 168 | * --------------------------------------------------------------------------- 169 | * Returns true if token represents a special symbol, otherwise false. 170 | * ------------------------------------------------------------------------ */ 171 | 172 | // public static bool IsSpecialSymbol (Token token); 173 | 174 | 175 | /* --------------------------------------------------------------------------- 176 | * method TokenForResword(lexeme) 177 | * --------------------------------------------------------------------------- 178 | * Tests if the given lexeme represents a reserved word and returns the 179 | * corresponding token or Unknown if it does not match a reserved word. 180 | * ------------------------------------------------------------------------ */ 181 | 182 | // public static Token TokenForResword (string lexeme); 183 | 184 | 185 | /* --------------------------------------------------------------------------- 186 | * method LexemeForResword(token) 187 | * --------------------------------------------------------------------------- 188 | * Returns a string with the lexeme for the reserved word represented by 189 | * token. Returns null if the token does not represent a reserved word. 190 | * ------------------------------------------------------------------------ */ 191 | 192 | // public static string LexemeForResword (Token token); 193 | 194 | 195 | /* --------------------------------------------------------------------------- 196 | * method LexemeForSpecialSymbol(token) 197 | * --------------------------------------------------------------------------- 198 | * Returns a string with the lexeme for the special symbol represented by 199 | * token. Returns null if the token does not represent a special symbol. 200 | * ------------------------------------------------------------------------ */ 201 | 202 | // public static string LexemeForSpecialSymbol (Token token); 203 | 204 | 205 | /* --------------------------------------------------------------------------- 206 | * method NameForToken(token) 207 | * --------------------------------------------------------------------------- 208 | * Returns a string with a human readable name for token. Returns null if 209 | * token is not a valid token. 210 | * ------------------------------------------------------------------------ */ 211 | 212 | // public static string NameForToken (Token token); 213 | 214 | 215 | } /* ITerminals */ 216 | 217 | } /* namespace */ 218 | 219 | /* END OF FILE */ 220 | -------------------------------------------------------------------------------- /ITokenSet.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * ITokenSet.cs 26 | * 27 | * Public interface for token set type. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace M2SF.M2Sharp { 47 | 48 | public interface ITokenSet { 49 | 50 | /* -------------------------------------------------------------------------- 51 | * constructor newFromList(token, ...) 52 | * -------------------------------------------------------------------------- 53 | * Returns a newly allocated tokenset object that includes the tokens passed 54 | * as arguments of a variadic argument list. 55 | * ----------------------------------------------------------------------- */ 56 | 57 | // public static TokenSet newFromList (params Token[] tokenList); 58 | 59 | 60 | /* -------------------------------------------------------------------------- 61 | * constructor newFromUnion(set, ...) 62 | * -------------------------------------------------------------------------- 63 | * Returns a newly allocated tokenset object that represents the set union of 64 | * the tokensets passed as arguments of a non-empty variadic argument list. 65 | * ----------------------------------------------------------------------- */ 66 | 67 | // public static TokenSet newFromUnion (params TokenSet[] setList); 68 | 69 | 70 | /* -------------------------------------------------------------------------- 71 | * method Count() 72 | * -------------------------------------------------------------------------- 73 | * Returns the number of elements in the receiver. 74 | * ----------------------------------------------------------------------- */ 75 | 76 | public uint Count (); 77 | 78 | 79 | /* -------------------------------------------------------------------------- 80 | * method IsElement(token) 81 | * -------------------------------------------------------------------------- 82 | * Returns true if token is an element of the receiver, else false. 83 | * ----------------------------------------------------------------------- */ 84 | 85 | public bool IsElement (Token token); 86 | 87 | 88 | /* -------------------------------------------------------------------------- 89 | * method IsSubset(set) 90 | * -------------------------------------------------------------------------- 91 | * Returns true if each element in set is also in the receiver, else false. 92 | * ----------------------------------------------------------------------- */ 93 | 94 | public bool IsSubset (TokenSet set); 95 | 96 | 97 | /* -------------------------------------------------------------------------- 98 | * method IsDisjunct(set) 99 | * -------------------------------------------------------------------------- 100 | * Returns true if set has no common elements with the receiver, else false. 101 | * ----------------------------------------------------------------------- */ 102 | 103 | public bool IsDisjunct (TokenSet set); 104 | 105 | 106 | /* -------------------------------------------------------------------------- 107 | * method ElementList() 108 | * -------------------------------------------------------------------------- 109 | * Returns a token list of all elements in the receiver. 110 | * ----------------------------------------------------------------------- */ 111 | 112 | public List ElementList (); 113 | 114 | 115 | /* -------------------------------------------------------------------------- 116 | * method PrintSet(label) 117 | * -------------------------------------------------------------------------- 118 | * Prints a human readable representation of the receiver. 119 | * Format: label = { comma-separated list of tokens }; 120 | * ----------------------------------------------------------------------- */ 121 | 122 | public void PrintSet (string label); 123 | 124 | 125 | /* -------------------------------------------------------------------------- 126 | * method PrintList() 127 | * -------------------------------------------------------------------------- 128 | * Prints a human readable list of tokens in the receiver. 129 | * Format: first, second, third, ..., secondToLast or last 130 | * ----------------------------------------------------------------------- */ 131 | 132 | public void PrintList (); 133 | 134 | 135 | /* -------------------------------------------------------------------------- 136 | * method PrintLiteralStruct(ident) 137 | * -------------------------------------------------------------------------- 138 | * Prints a struct definition for a tokenset literal for the receiver. 139 | * Format: struct ident { uint s0; uint s1; uint s2; ...; uint n }; 140 | * ----------------------------------------------------------------------- */ 141 | 142 | public void PrintLiteralStruct (string ident); 143 | 144 | 145 | /* -------------------------------------------------------------------------- 146 | * method PrintLiteral() 147 | * -------------------------------------------------------------------------- 148 | * Prints a sequence of hex values representing the bit pattern of receiver. 149 | * Format: ( 0xHHHHHHHH, 0xHHHHHHHH, ..., count ); 150 | * ----------------------------------------------------------------------- */ 151 | 152 | public void PrintLiteral (); 153 | 154 | 155 | } /* ITokenSet */ 156 | 157 | } /* M2SF.M2Sharp */ 158 | 159 | /* END OF FILE */ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## M2Sharp — A CLR hosted Modula-2 to C# Translator ## 2 | Welcome to the M2Sharp Modula-2 to C# Translator and Compiler Project 3 | 4 | 5 | ### Objective ### 6 | 7 | The objective of this project is to develop a CLR hosted Modula-2 to C# translator 8 | and via-C# compiler that generates CLR executable code. 9 | 10 | In translator mode, M2Sharp translates Modula-2 source to C# source files. In compiler 11 | mode, M2Sharp compiles Modula-2 source via C# source files to object code or executables 12 | using the host system's C# compiler. 13 | 14 | 15 | ### Language ### 16 | 17 | M2Sharp supports the bootstrap kernel (BSK) subset of Modula-2 R10 (M2R10). 18 | 19 | An online version of the language specification is here: 20 | 21 | https://github.com/m2sf/m2bsk/wiki/Language-Specification 22 | 23 | The authoritative language specification (PDF) is available for download: 24 | 25 | https://github.com/m2sf/PDFs/blob/master/M2BSK%20Language%20Description.pdf 26 | 27 | **M2C does not support the earlier PIM or ISO dialects.** 28 | 29 | 30 | ### Grammar ### 31 | 32 | The grammar of M2C's command line interface is in the project repository 33 | 34 | https://github.com/m2sf/m2c/blob/main/grammar/cli-grammar.gll 35 | 36 | The grammar of Modula-2 R10 is in the project repository 37 | 38 | https://github.com/m2sf/m2c/blob/main/grammar/m2c-grammar.gll 39 | 40 | For a graphical representation of the grammar, see section 41 | [Syntax Diagrams](https://github.com/m2sf/m2bsk/wiki/Language-Specification-(D)-:-Syntax-Diagrams). 42 | 43 | 44 | ### Targets ### 45 | 46 | M2Sharp will generate C# sources compileable with any C# compiler. 47 | 48 | **There are no dependencies on any third party libraries.** 49 | 50 | 51 | ### OS support ### 52 | 53 | M2Sharp will compile and run on any target system with a CLR execution environment. 54 | 55 | 56 | ### Development Languages ### 57 | 58 | * M2Sharp itself is written in C#. 59 | * The syntax diagram generator script is written in TCL/TK (not required to build M2Sharp) 60 | * Build configuration scripts are written in the prevalent shell language of the hosting platform 61 | 62 | 63 | ### Project Wiki ### 64 | 65 | For more details please visit the project wiki at the URL: 66 | https://github.com/m2sf/m2sharp/wiki 67 | 68 | 69 | ### Contact ### 70 | 71 | If you have questions or would like to contribute to the project, get in touch via 72 | 73 | * [Modula2 Telegram group](https://t.me/+hTKSWC2mWoM1OGVl) chat 74 | 75 | * [email](mailto:REMOVE+REVERSE.com.gmail@trijezdci) to the project maintainer 76 | 77 | +++ 78 | -------------------------------------------------------------------------------- /Result.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * Result.cs 26 | * 27 | * Paired result class. 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | /* -------------------------------------------------------------------------- 49 | * Generic container class to return paired results from constructors. 50 | * ----------------------------------------------------------------------- */ 51 | 52 | public class Result { 53 | 54 | private TValue value; 55 | private TStatus status; 56 | 57 | /* result pair constructor */ 58 | 59 | public Result (TValue value, TStatus status) { 60 | this.value = value; 61 | this.status = status; 62 | } /* Result */ 63 | 64 | /* method to return value component */ 65 | 66 | public TValue Value () { 67 | return this.value; 68 | } /* value */ 69 | 70 | /* method to return status component */ 71 | 72 | public TStatus Status () { 73 | return this.status; 74 | } /* status */ 75 | 76 | } /* Result */ 77 | 78 | } /* namespace */ 79 | 80 | /* END OF FILE */ 81 | -------------------------------------------------------------------------------- /compiler-option-grammar.txt: -------------------------------------------------------------------------------- 1 |  2 | M2C/M2J/M2Sharp Compiler Option Grammar 3 | 4 | 5 | /* N O N - T E R M I N A L S */ 6 | 7 | options : 8 | infoRequest | compilationRequest 9 | ; 10 | 11 | infoRequest : 12 | HELP | VERSION | LICENSE 13 | ; 14 | 15 | compilationRequest : 16 | dialect? products? capabilities? sourceFile diagnostics? 17 | ; 18 | 19 | dialect : 20 | ( PIM3 | PIM4 ) ( SAFER | COMPLIANT )? | EXT 21 | ; 22 | 23 | products : 24 | ( singleProduct | multipleProducts ) identifierOption? commentOption? 25 | ; 26 | 27 | singleProduct : 28 | SYNTAX_ONLY | AST_ONLY | GRAPH_ONLY | XLAT_ONLY | OBJ_ONLY 29 | ; 30 | 31 | multipleProducts : 32 | ( ast | graph | xlat | obj )+ 33 | ; 34 | 35 | ast : 36 | AST | NO_AST 37 | ; 38 | 39 | graph : 40 | GRAPH | NO_GRAPH 41 | ; 42 | 43 | xlat : 44 | XLAT | NO_XLAT 45 | ; 46 | 47 | obj : 48 | OBJ | NO_OBJ 49 | ; 50 | 51 | identifierOption : 52 | USE_IDENTIFIERS_VERBATIM | TRANSLITERATE_IDENTIFIERS 53 | ; 54 | 55 | commentOption : 56 | PRESERVE_COMMENTS | STRIP_COMMENTS 57 | ; 58 | 59 | capabilities : 60 | ( synonyms | octalLiterals | explicitCast | 61 | coroutines | variantRecords | localModules | 62 | lowlineIdentifiers | toDoStatement )+ 63 | ; 64 | 65 | synonyms : 66 | SYNONYMS | NO_SYNONYMS 67 | ; 68 | 69 | octalLiterals : 70 | OCTAL_LITERALS | NO_OCTAL_LITERALS 71 | ; 72 | 73 | explicitCast : 74 | EXPLICIT_CAST | NO_EXPLICIT_CAST 75 | ; 76 | 77 | coroutines : 78 | COROUTINES | NO_COROUTINES 79 | ; 80 | 81 | variantRecords : 82 | VARIANT_RECORDS | NO_VARIANT_RECORDS 83 | ; 84 | 85 | localModules : 86 | LOCAL_MODULES | NO_LOCAL_MODULES 87 | ; 88 | 89 | lowlineIdentifiers : 90 | LOWLINE_IDENTIFIERS | NO_LOWLINE_IDENTIFIERS 91 | ; 92 | 93 | toDoStatement : 94 | TO_DO_STATEMENT | NO_TO_DO_STATEMENT 95 | ; 96 | 97 | sourceFile : 98 | ; 99 | 100 | diagnostics : 101 | ( VERBOSE | LEXER_DEBUG | PARSER_DEBUG | PRINT_SETTINGS | 102 | ERRANT_SEMICOLONS )+ 103 | ; 104 | 105 | 106 | /* T E R M I N A L S */ 107 | 108 | HELP : '--help' | '-h' ; 109 | 110 | VERSION : '--version' | '-V' ; 111 | 112 | LICENSE : '--license' ; 113 | 114 | PIM3 : '--pim3' ; 115 | 116 | PIM4 : '--pim4' ; 117 | 118 | SAFER : '--safer' ; 119 | 120 | COMPLIANT : '--compliant' ; 121 | 122 | EXT : '--ext' ; 123 | 124 | VERBOSE : '--verbose' | '-v' ; 125 | 126 | LEXER_DEBUG : '--lexer-debug' ; 127 | 128 | PARSER_DEBUG : '--parser-debug' ; 129 | 130 | PRINT_SETTINGS : '--print-settings' ; 131 | 132 | ERRANT_SEMICOLONS : '--errant-semicolons' ; 133 | 134 | SYNTAX_ONLY : '--syntax-only' ; 135 | 136 | AST_ONLY : '--ast-only' ; 137 | 138 | GRAPH_ONLY : '--graph-only' ; 139 | 140 | XLAT_ONLY : '--xlat-only' ; 141 | 142 | OBJ_ONLY : '--obj-only' ; 143 | 144 | AST : '--ast' ; 145 | 146 | NO_AST : '--no-ast' ; 147 | 148 | GRAPH : '--graph' ; 149 | 150 | NO_GRAPH : '--no-graph' ; 151 | 152 | XLAT : '--xlat' ; 153 | 154 | NO_XLAT : '--no-xlat' ; 155 | 156 | OBJ : '--obj' ; 157 | 158 | NO_OBJ : '--no-obj' ; 159 | 160 | USE_IDENTIFIERS_VERBATIM : '--use-identifiers-verbatim' ; 161 | 162 | TRANSLITERATE_IDENTIFIERS : '--transliterate-identifiers' ; 163 | 164 | PRESERVE_COMMENTS : '--preserve-comments' ; 165 | 166 | STRIP_COMMENTS : '--strip-comments' ; 167 | 168 | SYNONYMS : '--synonyms' ; 169 | 170 | NO_SYNONYMS : '--no-synonyms' ; 171 | 172 | OCTAL_LITERALS : '--octal-literals' ; 173 | 174 | NO_OCTAL_LITERALS : '--no-octal-literals' ; 175 | 176 | EXPLICIT_CAST : '--explicit-cast' ; 177 | 178 | NO_EXPLICIT_CAST : '--no-explicit-cast' ; 179 | 180 | COROUTINES : '--coroutines' ; 181 | 182 | NO_COROUTINES : '--no-coroutines' ; 183 | 184 | VARIANT_RECORDS : '--variant-records' ; 185 | 186 | NO_VARIANT_RECORDS : '--no-variant-records' ; 187 | 188 | LOCAL_MODULES : '--local-modules' ; 189 | 190 | NO_LOCAL_MODULES : '--no-local-modules' ; 191 | 192 | LOWLINE_IDENTIFIERS : '--lowline-identifiers' ; 193 | 194 | NO_LOWLINE_IDENTIFIERS : '--no-lowline-identifiers' ; 195 | 196 | TO_DO_STATEMENT : '--to-do-statement' ; 197 | 198 | NO_TO_DO_STATEMENT : '--no-to-do-statement' ; 199 | 200 | 201 | /* END OF FILE */ -------------------------------------------------------------------------------- /compiler-options.txt: -------------------------------------------------------------------------------- 1 | M2C, M2J, M2Sharp Compiler Options 2 | 3 | Document version 2.0.1, status 2016-09-20 4 | 5 | 6 | (1) information options, all dialects 7 | 8 | --help, -h 9 | --version, -V 10 | --license 11 | 12 | 13 | (2) diagnostic options, all dialects 14 | 15 | --verbose, -v 16 | --lexer-debug 17 | --parser-debug 18 | --print-settings 19 | --errant-semicolons 20 | 21 | defaults: 22 | 23 | all diagnostic options are off 24 | 25 | 26 | (3) compilation product options, all dialects 27 | 28 | (a) all compilers 29 | 30 | --ast, --no-ast 31 | --graph, --no-graph 32 | --xlat, --no-xlat 33 | --obj, --no-obj 34 | 35 | --syntax-only (--no-ast, --no-graph, --no-xlat, --no-obj) 36 | --ast-only (--no-graph, --no-xlat, --no-obj) 37 | --graph-only (--no-ast, --no-xlat, --no-obj) 38 | --xlat-only (--no-ast, --no-graph, --no-obj) 39 | --obj-only (--no-ast, --no-graph, --no-xlat) 40 | 41 | in combination with --xlat 42 | 43 | --strip-comments 44 | --preserve-comments 45 | 46 | defaults: 47 | 48 | --ast, --xlat, --obj, --preserve-comments 49 | 50 | 51 | (b) compiler specific 52 | 53 | available on M2C: 54 | 55 | --c (same as --xlat) 56 | --no-c (same as --no-xlat) 57 | --c-only (--c, --no-ast, --no-graph, --no-obj) 58 | 59 | available on M2J: 60 | 61 | --java (same as --xlat) 62 | --no-java (same as --no-xlat) 63 | --java-only (--java, --no-ast, --no-graph, --no-obj) 64 | 65 | available on M2Sharp: 66 | 67 | --cs (same as --xlat) 68 | --no-cs (same as --no-xlat) 69 | --cs-only (--cs, --no-ast, --no-graph, --no-obj) 70 | 71 | 72 | (4) Dialect options 73 | 74 | --ext (see capability table below) 75 | --pim3 (see capability table below) 76 | --pim4 (see capability table below) 77 | 78 | default: 79 | 80 | --ext 81 | 82 | 83 | (5) Capability options 84 | 85 | (a) available in Extended dialect mode on all compilers 86 | 87 | --lowline-identifiers, --no-lowline-identifiers 88 | --to-do-statement, --no-to-do-statement 89 | 90 | defaults: 91 | 92 | --no-lowline-identifiers, --to-do-statement 93 | 94 | 95 | (b) available in PIM3 and PIM4 dialect modes on all compilers 96 | 97 | --synonyms, --no-synonyms 98 | --octal-literals, --no-octal-literals 99 | --explicit-cast, --no-explicit-cast 100 | --coroutines, --no-coroutines 101 | --variant-records, --no-variant-records 102 | --local-modules, --no-local-modules 103 | 104 | --safer 105 | (--no-synonyms, --no-octal-literals, --explicit-cast, 106 | --no-coroutines, --no-variant-records, --no-local-modules) 107 | 108 | --compliant 109 | (--synonyms, --octal-literals, --no-explicit-cast, 110 | --coroutines, --variant-records, --local-modules) 111 | 112 | defaults: 113 | 114 | --safer 115 | 116 | 117 | (6) Capability overview 118 | 119 | most capabilities are determined by the dialect mode, some are user selectable 120 | 121 | Capabilities | Extended | PIM3 | PIM4 122 | ---------------------------+----------------+----------------+---------------- 123 | Synonyms ~, &, <> | off, immutable | off, mutable | off, mutable 124 | Line comments | on, immutable | off, immutable | off, immutable 125 | Prefix literals | on, immutable | off, immutable | off, immutable 126 | Suffix literals | off, immutable | on, immutable | on, immutable 127 | Octal literals | off, immutable | off, mutable | off, mutable 128 | Lowline identifiers | off, mutable | off, immutable | off, immutable 129 | Escape tab and newline | on, immutable | off, immutable | off, immutable 130 | Set difference operator \ | on, immutable | off, immutable | off, immutable 131 | Postfix ++/-- statements | on, immutable | off, immutable | off, immutable 132 | Pragma delimiters <*, *> | on, immutable | off, immutable | off, immutable 133 | ---------------------------+----------------+----------------+---------------- 134 | Subtyped cardinals | off, immutable | off, immutable | on, immutable 135 | Safe string termination | on, immutable | off, immutable | on, immutable 136 | Import variables immutable | on, immutable | off, immutable | off, immutable 137 | CONST parameters | on, immutable | off, immutable | off, immutable 138 | Variadic parameters | on, immutable | off, immutable | off, immutable 139 | Additional types | on, immutable | off, immutable | off, immutable 140 | Additional functions | on, immutable | off, immutable | off, immutable 141 | Unified conversion | on, immutable | off, immutable | off, immutable 142 | Explicit cast | on, immutable | on, mutable | on, mutable 143 | ---------------------------+----------------+----------------+---------------- 144 | Coroutines | off, immutable | off, mutable | off, mutable 145 | Variant records | off, immutable | off, mutable | off, mutable 146 | Extensible records | on, immutable | off, immutable | off, immutable 147 | Indeterminate records | on, immutable | off, immutable | off, immutable 148 | Unqualified import | off, immutable | on, immutable | on, immutable 149 | Local modules | off, immutable | off, mutable | off, mutable 150 | WITH statement | off, immutable | on, immutable | on, immutable 151 | TO DO statement | on, mutable | off, immutable | off, immutable 152 | ------------------------------------------------------------------------------- 153 | 154 | END OF FILE -------------------------------------------------------------------------------- /imp/Dialects.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * Dialects.cs 26 | * 27 | * compiler dialect settings class 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | namespace org.m2sf.m2sharp { 47 | 48 | public class Dialects : IDialects { 49 | 50 | /* --------------------------------------------------------------------------- 51 | * bitset constant PIM3Defaults 52 | * --------------------------------------------------------------------------- 53 | * Represents a capability set containing default capabilities for PIM3 54 | * ------------------------------------------------------------------------ */ 55 | 56 | private const uint PIM3Defaults = 57 | (1 << (int)Capability.SuffixLiterals) | 58 | (1 << (int)Capability.ExplicitCast) | 59 | (1 << (int)Capability.UnqualifiedImport) | 60 | (1 << (int)Capability.WithStatement); 61 | 62 | 63 | /* --------------------------------------------------------------------------- 64 | * bitset constant PIM3Mutables 65 | * --------------------------------------------------------------------------- 66 | * Represents a capability set containing mutable capabilities for PIM3 67 | * ------------------------------------------------------------------------ */ 68 | 69 | private const uint PIM3Mutables = 70 | (1 << (int)Capability.Synonyms) | 71 | (1 << (int)Capability.OctalLiterals) | 72 | (1 << (int)Capability.ExplicitCast) | 73 | (1 << (int)Capability.Coroutines) | 74 | (1 << (int)Capability.VariantRecords) | 75 | (1 << (int)Capability.LocalModules); 76 | 77 | 78 | /* --------------------------------------------------------------------------- 79 | * bitset constant PIM4Defaults 80 | * --------------------------------------------------------------------------- 81 | * Represents a capability set containing default capabilities for PIM4 82 | * ------------------------------------------------------------------------ */ 83 | 84 | private const uint PIM4Defaults = 85 | (1 << (int)Capability.SuffixLiterals) | 86 | (1 << (int)Capability.SubtypeCardinals) | 87 | (1 << (int)Capability.SafeStringTermination) | 88 | (1 << (int)Capability.ExplicitCast) | 89 | (1 << (int)Capability.UnqualifiedImport) | 90 | (1 << (int)Capability.WithStatement); 91 | 92 | 93 | /* --------------------------------------------------------------------------- 94 | * bitset constant PIM4Mutables 95 | * --------------------------------------------------------------------------- 96 | * Represents a capability set containing mutable capabilities for PIM4 97 | * ------------------------------------------------------------------------ */ 98 | 99 | private const uint PIM4Mutables = 100 | (1 << (int)Capability.Synonyms) | 101 | (1 << (int)Capability.OctalLiterals) | 102 | (1 << (int)Capability.ExplicitCast) | 103 | (1 << (int)Capability.Coroutines) | 104 | (1 << (int)Capability.VariantRecords) | 105 | (1 << (int)Capability.LocalModules); 106 | 107 | 108 | /* --------------------------------------------------------------------------- 109 | * bitset constant ExtDefaults 110 | * --------------------------------------------------------------------------- 111 | * Represents a capability set containing default capabilities for Extended 112 | * ------------------------------------------------------------------------ */ 113 | 114 | private const uint ExtDefaults = 115 | (1 << (int)Capability.LineComments) | 116 | (1 << (int)Capability.PrefixLiterals) | 117 | (1 << (int)Capability.EscapeTabAndNewline) | 118 | (1 << (int)Capability.BackslashSetDiffOp) | 119 | (1 << (int)Capability.PostfixIncAndDec) | 120 | (1 << (int)Capability.IsoPragmaDelimiters) | 121 | (1 << (int)Capability.SafeStringTermination) | 122 | (1 << (int)Capability.ImportVarsImmutable) | 123 | (1 << (int)Capability.ConstParameters) | 124 | (1 << (int)Capability.VariadicParameters) | 125 | (1 << (int)Capability.AdditionalTypes) | 126 | (1 << (int)Capability.AdditionalFunctions) | 127 | (1 << (int)Capability.UnifiedConversion) | 128 | (1 << (int)Capability.ExplicitCast) | 129 | (1 << (int)Capability.ExtensibleRecords) | 130 | (1 << (int)Capability.IndeterminateRecords) | 131 | (1 << (int)Capability.ToDoStatement); 132 | 133 | 134 | /* --------------------------------------------------------------------------- 135 | * bitset constant ExtMutables 136 | * --------------------------------------------------------------------------- 137 | * Represents a capability set containing mutable capabilities for Extended 138 | * ------------------------------------------------------------------------ */ 139 | 140 | private const uint ExtMutables = 141 | (1 << (int)Capability.LowlineIdentifiers) | 142 | (1 << (int)Capability.ToDoStatement); 143 | 144 | 145 | /* --------------------------------------------------------------------------- 146 | * method DefaultCapabilitiesFor(dialect) 147 | * --------------------------------------------------------------------------- 148 | * Returns an unsigned integer representing a bitset that contains all default 149 | * capabilities for the given dialect. Bits are indexed, with the least sig- 150 | * nificant bit having index 0. The indices correspond to the enumerated 151 | * capability values of enumeration type Capability. 152 | * ------------------------------------------------------------------------ */ 153 | 154 | public static uint DefaultCapabilitiesFor ( Dialect dialect ) { 155 | 156 | switch (dialect) { 157 | 158 | case Dialect.PIM3 : 159 | return PIM3Defaults; 160 | 161 | case Dialect.PIM4 : 162 | return PIM4Defaults; 163 | 164 | case Dialect.Extended : 165 | return ExtDefaults; 166 | 167 | default : 168 | return 0; 169 | 170 | } /* end switch */ 171 | 172 | } /* end DefaultCapabilitiesFor */ 173 | 174 | 175 | /* --------------------------------------------------------------------------- 176 | * method IsDefaultCapability(dialect, capability) 177 | * --------------------------------------------------------------------------- 178 | * Returns true if the given capability is a default capability of the given 179 | * dialect, else false. 180 | * ------------------------------------------------------------------------ */ 181 | 182 | public static bool IsDefaultCapability ( Dialect dialect, Capability cap ) { 183 | 184 | switch (dialect) { 185 | 186 | case Dialect.PIM3 : 187 | return IsCapabilityInSet(PIM3Defaults, cap); 188 | 189 | case Dialect.PIM4 : 190 | return IsCapabilityInSet(PIM4Defaults, cap); 191 | 192 | case Dialect.Extended : 193 | return IsCapabilityInSet(ExtDefaults, cap); 194 | 195 | default : 196 | return false; 197 | 198 | } /* end switch */ 199 | 200 | } /* end IsDefaultCapability */ 201 | 202 | 203 | /* --------------------------------------------------------------------------- 204 | * method IsMutableCapability(dialect, capability) 205 | * --------------------------------------------------------------------------- 206 | * Returns true if the given capability is a mutable capability of the given 207 | * dialect, else false. 208 | * ------------------------------------------------------------------------ */ 209 | 210 | public static bool IsMutableCapability ( Dialect dialect, Capability cap ) { 211 | 212 | switch (dialect) { 213 | 214 | case Dialect.PIM3 : 215 | return IsCapabilityInSet(PIM3Mutables, cap); 216 | 217 | case Dialect.PIM4 : 218 | return IsCapabilityInSet(PIM4Mutables, cap); 219 | 220 | case Dialect.Extended : 221 | return IsCapabilityInSet(ExtMutables, cap); 222 | 223 | default : 224 | return false; 225 | 226 | } /* end switch */ 227 | 228 | } /* end IsDefaultCapability */ 229 | 230 | 231 | /* --------------------------------------------------------------------------- 232 | * private method IsCapabilityInSet(capabilitySet, capability) 233 | * --------------------------------------------------------------------------- 234 | * Returns true if capability is present in bitset capabilitySet, else false. 235 | * ------------------------------------------------------------------------ */ 236 | 237 | private static bool IsCapabilityInSet (uint capabilitySet, Capability cap) { 238 | 239 | return (capabilitySet & (1 << (int)cap)) != 0; 240 | 241 | } /* end IsCapabilityInSet */ 242 | 243 | 244 | } /* Dialects */ 245 | 246 | } /* namespace */ 247 | 248 | /* END OF FILE */ -------------------------------------------------------------------------------- /imp/PlatformInfo.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * PlatformInfo.cs 26 | * 27 | * platform information 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | using System; 47 | using Mono.Unix.Native; 48 | 49 | namespace org.m2sf.m2sharp { 50 | 51 | 52 | /* --------------------------------------------------------------------------- 53 | * singleton class PlatformInfo 54 | * --------------------------------------------------------------------------- 55 | * All methods are static. All methods return singleton values. 56 | * ------------------------------------------------------------------------ */ 57 | 58 | public sealed class PlatformInfo : IPlatformInfo { 59 | 60 | /* --------------------------------------------------------------------------- 61 | * private class variable initialized 62 | * --------------------------------------------------------------------------- 63 | * Set to true if class variables have been initialised, otherwise false. 64 | * ------------------------------------------------------------------------ */ 65 | 66 | private static bool initialized = false; 67 | 68 | 69 | /* --------------------------------------------------------------------------- 70 | * private class variable name 71 | * --------------------------------------------------------------------------- 72 | * Holds the human readable name of the current operating system. 73 | * ------------------------------------------------------------------------ */ 74 | 75 | private static string name; 76 | 77 | 78 | /* --------------------------------------------------------------------------- 79 | * private class variable uname 80 | * --------------------------------------------------------------------------- 81 | * Holds the human readable name of the current Unix kernel. 82 | * The name will be the empty string on non-Unix operating systems. 83 | * ------------------------------------------------------------------------ */ 84 | 85 | private static string uname; 86 | 87 | 88 | /* --------------------------------------------------------------------------- 89 | * private class variable type 90 | * --------------------------------------------------------------------------- 91 | * Holds the enumerated value of the current operating system. 92 | * ------------------------------------------------------------------------ */ 93 | 94 | private static PlatformOSType type = PlatformOSType.Unsupported; 95 | 96 | 97 | /* --------------------------------------------------------------------------- 98 | * private class variable vmajor 99 | * --------------------------------------------------------------------------- 100 | * Holds the major version of the current operating system. 101 | * ------------------------------------------------------------------------ */ 102 | 103 | private static int vmajor; 104 | 105 | 106 | /* --------------------------------------------------------------------------- 107 | * private class variable vminor 108 | * --------------------------------------------------------------------------- 109 | * Holds the minor version of the current operating system. 110 | * ------------------------------------------------------------------------ */ 111 | 112 | private static int vminor; 113 | 114 | 115 | /* --------------------------------------------------------------------------- 116 | * private class variable build 117 | * --------------------------------------------------------------------------- 118 | * Holds the build number of the current operating system. 119 | * ------------------------------------------------------------------------ */ 120 | 121 | private static int build; 122 | 123 | 124 | /* --------------------------------------------------------------------------- 125 | * private constructor PlatformInfo() 126 | * --------------------------------------------------------------------------- 127 | * Prevents clients from invoking the constructor. 128 | * ------------------------------------------------------------------------ */ 129 | 130 | private PlatformInfo() { 131 | // no operation 132 | } /* end PlatformInfo */ 133 | 134 | 135 | /* --------------------------------------------------------------------------- 136 | * private initialiser Init() 137 | * --------------------------------------------------------------------------- 138 | * Initialises all class variables from the current environment. 139 | * ------------------------------------------------------------------------ */ 140 | 141 | private static void Init () { 142 | if (initialized) { 143 | return; 144 | } /* end if */ 145 | 146 | /* obtain platform ID */ 147 | OperatingSystem os = Environment.OSVersion; 148 | switch (os.Platform) { 149 | case PlatformID.MacOSX : 150 | name = PlatformOSName.MacOSX; 151 | type = PlatformOSType.MacOSX; 152 | break; 153 | case PlatformID.Unix : 154 | name = PlatformOSName.Posix; 155 | type = PlatformOSType.Posix; 156 | break; 157 | case PlatformID.Win32NT : 158 | name = PlatformOSName.Windows; 159 | type = PlatformOSType.Windows; 160 | break; 161 | default : 162 | name = PlatformOSName.Unsupported; 163 | type = PlatformOSType.Unsupported; 164 | break; 165 | } /* end switch */ 166 | 167 | /* obtain uname on Unix platforms */ 168 | if ((os.Platform == PlatformID.MacOSX) || 169 | (os.Platform == PlatformID.Unix)) { 170 | Utsname uts; 171 | if (Syscall.uname(out uts) == 0) { 172 | /* $ uname -s */ 173 | uname = uts.sysname; 174 | } 175 | else /* uname call failed */ { 176 | uname = ""; 177 | } /* end if */ 178 | } 179 | else /* not a Unix platform */ { 180 | uname = ""; 181 | } /* end if */ 182 | 183 | /* fix incorrect result of Environment.OSVersion for MacOS X */ 184 | if (string.Compare(uname, "darwin", true) == 0) { 185 | name = PlatformOSName.MacOSX; 186 | type = PlatformOSType.MacOSX; 187 | } /* end if */ 188 | 189 | /* obtain version */ 190 | Version version = os.Version; 191 | vmajor = version.Major; 192 | vminor = version.Minor; 193 | build = version.Build; 194 | 195 | /* remember initialisation */ 196 | initialized = true; 197 | 198 | } /* end Init */ 199 | 200 | 201 | /* --------------------------------------------------------------------------- 202 | * method Name() 203 | * --------------------------------------------------------------------------- 204 | * Returns the human readable name of the current operating system. 205 | * ------------------------------------------------------------------------ */ 206 | 207 | public static string Name () { 208 | if (initialized == false) { 209 | PlatformInfo.Init(); 210 | } /* end if */ 211 | return name; 212 | } /* end Name */ 213 | 214 | 215 | /* --------------------------------------------------------------------------- 216 | * method Uname() 217 | * --------------------------------------------------------------------------- 218 | * Returns the human readable name of the current Unix kernel. 219 | * Returns an empty string on non-Unix operating systems. 220 | * ------------------------------------------------------------------------ */ 221 | 222 | public static string Uname () { 223 | if (initialized == false) { 224 | PlatformInfo.Init(); 225 | } /* end if */ 226 | return uname; 227 | } /* end Uname */ 228 | 229 | 230 | /* --------------------------------------------------------------------------- 231 | * method OSType() 232 | * --------------------------------------------------------------------------- 233 | * Returns an enumerated value indicating the current operating system. 234 | * ------------------------------------------------------------------------ */ 235 | 236 | public static PlatformOSType OSType () { 237 | if (initialized == false) { 238 | PlatformInfo.Init(); 239 | } /* end if */ 240 | return type; 241 | } /* end OSType */ 242 | 243 | 244 | /* --------------------------------------------------------------------------- 245 | * method VersionMajor() 246 | * --------------------------------------------------------------------------- 247 | * Returns the major version of the current operating system. 248 | * ------------------------------------------------------------------------ */ 249 | 250 | public static int VersionMajor () { 251 | if (initialized == false) { 252 | PlatformInfo.Init(); 253 | } /* end if */ 254 | return vmajor; 255 | } /* end VersionMajor */ 256 | 257 | 258 | /* --------------------------------------------------------------------------- 259 | * method VersionMinor() 260 | * --------------------------------------------------------------------------- 261 | * Returns the minor version of the current operating system. 262 | * ------------------------------------------------------------------------ */ 263 | 264 | public static int VersionMinor () { 265 | if (initialized == false) { 266 | PlatformInfo.Init(); 267 | } /* end if */ 268 | return vminor; 269 | } /* end VersionMinor */ 270 | 271 | 272 | /* --------------------------------------------------------------------------- 273 | * method Build() 274 | * --------------------------------------------------------------------------- 275 | * Returns the build number of the current operating system. 276 | * ------------------------------------------------------------------------ */ 277 | 278 | public static int Build () { 279 | if (initialized == false) { 280 | PlatformInfo.Init(); 281 | } /* end if */ 282 | return build; 283 | } /* end Build */ 284 | 285 | 286 | /* --------------------------------------------------------------------------- 287 | * method IsSupported() 288 | * --------------------------------------------------------------------------- 289 | * Returns true if the current operating system is supported, else false. 290 | * ------------------------------------------------------------------------ */ 291 | 292 | public static bool IsSupported () { 293 | if (initialized == false) { 294 | PlatformInfo.Init(); 295 | } /* end if */ 296 | return (type != PlatformOSType.Unsupported); 297 | } /* end IsSupported */ 298 | 299 | 300 | /* --------------------------------------------------------------------------- 301 | * method IsUnsupported() 302 | * --------------------------------------------------------------------------- 303 | * Returns true if the current operating system is unsupported, else false. 304 | * ------------------------------------------------------------------------ */ 305 | 306 | public static bool IsUnsupported () { 307 | if (initialized == false) { 308 | PlatformInfo.Init(); 309 | } /* end if */ 310 | return (type == PlatformOSType.Unsupported); 311 | } /* end IsSupported */ 312 | 313 | } /* PlatformInfo */ 314 | } /* namespace */ 315 | 316 | /* END OF FILE */ 317 | -------------------------------------------------------------------------------- /imp/ProductInfo.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * ProductInfo.cs 26 | * 27 | * product information 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | using System.Reflection; 47 | 48 | namespace org.m2sf.m2sharp { 49 | 50 | /* --------------------------------------------------------------------------- 51 | * singleton class ProductInfo 52 | * --------------------------------------------------------------------------- 53 | * All methods are static. All methods return singleton values. 54 | * ------------------------------------------------------------------------ */ 55 | 56 | public sealed class ProductInfo : IProductInfo { 57 | 58 | private const string DefaultLicense = "LGPL version 2.1 and 3.0"; 59 | 60 | /* --------------------------------------------------------------------------- 61 | * private class variable initialized 62 | * --------------------------------------------------------------------------- 63 | * Set to true if class variables have been initialised, otherwise false. 64 | * ------------------------------------------------------------------------ */ 65 | 66 | private static bool initialized = false; 67 | 68 | 69 | /* --------------------------------------------------------------------------- 70 | * private class variable name 71 | * --------------------------------------------------------------------------- 72 | * Holds the human readable name of the current assembly. 73 | * ------------------------------------------------------------------------ */ 74 | 75 | private static string name; 76 | 77 | 78 | /* --------------------------------------------------------------------------- 79 | * private class variable vmajor 80 | * --------------------------------------------------------------------------- 81 | * Holds the major version of the current assembly. 82 | * ------------------------------------------------------------------------ */ 83 | 84 | private static int vmajor; 85 | 86 | 87 | /* --------------------------------------------------------------------------- 88 | * private class variable vminor 89 | * --------------------------------------------------------------------------- 90 | * Holds the minor version of the current assembly. 91 | * ------------------------------------------------------------------------ */ 92 | 93 | private static int vminor; 94 | 95 | 96 | /* --------------------------------------------------------------------------- 97 | * private class variable build 98 | * --------------------------------------------------------------------------- 99 | * Holds the build number of the current assembly. 100 | * ------------------------------------------------------------------------ */ 101 | 102 | private static int build; 103 | 104 | 105 | /* --------------------------------------------------------------------------- 106 | * private class variable description 107 | * --------------------------------------------------------------------------- 108 | * Holds the human readable description of the current assembly. 109 | * ------------------------------------------------------------------------ */ 110 | 111 | private static string description; 112 | 113 | 114 | /* --------------------------------------------------------------------------- 115 | * private class variable description 116 | * --------------------------------------------------------------------------- 117 | * Holds the human readable copyright of the current assembly. 118 | * ------------------------------------------------------------------------ */ 119 | 120 | private static string copyright; 121 | 122 | 123 | /* --------------------------------------------------------------------------- 124 | * private class variable description 125 | * --------------------------------------------------------------------------- 126 | * Holds the human readable license name of this product. 127 | * ------------------------------------------------------------------------ */ 128 | 129 | private static string license; 130 | 131 | 132 | /* --------------------------------------------------------------------------- 133 | * private constructor ProductInfo() 134 | * --------------------------------------------------------------------------- 135 | * Prevents clients from invoking the constructor. 136 | * ------------------------------------------------------------------------ */ 137 | 138 | private ProductInfo() { 139 | // no operation 140 | } /* end ProductInfo */ 141 | 142 | 143 | /* --------------------------------------------------------------------------- 144 | * private initialiser Init() 145 | * --------------------------------------------------------------------------- 146 | * Initialises all class variables from the current assembly. 147 | * ------------------------------------------------------------------------ */ 148 | 149 | private static void Init () { 150 | 151 | if (initialized) { 152 | return; 153 | } /* end if */ 154 | 155 | Assembly bundle = Assembly.GetExecutingAssembly(); 156 | object[] attributes = bundle.GetCustomAttributes(false); 157 | 158 | /* obtain assembly name */ 159 | name = bundle.GetName().Name; 160 | 161 | /* obtain major version */ 162 | vmajor = Assembly.GetExecutingAssembly().GetName().Version.Major; 163 | 164 | /* obtain minor version */ 165 | vminor = Assembly.GetExecutingAssembly().GetName().Version.Minor; 166 | 167 | /* obtain build number */ 168 | build = Assembly.GetExecutingAssembly().GetName().Version.Build; 169 | 170 | /* obtain custom attributes */ 171 | foreach (object attr in attributes) { 172 | if (attr is AssemblyDescriptionAttribute) { 173 | AssemblyDescriptionAttribute a = (AssemblyDescriptionAttribute)attr; 174 | description = a.Description; 175 | } 176 | else if (attr is AssemblyCopyrightAttribute) { 177 | AssemblyCopyrightAttribute a = (AssemblyCopyrightAttribute)attr; 178 | copyright = a.Copyright; 179 | } /* end if */ 180 | } /* foreach */ 181 | 182 | /* set license info */ 183 | license = DefaultLicense; 184 | 185 | initialized = true; 186 | 187 | return; 188 | } /* end Init */ 189 | 190 | 191 | /* --------------------------------------------------------------------------- 192 | * method Name() 193 | * --------------------------------------------------------------------------- 194 | * Returns the human readable name of the current assembly. 195 | * ------------------------------------------------------------------------ */ 196 | 197 | public static string Name () { 198 | if (initialized == false) { 199 | ProductInfo.Init(); 200 | } /* end if */ 201 | return name; 202 | } /* end Name */ 203 | 204 | 205 | /* --------------------------------------------------------------------------- 206 | * method VersionMajor() 207 | * --------------------------------------------------------------------------- 208 | * Returns the major version of the current assembly. 209 | * ------------------------------------------------------------------------ */ 210 | 211 | public static int VersionMajor () { 212 | if (initialized == false) { 213 | ProductInfo.Init(); 214 | } /* end if */ 215 | return vmajor; 216 | } /* end VersionMajor */ 217 | 218 | 219 | /* --------------------------------------------------------------------------- 220 | * method VersionMinor() 221 | * --------------------------------------------------------------------------- 222 | * Returns the minor version of the current assembly. 223 | * ------------------------------------------------------------------------ */ 224 | 225 | public static int VersionMinor () { 226 | if (initialized == false) { 227 | ProductInfo.Init(); 228 | } /* end if */ 229 | return vminor; 230 | } /* end VersionMinor */ 231 | 232 | 233 | /* --------------------------------------------------------------------------- 234 | * method Build() 235 | * --------------------------------------------------------------------------- 236 | * Returns the build number of the current assembly. 237 | * ------------------------------------------------------------------------ */ 238 | 239 | public static int Build () { 240 | if (initialized == false) { 241 | ProductInfo.Init(); 242 | } /* end if */ 243 | return build; 244 | } /* end Build */ 245 | 246 | 247 | /* --------------------------------------------------------------------------- 248 | * method Description() 249 | * --------------------------------------------------------------------------- 250 | * Returns the human readable descroption of the current assembly. 251 | * ------------------------------------------------------------------------ */ 252 | 253 | public static string Description () { 254 | if (initialized == false) { 255 | ProductInfo.Init(); 256 | } /* end if */ 257 | return description; 258 | } /* end Description */ 259 | 260 | 261 | /* --------------------------------------------------------------------------- 262 | * method Copyright() 263 | * --------------------------------------------------------------------------- 264 | * Returns the human readable copyright of the current assembly. 265 | * ------------------------------------------------------------------------ */ 266 | 267 | public static string Copyright () { 268 | if (initialized == false) { 269 | ProductInfo.Init(); 270 | } /* end if */ 271 | return copyright; 272 | } /* end Copyright */ 273 | 274 | 275 | /* --------------------------------------------------------------------------- 276 | * method License() 277 | * --------------------------------------------------------------------------- 278 | * Returns the human readable license name for this product. 279 | * ------------------------------------------------------------------------ */ 280 | 281 | public static string License () { 282 | if (initialized == false) { 283 | ProductInfo.Init(); 284 | } /* end if */ 285 | return license; 286 | } /* end License */ 287 | 288 | 289 | } /* ProductInfo */ 290 | 291 | } /* namespace */ 292 | 293 | /* END OF FILE */ 294 | -------------------------------------------------------------------------------- /imp/Program.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * Program.cs 26 | * 27 | * Main program 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | using System; 47 | 48 | namespace org.m2sf.m2sharp { 49 | 50 | class MainClass { 51 | 52 | /* --------------------------------------------------------------------------- 53 | * method PrintBanner() 54 | * --------------------------------------------------------------------------- 55 | * Prints a banner to the console with name, version, copyright and license. 56 | * ------------------------------------------------------------------------ */ 57 | 58 | public static void PrintBanner () { 59 | 60 | Console.WriteLine("{0} version {1}.{2} ({3}), {4}", 61 | ProductInfo.Name(), 62 | ProductInfo.VersionMajor(), ProductInfo.VersionMinor(), 63 | ProductInfo.Build(), ProductInfo.Description()); 64 | 65 | PrintCopyright(); 66 | PrintLicense(); 67 | 68 | } /* end PrintBanner */ 69 | 70 | 71 | /* --------------------------------------------------------------------------- 72 | * constant HelpText 73 | * ------------------------------------------------------------------------ */ 74 | 75 | const string HelpText = 76 | "usage:\n" + 77 | " m2sharp info | compilation\n\n" + 78 | 79 | "info:\n" + 80 | " --help, -h print help\n" + 81 | " --version, -V print version info\n" + 82 | " --license print license info\n\n" + 83 | 84 | "compilation:\n" + 85 | " [dialect] [products] [capabilities] sourcefile [diagnostics]\n\n" + 86 | 87 | "dialect:\n" + 88 | " --pim3 [qualifier] follow PIM, third edition\n" + 89 | " --pim4 [qualifier] follow PIM, fourth edition\n" + 90 | " --ext (D) use extended dialect mode\n\n" + 91 | 92 | " dialect qualifiers:\n" + 93 | " --safer (D) restricted mode\n" + 94 | " --compliant strict compliance mode\n\n" + 95 | 96 | "products:\n" + 97 | " --ast, --no-ast .ast file output\n" + 98 | " --graph, --no-graph .dot file output\n" + 99 | " --xlat (D), --no-xlat .cs file output\n" + 100 | " --obj (D), --no-obj .obj and .sym file output\n\n" + 101 | 102 | " --syntax-only syntax check only\n" + 103 | " --ast-only .ast file only output\n" + 104 | " --graph-only .dot file only output\n" + 105 | " --xlat-only .cs file only output\n" + 106 | " --obj-only .obj and .sym file only output\n\n" + 107 | 108 | " in combination with --xlat or --obj:\n" + 109 | " --use-identifiers-verbatim (D) use original identifiers\n" + 110 | " --transliterate-identifiers transliterate identifiers\n\n" + 111 | 112 | " in combination with --xlat:\n" + 113 | " --preserve-comments (D) preserve comments in .cs files\n" + 114 | " --strip-comments strip comments from .cs files\n\n" + 115 | 116 | "capabilities:\n\n" + 117 | 118 | " in combination with --pim3 or --pim4:\n" + 119 | " --synonyms, --no-synonyms (D)\n" + 120 | " --octal-literals, --no-octal-literals (D)\n" + 121 | " --explicit-cast, --no-explicit-cast (D)\n" + 122 | " --coroutines, --no-coroutines (D)\n" + 123 | " --variant-records, --no-variant-records (D)\n" + 124 | " --local-modules, --no-local-modules (D)\n\n" + 125 | 126 | " in combination with --ext:\n" + 127 | " --lowline-identifiers, --no-lowline-identifiers (D)\n" + 128 | " --to-do-statement (D), --no-to-do-statement\n\n" + 129 | 130 | "sourcefile:\n" + 131 | " may have directory path prepended\n" + 132 | " must match module identifier\n" + 133 | " must have suffix .def or .mod\n\n" + 134 | 135 | "diagnostics:\n" + 136 | " --verbose, -v verbose mode\n" + 137 | " --lexer-debug lexer debug mode\n" + 138 | " --parser-debug parser debug mode\n" + 139 | " --show-settings print compiler settings\n" + 140 | " --errant-semicolons tolerate errant semicolons\n\n" + 141 | 142 | "Default settings are marked with (D)"; 143 | 144 | 145 | /* --------------------------------------------------------------------------- 146 | * method PrintHelp() 147 | * --------------------------------------------------------------------------- 148 | * Prints usage help to the console. 149 | * ------------------------------------------------------------------------ */ 150 | 151 | public static void PrintHelp () { 152 | Console.WriteLine(HelpText); 153 | } /* end PrintHelp */ 154 | 155 | 156 | /* --------------------------------------------------------------------------- 157 | * method PrintVersion() 158 | * --------------------------------------------------------------------------- 159 | * Prints version info to the console. 160 | * ------------------------------------------------------------------------ */ 161 | 162 | public static void PrintVersion () { 163 | Console.WriteLine("version {0}.{1} ({2})", 164 | ProductInfo.VersionMajor(), 165 | ProductInfo.VersionMinor(), 166 | ProductInfo.Build()); 167 | } /* end PrintVersion */ 168 | 169 | 170 | /* --------------------------------------------------------------------------- 171 | * method PrintCopyright() 172 | * --------------------------------------------------------------------------- 173 | * Prints copyright info to the console. 174 | * ------------------------------------------------------------------------ */ 175 | 176 | public static void PrintCopyright () { 177 | Console.WriteLine("copyright {0}", ProductInfo.Copyright()); 178 | } /* end PrintCopyright */ 179 | 180 | 181 | /* --------------------------------------------------------------------------- 182 | * method PrintLicense() 183 | * --------------------------------------------------------------------------- 184 | * Prints license info to the console. 185 | * ------------------------------------------------------------------------ */ 186 | 187 | public static void PrintLicense () { 188 | Console.WriteLine("licensed under the {0}", ProductInfo.License()); 189 | } /* end PrintLicense */ 190 | 191 | 192 | /* --------------------------------------------------------------------------- 193 | * method PrintOSInfo() 194 | * --------------------------------------------------------------------------- 195 | * Prints OS name and version info to the console. 196 | * ------------------------------------------------------------------------ */ 197 | 198 | public static void PrintOSInfo () { 199 | 200 | if (PlatformInfo.IsSupported()) { 201 | Console.WriteLine("running on {0}, {1} version {2}.{3} ({4})", 202 | PlatformInfo.Name(), 203 | PlatformInfo.Uname(), 204 | PlatformInfo.VersionMajor(), 205 | PlatformInfo.VersionMinor(), PlatformInfo.Build()); 206 | } 207 | else /* unsupported platform */ { 208 | Console.WriteLine("platform not supported"); 209 | } /* end if */ 210 | 211 | } /* end PrintOSInfo */ 212 | 213 | 214 | /* --------------------------------------------------------------------------- 215 | * method Main(args) 216 | * --------------------------------------------------------------------------- 217 | * Starts the compiler. 218 | * ------------------------------------------------------------------------ */ 219 | 220 | public static void Main (string[] args) { 221 | ArgumentStatus argStatus; 222 | 223 | if (PlatformInfo.IsSupported() == false) { 224 | Console.WriteLine("platform not supported"); 225 | Environment.Exit(1); 226 | } /* end if */ 227 | 228 | argStatus = ArgumentParser.ParseOptions(args); 229 | 230 | switch (argStatus) { 231 | 232 | case ArgumentStatus.Success : 233 | PrintBanner(); 234 | break; 235 | 236 | case ArgumentStatus.HelpRequested : 237 | PrintHelp(); 238 | Environment.Exit(0); 239 | break; 240 | 241 | case ArgumentStatus.VersionRequested : 242 | PrintVersion(); 243 | Environment.Exit(0); 244 | break; 245 | 246 | case ArgumentStatus.LicenseRequested : 247 | PrintLicense(); 248 | Environment.Exit(0); 249 | break; 250 | 251 | case ArgumentStatus.ErrorsEncountered : 252 | Console.WriteLine("use m2sharp --help for usage info"); 253 | Environment.Exit(1); 254 | break; 255 | 256 | } /* end switch */ 257 | 258 | // TO DO : call parser on input file 259 | 260 | if (CompilerOptions.ShowSettings()) { 261 | CompilerOptions.PrintSettings(); 262 | } /* end if */ 263 | 264 | } /* end Main */ 265 | 266 | } /* MainClass */ 267 | 268 | } /* namespace */ 269 | 270 | /* END OF FILE */ -------------------------------------------------------------------------------- /modula2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | ALIAS 27 | AND 28 | ARGLIST 29 | ARRAY 30 | BARE 31 | BEGIN 32 | BLUEPRINT 33 | BY 34 | CASE 35 | CONST 36 | COPY 37 | DEFINITION 38 | DIV 39 | DO 40 | ELSE 41 | ELSIF 42 | END 43 | EXIT 44 | FOR 45 | FROM 46 | GENLIB 47 | IF 48 | IMPLEMENTATION 49 | IMPORT 50 | IN 51 | LOOP 52 | MOD 53 | MODULE 54 | NEW 55 | NONE 56 | NOT 57 | OF 58 | OPAQUE 59 | OR 60 | POINTER 61 | PROCEDURE 62 | RECORD 63 | REFERENTIAL 64 | RELEASE 65 | REPEAT 66 | RETAIN 67 | RETURN 68 | SET 69 | THEN 70 | TO 71 | TYPE 72 | UNTIL 73 | VAR 74 | WHILE 75 | YIELD 76 | 77 | 78 | 79 | 80 | 81 | NIL 82 | EMPTY 83 | TRUE 84 | FALSE 85 | BOOLEAN 86 | OCTET 87 | CAST 88 | CHAR 89 | UNICHAR 90 | LONGCARD 91 | INTEGER 92 | LONGINT 93 | REAL 94 | LONGREAL 95 | ABS 96 | CAPACITY 97 | FIRST 98 | APPEND 99 | CHR 100 | COUNT 101 | COROUTINE 102 | EXISTS 103 | INSERT 104 | LENGTH 105 | MIN 106 | MAX 107 | NEG 108 | ODD 109 | ORD 110 | PRED 111 | PTR 112 | READ 113 | READNEW 114 | REMOVE 115 | SORT 116 | SORTNEW 117 | SUCC 118 | TDYN 119 | TFLAGS 120 | TLIMIT 121 | TMAX 122 | TMIN 123 | TORDERED 124 | TREFC 125 | TSCALAR 126 | TSIZE 127 | TSORTED 128 | VALUE 129 | WRITE 130 | WRITEF 131 | 132 | 133 | 134 | 135 | " 136 | ' 137 | 138 | 139 | 140 | ! 141 | 142 | ! 143 | 144 | 145 | 146 | (* 147 | *) 148 | 149 | 150 | (* 151 | *) 152 | 153 | 154 | 155 | 156 | <* 157 | *> 158 | 159 | 160 | <* 161 | *> 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /tests/first-and-follow/dummy: -------------------------------------------------------------------------------- 1 | dummy 2 | -------------------------------------------------------------------------------- /tests/m2sources/CRLF.mod: -------------------------------------------------------------------------------- 1 | (* This file has CR LF end-of-file markers, verify with --parser-debug *) 2 | MODULE CRLF; (* line 2 *) 3 | (* line 3 *) 4 | BEGIN (* line 4 *) 5 | Foo; (* line 5 *) 6 | Bar; (* line 6 *) 7 | Baz (* line 7 *) 8 | END CRLF. (* line 8 *) 9 | -------------------------------------------------------------------------------- /tests/m2sources/Comments.PIM.mod: -------------------------------------------------------------------------------- 1 | MODULE Comments; (* PIM version *) 2 | 3 | (* Block comment with line feeds 4 | comment line 2 5 | comment line 3 *) 6 | 7 | (* Block comment with -> <-tab *) 8 | 9 | BEGIN 10 | (* 11 | (* WriteString("*)"); 12 | (* WriteString('*)'); 13 | *) 14 | 15 | (* disabling a code section *) 16 | ?< 17 | WriteString("*)"); 18 | WriteString('*)'); 19 | >? 20 | END Comments. 21 | -------------------------------------------------------------------------------- /tests/m2sources/Comments.mod: -------------------------------------------------------------------------------- 1 | MODULE Comments; (* M2C version *) 2 | 3 | ! Single line comment 4 | 5 | ! Single line comment with -> <-tab 6 | 7 | (* Block comment with line feeds 8 | comment line 2 9 | comment line 3 *) 10 | 11 | (* Block comment with -> <-tab *) 12 | 13 | BEGIN 14 | (* 15 | (* WriteString("*)"); 16 | (* WriteString('*)'); 17 | *) 18 | 19 | (* disabling a code section *) 20 | ?< 21 | WriteString("*)"); 22 | WriteString('*)'); 23 | >? 24 | END Comments. 25 | -------------------------------------------------------------------------------- /tests/m2sources/DerivOrSubrType.def: -------------------------------------------------------------------------------- 1 | DEFINITION MODULE DerivOrSubrType; 2 | 3 | TYPE Derived = INTEGER; 4 | 5 | TYPE ImplicitSubrage = [0..32767]; 6 | 7 | TYPE ExplicitSubrage = INTEGER [0..32767]; 8 | 9 | END DerivOrSubrType. 10 | -------------------------------------------------------------------------------- /tests/m2sources/EmptyFieldListSeq.PIM.mod: -------------------------------------------------------------------------------- 1 | MODULE EmptyFieldListSeq; (* PIM version *) 2 | 3 | (* Ordinary Record Type *) 4 | TYPE R = RECORD (* empty field list sequence *) END; 5 | 6 | (* Variant Record Type *) 7 | TYPE R = RECORD 8 | CASE foo : Foo OF 9 | bar : (* empty field list sequence *) 10 | | baz : (* empty field list sequence *) 11 | ELSE 12 | (* empty field list sequence *) 13 | END 14 | END; 15 | 16 | END EmptyFieldListSeq. 17 | -------------------------------------------------------------------------------- /tests/m2sources/EmptyFieldListSeq.mod: -------------------------------------------------------------------------------- 1 | MODULE EmptyFieldListSeq; (* M2C version *) 2 | 3 | (* Ordinary Record Type *) 4 | TYPE R = RECORD (* empty field list sequence *) END; 5 | 6 | (* Extensible Record Type *) 7 | TYPE R = RECORD ( NIL ) 8 | (* empty field list sequence *) 9 | END; 10 | 11 | (* Variable Size Record Type *) 12 | TYPE VLA = VAR RECORD 13 | (* empty field list sequence *) 14 | IN 15 | (* empty field list sequence *) 16 | END; 17 | 18 | END EmptyFieldListSeq. 19 | -------------------------------------------------------------------------------- /tests/m2sources/EmptyStmtSeq.mod: -------------------------------------------------------------------------------- 1 | MODULE EmptyStmtSeq; 2 | 3 | PROCEDURE Foo ( bar : Bar ); 4 | BEGIN 5 | (* empty statement sequence *) 6 | END Foo; 7 | 8 | BEGIN 9 | WITH foo DO (* empty statement sequence *) END; 10 | 11 | IF foo THEN (* empty statement sequence *) END; 12 | 13 | IF foo THEN bar ELSIF baz THEN (* empty statement sequence *) END; 14 | 15 | IF foo THEN bar ELSE (* empty statement sequence *) END; 16 | 17 | CASE foo OF 18 | bar : (* empty statement sequence *) 19 | | baz : (* empty statement sequence *) 20 | ELSE 21 | (* empty statement sequence *) 22 | END; 23 | 24 | LOOP (* empty statement sequence *) END; 25 | 26 | WHILE foo DO (* empty statement sequence *) END; 27 | 28 | REPEAT (* empty statement sequence *) UNTIL foo; 29 | 30 | FOR i := 0 TO 99 DO (* empty statement sequence *) END 31 | 32 | END EmptyStmtSeq. 33 | -------------------------------------------------------------------------------- /tests/m2sources/IdentList.def: -------------------------------------------------------------------------------- 1 | DEFINITION MODULE IdentList; 2 | 3 | (* duplicate identifier in import list *) 4 | IMPORT Foo, Bar, Baz, Bar, Bam; 5 | 6 | (* duplicate identifier in import list *) 7 | FROM Foo IMPORT foo, bar, baz, bar, bam; 8 | 9 | (* duplicate identifier in enumeration *) 10 | TYPE Enum = ( red, green, blue, cyan, magenta, blue, orange ); 11 | 12 | (* duplicate identifier in variable declaration *) 13 | VAR foo, bar, baz, bar, bam, baz : REAL; 14 | 15 | END IdentList. 16 | -------------------------------------------------------------------------------- /tests/m2sources/Lex.mod: -------------------------------------------------------------------------------- 1 | MODULE Lex; (* M2C version *) 2 | 3 | CONST s1 = "faulty string'; 4 | 5 | CONST s2 = "tab escape\t"; 6 | 7 | CONST s3 = "newline escape\n"; 8 | 9 | CONST s4 = 'illegal escape\0'; 10 | 11 | END Lex. 12 | -------------------------------------------------------------------------------- /tests/m2sources/ProcType.PIM.def: -------------------------------------------------------------------------------- 1 | DEFINITION MODULE ProcType; (* PIM version *) 2 | 3 | TYPE P1 = PROCEDURE; 4 | 5 | TYPE P2 = PROCEDURE ( ); 6 | 7 | TYPE P3 = PROCEDURE ( CHAR ); 8 | 9 | TYPE P4 = PROCEDURE ( VAR CHAR ); 10 | 11 | TYPE P5 = PROCEDURE ( ARRAY OF CHAR ); 12 | 13 | TYPE P6 = PROCEDURE ( VAR ARRAY OF CHAR ); 14 | 15 | TYPE P7 = PROCEDURE ( INTEGER, CHAR ); 16 | 17 | TYPE P8 = PROCEDURE ( VAR INTEGER, CHAR ); 18 | 19 | TYPE P9 = PROCEDURE ( ARRAY OF INTEGER, CHAR ); 20 | 21 | TYPE P10 = PROCEDURE ( VAR ARRAY OF INTEGER, CHAR ); 22 | 23 | TYPE P11 = PROCEDURE ( INTEGER, VAR CHAR ); 24 | 25 | TYPE P12 = PROCEDURE ( INTEGER, ARRAY OF CHAR ); 26 | 27 | TYPE P13 = PROCEDURE ( INTEGER, VAR ARRAY OF CHAR ); 28 | 29 | TYPE F1 = PROCEDURE : BOOLEAN; 30 | 31 | TYPE F2 = PROCEDURE ( ) : BOOLEAN; 32 | 33 | TYPE F3 = PROCEDURE ( CHAR ) : BOOLEAN; 34 | 35 | TYPE F4 = PROCEDURE ( VAR CHAR ) : BOOLEAN; 36 | 37 | TYPE F5 = PROCEDURE ( ARRAY OF CHAR ) : BOOLEAN; 38 | 39 | TYPE F6 = PROCEDURE ( VAR ARRAY OF CHAR ) : BOOLEAN; 40 | 41 | TYPE F7 = PROCEDURE ( INTEGER, CHAR ) : BOOLEAN; 42 | 43 | TYPE F8 = PROCEDURE ( VAR INTEGER, CHAR ) : BOOLEAN; 44 | 45 | TYPE F9 = PROCEDURE ( ARRAY OF INTEGER, CHAR ) : BOOLEAN; 46 | 47 | TYPE F10 = PROCEDURE ( VAR ARRAY OF INTEGER, CHAR ) : BOOLEAN; 48 | 49 | TYPE F11 = PROCEDURE ( INTEGER, VAR CHAR ) : BOOLEAN; 50 | 51 | TYPE F12 = PROCEDURE ( INTEGER, ARRAY OF CHAR ) : BOOLEAN; 52 | 53 | TYPE F13 = PROCEDURE ( INTEGER, VAR ARRAY OF CHAR ) : BOOLEAN; 54 | 55 | END ProcType. -------------------------------------------------------------------------------- /tests/m2sources/ProcType.def: -------------------------------------------------------------------------------- 1 | DEFINITION MODULE ProcType; (* M2C version *) 2 | 3 | TYPE P1 = PROCEDURE; 4 | 5 | TYPE P2 = PROCEDURE ( ); 6 | 7 | TYPE P3 = PROCEDURE ( CHAR ); 8 | 9 | TYPE P4 = PROCEDURE ( CONST CHAR ); 10 | 11 | TYPE P5 = PROCEDURE ( VAR CHAR ); 12 | 13 | TYPE P6 = PROCEDURE ( ARRAY OF CHAR ); 14 | 15 | TYPE P7 = PROCEDURE ( CONST ARRAY OF CHAR ); 16 | 17 | TYPE P8 = PROCEDURE ( VAR ARRAY OF CHAR ); 18 | 19 | TYPE P9 = PROCEDURE ( INTEGER, CHAR ); 20 | 21 | TYPE P10 = PROCEDURE ( CONST INTEGER, CHAR ); 22 | 23 | TYPE P11 = PROCEDURE ( VAR INTEGER, CHAR ); 24 | 25 | TYPE P12 = PROCEDURE ( ARRAY OF INTEGER, CHAR ); 26 | 27 | TYPE P13 = PROCEDURE ( CONST ARRAY OF INTEGER, CHAR ); 28 | 29 | TYPE P14 = PROCEDURE ( VAR ARRAY OF INTEGER, CHAR ); 30 | 31 | TYPE P15 = PROCEDURE ( INTEGER, CONST CHAR ); 32 | 33 | TYPE P16 = PROCEDURE ( INTEGER, VAR CHAR ); 34 | 35 | TYPE P17 = PROCEDURE ( INTEGER, ARRAY OF CHAR ); 36 | 37 | TYPE P18 = PROCEDURE ( INTEGER, CONST ARRAY OF CHAR ); 38 | 39 | TYPE P19 = PROCEDURE ( INTEGER, VAR ARRAY OF CHAR ); 40 | 41 | TYPE F1 = PROCEDURE : BOOLEAN; 42 | 43 | TYPE F2 = PROCEDURE ( ) : BOOLEAN; 44 | 45 | TYPE F3 = PROCEDURE ( CHAR ) : BOOLEAN; 46 | 47 | TYPE F4 = PROCEDURE ( VAR CHAR ) : BOOLEAN; 48 | 49 | TYPE F5 = PROCEDURE ( CONST CHAR ) : BOOLEAN; 50 | 51 | TYPE F6 = PROCEDURE ( ARRAY OF CHAR ) : BOOLEAN; 52 | 53 | TYPE F7 = PROCEDURE ( CONST ARRAY OF CHAR ) : BOOLEAN; 54 | 55 | TYPE F8 = PROCEDURE ( VAR ARRAY OF CHAR ) : BOOLEAN; 56 | 57 | TYPE F9 = PROCEDURE ( INTEGER, CHAR ) : BOOLEAN; 58 | 59 | TYPE F10 = PROCEDURE ( CONST INTEGER, CHAR ) : BOOLEAN; 60 | 61 | TYPE F11 = PROCEDURE ( VAR INTEGER, CHAR ) : BOOLEAN; 62 | 63 | TYPE F12 = PROCEDURE ( ARRAY OF INTEGER, CHAR ) : BOOLEAN; 64 | 65 | TYPE F13 = PROCEDURE ( CONST ARRAY OF INTEGER, CHAR ) : BOOLEAN; 66 | 67 | TYPE F14 = PROCEDURE ( VAR ARRAY OF INTEGER, CHAR ) : BOOLEAN; 68 | 69 | TYPE F15 = PROCEDURE ( INTEGER, CONST CHAR ) : BOOLEAN; 70 | 71 | TYPE F16 = PROCEDURE ( INTEGER, VAR CHAR ) : BOOLEAN; 72 | 73 | TYPE F17 = PROCEDURE ( INTEGER, ARRAY OF CHAR ) : BOOLEAN; 74 | 75 | TYPE F18 = PROCEDURE ( INTEGER, CONST ARRAY OF CHAR ) : BOOLEAN; 76 | 77 | TYPE F19 = PROCEDURE ( INTEGER, VAR ARRAY OF CHAR ) : BOOLEAN; 78 | 79 | END ProcType. -------------------------------------------------------------------------------- /tests/m2sources/Semicolon.PIM.mod: -------------------------------------------------------------------------------- 1 | MODULE Semicolon; (* PIM version *) 2 | 3 | (* Ordinary Record Type *) 4 | TYPE R = RECORD i : INTEGER; (* errant semicolon *) END; 5 | 6 | (* Variant Record Type *) 7 | TYPE R = RECORD 8 | CASE foo : Foo OF 9 | bar : i : INTEGER; (* errant semicolon *) 10 | | baz : n : CARDINAL; (* errant semicolon *) 11 | ELSE 12 | ch : CHAR; (* errant semicolon *) 13 | END; (* errant semicolon *) 14 | END; 15 | 16 | (* Formal Parameter List *) 17 | PROCEDURE Foo ( bar : Bar; baz : Baz; (* errant semicolon *) ); 18 | BEGIN 19 | Barbaz; (* errant semicolon *) 20 | END Foo; 21 | 22 | (* Statement Sequence *) 23 | BEGIN 24 | WITH foo DO bar; (* errant semicolon *) END; 25 | 26 | IF foo THEN bar; (* errant semicolon *) END; 27 | 28 | IF foo THEN bar ELSIF baz THEN bam; (* errant semicolon *) END; 29 | 30 | IF foo THEN bar ELSE baz; (* errant semicolon *) END; 31 | 32 | CASE foo OF 33 | bar : bam; (* errant semicolon *) 34 | | baz : boo; (* errant semicolon *) 35 | ELSE 36 | dodo; (* errant semicolon *) 37 | END; 38 | 39 | LOOP bar; (* errant semicolon *) END; 40 | 41 | WHILE foo DO bar; (* errant semicolon *) END; 42 | 43 | REPEAT bar; (* errant semicolon *) UNTIL foo; 44 | 45 | FOR i := 0 TO 99 DO bar; (* errant semicolon *) END; 46 | 47 | Foobar; Bazbam; (* errant semicolon *) 48 | 49 | END Semicolon. 50 | -------------------------------------------------------------------------------- /tests/m2sources/Semicolon.mod: -------------------------------------------------------------------------------- 1 | MODULE Semicolon; (* Extended version *) 2 | 3 | (* Ordinary Record Type *) 4 | TYPE R = RECORD i : INTEGER; (* errant semicolon *) END; 5 | 6 | (* Extensible Record Type *) 7 | TYPE R = RECORD ( NIL ) 8 | i : INTEGER; (* errant semicolon *) 9 | END; 10 | 11 | (* Variable Size Record Type *) 12 | TYPE VLA = VAR RECORD 13 | size : CARDINAL; (* errant semicolon *) 14 | IN 15 | buffer : ARRAY size OF CHAR; (* errant semicolon *) 16 | END; 17 | 18 | (* Formal Parameter List *) 19 | PROCEDURE Foo ( bar : Bar; baz : Baz; (* errant semicolon *) ); 20 | BEGIN 21 | Barbaz; (* errant semicolon *) 22 | END Foo; 23 | 24 | (* Statement Sequence *) 25 | BEGIN 26 | WITH foo DO bar; (* errant semicolon *) END; 27 | 28 | IF foo THEN bar; (* errant semicolon *) END; 29 | 30 | IF foo THEN bar ELSIF baz THEN bam; (* errant semicolon *) END; 31 | 32 | IF foo THEN bar ELSE baz; (* errant semicolon *) END; 33 | 34 | CASE foo OF 35 | | bar : bam; (* errant semicolon *) 36 | | baz : boo; (* errant semicolon *) 37 | ELSE 38 | dodo; (* errant semicolon *) 39 | END; 40 | 41 | LOOP bar; (* errant semicolon *) END; 42 | 43 | WHILE foo DO bar; (* errant semicolon *) END; 44 | 45 | REPEAT bar; (* errant semicolon *) UNTIL foo; 46 | 47 | FOR i := 0 TO 99 DO bar; (* errant semicolon *) END; 48 | 49 | Foobar; Bazbam; (* errant semicolon *) 50 | 51 | END Semicolon. 52 | -------------------------------------------------------------------------------- /tests/m2sources/VarSizeRec.mod: -------------------------------------------------------------------------------- 1 | MODULE VarSizeRec; 2 | 3 | TYPE VLA = VAR RECORD 4 | size : CARDINAL; 5 | foo : INTEGER; 6 | bar : REAL 7 | IN 8 | buffer : ARRAY size OF CHAR 9 | END; 10 | 11 | PROCEDURE InitVLA ( VAR r : VLA ); 12 | VAR 13 | index : CARDINAL; 14 | BEGIN 15 | IF r = NIL THEN RETURN END; 16 | 17 | r^.foo := 0; 18 | r^.bar := 0.0; 19 | 20 | FOR i := 0 TO r^.size - 1 DO 21 | r^.buffer[index] := 0 22 | END; 23 | 24 | RETURN 25 | END InitVLA; 26 | 27 | VAR r : VLA; 28 | 29 | BEGIN 30 | NEW(r, 100); 31 | InitVLA(r) 32 | END VarSizeRec. 33 | -------------------------------------------------------------------------------- /xamarin/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | /* M2Sharp -- Modula-2 to C# Translator & Compiler 2 | * 3 | * Copyright (c) 2016 The Modula-2 Software Foundation 4 | * 5 | * Author & Maintainer: Benjamin Kowarsch 6 | * 7 | * @synopsis 8 | * 9 | * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. 10 | * It supports the dialects described in the 3rd and 4th editions of Niklaus 11 | * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, 12 | * and an extended mode with select features from the revised language by 13 | * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). 14 | * 15 | * In translator mode, M2Sharp translates Modula-2 source to C# source files. 16 | * In compiler mode, M2Sharp compiles Modula-2 source via C# source files 17 | * to object code or executables using the host system's C# compiler. 18 | * 19 | * @repository 20 | * 21 | * https://github.com/m2sf/m2sharp 22 | * 23 | * @file 24 | * 25 | * AssemblyInfo.cs 26 | * 27 | * M2Sharp assembly 28 | * 29 | * @license 30 | * 31 | * M2Sharp is free software: you can redistribute and/or modify it under the 32 | * terms of the GNU Lesser General Public License (LGPL) either version 2.1 33 | * or at your choice version 3 as published by the Free Software Foundation. 34 | * However, you may not alter the copyright, author and license information. 35 | * 36 | * M2Sharp is distributed in the hope that it will be useful, but WITHOUT 37 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 | * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. 39 | * 40 | * You should have received a copy of the GNU Lesser General Public License 41 | * along with M2Sharp. If not, see . 42 | * 43 | * NB: Components in the domain part of email addresses are in reverse order. 44 | */ 45 | 46 | using System.Reflection; 47 | using System.Runtime.CompilerServices; 48 | 49 | [assembly: AssemblyTitle("m2sharp")] 50 | [assembly: AssemblyDescription("Modula-2 to C# Translator & Compiler")] 51 | [assembly: AssemblyConfiguration("")] 52 | [assembly: AssemblyCompany("Modula-2 Software Foundation")] 53 | [assembly: AssemblyProduct("m2sharp")] 54 | [assembly: AssemblyCopyright("(c) 2016 The Modula-2 Software Foundation")] 55 | [assembly: AssemblyTrademark("")] 56 | [assembly: AssemblyCulture("")] 57 | 58 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 59 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 60 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 61 | 62 | [assembly: AssemblyVersion("1.0.*")] 63 | 64 | // The following attributes are used to specify the signing key for the assembly, 65 | // if desired. See the Mono documentation for more information about signing. 66 | 67 | //[assembly: AssemblyDelaySign(false)] 68 | //[assembly: AssemblyKeyFile("")] 69 | 70 | /* END OF FILE */ -------------------------------------------------------------------------------- /xamarin/m2sharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | {BC1EE1FB-F484-40BC-9A99-EC7B5BA9E3CC} 7 | Exe 8 | org.m2sf.m2sharp 9 | m2sharp 10 | v4.5 11 | Modula-2 to C# Translator & Compiler 12 | 13 | 14 | true 15 | full 16 | false 17 | bin\Debug 18 | DEBUG; 19 | prompt 20 | 4 21 | true 22 | x86 23 | 24 | 25 | true 26 | bin\Release 27 | prompt 28 | 4 29 | true 30 | x86 31 | 32 | 33 | 34 | 35 | 36 | ..\ascii\bin\Debug\ascii.dll 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 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /xamarin/m2sharp.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "m2sharp", "m2sharp.csproj", "{BC1EE1FB-F484-40BC-9A99-EC7B5BA9E3CC}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|x86 = Debug|x86 9 | Release|x86 = Release|x86 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {BC1EE1FB-F484-40BC-9A99-EC7B5BA9E3CC}.Debug|x86.ActiveCfg = Debug|x86 13 | {BC1EE1FB-F484-40BC-9A99-EC7B5BA9E3CC}.Debug|x86.Build.0 = Debug|x86 14 | {BC1EE1FB-F484-40BC-9A99-EC7B5BA9E3CC}.Release|x86.ActiveCfg = Release|x86 15 | {BC1EE1FB-F484-40BC-9A99-EC7B5BA9E3CC}.Release|x86.Build.0 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(MonoDevelopProperties) = preSolution 18 | Policies = $0 19 | $0.StandardHeader = $1 20 | $1.Text = @/* M2Sharp -- Modula-2 to C# Translator & Compiler\n *\n * Copyright (c) ${Year} The Modula-2 Software Foundation\n *\n * Author & Maintainer: ${AuthorName} <${AuthorEmail}>\n *\n * @synopsis\n *\n * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler.\n * It supports the dialects described in the 3rd and 4th editions of Niklaus\n * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag,\n * and an extended mode with select features from the revised language by\n * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10).\n *\n * In translator mode, M2Sharp translates Modula-2 source to C# source files.\n * In compiler mode, M2Sharp compiles Modula-2 source via C# source files\n * to object code or executables using the host system's C# compiler.\n *\n * @repository\n *\n * https://github.com/m2sf/m2sharp\n *\n * @file\n *\n * ${filename}\n *\n * \n *\n * @license\n *\n * M2Sharp is free software: you can redistribute and/or modify it under the\n * terms of the GNU Lesser General Public License (LGPL) either version 2.1\n * or at your choice version 3 as published by the Free Software Foundation.\n * However, you may not alter the copyright, author and license information.\n *\n * M2Sharp is distributed in the hope that it will be useful, but WITHOUT\n * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details.\n *\n * You should have received a copy of the GNU Lesser General Public License\n * along with M2Sharp. If not, see .\n *\n * NB: Components in the domain part of email addresses are in reverse order.\n */\n\nnamespace org.m2sf.m2sharp {\n\n\n\n} /* namespace */\n\n/* END OF FILE */ 21 | $1.IncludeInNewFiles = False 22 | $0.DotNetNamingPolicy = $2 23 | $2.DirectoryNamespaceAssociation = None 24 | $2.ResourceNamePolicy = FileFormatDefault 25 | $0.TextStylePolicy = $3 26 | $3.TabWidth = 2 27 | $3.IndentWidth = 2 28 | $3.EolMarker = Unix 29 | $3.inheritsSet = VisualStudio 30 | $3.inheritsScope = text/plain 31 | $3.scope = text/plain 32 | $3.FileWidth = 120 33 | $3.TabsToSpaces = False 34 | $0.CSharpFormattingPolicy = $4 35 | $4.NewLinesForBracesInTypes = False 36 | $4.NewLinesForBracesInMethods = False 37 | $4.NewLineForElse = True 38 | $4.NewLineForCatch = True 39 | $4.NewLineForFinally = True 40 | $4.SpaceAfterMethodCallName = False 41 | $4.SpaceBeforeOpenSquareBracket = False 42 | $4.inheritsSet = Mono 43 | $4.inheritsScope = text/x-csharp 44 | $4.scope = text/x-csharp 45 | $0.NameConventionPolicy = $5 46 | $5.Rules = $6 47 | $6.NamingRule = $7 48 | $7.Name = Namespaces 49 | $7.AffectedEntity = Namespace 50 | $7.VisibilityMask = VisibilityMask 51 | $7.NamingStyle = PascalCase 52 | $7.IncludeInstanceMembers = True 53 | $7.IncludeStaticEntities = True 54 | $6.NamingRule = $8 55 | $8.Name = Types 56 | $8.AffectedEntity = Class, Struct, Enum, Delegate 57 | $8.VisibilityMask = Public 58 | $8.NamingStyle = PascalCase 59 | $8.IncludeInstanceMembers = True 60 | $8.IncludeStaticEntities = True 61 | $6.NamingRule = $9 62 | $9.Name = Interfaces 63 | $9.RequiredPrefixes = $10 64 | $10.String = I 65 | $9.AffectedEntity = Interface 66 | $9.VisibilityMask = Public 67 | $9.NamingStyle = PascalCase 68 | $9.IncludeInstanceMembers = True 69 | $9.IncludeStaticEntities = True 70 | $6.NamingRule = $11 71 | $11.Name = Attributes 72 | $11.RequiredSuffixes = $12 73 | $12.String = Attribute 74 | $11.AffectedEntity = CustomAttributes 75 | $11.VisibilityMask = Public 76 | $11.NamingStyle = PascalCase 77 | $11.IncludeInstanceMembers = True 78 | $11.IncludeStaticEntities = True 79 | $6.NamingRule = $13 80 | $13.Name = Event Arguments 81 | $13.RequiredSuffixes = $14 82 | $14.String = EventArgs 83 | $13.AffectedEntity = CustomEventArgs 84 | $13.VisibilityMask = Public 85 | $13.NamingStyle = PascalCase 86 | $13.IncludeInstanceMembers = True 87 | $13.IncludeStaticEntities = True 88 | $6.NamingRule = $15 89 | $15.Name = Exceptions 90 | $15.RequiredSuffixes = $16 91 | $16.String = Exception 92 | $15.AffectedEntity = CustomExceptions 93 | $15.VisibilityMask = VisibilityMask 94 | $15.NamingStyle = PascalCase 95 | $15.IncludeInstanceMembers = True 96 | $15.IncludeStaticEntities = True 97 | $6.NamingRule = $17 98 | $17.Name = Methods 99 | $17.AffectedEntity = Methods 100 | $17.VisibilityMask = Protected, Public 101 | $17.NamingStyle = PascalCase 102 | $17.IncludeInstanceMembers = True 103 | $17.IncludeStaticEntities = True 104 | $6.NamingRule = $18 105 | $18.Name = Static Readonly Fields 106 | $18.AffectedEntity = ReadonlyField 107 | $18.VisibilityMask = Protected, Public 108 | $18.NamingStyle = PascalCase 109 | $18.IncludeInstanceMembers = False 110 | $18.IncludeStaticEntities = True 111 | $6.NamingRule = $19 112 | $19.Name = Fields 113 | $19.AffectedEntity = Field 114 | $19.VisibilityMask = Protected, Public 115 | $19.NamingStyle = PascalCase 116 | $19.IncludeInstanceMembers = True 117 | $19.IncludeStaticEntities = True 118 | $6.NamingRule = $20 119 | $20.Name = ReadOnly Fields 120 | $20.AffectedEntity = ReadonlyField 121 | $20.VisibilityMask = Protected, Public 122 | $20.NamingStyle = PascalCase 123 | $20.IncludeInstanceMembers = True 124 | $20.IncludeStaticEntities = False 125 | $6.NamingRule = $21 126 | $21.Name = Constant Fields 127 | $21.AffectedEntity = ConstantField 128 | $21.VisibilityMask = Protected, Public 129 | $21.NamingStyle = PascalCase 130 | $21.IncludeInstanceMembers = True 131 | $21.IncludeStaticEntities = True 132 | $6.NamingRule = $22 133 | $22.Name = Properties 134 | $22.AffectedEntity = Property 135 | $22.VisibilityMask = Protected, Public 136 | $22.NamingStyle = PascalCase 137 | $22.IncludeInstanceMembers = True 138 | $22.IncludeStaticEntities = True 139 | $6.NamingRule = $23 140 | $23.Name = Events 141 | $23.AffectedEntity = Event 142 | $23.VisibilityMask = Protected, Public 143 | $23.NamingStyle = PascalCase 144 | $23.IncludeInstanceMembers = True 145 | $23.IncludeStaticEntities = True 146 | $6.NamingRule = $24 147 | $24.Name = Enum Members 148 | $24.AffectedEntity = EnumMember 149 | $24.VisibilityMask = VisibilityMask 150 | $24.NamingStyle = PascalCase 151 | $24.IncludeInstanceMembers = True 152 | $24.IncludeStaticEntities = True 153 | $6.NamingRule = $25 154 | $25.Name = Parameters 155 | $25.AffectedEntity = Parameter 156 | $25.VisibilityMask = VisibilityMask 157 | $25.NamingStyle = CamelCase 158 | $25.IncludeInstanceMembers = True 159 | $25.IncludeStaticEntities = True 160 | $6.NamingRule = $26 161 | $26.Name = Type Parameters 162 | $26.RequiredPrefixes = $27 163 | $27.String = T 164 | $26.AffectedEntity = TypeParameter 165 | $26.VisibilityMask = VisibilityMask 166 | $26.NamingStyle = PascalCase 167 | $26.IncludeInstanceMembers = True 168 | $26.IncludeStaticEntities = True 169 | EndGlobalSection 170 | EndGlobal 171 | --------------------------------------------------------------------------------