ソースを参照

Update some samples and add 2 others

mingodad 8 年 前
コミット
6cf506d15f

+ 12 - 4
SquiLu/samples/lexer.nut

@@ -81,10 +81,16 @@ class Lexer {
         var _process_number = function()
         {
             var endpos = this.pos + 1;
-            while (endpos < this.buflen &&
-                    _isdigit(this.buf[endpos]))
+	    var hasDot = false;
+            while (endpos < this.buflen)
             {
-                endpos++;
+		var ch = this.buf[endpos];
+                if(!_isdigit(ch))
+		{
+			if(!hasDot && (ch == '.')) hasDot = true;
+			else break;
+		}
+		endpos++;
             }
 
             var tok = {
@@ -236,11 +242,13 @@ class Lexer {
     }
 }
 
-var txt = "var lex = new Lexer(23, \"dad\");";
+var txt = "var lex = new Lexer(26.389, \"dad\");";
 var lex = new Lexer();
+/*
 var fd = file("lexer.nut", "r");
 txt = fd.read(fd.len());
 fd.close();
+*/
 
 lex.input(txt);
 var tok =  lex.token();

+ 1 - 1
SquiLu/samples/sqparser.nut

@@ -2335,7 +2335,7 @@ error:
 };
 
 string_t txt = readfile("test-raii.nut")
-               var lex = new SQLexer(txt, true /*we want comments*/);
+var lex = new SQLexer(txt, true /*we want comments*/);
 
 var first_enum_token = lex.first_enum_token();
 var last_enum_token = lex.last_enum_token();

+ 43 - 0
SquiLu/samples/test-str-ushort.nut

@@ -0,0 +1,43 @@
+local str = "\x10\x10";
+print(str[0], str[1], str.uchar(0), str.uchar(1), str.ushort(0), (str.uchar(0) * 256) + str.uchar(1));
+
+auto max_loop = 64 * 1024;
+str = str.rep(max_loop);
+
+auto start_time = os.clock();
+for(auto i=0; i < max_loop; ++i)
+{
+	auto ch = str[i];
+}
+print("Time spent", max_loop, os.clock() - start_time);
+
+start_time = os.clock();
+for(auto i=0; i < max_loop; ++i)
+{
+	auto ch = str.uchar(i);
+}
+print("Time spent", max_loop, os.clock() - start_time);
+
+start_time = os.clock();
+for(auto i=0, len = max_loop/2; i < len; ++i)
+{
+	auto ch = str.ushort(i);
+}
+print("Time spent", max_loop, os.clock() - start_time);
+
+start_time = os.clock();
+for(auto i=0, len = max_loop/2; i < len; ++i)
+{
+	auto ch = (str[i] * 256) + str[i+1];
+}
+print("Time spent", max_loop, os.clock() - start_time);
+
+local buf = blob();
+buf.write(str);
+buf.seek(0);
+start_time = os.clock();
+for(auto i=0, len = max_loop/2; i < len; ++i)
+{
+	auto ch = buf.readn('w');
+}
+print("Time spent", max_loop, os.clock() - start_time);

+ 55 - 0
SquiLu/samples/topological-sort.nut

@@ -0,0 +1,55 @@
+auto recipes = [
+    ["martian stew", "thingy,pig,cheese danish,chicken"],
+    ["cheese danish", "flour,butter,egg,vanilla,cream cheese,sugar"],
+    ["butter", "milk,salt"],
+    ["thingy", "iron,apple,vanilla"],
+    ["cream cheese", "milk,salt"],
+    ["chicken", "worm"],
+    ["worm", "apple"],
+    ["egg", "chicken"],
+    ["milk", "cow"],
+    ["cow", "grass"],
+    ["pig", "apple,worm"]
+];
+
+auto function to_graph(recipes)
+{
+    //get recipes into a canonical form for further manipulation
+    auto nodes = [];
+    auto nodemap = {};
+    local function add_node(name)
+    {
+	auto node;
+        if( name in nodemap )
+            node = nodemap[name];
+        else
+	{
+            node = {"name": name};
+            nodes.append(node);
+            nodemap[name] <- node;
+	}
+        return node;
+    }
+    foreach( recipe, ingredients in recipes )
+    {
+        ingredients = ingredients.split(",");
+        auto node = add_node(recipe);
+        node["edges"] <- ingredients;
+        foreach( ingredient in ingredients )
+            node = add_node(ingredient);
+    }
+    foreach( node in nodes )
+    {
+        if( "edges" in node )
+            node["edges"] = tuple(nodemap[ingredient] for ingredient in node["edges"]);
+        else
+            node["edges"] = ();
+    }
+    return nodes;
+}
+
+// Print a list of the nodes along with the outgoing edges of each:
+foreach( n in to_graph(recipes) )
+{
+    print(format("%-20s", n["name"]), [n2["name"] for n2 in n["edges"]]);
+}