Просмотр исходного кода

struct debug, command line arguments validation, unit tests

221V 1 месяц назад
Родитель
Сommit
d2d67b3a6c
4 измененных файлов с 172 добавлено и 18 удалено
  1. 4 1
      Makefile
  2. 2 2
      dub.json
  3. 0 15
      source/app.d
  4. 166 0
      source/dopp.d

+ 4 - 1
Makefile

@@ -2,5 +2,8 @@
 d:
 	dub build --compiler ldc2 --build release --force
 
+t:
+	dub test
+
 default: d
-.PHONY: d
+.PHONY: d t

+ 2 - 2
dub.json

@@ -6,8 +6,8 @@
   "description": "dopp = DEF compiler / D preprocessor",
   "license": "MIT",
   "name": "dopp",
-  "mainSourceFile": "source/app.d",
-  "sourceFiles": ["source/app.d"],
+  "mainSourceFile": "source/dopp.d",
+  "sourceFiles": ["source/dopp.d", "source/dopp_lexer.d", "source/dopp_parser.d"],
   "targetName": "dopp",
   "targetType": "executable",
   "dflags": ["-w", "-O", "-static"],

+ 0 - 15
source/app.d

@@ -1,15 +0,0 @@
-
-import toml;
-import std.stdio : writeln;
-import std.file : read;
-
-
-TOMLDocument toml_s;
-
-
-void main(){
-  toml_s = parseTOML(cast(string)read("dopp.toml"));
-  writeln("indent_matter: ", toml_s["indent_matter"]); // 4
-}
-
-

+ 166 - 0
source/dopp.d

@@ -0,0 +1,166 @@
+
+//import toml;
+
+public{
+  import std.stdio : writeln;
+  import std.file : read;
+  import std.array;
+  import std.string;
+  import std.uni : isWhite, isAlpha, isAlphaNum;
+  import std.ascii : isDigit;
+  import std.conv : to;
+  import std.algorithm;
+}
+
+//import dopp_lexer : tokenize, isKeyword;
+//import dopp_parser : parse;
+
+
+//TOMLDocument toml_s;
+
+
+export enum TokenType{
+  Identifier,
+  Keyword,
+  Integer,
+  Float,
+  String,
+  Symbol,
+  Whitespace
+}
+
+
+export struct Token{
+  TokenType type;
+  string lexeme;
+  
+  // for debug
+  string toString(){
+    import std.format : format;
+    //return "Token(type: %d, lexeme: %s)".format(type, lexeme);
+    return "Token = type: %d, lexeme: %s\n".format(type, lexeme);
+  }
+}
+
+
+
+// ./dopp -d "/test" -o "/test2" == input dir path + output dir path (default output dir = input dir)
+// ./dopp -f test.de -o test.d == input file name + output file name (default output file name = input file name [but .d instead .de] )
+// ./dopp -f "/test/test.de" -o "/result/test.d" == input file path + output file path (default output file name and path = input file name and path [but .d instead .de] )
+bool is_valid_argv(ref string[] argv){
+  //ulong num = argv[1 .. $].length; // uint64
+  ulong num = argv.length; // uint64
+  if(num != 1 && num != 2 && num != 4){
+    return false;
+  
+  }else if(num == 1){ // one argument only (just filename or /path/filename) == shortened ./dopp -f test.de = ./dopp test.de
+    if(argv[0].length >= 4 && argv[0][$-3 .. $] == ".de"){
+      return true;
+    }else{
+      return false;
+    }
+  
+  }else if(num == 2){ // two arguments = input file name/path or input dir path
+    if(argv[0] != "-d" && argv[0] != "-f"){
+      return false;
+    
+    }else if(argv[0] == "-f"){ // must be file
+       if(argv[1].length >= 4 && argv[1][$-3 .. $] == ".de"){
+         return true;
+       }else{
+         return false;
+       }
+    
+    }else{ // dir
+      if(argv[1].length > 0){
+        return true;
+      }else{
+        return false;
+      }
+    }
+  
+  }else{ // num == 4
+    string[] argv2 = argv[0 .. 2];
+    if( is_valid_argv(argv2) ){ // valid first 2
+      string[] argv4 = argv[2 .. $];
+      if( argv[2] != "-o" ){ // valid next - last 2
+        return false;
+      }else if(argv[0] == "-f"){ // must be file
+        if(argv[3].length >= 3 && argv[3][$-2 .. $] == ".d"){
+          return true;
+        }else{
+          return false;
+        }
+      
+      }else{ // dir
+        if(argv[3].length > 0){
+          return true;
+        }else{
+          return false;
+        }
+      }
+      
+    }else{
+      return false;
+    }
+  }
+}
+
+
+
+//void main(string[] argv){
+int main(string[] argv){
+  /*
+  toml_s = parseTOML(cast(string)read("dopp.toml"));
+  writeln("indent_matter: ", toml_s["indent_matter"]); // 4
+  writeln("xyz: ", toml_s["xyz"]); // Address boundary error
+  */
+  
+  /*
+  int i = 0;
+  foreach(string s; argv[1 .. $]){
+    writeln(s);
+    i++;
+  }
+  writeln("i = ", i);
+  */
+  
+  string[] argv1 = argv[1 .. $]; // argv[0] always = program name ( ./dopp in my case in linux )
+  bool valid1 = is_valid_argv(argv1);
+  writeln("valid1 = ", valid1);
+  
+  if(!valid1){
+    return 0;
+  }
+  
+  
+  
+  //string source = `if (x > 0) { writeln("Positive"); }`;
+  //auto tokens = tokenize(source);
+  //writeln(tokens);
+  //parse(tokens);
+  
+  return 1;
+}
+
+
+unittest{
+  assert( is_valid_argv([]) == false );
+  assert( is_valid_argv([""]) == false );
+  assert( is_valid_argv(["-d"]) == false );
+  assert( is_valid_argv(["-d", ""]) == false );
+  assert( is_valid_argv(["-d", "/test"]) == true );
+  assert( is_valid_argv(["-d", "/test", "-o", "/test2"]) == true );
+  assert( is_valid_argv(["test.de"]) == true );
+  assert( is_valid_argv(["-f", "test.de"]) == true );
+  assert( is_valid_argv(["-f", "test.de", "-o", "test.d"]) == true );
+  assert( is_valid_argv(["test.txt"]) == false );
+  assert( is_valid_argv(["-f", "test.txt"]) == false );
+  assert( is_valid_argv(["-f", "test.txt", "-o", "test.d"]) == false );
+  assert( is_valid_argv(["-f", "test.de", "-o", "test.txt"]) == false );
+}
+
+// ./dopp -d "/test" -o "/test2" == input dir path + output dir path (default output dir = input dir)
+// ./dopp -f test.de -o test.d == input file name + output file name (default output file name = input file name [but .d instead .de] )
+// ./dopp -f "/test/test.de" -o "/result/test.d" == input file path + output file path (default output file name and path = input file name and path [but .d instead .de] )
+