On 03/23/2014 11:04 AM, Seth Tisue wrote:
If you want to do static analysis of NetLogo code, even that
representation might be too low-level for you; you might want to work
directly with the output of the front end of the compiler, not the
back end. The AST (abstract syntax tree) classes output by the front
end are defined in
https://github.com/NetLogo/NetLogo/blob/5.0.x/src/main/org/nlogo/compiler/AstNode.scala
For examples of how different kinds of code end up being represented
using these classes, scroll to the bottom of
https://github.com/NetLogo/NetLogo/blob/5.0.x/src/test/org/nlogo/compiler/ExpressionParserTests.scala
and look for `testStartAndEndPositions10` which uses the same code
as the example above. The major difference is that the AST classes
preserve the tree structure of the original code, whereas after the
back end has run, the structure has been flattened so that commands
are no longer nested inside each other. (Reporters are left
unflattened.)
Just to follow up on this in case anyone cares. Here's what I've done.
I'm sure it would be cleaner to write my own class like
ExpressionParserTests. Or perhaps there's already a built-in way to get
this output. But I just wanted to, quick and dirty, see what a whole
model would look like. With this change in the headless branch:
diff --git a/src/main/compile/Compiler.scala
b/src/main/compile/Compiler.scala
index 3d056bd..ef56e45 100644
--- a/src/main/compile/Compiler.scala
+++ b/src/main/compile/Compiler.scala
@@ -36,6 +36,10 @@ object Compiler extends nvm.CompilerInterface {
flags: nvm.CompilerFlags): nvm.CompilerResults = {
val (defs, structureResults) =
frontEnd.frontEndHelper(source, displayName, program,
subprogram, oldProcedures, extensionManager)
+ val (booga) = frontEnd.frontEnd(source, oldProcedures, program) match {
+ case (procs, _) => procs.map(_.statements)
+ }
+ print(booga)
middleEnd.middleEnd(defs, flags)
backEnd.backEnd(defs, structureResults.program, source,
extensionManager.profilingEnabled, flags)
}
Recompiled. Then:
./sbt |tee model.ast
run --model model.nlogo --experiment experiment.xml
Which produced stuff like this (except without the formatting, of course):
Vector(
_nodisplay[]
_clearall[]
_ask:+0[_patches[],
[_set
[_patchvariable:2[], _constdouble:9.9[]]
]
]
_set[_observervariable:17[], _mult[_observervariable:16[],
_constdouble:10.0[]]]
_set[_observervariable:18[], _constdouble:5.0[]]
_set[_observervariable:19[], _constdouble:1.0[]]
_set[_observervariable:20[], _mult[_constdouble:10.0[],
_observervariable:18[]]]
_set[_observervariable:21[],
_div[_mult
[_floor [_observervariable:2[]], _observervariable:15[]],
_constdouble:100.0[]
]
]
_set[
_observervariable:22[],
_div[_mult
[
_ceil
[_observervariable:2[]],
_minus[
_constdouble:100.0[],
_observervariable:15[]
]
],
_constdouble:100.0[]
]
]
_call:INITIALISE-BUGS[]
_ask:+0[_breed:BUGS[],
[_call:SETUP-BUGS[]]
]
_call:SHOW-PLOTS[],
_createturtles:BUGS,
+0[_observervariable:4[],
[_hideturtle[]
_set[_breedvariable:ENERGY[], _observervariable:16[]]
]
]
... etc. ...
Thanks again, Seth!
--
glen ep ropella -- 971-255-2847
--
You received this message because you are subscribed to the Google Groups
"netlogo-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.