lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I asked:

> > The question is now -- what am I going to do with a Lua parser? ;-)

Roberto replied:

> A source-code optimizer? :)  Because local variables correspond to
> registers in the virtual machine, there are several optimizations
> that can be done at source level (assuming that ocasional metamethods
> are well-behaved):
>
> - constant-expression evaluation
> - constant propagation
> - moving loop invariants out of the loop
> - CSE elimination
> - minimization of number of local variables used by a function

That seems about right. The side-effect of a $-parse is a tree, and thus the
above optimizations seem reasonable to try. Constant expression evaluation
is probably the easiest to achieve with what I've got here, since I can
actually call into the Lua engine from within my grammar reductions to
evaluate expressions during the parse, and then replace them with whatever
Lua says they evaluate to at compile time, such that:

function bar()
	local function foo(x)
		return x*5;
	end

	local a = 10;
      local b = 10 + a;

	-- assuming b is not passed to anything that changes it

	if foo(b) == 100 then
	print(foo(50));
	end
end

Could become simply:

function bar()
	print(250);
end

In this way, the source-optimizer would work like a macro-expander.

Since this would be a source-optimizer, and behave like a preprocessor,
would the addition of a new hint keyword "const" be terribly bothersome to
Lua-purists? Such a keyword would make the optimizations much simpler, in
terms of flow analysis. "const" could have the traditional meaning, of
course (that is -- evaluate at instantiation and never allow assignment
after instantiation).

BTW, Roberto -- I've written grammars for C++, Perl, C#, and other
languages, and have to say that writing a grammar for Lua was a real
pleasure. It's an amazing language, and once I got my head around a few
gotchas like local t,f={},1 ... I was able to write the cleanest "real
language" parser I've ever managed -- very few warts.

--
Quinn Tyler Jackson
http://members.shaw.ca/qjackson/