summary refs log tree commit diff stats
path: root/dirtycompiler.lua
diff options
context:
space:
mode:
Diffstat (limited to 'dirtycompiler.lua')
-rw-r--r--dirtycompiler.lua71
1 files changed, 51 insertions, 20 deletions
diff --git a/dirtycompiler.lua b/dirtycompiler.lua
index ba499b3..3ece694 100644
--- a/dirtycompiler.lua
+++ b/dirtycompiler.lua
@@ -160,14 +160,8 @@ defs[parser.FALLBACK] = function(state, token)
             -- figure out whether we're in funcname
             local i = results.n - 1 -- skip the ':'
             local find_statement = true
-            --while is_tk(results, i, '\n') do -- ignore newlines
-            --    i = i - 1
-            --end
             i = ignore_newlines(results, i)
             while results[i-1] == TK.NAME do
-                --while is_tk(results, i-2, '\n') do -- ignore newlines
-                --    i = i - 1
-                --end
                 i = ignore_newlines(results, i-2) + 2
                 if is_tk(results, i-2, '.') then
                     -- keep going
@@ -194,9 +188,6 @@ defs[parser.FALLBACK] = function(state, token)
             end
             if find_statement then
                 while true do
-                    --while is_tk(results, i, '\n') do -- ignore newlines
-                    --    i = i - 1
-                    --end
                     i = ignore_newlines(results, i)
                     if is_tk(results, i, ')') then
                         -- (prefixexp) or (funcargs)
@@ -208,6 +199,8 @@ defs[parser.FALLBACK] = function(state, token)
                                 depth = depth - 1
                             elseif is_tk(results, i, ')') then
                                 depth = depth + 1
+                            elseif not results[i] then
+                                error("syntax error (unbalanced '()')")
                             end
                         until depth == 0
                     elseif is_tk(results, i, ']') then
@@ -220,6 +213,8 @@ defs[parser.FALLBACK] = function(state, token)
                                 depth = depth - 1
                             elseif is_tk(results, i, ']') then
                                 depth = depth + 1
+                            elseif not results[i] then
+                                error("syntax error (unbalanced '[]')")
                             end
                         until depth == 0
                     elseif results[i-1] == TK.NAME then
@@ -243,6 +238,8 @@ defs[parser.FALLBACK] = function(state, token)
                                 depth = depth - 1
                             elseif is_tk(results, newi, ']') then
                                 depth = depth + 1
+                            elseif not results[i] then
+                                error("syntax error (unbalanced '{}')")
                             end
                         until depth == 0
                         local checki = ignore_newlines(results, newi-1)
@@ -328,7 +325,6 @@ defs[parser.FALLBACK] = function(state, token)
                         local tk_mytrait = i-1
                         i = ignore_newlines(results, i-2)
                         if results[i-1] == END_OF_STMT then
-                            assert(token == '(', "unimplemented")
                             -- definitely cratera (stmt ':' Name '.' Name '(')
                             -- convert into '(' stmt ',' String ',' String
                             -- convert names into strings
@@ -344,13 +340,14 @@ defs[parser.FALLBACK] = function(state, token)
                                 depth = depth - 1
                             elseif is_tk(results, i, ']') then
                                 depth = depth + 1
+                            elseif not results[i] then
+                                error("syntax error (unbalanced '[]')")
                             end
                         until depth == 0
                         local tk_left = i
                         i = ignore_newlines(results, i-1)
                         if results[i-1] == END_OF_STMT then
-                            assert(token == '(', "unimplemented")
-                            -- definitely cratera (':' '[' exp ']' '.' Name '(')
+                            -- definitely cratera (stmt ':' '[' exp ']' '.' Name '(')
                             -- convert into '(' stmt ',' '(' exp ')' ',' String
                             -- replace '[' and ']'
                             results[tk_right] = ')'
@@ -359,6 +356,7 @@ defs[parser.FALLBACK] = function(state, token)
                         end -- else not cratera
                     end
                     if inject_cratera then
+                        --assert(token == '(', "unimplemented")
                         -- convert name into string
                         results[tk_myfunction] = TK.STRING
                         -- replace '.' with ','
@@ -370,8 +368,16 @@ defs[parser.FALLBACK] = function(state, token)
                         results.n = results.n - 2
                         -- replace ':' with ','
                         results[ignore_newlines(results, i-2)] = ','
-                        -- replace START_OF_STMT with '(', and '(' with ','
-                        results[pos], results[results.n] = '(', ','
+                        -- replace START_OF_STMT with '('
+                        results[pos] = '('
+                        if token == '(' then
+                            -- replace '(' with ','
+                            results[results.n] = ','
+                        else
+                            -- insert ',' before argument
+                            table.insert(results, results.n, ',')
+                            results.n = results.n + 1
+                        end
                         -- inject cratera
                         table.insert(results, pos, ')')
                         table.insert(results, pos, CRATERA_FUNCTION)
@@ -386,19 +392,38 @@ defs[parser.FALLBACK] = function(state, token)
                             results.n = results.n + 1
                         end
                         results.n = results.n + 3
-                        -- tag it for '(' ')' (no argument) calls
+                        -- tag it so we know to insert a ')' to close our '('
+                        -- and to handle '(' ')' (no argument) calls
+                        -- we add the tag before TK.STRING/'{'/','
+                        table.insert(results, results.n, END_OF_CRATERA)
                         results.n = results.n + 1
-                        results[results.n] = END_OF_CRATERA
                     end
                 end -- else not cratera
             end
         elseif token == '}' then
-            -- TODO unimplemented
+            local i = results.n -- we'll be subtracting anyway, see below
+            local depth = 1
+            repeat
+                i = i - 1
+                if is_tk(results, i, '{') then
+                    depth = depth - 1
+                elseif is_tk(results, i, '}') then
+                    depth = depth + 1
+                elseif not results[i] then
+                    error("syntax error (unbalanced '{}')")
+                end
+            until depth == 0
+            assert(is_tk(results, i, '{'))
+            if results[i-1] == END_OF_CRATERA then
+                -- need to add ')' to close our '('
+                table.remove(results, i-1)
+                results[results.n] = ')'
+            end
         elseif token == ')' then
             local i = results.n - 1 -- skip the ')'
             i = ignore_newlines(results, i)
-            if results[i] == END_OF_CRATERA then
-                -- '(' CRATERA_FUNCTION ')' '(' something ',' END_OF_CRATERA ')'
+            if results[i] == ',' and results[i-1] == END_OF_CRATERA then
+                -- '(' CRATERA_FUNCTION ')' '(' something END_OF_CRATERA ',' ')'
                 -- need to fix it up into 
                 -- '(' CRATERA_FUNCTION ')' '(' something ')'
                 table.remove(results, i-1)
@@ -419,11 +444,17 @@ defs[parser.FALLBACK] = function(state, token)
                         results.n = results.n - 1
                         break
                     elseif not results[i] then
-                        error("syntax error")
+                        error("syntax error (unbalanced '()')")
                     end
                 until depth == 0
             end
         end
+    else -- we skipped a string literal
+        if results[results.n-1] == TK.STRING and results[results.n-2] == END_OF_CRATERA then
+            -- need to add ')' to close our '('
+            table.remove(results, results.n-2)
+            results[results.n] = ')'
+        end
     end
     results.skip = EXTRA_DATA[token]
     return SELF