Commits

Cat's Eye Technologies  committed 00487a6

Fix lua impl's scanner (actually loader), --scan on both impls.

  • Participants
  • Parent commits 4aadde3

Comments (0)

Files changed (4)

File impl/velo.lua/src/velo.lua

 
 --[[ ========== DEBUG ========= ]]--
 
+local do_debug = false
+local debug_scan = false
+
 local debug = function(s)
-    --print("--> (" .. s .. ")")
+    if do_debug then
+        print("--> (" .. s .. ")")
+    end
 end
 
 --[[ ========== EXCEPTIONS ========= ]]--
     return string.find("(),.;=", s, 1, true) ~= nil
 end
 
+function isspace(s)
+    return string.find(" \t", s, 1, true) ~= nil
+end
+
+function iseol(s)
+    return string.find("\n\r;", s, 1, true) ~= nil
+end
+
 --[[ ========== SCANNER ========= ]]--
 
 Scanner = {}
 
     methods.scan = function()
         methods.scan_impl()
-        debug("scanned " .. _text .. " (" .. _type .. ")")
+        if debug_scan then
+            print("scanned '" .. _text .. "' (" .. _type .. ")")
+        end
         return _text
     end
 
     methods.scan_impl = function()
         -- discard leading whitespace
-        while string:sub(1,1) == " " or string:sub(1,1) == "\t" do
+        while isspace(string:sub(1,1)) and string ~= "" do
             string = string:sub(2)
-            --debug "consumed whitespace, string now '#{@string}'"
         end
         
         if string == "" then
             return
         end
 
-        local match = (string:sub(1,1) == "\n" or string:sub(1,1) == "\r")
+        local match = iseol(string:sub(1,1))
         if match then
-            while match do
+            while match and string ~= "" do
                 string = string:sub(2)
-                match = (string:sub(1,1) == "\n" or string:sub(1,1) == "\r" or
-                         string:sub(1,1) == " " or string:sub(1,1) == "\t")
+                match = iseol(string:sub(1,1)) or isspace(string:sub(1,1))
             end
             methods.set_token("EOL", "EOL")
             return
 
 --[[ ================== MAIN =============== ]]--
 
-local ast = false
-if arg[1] == "--ast" then
-    arg[1] = arg[2]
-    ast = true
+local dump_ast = false
+
+while #arg > 0 do
+    if arg[1] == "--ast" then
+        dump_ast = true
+    elseif arg[1] == "--debug" then
+        do_debug = true
+    elseif arg[1] == "--scan" then
+        debug_scan = true
+    else
+        text = ""
+        for line in io.lines(arg[1]) do
+            text = text .. line .. "\n"
+        end
+
+        local p = Parser.new(text)
+        local s = p.script()
+        if ast then
+            print(s.to_s())
+        else
+            local o = VeloObject.new('main-script')
+            s.eval(o, {})   -- XXX could pass command-line arguments here...
+        end
+    end
+    table.remove(arg, 1)
 end
-
-text = ""
-for line in io.lines(arg[1]) do
-    text = text .. line
-end
-
-local p = Parser.new(text)
-local s = p.script()
-if ast then
-    print(s.to_s())
-else
-    local o = VeloObject.new('main-script')
-    s.eval(o, {})   -- XXX could pass command-line arguments here...
-end
 
 ############ Main ############
 
-ast = false
+dump_ast = false
 ARGV.each do |arg|
   if arg == '--debug'
     $debug = true
     next
   end
   if arg == '--ast'
-    ast = true
+    dump_ast = true
+    next
+  end
+  if arg == '--scan'
+    $debug_scan = true
     next
   end
   File.open(arg, 'r') do |f|
     end
     p = Parser.new(text)
     s = p.script
-    if ast
+    if dump_ast
       puts s
     else
       o = VeloObject.new 'main-script'

File src/velo/debug.rb

 $debug = false
+$debug_scan = false
 
 def debug s
   if $debug

File src/velo/scanner.rb

 
   def scan
     scan_impl
-    debug "scanned '#{@text}' (#{@type})"
+    if $debug_scan
+      puts "scanned '#{@text}' (#{@type})"
+    end
     return @text
   end