-
-
Notifications
You must be signed in to change notification settings - Fork 80
MetaProgramming
Zhiyuan edited this page Jan 5, 2017
·
11 revisions
Meta programming allows you to extend NPL syntax using the NPL language itself. For example, following code are valid in NPL.
local a=1;
loop(){ please execute the code 10 times with i
echo(a+i)
async(){
echo("This is from worker thread");
}
}
Here loop, async are extended NPL syntax defined elsewhere.
The concept is first developed by LISP programming language in 1960s. However, despite the power of LISP, its syntax is hard to read for most programmers nowadays. NPL introduces a similar concept called Function-Expression
, which can be mixed very well with the original NPL syntax.
The syntax of NPL's Function-Expression is name(input, ...){ ... }
Files with *.npl
extension support Function-Expression syntax by default. For example
NPL.load("(gl)script/tests/helloworld.npl")
NPL.loadstring("-- source code here", "filename_here")
def(<name>, <params>){
--mode:<mode>
statements
}
- <name>:name of new structure to be defined, name could not be null
- <params>:parameters to be passed to defined structure multiple parameters should be seperated by comma unknown number parameters, using ...
- <mode>:mode is set at the first line of comment inside block. Mode
could be strict, line and token. When different mode is set,
different parsing strategy is used in defined function expression.
If nothing specified, strict mode is used. - <statements>: statements here are template code, which would be applied
to final code without any change. However, one exception is +{} structure. Code inside +{} would be executed during compiling.
And some default functions are provided to give users more control.
def("loop"){
--mode:line
+{local line = ast:getLines(1,1)
local times, i = line:match("execute the code (%w+) times with (%l)")
if not times then times="1" end
if not i then i="i" end
}
for +{emit(i)}=1, +{emit(times)} do
+{emitline(2)}
end
}
loop(){execute the code 10 times with j
print(2+j)
print(3)
print(4)
}
For more examples, please see our test here
Download Paracraft | ParacraftSDK | copyright by tatfook 2016 | upload image