Introspection: Difference between revisions

Added Wren
m (→‎{{header|Raku}}: Fix link: Perl 6 --> Raku)
(Added Wren)
Line 2,318:
 
<lang vb>If Application.Version < 15 Then Exit Sub</lang>
 
=={{header|Wren}}==
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
Note that:
 
* ''Process.version'' is not available in the release version of Wren CLI v0.3.0 but is available in the latest build (recommended for RC use).
* Wren doesn't have reflection as such but is able to obtain and analyze the running script's source code. It is assumed for this purpose that the latter is sensibly formatted - only global declarations are not indented.
* Wren is dynamically typed and has a Num type but not an integer type. As far as the extra credit is concerned, the simplifying assumption has been made that a global variable is an integer if it's assigned an integer literal when it's declared.
<lang ecmascript>import "os" for Platform, Process
import "io" for File
import "meta" for Meta
import "/pattern" for Pattern
import "/math" for Nums
 
var a = 4 /* 1st integer variable */
var b = 0xA /* 2nd integer variable */
var c = -8 /* 3rd integer variable */
 
var bloop = -17.3
 
var checkVersion = Fn.new {
var version = Process.version
var components = version.split(".")
if (Num.fromString(components[1]) < 3) {
Fiber.abort("Wren version (%(version)) is too old.")
}
}
 
var globalIntVars = Fn.new {
var sep = Platform.isWindows ? "\r\n" : "\n"
var lines = File.read(Process.allArguments[1]).split(sep)
var p = Pattern.new("var+1/s[+1/x]+1/s/=+1/s[0x+1/h|~-+1/d]+0/s", Pattern.whole)
var q = Pattern.new("[////|//*]+0/z", Pattern.end)
var vars = []
var vals = []
for (line in lines) {
line = q.replaceAll(line, "") // get rid of any comments
var m = p.find(line)
if (m) {
vars.add(m.capsText[0])
vals.add(Num.fromString(m.capsText[1]))
}
}
return [vars, vals]
}
 
checkVersion.call()
var res = globalIntVars.call()
var d
Meta.eval("d = bloop.abs") // this won't compile if either 'bloop' or 'abs' not available
System.print("bloop.abs = %(d)")
var vars = res[0]
var vals = res[1]
System.print("The sum of the %(vars.count) integer variables, %(vars), is %(Nums.sum(vals))"</lang>
 
{{out}}
<pre>
bloop.abs = 17.3
The sum of the 3 integer variables, [a, b, c], is 6
</pre>
 
=={{header|Yabasic}}==
9,490

edits