Commits

Bart van Strien  committed 9b1f989

Fixes and tables

  • Participants
  • Parent commits 5e7372f

Comments (0)

Files changed (6)

File fplua/compiler.lua

 		output = ("repeat\n%s\nuntil %s"):
 			format(table.concat(subBlocks, "\n"), condition)
 	elseif command == "[]" then
-		output = ("(%s)[%s]"):format(ast[1], compileItem(ast[2]))
+		output = ("(%s)[%s]"):format(compileItem(ast[1]), compileItem(ast[2]))
 	elseif command == "return" then
 		local subBlocks = {}
 		for i, v in ipairs(ast) do
 		output = ("return %s"):format(table.concat(subBlocks, ", "))
 	elseif command == "break" then
 		output = "break"
+	elseif command == "{" then
+		local subBlocks = {}
+		for i, v in ipairs(ast) do
+			table.insert(subBlocks, compileItem(v))
+		end
+		output = ("{ %s }"):format(table.concat(subBlocks, ", "))
 	elseif infixOps[command] then
 		local out = {}
 		for i, v in ipairs(ast) do
 	return item
 end
 
-local function compileChunk(chunk)
-	local ast = reader("do " .. chunk)
-
-	--utils.dumptree(ast)
+local function compileAst(ast)
 	return compileItem(ast)
 end
 
-return compileChunk
+return compileAst

File fplua/fplua.lua

 local fplua = setmetatable({
 	reader = require "fplua.reader",
-	compileChunk = require "fplua.compiler"
+	compileAst = require "fplua.compiler"
 }, {__index = require "fplua.utils"})
 
 function fplua.loadstring(s)
-	return loadstring(fplua.compileChunk(s))
+	return loadstring(
+		fplua.compileChunk(
+			fplua.buildAst(s)))
 end
 
 function fplua.loadfile(filename)
 	return nil, "No fplua file found in path"
 end
 
+function fplua.buildAst(s)
+	return fplua.reader("do " .. s)
+end
+
 return fplua

File fplua/reader.lua

 	local word = {}
 	local instring = false
 	local insexpr = 0
+	local intable = 0
 	local escape = false
 	local comment = false
 	contents = contents .. " "
 		elseif c == ')' and not instring and not escape then
 			insexpr = insexpr - 1
 			table.insert(word, c)
-			if insexpr == 0 then
+			if insexpr == 0 and intable == 0 then
 				table.insert(tree, reader(table.concat(word):sub(2, -2)))
 				word = {}
 			end
+		elseif c == '{' and not instring and not escape then
+			intable = intable + 1
+			table.insert(word, c)
+		elseif c == '}' and not instring and not escape then
+			intable = intable - 1
+			table.insert(word, c)
+			if intable == 0 and insexpr == 0 then
+				local subtree = reader(table.concat(word):sub(2, -2))
+				table.insert(subtree, 1, '{')
+				table.insert(tree, subtree)
+				word = {}
+			end
 		elseif c == '"' then
 			if escape then
 				escape = false
 		elseif c:match("%s") and
 				not instring and
 				insexpr == 0 and
+				intable == 0 and
 				not escape then
 			word = table.concat(word)
 			word = word:match("^%s*(.-)%s*$")
 			table.insert(word, c)
 		end
 	end
-	assert(#word == 0, "Parentheses don't match")
+	assert(table.concat(word):match("^%s*$"), "Parentheses don't match")
 	return tree
 end
 

File fplua/utils.lua

+local utils = {}
+
+function utils.dumptree(tree, prefix)
+	prefix = prefix or ""
+	local first = true
+	for i, v in ipairs(tree) do
+		if type(v) == "table" then
+			if not first then
+				print()
+			end
+			utils.dumptree(v, prefix .. "  ")
+			first = false
+		else
+			print(prefix .. v)
+		end
+	end
+end
+
+return utils
 
 (local= result
   (polynomial
-    {5 8 -3} 5))
+    { 5 8 -3 } 5))
 (if
   (==
     result
   (until (or (== i 8) (== i 9))
     (table.insert results
 	  (polynomial
-        {2 8 1 4} i))))
+        { 2 8 1 4 } i))))
 
 (for (i v) (ipairs results)
   (print v))
 f:close()
 print(code)
 
+print("----AST----")
+code = fplua.buildAst(code)
+fplua.dumptree(code)
+
 print("----Compiled----")
-code = fplua.compileChunk(code)
+code = fplua.compileAst(code)
 print(code)
 
 print("----Load----")